<!DOCTYPE html> <html>
<head> <link rel="stylesheet" href="index.css" type="text/css" media="screen" /> <script src="jquery-1.6.min.js" type="text/javascript"></script> <script src="../dist/jquery.syntax.js" type="text/javascript"></script> <script src="../dist/jquery.syntax.cache.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(function($) { $.syntax(); }); </script> </head> <body> <h1>Syntax: C, C++, Objective-C</h1> <h2>C++</h2> <pre><code class="syntax brush-clang">#include <iostream>
// If we increase row by 1, the offset will increase by sz (number of elements per row i.e. number of columns) // If we increase col by 1, the offset will increase by 1 unsigned rowMajorOffset(unsigned row, unsigned col, unsigned sz) {
return col + row * sz;
}
// If we increase col by 1, the offset will increase by sz (number of elements per column i.e. number of rows) // If we increase row by 1, the offset will increase by 1 unsigned columnMajorOffset(unsigned row, unsigned col, unsigned sz) {
return row + col * sz;
}
template <typename _ValueT, unsigned _R, unsigned _C, bool _ColumnMajor> class Matrix { protected:
enum { ColumnMajor = _ColumnMajor }; enum { R = _R }; enum { C = _C }; typedef _ValueT ValueT; ValueT m_values[C*R];
public:
const ValueT & at (unsigned r, unsigned c) const { if (ColumnMajor) return m_values[columnMajorOffset(r, c, R)]; else return m_values[rowMajorOffset(r, c, C)]; } ValueT & at (unsigned r, unsigned c) { if (ColumnMajor) return m_values[columnMajorOffset(r, c, R)]; else return m_values[rowMajorOffset(r, c, C)]; } void loadTestPattern () { for (unsigned r = 0; r < R; r += 1) for (unsigned c = 0; c < C; c += 1) at(r, c) = (r+1) * 1000 + (c+1); } void debug () { using namespace std; if (ColumnMajor) cout << "Column-Major Matrix " << "(" << R << "," << C << ")" << " @ " << this << endl; else cout << "Row-Major Matrix " << "(" << R << "," << C << ")" << " @ " << this << endl; cout << "Memory Offset: "; for (unsigned i = 0; i < (R*C); i += 1) cout << i << " "; cout << endl; cout << " Values: "; for (unsigned i = 0; i < (R*C); i += 1) cout << m_values[i] << " "; cout << endl; cout << "Standard Mathematical Notation:" << endl; cout << " "; for (unsigned c = 0; c < C; c += 1) cout << "Col " << c << " "; cout << endl; for (unsigned r = 0; r < R; r += 1) { cout << "Row " << r << " "; for (unsigned c = 0; c < C; c += 1) cout << at(r, c) << " "; cout << endl; } cout << endl; } Matrix<ValueT, R, C, !ColumnMajor> transposeStorage () const { Matrix<ValueT, R, C, !ColumnMajor> result; for (unsigned r = 0; r < R; r += 1) for (unsigned c = 0; c < C; c += 1) result.at(r, c) = at(r, c); return result; } Matrix<ValueT, C, R, !ColumnMajor> transposeMatrix () const { Matrix<ValueT, C, R, !ColumnMajor> result; memcpy(&result.at(0,0), m_values, sizeof(m_values)); return result; }
};
int main (int argc, char * const argv[]) {
Matrix<float, 4, 2, false> rowMajorMatrix; Matrix<float, 4, 2, true> columnMajorMatrix; rowMajorMatrix.loadTestPattern(); rowMajorMatrix.debug(); columnMajorMatrix.loadTestPattern(); columnMajorMatrix.debug(); rowMajorMatrix = columnMajorMatrix.transposeStorage(); rowMajorMatrix.debug(); Matrix<float, 2, 4, false> transposedMatrix = columnMajorMatrix.transposeMatrix(); transposedMatrix.debug(); return 0;
}</code></pre>
<h2>Objective-C Header</h2> <pre><code class="syntax brush-clang">//
// GHInventoryEditor.h // Goblin Hacker // // Created by Samuel Williams on 19/12/07. // Copyright 2007 Samuel Williams, Orion Transfer Ltd. All rights reserved.
// This software was originally produced by Orion Transfer Ltd. // Please see www.oriontransfer.org for more details. //
/*
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import <Cocoa/Cocoa.h>
import "GHEditorController.h" import "GHSavedGameDocument+Slots.h"
@interface GHInventoryEditor : GHEditorController {
NSInteger armorType, weaponType, potionQuality;
}
+ (NSArray*) items;
@property NSInteger armorType; @property NSInteger weaponType; @property NSInteger potionQuality;
-
(IBAction)healingPotions: (id)sender;
-
(IBAction)manaPotions: (id)sender;
-
(IBAction)cureDiseasePotions: (id)sender;
-
(IBAction)catsEyePotions: (id)sender;
-
(IBAction)predatorSightPotions:(id)sender;
-
(IBAction)leatherSkinPotions: (id)sender;
-
(IBAction)hastePotions: (id)sender;
-
(IBAction)invisibilityPotions: (id)sender;
-
(IBAction)basicWeapon: (id)sender;
-
(IBAction)goodWeapon: (id)sender;
-
(IBAction)superbWeapon: (id)sender;
-
(IBAction)avatarWeapon: (id)sender;
-
(IBAction)basicArmor: (id)sender;
-
(IBAction)goodArmor: (id)sender;
-
(IBAction)superbArmor: (id)sender;
-
(IBAction)avatarArmor: (id)sender;
-
(IBAction)alchemyBooks: (id)sender;
-
(IBAction)amulets: (id)sender;
-
(IBAction)lockpicks: (id)sender;
-
(IBAction)reagents: (id)sender;
-
(IBAction)reactants: (id)sender;
-
(IBAction)rings: (id)sender;
-
(IBAction)torches: (id)sender;
-
(IBAction)avatarJewelry: (id)sender;
@end</code></pre>
<h2>Objective-C Implementation</h2> <pre><code class="syntax brush-clang">//
// GPointSet.m // Gocoa // // Created by Samuel Williams on 9/05/05. // Copyright 2005 Orion Transfer Ltd. All rights reserved. //
import "GPointSet.h"
void adjacentPoints (NSPoint p, NSPoint pts) { //pts must have four elements
pts[3] = NSMakePoint (p.x - 1, p.y); pts[0] = NSMakePoint (p.x, p.y + 1); pts[1] = NSMakePoint (p.x + 1, p.y); pts[2] = NSMakePoint (p.x, p.y - 1);
}
@implementation GPointSet + (NSPoint) pointForIndex:(unsigned int)index withSize:(NSSize)s {
int x = index % (int)(s.width); return NSMakePoint (x, (int)((index - x) / s.width));
}
+ (unsigned int) indexForPoint:(NSPoint)point withSize:(NSSize)s {
return point.x + (point.y * s.width);
}
-
(id) init {
NSLog(@"%s does not have default constructor", __FUNCTION__); return nil;
}
-
(id) initWithSize:(NSSize)newSize {
if (self = [super init]) { size = newSize; indexSet = [[NSMutableIndexSet alloc] init]; } return self;
}
-
(void) dealloc {
[super dealloc]; [indexSet release];
}
-
(NSMutableIndexSet*) indexSet {
return [[indexSet retain] autorelease];
}
-
(void) addPoint:(NSPoint)point {
[indexSet addIndex:[GPointSet indexForPoint:point withSize:size]];
}
-
(void) removePoint:(NSPoint)point {
[indexSet removeIndex:[GPointSet indexForPoint:point withSize:size]];
}
-
(BOOL) containsPoint:(NSPoint)point {
return [indexSet containsIndex:[GPointSet indexForPoint:point withSize:size]];
}
-
(NSPoint) pointForIndex:(unsigned int)index {
return [GPointSet pointForIndex:index withSize:size];
}
-
(unsigned int) indexForPoint:(NSPoint)point {
return [GPointSet indexForPoint:point withSize:size];
}
-
(NSString*)description {
NSMutableString *result = [NSMutableString new]; [result appendFormat:@"<GPointSet %h", self]; unsigned index = [indexSet firstIndex]; while (index != NSNotFound) { [result appendFormat:@"%@ ", NSStringFromPoint([self pointForIndex:index])]; index = [indexSet indexGreaterThanIndex:index]; } [result appendString:@">"]; return result;
}
@end</code></pre>
<h2>Strings with embedded escapes</h2> <pre><code class="syntax brush-clang">
const char * text = “The elephantn was in the room.”;
</code></pre> <h2>Types and Functions</h2> <pre><code class="syntax c++">
int pthread_setcancelstate(int, int *); int pthread_setcanceltype(int, int *); int pthread_setconcurrency(int); int pthread_setschedparam(pthread_t, int ,
const struct sched_param *);
int pthread_setspecific(pthread_key_t, const void *); void pthread_testcancel(void);
namespace Bob {
namespace moo { }
}
</code></pre> </body>
</html>