<!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 &lt;iostream&gt;

// 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 &lt;typename _ValueT, unsigned _R, unsigned _C, bool _ColumnMajor&gt; class Matrix { protected:

enum { ColumnMajor = _ColumnMajor };
enum { R = _R };
enum { C = _C };

typedef _ValueT ValueT;

ValueT m_values[C*R];

public:

const ValueT &amp; at (unsigned r, unsigned c) const
{
        if (ColumnMajor)
                return m_values[columnMajorOffset(r, c, R)];
        else
                return m_values[rowMajorOffset(r, c, C)];
}

ValueT &amp; 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 &lt; R; r += 1)
                for (unsigned c = 0; c &lt; C; c += 1)
                        at(r, c) = (r+1) * 1000 + (c+1);
}

void debug ()
{
        using namespace std;

        if (ColumnMajor)
                cout &lt;&lt; &quot;Column-Major Matrix &quot; &lt;&lt; &quot;(&quot; &lt;&lt; R &lt;&lt; &quot;,&quot; &lt;&lt; C &lt;&lt; &quot;)&quot; &lt;&lt; &quot; @ &quot; &lt;&lt; this &lt;&lt; endl;
        else
                cout &lt;&lt; &quot;Row-Major Matrix &quot; &lt;&lt; &quot;(&quot; &lt;&lt; R &lt;&lt; &quot;,&quot; &lt;&lt; C &lt;&lt; &quot;)&quot; &lt;&lt; &quot; @ &quot; &lt;&lt; this &lt;&lt; endl;

        cout &lt;&lt; &quot;Memory Offset: &quot;;
        for (unsigned i = 0; i &lt; (R*C); i += 1)
                cout &lt;&lt; i &lt;&lt; &quot;    &quot;;
        cout &lt;&lt; endl;

        cout &lt;&lt; &quot;       Values: &quot;;      
        for (unsigned i = 0; i &lt; (R*C); i += 1)
                cout &lt;&lt; m_values[i] &lt;&lt; &quot; &quot;;
        cout &lt;&lt; endl;

        cout &lt;&lt; &quot;Standard Mathematical Notation:&quot; &lt;&lt; endl;
        cout &lt;&lt; &quot;      &quot;;
        for (unsigned c = 0; c &lt; C; c += 1)
                cout &lt;&lt; &quot;Col &quot; &lt;&lt; c &lt;&lt; &quot; &quot;;
        cout &lt;&lt; endl;

        for (unsigned r = 0; r &lt; R; r += 1) {
                cout &lt;&lt; &quot;Row &quot; &lt;&lt; r &lt;&lt; &quot; &quot;;
                for (unsigned c = 0; c &lt; C; c += 1)
                        cout &lt;&lt; at(r, c) &lt;&lt; &quot;  &quot;;
                cout &lt;&lt; endl;
        }
        cout &lt;&lt; endl;
}

Matrix&lt;ValueT, R, C, !ColumnMajor&gt; transposeStorage () const
{
        Matrix&lt;ValueT, R, C, !ColumnMajor&gt; result;

        for (unsigned r = 0; r &lt; R; r += 1)
                for (unsigned c = 0; c &lt; C; c += 1)
                        result.at(r, c) = at(r, c);

        return result;
}

Matrix&lt;ValueT, C, R, !ColumnMajor&gt; transposeMatrix () const
{
        Matrix&lt;ValueT, C, R, !ColumnMajor&gt; result;

        memcpy(&amp;result.at(0,0), m_values, sizeof(m_values));

        return result;
}

};

int main (int argc, char * const argv[]) {

Matrix&lt;float, 4, 2, false&gt; rowMajorMatrix;
Matrix&lt;float, 4, 2, true&gt; columnMajorMatrix;

rowMajorMatrix.loadTestPattern();
rowMajorMatrix.debug();

columnMajorMatrix.loadTestPattern();
columnMajorMatrix.debug();

rowMajorMatrix = columnMajorMatrix.transposeStorage();
rowMajorMatrix.debug();

Matrix&lt;float, 2, 4, false&gt; 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 &lt;http://www.gnu.org/licenses/&gt;.

*/

import &lt;Cocoa/Cocoa.h&gt;

import &quot;GHEditorController.h&quot; import &quot;GHSavedGameDocument+Slots.h&quot;

@interface GHInventoryEditor : GHEditorController {

NSInteger armorType, weaponType, potionQuality;

}

+ (NSArray*) items;

@property NSInteger armorType; @property NSInteger weaponType; @property NSInteger potionQuality;

@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 &quot;GPointSet.h&quot;

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);

}

}

}

}

}

}

}

}

}

}

}

@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>