tag:blogger.com,1999:blog-213397542024-02-27T20:38:08.076-08:00Load CodeCoding tutorials, code snippets, programming tools, tips and tricks for beginners to advance programmers.Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-21339754.post-65646482901858416952010-05-03T12:16:00.000-07:002010-05-03T12:16:12.895-07:00New blog, hacking, cache influence on performance, iPad etcI noticed I have a bunch of followers here. I don't know how much you guys follow up on my posts, but if you are still interested in reading about the stuff I write about I recommend you check out my new blog at<a href="http://assoc.tumblr.com/">http://assoc.tumblr.com/</a>. It is basically the same topics as this blog. I am not completely convinced yet, but I might discontinue using blogger and start with tumblr instead. Seems easier and prettier. Still haven't found any bloggging service well suited for writing about programming (syntax coloring of code snippets etc).
The last topics has been about what is in the headline. In particular I recommend reading about how the cache affects the performance of your program. It really changes how you write and think about code. When calculating a*(b+345)+(d/89)*c+4... and it is faster than reading a value from memory it really changes your ideas about how to code. People are still too stuck in the idea that CPU's are slow and memory fast. In fact memory is so dam slow that calculating something and storing the result for later retrieval is usually considerably slower than just redoing the calculation. Ok, it is a bit more complicated than that. It all depends on what is in the cache and what isn't. But you can read that on tumblr.Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com1tag:blogger.com,1999:blog-21339754.post-28312551632191463782010-01-17T02:17:00.001-08:002010-01-17T02:18:51.888-08:00C/C++ Quiz: the comma operator<p>I never cease to amaze me how I am always learning new things when programming C/C++ even after this many years. However I thought that mostly related to quirks in C++ and C++ template stuff. Yesterday I discovered something in C, which I kind of think I should have known. Especially since I have used it countless times without really knowing it. Why not make this a little quiz, to see if you really thought about it too.</p>
<p>You have probably written code like this before <code>int x = 2, y = 3</code> or <code>for (i = 0, j = 1; i < size; ++i, ++j)</code>. So here is the quiz. Without looking up in a reference or googling. What does this do, and print out?</p>
<pre>
<code>
int i = 5, size = 5;
i < size || (i = -1), ++i;
printf("%d\n", i);
</code>
</pre>
<p>Answer: It increments <em>i</em> but wraps when becomes <em>size</em> or bigger. Meaning incrementing <em>i</em> when it is <em>size</em> or larger sets it to zero. So 0 is printed out. How is how it works:</p>
<ol>
<li>The <code>||</code> operator will only evaluate the right expression if the left one was false. Because the right expression does not need to be evaluated to determine that the whole expression is true when left is true.</li>
<li>So when <code>i</code> is 5 the left expression is false and i is thus set to -1.</li>
<li>The comma operator is an expression operator. <code>a, b</code> makes sure <em>b</em> is evaluated after <em>a</em>. Meaning <code>++i</code> will always be evaluated, but after all the other expressions that needs to be evaluated to find the value of the expression.</li>
</ol>
So it could have been written as:
<pre>
<code>
int i = 5, size = 5;
if (i >= size)
i = 0;
++i;
printf("%d\n", i);
</code>
</pre>
<p>
What got me into this was trying to understand this macro found in the source code of the lua script language:
</p>
<pre>
<code>
#define luaL_addchar(B,c) \
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
(*(B)->p++ = (char)(c)))
</code>
</pre>
<p>
It is basically used to copy characters into a buffer. <code>B->p</code> is current position in buffer. The code makes sure that <em>luaL_prepbuffer</em> is called if the size of the buffer is exceeded. That function will empty the current buffer (passing on the contents) and making it ready to receive more characters. So the current pointer is reset to the start.
</p>
<p>
The question is of course why do this? Probably because it makes it possible to treat the macro more like a function. An expression can be placed most places a function call can be placed. However multiple statement, even if they are enclosed by <code>{}</code> can not.
</p>
This is legal after current definition
<pre>
<code>
for (i < 0; i < size; luaL_addchar(B,c))
</code>
</pre>
But needles to say it would not have been legal if <code>luaL_addchar(B,c)</code> had expanded to something like this in the for statement:
<pre>
<code>
for (i < 0; i < size; {if (B->p >= B->buffer+LUAL_BUFFERSIZE)
luaL_prepbuffer(B);
*B->p++ = c;})
</code>
</pre>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com4tag:blogger.com,1999:blog-21339754.post-27146369424679479122010-01-13T14:02:00.001-08:002010-01-14T09:32:06.461-08:00Interpreter pattern in Go using closures<p>I recently had to create some code using the <a href="http://en.wikipedia.org/wiki/Interpreter_pattern">interpreter pattern</a> from GoF. However I didn't have my Design Pattern by the Gang of Four ready so I had to look it up at <a href="http://en.wikipedia.org/wiki/Interpreter_pattern">wikipedia.</a> That got me looking at example implementations in C# and Java. Being all about Go for the time being it got me thinking what the same code would look like in Go using closures instead of classes. Just like I demonstrated with other patterns in previous posts.</p>
<p>
The code below demonstrates the interpreter pattern by implementing an interpreter of
<a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish notation</a> as described on the <a href="http://en.wikipedia.org/wiki/Interpreter_pattern">wiki page</a>. The wiki example uses a class to implement each grammar rule. Here we use
a closure. A higher order function will return a function which when run will evaluate the grammar in given context.
</p>
<pre>
<code>
import "fmt"
import . "container/vector"
import . "strings"
type Expression func(variables map[string]int) int
func Number(number int) Expression {
return func(variables map[string]int) int {
return number
}
}
func Plus(left, right Expression) Expression {
return func(variables map[string]int) int {
return left(variables) + right(variables)
}
}
func Minus(left, right Expression) Expression {
return func(variables map[string]int) int {
return left(variables) - right(variables)
}
}
func Variable(name string) Expression {
return func(variables map[string]int) int {
return variables[name]
}
}
</code>
</pre>
<p>What is interesting to notice is that the Java version using classes to define grammars takes up 43 lines, while the Go version takes 25 lines. The C# version takes even more lines: 63. However that is in large part because the approach is a bit different.
The <em>Evaluator</em> takes about the same number of lines.
</p>
<pre>
<code>
func Evaluator(expression string) Expression {
var expStack Vector
for _, token := range Split(expression, " ", 0) {
switch {
case token == "+":
subExpression := Plus(expStack().(Expression),
expStack().(Expression))
expressionStack.Push(subExpression)
case token == "-":
subExpression := Minus(expStack().(Expression),
expStack().(Expression))
expressionStack.Push(subExpression)
default:
expressionStack.Push(Variable(token))
}
}
syntaxTree := expStack().(Expression)
return func(context map[string]int) int {
return syntaxTree(context)
}
}
</code>
</pre>
The test code saves some lines on having map as a builtin type.
<pre>
<code>
func main() {
expression := "w x z - +"
sentence := Evaluator(expression)
variables := map[string]int {"w" : 5, "x" : 10, "z" : 42}
result := sentence(variables)
fmt.Println(result)
}
</code>
</pre>
One could change the code to have the + and - operator share some more code. But it wouldn't increase the code size as in the C# example.
<pre>
<code>
func plus(a, b int) int { return a + b }
func minus(a, b int) int{ return a - b }
func BinOp(left, right Expression, op func(a, b int) int) Expression {
return func(variables map[string]int) int {
return op(left(variables), right(variables))
}
}
</code>
</pre>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-40844847134683807342009-12-31T14:40:00.000-08:002009-12-31T14:43:54.373-08:00Design patterns in Go programming language<p>It has been said that design patterns is a symptom of language deficiency. If you need a pattern that indicated a language feature should have existed instead.
</p>
<p>Here I'd like to show how many class design patterns can be simplified in the Go programming language to look less like patterns and more like language features. This post was inspired by previous <a href="http://assets.en.oreilly.com/1/event/12/_The%20Lack%20of_%20Design%20Patterns%20in%20Python%20Presentation.pdf">discussions online (Joe Gregorio)</a> about how design patterns were rarely talked about in the Python community, due to the simple fact that in dynamic languages, the need for specific patterns isn't really there. The language often supports directly what you simulate with a pattern.
The examples I will use will mostly be related to computer games. Meaning examples will be about classes and objects you typically find in a game engine</p>
<h4>Command Pattern</h4>
<p>Let's say we have a game editor which lets us place sprites in a game world.
</p>
<p>The code below shows how we might imagine this could be done in C++. When the user click on a position, that position <em>pos</em> is given to a function along with the sprite which will be placed there.
</p>
<pre><code>
class Command {
virtual void Do() = 0;
virtual void Undo() = 0;
};
class PlaceSpriteCmd : public Command {
Sprite* _sprite;
Point _oldpos, _newpos;
public:
PlaceSpriteCmd(Sprite* sprite, Point pos) :
_sprite(sprite), _newpos(pos)
{
_oldpos = sprite->position();
}
void Do() {
sprite->setPosition(_newpos);
}
void Undo() {
sprite->setPosition(_oldpos);
}
};
static CommandStack _undoStack, _redoStack;
void PerformUndoableCmd(Command *cmd) {
cmd->Do();
_undoStack.push(cmd);
}
void UndoLastCmd() {
Command* cmd = _undoStack.pop();
cmd->Undo();
_redoStack.push(cmd);
}
void onPlaceSprite(Sprite* sprite, Point pos) {
Command *cmd = new PlaceSpriteCmd(sprite, pos);
PerformUndoableCmd(cmd);
}
</code></pre>
<p>The code is written with clarity in mind, so it is not meant to compile. It has no error checking, would probably leak memory etc.
</p>
It should thus be clear that supporting Undo in C++ require us to:
<ol>
<li>Create a <em>Command</em> base interface.</li>
<li>For each command we need a concrete subclass of this interface.</li>
<li>Three methods have to be implemented at least in these subclasses: Constructor, Do and Undo.</li>
<li>We need to define some member variables in concrete subclass to store enough information to be able to perform the undo. E.g. in the case above we store the both current and new position to support undo and redo.
</li></ol>
<p>Since Go supports closures, functions are essentially first class objects which is in many ways what we simulate with Command subclasses, this becomes a lot simpler:</p>
<pre><code>
func MakePlaceSpriteCmd(sprite *Sprite, newpos Point) (undo, redo func()) {
oldpos := sprite.Position()
undo = func() {
sprite.SetPosition(oldpos)
}
redo = func() {
sprite.SetPosition(newpos)
}
return
}
type Command struct {
undo, redo func()
}
var undoStack, redoStack CommandStack
func PerformUndoableCmd(undo, redo func()) {
redo()
undoStack.push(Command{undo, redo})
}
func UndoLastCmd() {
cmd := undoStack.pop()
cmd.undo()
redoStack.push(cmd);
}
func onPlaceSprite(sprite *Sprite, pos Point) {
undo, redo := MakePlaceSpriteCmd(sprite, pos)
PerformUndoableCmd(undo, redo);
}
</code></pre>
<p>It might not look a <em>lot</em> simpler, but that is mainly because of a certain fixed amount of code that has to be both places. What should hopefully be clear from the example however is that the difference becomes more noticeable as one starts adding commands. In the C++ case that involves creating a whole class. </p>
<h4>Strategy Pattern</h4>
<p>The main motivation behind the strategy pattern is to avoid an explosion in number of subclasses and code duplication. E.g. consider a real time strategy game with sprites for Allied tanks, Axis tanks and Soviet tanks. All the sprites are rendered differently, and might have different amounts of armor, gas tank volume etc. The tanks might have different behavior like cautious, aggressive, sneaky etc. The naive approach would be to create a subclass of the allied tank for each of those behaviors. However we would have to do this for each tank type even though the behavior code might be exactly the same. This leads to an explosion in subclasses.</p>
<p>Hence the use of the strategy pattern, which involves encapsulating the behavior into a separate objects and assign different behavior objects to tank objects at runtime. The previous post on Go vs Java touches upon this with the discussion of steering behaviors. Essentially steering behaviors could be thought of as the same as behaviors for sprites. The Go vs Java example shows how a language like Go with first class functions simplifies greatly the creation of strategy objects, in much the same way as we demonstrated in the <em>Command pattern</em> section above.</p>
<p>In Java and C++ we would have needed to create subclasses such as <em>Agressive</em>, <em>Sneaky</em> and <em>Cautious</em> of an interface, say <em>Behavior</em>. Go gets away with defining closures.</p>
We could even apply the strategy pattern to how sprites are drawn:
<pre><code>type Sprite struct {
pos Point
draw func(pos Point)
}
func (sprite *Sprite) Draw() {
sprite.draw(sprite.pos)
}</code></pre>
<h4>Factory Pattern</h4>
<p>In the Design Patterns book by the Gang of Four, different construction patterns are demonstrated using the example of a maze. The idea is that a maze consists of a number of connected rooms of different types. The reason for using some kind of factory is that we might want to change the types of rooms used but we don't want to have to change the code that creates the maze by instantiating room objects and connecting them.
Here is an example of what the maze construction code might look like in C++:</p>
<pre><code>
void BuildMaze(RoomFactory* factory) {
Room* a = factory->CreateRoom("Prison");
Room* b = factory->CreateRoom("GuardRoom");
Connect(a, b);
}
</code></pre>
<p>In Go we don't need to use an instance of a class at all. It is more
convenient to simply use a function object:
</p>
<pre><code>
func BuildMaze(createRoom func(roomType string) Room) {
a := createRoom("Prison");
b := createRoom("GuardRoom");
Connect(a, b);
}
</code></pre>
<p>But in practice it goes beyond this. Those familiar with more dynamic OOP languages like Smalltalk and Objective-C know that classes are objects and can thus be passed around in the code. In languages like C++ and Java we usually simulate this by creating a factory class for each class we want to be able to pass around as if the class was an object.
</p>
<p>In Go we can achieve much the same without extra code. The idiomatic way of creating an instance of a struct is to call a free function. This is because Go does not support constructors. So instead of coding constructors to instantiate structs you code free functions. The beneficial side effect of this is that because functions are first class objects, these creation functions can be passed around in similar fashion as Objective-C class objects. Constructors are not regular functions and can thus not be passed around in this fashion. Thus separate factory functions or classes needs to be created in languages like Java and C++.</p>
<h4>Conclusion</h4>
<p>While writing this blog entry, I tried to compare Go with Python. It became apparent that while Go can achieve a lot of the same simplicity as Python, it still can't do all patterns as simple as in Python. The most problematic pattern I noticed was <em>subject-observer</em>. In Joe Gregorio's presentation on the lack of design patterns in Python, it is written as this:</p>
<pre><code>class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def scale(self, n):
self.x = n * self.x
self.y = n * self.y
def notify(f):
def g(self, n):
print n
return f(self, n)
return g
Point.scale = notify(Point.scale)
p = Point(2.0, 3.0)
p.scale(2.5)</code>
</pre>
<p>Which can't be easily reproduced in Go. However the other patterns mentioned by Joe Gregorio: <em>Iterator, Factory</em> and <em>Strategy</em> should be just as easy as I hope I demonstrated clearly here. Another aspect which I haven't seen anybody write about in detail is lack of refactoring in Python and Ruby code bases. I am not saying it doesn't happen but I notice a couple developers who develop in both C++, Java and Ruby point out that they almost never refactor their Ruby code, while it is frequent occurrence in Java and C++. The reason being given that the need seldom seem to arise. I think it would be interesting to see in the future when more experience is gained from using Go, what the experience will be for Go. Will there be a lot of design patterns and refactoring or will the experiences be more similar to that of Python and Ruby developers? </p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com1tag:blogger.com,1999:blog-21339754.post-53622556297245486582009-12-02T06:37:00.001-08:002010-01-04T05:21:22.250-08:00Go vs Java<p>On the surface Go and Java might seem to have a lot in common. They are both modern C/C++ like languages with garbage collection, supporting object oriented programming.</p>
<p>But beyond that there are quite a lot of differences. I will not highlight so much of Java's strengths compared to Go, as Java has been around for a long time. What is more interesting is why a developer should want to choose Go over Java, given Java's ubiquity, large number of frameworks, tools etc.</p>
<p>First issue is efficiency both with respect to memory usage and performance. Go allows much more low level tuning similar to C. A problem in Java is that all types except primitive types are reference types. That means related data can't be stored in one location. E.g. say we have a Car object with 1 Engine object, 4 Wheel objects etc. All those objects are stored in different locations in memory. While in C or Go you could store all the Car related data as one continuous block of memory. Why is that important? In modern computers CPU's can process data a lot faster than it can be feed to it by regular RAM memory. Due to this frequently used parts of the main RAM memory are stored in a super fast memory called cache. For caches to be efficient related data needs to be close in address space. That is hard to achieve in Java.</p>
<p>An example of this in practice is the distributed version control system git. It is known to be very fast. It is written in C. A Java version JGit was made. It was considerably slower. Handling of memory and lack of unsigned types was some of the important reasons.</p>
Shawn O. Pearce wrote on the <a href="http://marc.info/?l=git&m=124111702609723&w=2">git mailinglist:</a>
<blockquote>JGit struggles with not having an efficient way to represent a SHA-1.
C can just say "unsigned char[20]" and have it inline into the
container's memory allocation. A byte[20] in Java will cost an
*additional* 16 bytes of memory, and be slower to access because
the bytes themselves are in a different area of memory from the
container object. We try to work around it by converting from a
byte[20] to 5 ints, but that costs us machine instructions</blockquote>
<p>Like C, Go does allow unsigned types and defining data structures containing other data structures as continuos blocks of memory</p>
<h4>Method calls</h4>
<p>Before reading this it might be good to read <a href="http://javadude.com/articles/passbyvalue.htm">Scott Stanchfield's article</a> on why all java method calls use pass-by-value, and that there is no such thing as pass-by-reference in Java. However as mentioned Java does not support value types for other than primitive types. This causes problems for method calls. One problem is that small objects like a <em>Point</em> might often be faster to copy in a method call, rather than copy their reference which is what Java does.
More importantly perhaps is that value semantics is often easier understood. E.g. it would be natural to assume that <em>Point</em> would be a value type. If I pass a <em>Point</em> object to a function I don't expect my point to be modified by called function. And yet that can easily happen in Java.</p>
<h4>Too strong focus on OOP</h4>
<p>Since the decision was made that Java would have no free functions (even though static methods in a way is free functions) this has caused the Java designer to come up with very cumbersome syntax to deal with problem that would have been best handled with free functions. Instead of closures Java got nested classes:</p>
<code>
<pre>
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.toFront();
}
});
</pre>
</code>
The same thing is achieved a lot less verbosely in Go using closures:
<code>
<pre>
myButton.addActionListener(func(e ActionEvent) {
frame.toFront();
});
</pre>
</code>
<p>Actually the Java version requires even more code, because the <em>ActionListener</em> class needs to be defined somewhere. The function object passed in the Go example is defined right were it is used.</p>
<h4>Why Java code end up being considerably more verbose than Go code</h4>
<p>When you start building more complicated things this problem starts adding up, causing excessive amounts of code to be needed in Java, while short readable code can be used in Go for the same thing. Consider this example. For a game engine I was writing I used Lua as a script language. Algorithms for planning movement of Non player characters was based on combining different functions describing different behavior. Without a lot of background information the code below is not easy to follow. But bare with me:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">local</span></strong> flank = Geometry.makeFlank(player, <span style="color:#0000ff;">10</span>)<br />
<strong><span style="color:#881350;">local</span></strong> <span style="color:#400080;">seek</span> = Geometry.makeSeek(player:position())<br />
<strong><span style="color:#881350;">local</span></strong> combo = Geometry.combineBehavior({<span style="color:#0000ff;">0.001</span>,<span style="color:#400080;">seek</span>}, {<span style="color:#0000ff;">1</span>,flank})</div>
<p><em>flank, seek</em> and <em>combo</em> are functions created by other functions <em>makeFlank, makeSeek</em> etc. The <em>combo</em> function is created by combining the <em>flank</em> and <em>seek</em> functions. Basically each function takes two arguments referred to as <em>s0</em> and <em>s1</em>, which denotes a orientation and position in space. Below is the code that creates the seek function:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">function</span></strong> Geometry.makeSeek(target)<br />
<em><span style="color:#236e25;">-- Goodness of trajectory from state 's0' to 's1'<br />
</span></em> <strong><span style="color:#881350;">function</span></strong> <span style="color:#400080;">seek</span>(s0, s1)<br />
<strong><span style="color:#881350;">local</span></strong> p0 = s0:position()<br />
<strong><span style="color:#881350;">local</span></strong> p1 = s1:position()<br />
<strong><span style="color:#881350;">local</span></strong> d1 = s1:direction()<br />
<br />
<em><span style="color:#236e25;">-- direction to target<br />
</span></em> <strong><span style="color:#881350;">local</span></strong> dir_target = (target-p1):unit() <br />
<br />
<em><span style="color:#236e25;">-- direction of from current point to next on path<br />
</span></em> <strong><span style="color:#881350;">local</span></strong> dir_path = (p1-p0):unit() <br />
<strong><span style="color:#881350;">return</span></strong> <span style="color:#0000ff;">0.25</span>*(<span style="color:#0000ff;">1</span> + d1*dir_target)*(<span style="color:#0000ff;">1</span> + dir_path * dir_target)<br />
<strong><span style="color:#881350;">end</span></strong><br />
<strong><span style="color:#881350;">return</span></strong> <span style="color:#400080;">seek</span><br />
<strong><span style="color:#881350;">end</span></strong></div>
<p>The details of the code is not important. It is mainly vector operations used to calculate how desirable <em>s1</em> is with respect to getting the non player character to the goal position <em>target</em>. The candidate <em>s1</em> position and orientations are produced elsewhere by considering places in space which does not cause collision with other objects etc. The code below shows how we would do this in Java. Since Java does not have closures we have to use classes:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">interface</span> Behavior {<br />
<span style="color:#881350;">public</span> <span style="color:#881350;">float</span> <span style="color:#003369;">goodness</span>(State s0, State s1);<br />
<span style="color:#881350;">public</span> <span style="color:#881350;">void</span> <span style="color:#003369;">add</span>(Behavior b, <span style="color:#881350;">float</span> weight); <br />
}<br />
<br />
<span style="color:#881350;">class</span> Seek <span style="color:#881350;">implements</span> Behavior {<br />
Vec2 target;<br />
<br />
<span style="color:#881350;">public</span> <span style="color:#003369;">Seek</span>(Vec2 target) {<br />
this.target = target;<br />
}<br />
<br />
<span style="color:#881350;">public</span> <span style="color:#881350;">float</span> <span style="color:#003369;">goodness</span>(State s0, State s1) {<br />
Vec2 p0 = s0.<span style="color:#003369;">position</span>();<br />
Vec2 p1 = s1.<span style="color:#003369;">position</span>(); <br />
Vec2 d1 = s1.<span style="color:#003369;">direction</span>(); <br />
<br />
<em><span style="color:#236e25;">// direction to target <br />
</span></em> Vec2 dir_target = (target.<span style="color:#003369;">minus</span>(p1)).<span style="color:#003369;">unit</span>();<br />
<br />
<em><span style="color:#236e25;">// direction of from current point to next on path<br />
</span></em> Vec2 dir_path = (p1.<span style="color:#003369;">minus</span>(p0)).<span style="color:#003369;">unit</span>();<br />
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0.25</span>*(<span style="color:#0000ff;">1.0</span> + d1.<span style="color:#003369;">dot</span>(dir_target))*(<span style="color:#0000ff;">1.0</span> + dir_path.<span style="color:#003369;">dot</span>(dir_target)); <br />
} <br />
}</div>
<p>The different behaviors will then be combined as follows:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Behavior flank = <span style="color:#881350;">new</span> <span style="color:#003369;">Flank</span>(player, <span style="color:#0000ff;">10</span>);<br />
Behavior seek = <span style="color:#881350;">new</span> <span style="color:#003369;">Seek</span>(target);<br />
Behavior combo = <span style="color:#881350;">new</span> <span style="color:#003369;">Combo</span>();<br />
combo.<span style="color:#003369;">add</span>(flank, <span style="color:#0000ff;">1.0</span>);<br />
combo.<span style="color:#003369;">add</span>(seek, <span style="color:#0000ff;">0.001</span>);</div>
<p>With Go we can write the code more like we did in the dynamic script language Lua.</p>
<code>
<pre>
func makeSeek(target Vec2) func(s0, s1 State) float {
func seek(s0, s1 State) float {
p0 := s0.position();
p1 := s1.position();
d1 := s1.direction();
// direction to target
dir_target := (target.minus(p1)).unit();
// direction of from current point to next on path
dir_path := (p1.minus(p0)).unit();
return 0.25*(1.0 + d1.dot(dir_target))*(1.0 + dir_path.dot(dir_target));
}
return seek;
}
</pre>
</code>
<p>The the different behavior can be combined in much the same way as it was combined in Lua. The last statement can be done in many ways. The code below is valid since Go supports functions with arbitrary number of arguments, but it can't be properly type checked at compile time.</p>
<code>
<pre>
flank := makeFlank(player, 10);
seek := makeSeek(target);
combo := makeCombo(flank, 1.0, seek, 0.001);
</pre>
</code>
<p>For stronger type checking one might want to do something like the code below, which requires defining a struct <em>Elem</em> with a function pointer and float member.</p>
<code>
<pre>
combo := makeCombo(List{Elem{flank, 1.0}, Elem{seek, 0.001}});
</pre>
</code>
<p>Doing something similar to the Java way is also possible:</p>
<code>
<pre>
var funcs FuncList;
funcs.Add(flank, 1.0);
funcs.Add(seek, 0.001);
combo := makeCombo(&funcs);
</pre>
</code>
<p>What should be apparent is how much boilerplate code one has to write when making Java programs:
<ol>
<li>Every method needs public in front</li>
<li>Type has to be repeated for every argument in method definition.</li>
<li>An interface has to be defined every time we want to simulare combining two functions.</li>
<li>The type of a variable has to be specified every time even though the compiler
should be able to figure it out based on assignment</li>
</ol>
</p>
<p>The lua code goes on to create new functions:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">local</span></strong> eval = Geometry.makeEval(combo, <span style="color:#0000ff;">0.5</span>, <span style="color:#0000ff;">0</span>)<br />
<strong><span style="color:#881350;">local</span></strong> larger = makeBinary(eval, greaterThan)</div>
<p>Since <em>eval</em> and <em>larger</em> have different function signatures, we would need to create two new interfaces in Java and two class implementing them. Each class will have two methods and some member variables. In contrast the Go solution would only require a total of 4 new functions. No classes or interfaces needs to be created.</p>
<h4>Conclusion</h4>
<p>As stated earlier the aim of this article was to make the case for Go over Java, so you have to excuse my bias. Java has improved a lot over the years. It used to be extremely verbose for simple things like reading and writing to console. Java 5 improved upon that a great deal. But as these examples show, Java still requires a lot of boilerplate code that doesn't add anything to readability and ability to abstract or reason about the problem you are trying to solve.</p>
<p>I have not touched upon other clear Go advantages like compile time, channels for easing concurrency programming etc. I wanted to show that even without Go's most touted features here is a lot to gain. As new versions of Java comes out new features are added which addresses deficiencies in the language. However the problem is that gradually Java loses some of its original appeal which was that it was a very simple language. Go starts with a small feature set than present Java, perhaps more comparable to the original Java. However the features are better selected so similar kind of expressive power as todays Java can be expressed with far fewer features. I think that is worth somthing</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com3tag:blogger.com,1999:blog-21339754.post-85780467920621526442009-11-22T15:52:00.001-08:002009-12-02T11:45:35.018-08:00How does Go fit in with the C-family of languages<p>The C-family of languages is a pretty large group. It could be discussed which language is fit and which don't but I think it's worthwhile including the languages: C, C++, Objective-C, Java, D, C#.</p>
<p>First we got C++ which is trying to extend C into an object oriented language by taking inspiration from Simula. And then came Objective-C which took a more dynamic approach to object oriented programming and tried essentially to embed smalltalk in to C.</p>
<p>Years later we got Java which try to retain the simplicity of Objective-C compare to C++ but give it a more C++ looking syntax. C# tried to create better Java by using the basic ideas of Java but allow more the power of C++.</p>
<p>And right in the middle there comes D and tries to be what C++ should have been, or rather could have been if we could throw the nasty bits out.</p>
<p>A lot has been centered around C++, and I feel Go is about going back to the roots. Go feels a lot more like C: </p>
<ul>
<li>Fairly simple and clean language. Compared to C++ with its huge feature list</li>
<li>No inheritance</li>
<li>No classes</li>
<li>No constructors and destructors. No RAII</li>
<li>You just allocate structs. No code is run like on C++. Like C you have to create
separate initialization functions</li>
<li>The standard library is very similar to the C standard library. The way you deal with IO, strings etc, feel much more like C than C++</li>
<li>In C++ structs are not just simple structs. They can contain invisible fields you didn't explicitly put there like a vtable. In Go structs are just like C structs. There is no vtable</li>
</ul>
<p>Unlike Java and C# there is no virtual machine. However like those and unlike C++ if a crash happens you are not left in the cold. You get a stack backtrace on the console. In this respect Go is more in the C++ and D camp than in the Java and C# camp. While Java, C# and D have been about trying to keep the basic ideas from C++ and fix them, Go seems to be more about getting duck typing like found in languages like Python, Ruby into the C-family. Or perhaps one could say the designer skipped the whole class and inheritance thing and looked at all the great stuff done with C++ templates which essentially gives compile time duck typing and decided that was a model on to witch Go's dynamic dispatch should be built on</p>
<p><strong>Benefits of Go's interface type</strong></p>
Go doesn't use inheritance but achieve much the same through interfaces. However interfaces have some benefits over the traditional approach I'd like to highlight.
Below I am showing two code examples illustrating a struct or class <em>B</em> with a corresponding interface name <em>A</em>.
<code>
<pre>
struct A {
virtual int alfa(int a) = 0;
virtual int beta(int b) = 0;
};
struct B : public A {
int alfa(int a);
int beta(int b);
int d;
};
int B::alfa(int a) {
return d + a;
}
int B::beta(int b) {
return d + b + 3;
}
</pre>
</code>
Below is the Go version of the code above:
<code>
<pre>
type A interface {
alfa(a int) int;
beta(b int) int;
}
type B struct {
d int;
}
func (m B) alfa(a int) int {
return m.d + a;
}
func (m B) beta(b int) int {
return m.d + b + 3;
}
</pre>
</code>
In C++ we can call the methods defined on the struct <em>B</em> through its interface <em>A</em> like this:
<code>
<pre>
int x, y;
B b;
b.d = 3;
A *a = &b;
x = a->alfa(1);
y = a->beta(2);
</pre>
</code>
Likewise in Go:
<code>
<pre>
var x, y int;
b := B{3};
var a A = b;
x = a.alfa(1);
y = a.beta(2);
</pre>
</code>
On the surface this looks very similar but there are some notable differences. In C++ the inheritance hierarchy can be arbitrarily deep and so dynamic dispatch is dependent on traversing a vtable on a class to its superclass on so on until the correct implementation is found. In Go there is no inheritance tree, so each method will be accessed as if a single function pointer. In this way Go is more similar to how you typically create some kind of polymorphism in C. In C it is common to define structs with lists of function pointers, which are changed depending on type etc.
The other difference is that if you change the code on Go to this:
<code>
<pre>
var x, y int;
b := B{3};
x = b.alfa(1);
y = b.beta(2);
</pre>
</code>
Then the calls are resolved statically. There is no function pointer lookup, simply because there are no virtual functions in Go. If you call a method on a struct in Go, the compiler knows exactly what method you are calling.
However if you change the code in C++ likewise, you are still making a virtual method call. To be fair in some cases the compiler can figure out the right method call. But the <em>bp</em> pointer could have been passed around and you can't know if it points to a subclass of <em>B</em> or not. With Go, this problem doesn't exist since structs don't have subclasses, making the job much easier for the compiler.
<code>
<pre>
int x, y;
B b;
b.d = 3;
B *bp = &b;
x = bp->alfa(1);
y = bp->beta(2);
</pre>
</code>
<p><strong>Better consistency in type system</strong></p>
Having methods on structs be statically resolved also makes it trivial to support methods on basic types like ints and floats. Something which isn't possible in C++ or Java. The reason that isn't possible is because ints and floats don't have vtables. While in Go you don't need a vtable to have methods, so it is not a problem. In my view this blurs the distinction between basic types like ints and objects which are e.g. in Java two clearly different things. That is a good thing since it creates better consistency. There are less special cases.
<p><strong>How to deal with missing features in Go</strong></p>
A lot of people will probably complain about all those C++ not found in Go. E.g. without constructors and destructors how does one handle resources safely through RAII? Go doesn't really need RAII because it has closures. That is used extensively in e.g. Ruby to get the same benefits.
E.g. here is some code I wrote that opens a file, reads one line at a time and closes the file.
<code>
<pre>
ReadLines("struct-template.h", func(line string) {
fmt.Printf(doStuffWithLine(line));
})
</pre>
</code>
The <em>ReadLines</em> function was implemented like this:
<code>
<pre>
func ReadLines(file_name string, fn func(line string)) {
file, err := os.Open(file_name, os.O_RDONLY, 0);
defer file.Close();
if file == nil {
fmt.Printf("can't open file; err=%s\n", err.String());
os.Exit(1);
}
in := bufio.NewReader(file);
for s, err := in.ReadString('\n'); err != os.EOF; s, err = in.ReadString('\n') {
fn(s)
}
}
</pre>
</code>
Which shows another way to deal with RAII. One can use <em>defer</em> which will call method after defer when function goes out of scope.
Exceptions are not present in Go either but you can mimic the kind of error handling you do with exceptions by using multiple return values were one signals error and use the named return value feature.
<code>
<pre>
func ProcessFile(file_name string) (err os.Error) {
file, err := os.Open(file_name, os.O_RDONLY, 0);
// ...
return;
}
</pre>
</code>
In the simple example above <em>err</em> will automatically be bound to the return value. So if the function didn't handle the error returned in <em>err</em> it will be automatically propagated to the calling function.Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-38910082782174332722009-11-22T06:05:00.001-08:002009-12-02T11:46:35.407-08:00First impressions from the Go programming language<p>I spent the last week or so learning the new programming language release from Google called Go. The first program I written this a simple text processing tool. That is the kind of tools I've previously written in Python and Ruby. I would say that Python is still better suited for this kind of job. However that is mainly due to less functional regular expressions and string libraries found in Go. Although it is unavoidable that when using a statically typed language there is a bit more overhead. In particular with respect to typing.</p>
<p><b>Compared to regular statically typed languages</b></p>
<p>However for a statically typed language I can't find anything that can compare. I could write code in a manner to remind me a lot of how it feels to write code and script language. Other languages which I'm familiar with like C++, Objective-C, Java are much more verbose and clunky to use. Those are languages which encourage much more planning and feels better suited for larger applications.</p>
<p>C is a simple language but it lack so much features that it becomes cumbersome to do string processing. No string class and a bit too code spent managing memory.</p>
<p><b>Compared to Haskell and C#</b></p>
<p>Of course there are other statically typed languages like Haskell and C#. Now Haskell is probably a more innovative and elegant language then Go. But it is also the language requires much more understanding before it can be used productively. Most developers are not intimate with the functional languages. Especially pure functional languages like Haskell. With Go on the other hand I could use the skills I had developed while using languages like Python, C++ and C. That meant I could be productive quite quickly. With a sharp unafraid to comment because so much has happened without language to last year's and I haven't used in years. I know at least that the C# that I used to use could not compete with Go in ease-of-use.</p>
<p><b>Compared to Scheme</b></p>
<p>I have written text processing utility and scheme previously. The whole development process is nicer than most other languages I think. Mainly because of the interactive style development that scheme allows. I could quickly and easily test segments of my code in the interactive shell because everything in scheme is an expression. In this respect go is as cumbersome as any other traditional language. You run test your program one file at a time.</p>
<p>Of course came that same problem as Haskell. You can easily reuse programming skills developed while using C and C++ for many years. It's cumbersome to get used to reading scheme code that takes time to get used to what functions are called and how to print special characters like newline etc.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-87696989957451450962009-11-22T05:33:00.001-08:002009-12-02T11:47:20.012-08:00Advantages of C over C++It has been claimed that C++ is a better C them C. this is being taken to mean that when switching to C++ you can continue to code more or less as she did in C and use a little extra C++ functionality for convenience. The problem with that is that a lot of things which are perfectly safe to do and see are not safe to do while using C++.
So here is my list of issues not found in C. You can avoid many of these issues in C++ by limiting what features you use. But you never have any guarantees. You can't pick up random C++ code, look at it and be certain whether it is doing something safe or not when e.g. statically initializing a variable.
<ul>
<li>Static initialize is safe in C but not in C++, because in C++ static initialization can cause code to run, which depends on other variables having been statically initialized. It can also cause cleanup code to run at shutdown which you can't control sequence of (destructors).</li>
<li>C gives you better control over what happens when your code is executed. When reading seek out it is fairly straightforward to decipher one code is getting executed and when memory is just restart or primitive operations are performed.
In C++ on the other hand your have to deal with several potential problems:
<ul>
<li>A simple variable definition can cause code to run (constructors and instructors)</li>
<li>Implicitly generated and called functions. If you didn't define constructors, destructors and <em>operator=</em> you will get them generated for you.</li>
</ul>
See <a href="http://www.rachelslabnotes.com/2009/10/the-hidden-cost-of-c/">hidden cost of C++</a> or <a href="http://yosefk.com/c++fqa/defective.html.">Defective C++</a></li>
<li>C supports variable sized arrays on the stack. Which is much faster to allocate than on the heap. (C99 feature)</li>
<li>No name mangling. If you intend to read generated assembly code, this makes that much easier. It can be useful when trying to optimize code.</li>
<li>De facto standard application binary interface (ABI). Code produced by different compilers can easily be combined.</li>
<li>Much easier to interface with other languages. A lot of languages will let you call C functions directly. Binding to a C++ library is usually a much more elaborate job.</li>
<li>Compiling C programs is faster than compiling C++ programs, because parsing C is much easier than parsing C++.</li>
<li>Varargs cannot safely be used in C++. They're not entirely safe in in C either. However they're much more so in the C++, to the point that they are prohibited in the C++ coding standards (Sutter, Alexandrescu).</li>
<li>C requires less runtime support. Makes it more suitable for low-level environments such as embedded systems or OS components.</li>
<li>Standard way in C to do encapsulation is to forward declare a struct and only allow access to its data through functions. This method also creates compile time encapsulation. Compile time encapsulation allows us to change the data structures members without recompilation of client code (other code using our interface). The standard way of doing encapsulation C++ on the other hand (using classes) requires recompilation of client code when adding or removing private member variables.</li>
</ul>
Disliking C++ is not a fringe thing. It does not mean that one is not capable of understanding complex languages. Quite a lot of respect computer science people and language designers aren't fond of C++. See <a href="http://gigamonkeys.com/blog/2009/10/16/coders-c++.html">C++ coders</a>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com4tag:blogger.com,1999:blog-21339754.post-45173355887472153632009-09-10T12:33:00.001-07:002009-12-02T11:49:03.208-08:00Grand Central Dispatch versus Qt Concurrent<p>Lately we have been provided with several different solutions to how we can utilize multicore CPUs. Previously we had few other options besides creating multithreaded applications explicitly. By explicitly I mean the developer had to create and manage threats manually.</p>
<p>Both Grand Central dispatch and Qt Concurrent is based on the idea that the developer submits chunks of work to a library which will distribute the chunks of work onto several different threads which can be running on several different cores.</p>
<p>Below is a code example showing Qt's approach to distributing chunks of work to different threads. To use the right terminology each chunk of work is called a task. The code below shows how a strain is split by running the splitting method in a separate thread.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><em><span style="color:#236e25;">// call 'QStringList QString::split(const QString &sep, <br />
// SplitBehavior behavior, <br />
// Qt::CaseSensitivity cs) const' <br />
// in a separate thread<br />
</span></em>QString str = <span style="color:#760f15;">"comma, separated, text"</span>;<br />
QFuture<QStringList> future = QtConcurrent::<span style="color:#003369;">run</span>(str, <br />
&QString::split, <br />
<span style="color:#003369;">QString</span>(<span style="color:#760f15;">", "</span>), <br />
QString::KeepEmptyParts, <br />
Qt::CaseSensitive);<br />
<em><span style="color:#236e25;">// Do some more processing<br />
</span></em>cout << <span style="color:#760f15;">"str may or may not have been split at this point"</span> << endl;<br />
QStringList result = future.<span style="color:#003369;">result</span>();<br />
<br />
<em><span style="color:#236e25;">// Output split elements on new lines<br />
</span></em><span style="color:#003369;">foreach </span>(QString s, result)<br />
cout << s << endl;</div>
<p><b>Future objects</b></p>
<p>Run, is an asynchronous call. It will hand over its task to a separate thread, return and continue execution of the main thread. Qt uses the concept of a future object to store results from asynchronous method calls. The QFuture object won't actually contain the results of this split method until it's queried for the results. It's a separate thread has finished processing one the future objects is queried it simply returns the results. If it hasn't, the main thread will be blocked until separate thread finishes.</p>
<p>The next piece of code shows Grand Central's approach to the same problem. Instead of using future objects, to synchronize between different threads, Grand Central uses so called dispatch queues. the developer submits tasks to different queues, then Grand Central will dequeue the tasks and place them on to different threads. Tasks in different queues always run concurrently with respect to each other. However task within the same queue might or might not run concurrently with each other, depending on the type of queue. There are two types of cues: serial cues and concurrent queues. In a serial queue a task is not performed until the previously enqueued task has been performed. In the code below we create a serial queue.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><em><span style="color:#236e25;">// Creates a serial queue for tasks, where the next<br />
// task is performed when the previous is finished<br />
</span></em>dispatch_queue_t my_queue;<br />
my_queue = <span style="color:#003369;">dispatch_queue_create</span>(<span style="color:#760f15;">"my queue"</span>, <strong><span style="color:#881350;">NULL</span></strong>);<br />
<br />
QString str = <span style="color:#760f15;">"comma, separated, text"</span>;<br />
__block QStringList result;<br />
<span style="color:#003369;">dispatch_async</span>(my_queue, ^{<br />
result = str.<span style="color:#003369;">split</span>(<span style="color:#760f15;">", "</span>, QString::KeepEmptyParts, Qt::CaseSensitive);<br />
});<br />
<br />
<br />
<em><span style="color:#236e25;">// Do some more processing<br />
</span></em>cout << <span style="color:#760f15;">"str may or may not have been split at this point"</span> << endl;<br />
<br />
<span style="color:#003369;">dispatch_sync</span>(my_queue, ^{<br />
<em><span style="color:#236e25;">// Output split elements on new lines<br />
</span></em> <span style="color:#003369;">foreach </span>(QString s, result)<br />
cout << s << endl;<br />
});</div>
<p><b>Code blocks</b></p>
<p>In Grand Central dispatch a task is represented by a code block. Code blocks is an extension by Apple to the C-language and C++. In languages like Ruby and Python code blocks are referred to as closures. Essentially they let you define a function within another function. This function has access to all variables defined outside its scope and any variable referred to in the code block will continue to live even after the outer scope no longer exists. But only as read-only variables. If you want to write to a variable outside the scope of the block, then you have to prefix variable with <code>__block</code>.</p>
<p><b>Asynchronous dispatch</b></p>
<p>
<code>dispatch_async()</code> is similar to the run method in Qt. The provided code block is passed on to a separate thread amber function call returns immediately to the main thread. The code splitting the string will run in parallel to all the code it follows after the asynchronous dispatch. But since we don't have a future object, how can we safely read the results variable in the main thread? Normally one would have solved for these kind of thread issues with semaphores.
</p>
<p><b>Synchronous dispatch</b></p>
<p>
There are two ways to read the result variable safely. One can dispatch a task either asynchronous or synchronous to the same queue, which reads the result variable. Since the queue we have created is a serial queue, the reading task would not be executed until all previously and queued tasks have been finished. In this case we have chosen to call <code>dispatch_sync()</code>. It will block the main thread until a submitted task has been executed. Thus, in this case we know that after the <code>dispatch_sync()</code> call the only running thread is the main thread.
</p>
<p><b>Conclusions</b></p>
<p>
The metaphor used by queue might seem simpler to understand and deal with, but it trades and simplicity for flexibility. The biggest problem is that there is no obvious way to control access to shared resources without using semaphores. With Grand Central's approach one can take code that used to be in a critical section, plays in a code block and do a dispatch to a queue. Instead of protecting the access in to share resource with the same semaphore, one can protect the access by submitting tasks to the same serial queue. This will make sure that all code that accesses a share resource does so in strict sequence. This is a clear benefit over using semaphores, because with semaphores one does not know in which sequence the threads access a shared resource.
</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-14063455455326005132009-05-01T16:21:00.001-07:002009-12-02T11:51:49.005-08:00C++ is a horrible language<p>Over the years my dislike of C++ has increased. It seems like the more I know about language the more I dislike it. I believe that Linus Torvalds summed it up pretty well when he said:</p>
<blockquote>
<p><span class="caps">IOW</span>, C++ is in that inconvenient spot where it doesn’t help make things <br />
simple enough to be truly usable for prototyping or simple <span class="caps">GUI</span> <br />
programming, and yet isn’t the lean system programming language that C is <br />
that actively encourags you to use simple and direct constructs</p>
</blockquote>
<p>This comment is from the now infamous git mailing list, where Linus Torvalds details why you think C++ is a horrible language. I think the problem for most people dispute this point is that they don’t get what latest means when he says that C++ is a horrible language. For creating higher-level abstractions and large programs C++ is obviously better. But that is a silly way to compare the two languages. For what C. has been designed for does his job very well. It allows you to write low-level and high-performance code. <em>C++ on the other hand is a jack of all trades and master of none</em>.</p>
<p>The classic rebuttal of this from C++ fans, is that C++ can do everything that C. can do, so there’s no need to use C. But this is simply not true. A C programmer can read a statement and know fairly well how many objects are allocated on the heap or on the stack and know how many function calls are performed. This is almost impossible in C++. Due to the existence of constructors, descriptors and operator overloading a wise things can happen in the simplest statements.</p>
<p>In <em>theory</em> you can write C++ code which has higher performance than corresponding C code. However since C++ gives you less control over what happens in each statement, there is a higher chance that you doing something which kills performance then in C.</p>
<p>The other classic statement from C++ fans is that:</p>
<blockquote>
<p>you can just use the subset of the language which you are comfortable with. </p>
</blockquote>
<p>However even a small subset of C++ requires that you understand quirky attributes of the language. For instance, I would argue that using statically initialized variables is nothing fancy. You can do this easily in C. However in C++ this require a lot more care. Unlike C declaring static variables in C++ can cause code to run during initialization. Typically this will be the code for the constructors of the objects statically initialized. You cannot control in which sequence this code is executed. This can cause very hard to find bugs.</p>
<p>This is just one of many examples. You can just read <em>Scott Meyers book Effective C++</em> to find a long list of stuff you have to be aware of C++ to not shoot yourself in the foot. There are loads of books like this for C++. I do not know of any comparable books for the C-language.</p>
<p>The C-language is in no way perfect, but I simply don’t get why the deficiencies of C, should be remedied by using C++. If you need higher level abstractions or a more type safe language why not use other languages like OCaml, Haskell, <span class="caps">LISP</span> or Python? Say you select Python. It is not as high performance as C++, but in the places where you need performance she could implement the functionality in C-language. There is no need to use C++, because you’re not writing a large program where you need to be able to build abstractions. You are building a simple module, where advanced abstractions are not needed.</p>
<p>People will point to libraries like Qt as proof that you can write easy to understand code in C++. I however think it’s proof of the opposite. Not because I think Qt is bad. Quite the contrary is one of the better <span class="caps">GUI</span> toolkits out there. However that is not because C++ is a great language but because they essentially invented their own language by adding a preprocessor. It’s not that different from how Objective-C was originally made by adding a preprocessor to the C-language. In C++ was such a versatile and flexible language as some claim, there would never have been a need for the moc preprocessor.</p>
<p>I am not saying this as C developer who has just started dabbling in C++. I’ve been doing C++ all my professional life. I never used C very much at all. The realizations have just dawned upon me while working several years on large software projects in C++. Very little of our time is spent on writing performance critical code. In fact most of it is some sort of glue code. Code to make different components talk to each other and tell each other about changes that have just happened to them. All stuff that a script language like Python would have done much better.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com5tag:blogger.com,1999:blog-21339754.post-23676962425677443862008-09-23T12:48:00.001-07:002009-05-01T15:30:07.278-07:00On old and new in the Computer ScienceI remember several years ago in undergrad listening to my old professor. He would always jabber on about LISP. Back then I remember thinking, gee give it a break man, the language is what? 50 years old? Isn't it time to move on?<br />
<br />
These last weeks I have finally been able to play around with a dialect of LISP called PLT Scheme. It got triggered by reading the so called Wizard book: "Structure and Interpretation of Computer Programs"<br />
<br />
I must say I am very impressed with Scheme. I am still not sure if I could ever be fully comfortable with the language because of all the annoying parenthesis. But the power and flexibility of the language is mind-boggling. And there is something beautiful about that ultra simple syntax. Everything is just words separated by spaces and enclosed by parenthesis. That is all there is to it!<br />
<br />
Compare that to the extremely complicated syntax in C++. <br />
<br />
I have realized that thinking that new is always better than old is the dumbest belief I ever had. Sometimes new stuff is old already at the instance it is made, while old stuff is modern 20 years later.<br />
<br />
An example of this is the code base of our program at work. A lot of the code is crap and badly thought out. At first I assumed it was the old code that was bad. Then I realized that in fact the old stuff is what was fairly good and the crap was mostly new stuff. <br />
<br />
The stuff is a maintenance nightmare because nothing is commented. I talked to a guy who talked about a system he worked on that was from the 50s or 60s or something. The New York subway system I believe. I asked if that wasn't hard to do given that the original makers were probably not around anymore. But it wasn't because as he explained for each source code file there was a corresponding document file explaining that source code file in detail. It was all very well documented. <br />
<br />
I wonder how could they get software design right 50 years ago and we manage to screw it up today?<br />
<br />
Same with LISP, mainstream languages are only now starting to get some of the more important LISP features. How could they get it right that long time ago, and we mange to screw it up today with languages such as C++ and Java?<br />
<br />
I increasingly realize that languages like C++, Java , C# etc seldom offer anything new. They are just the same shit in new wrapping. New wrapping in this case means new syntax.<br />
<br />
But perhaps the biggest eye opener with respect to the value of old stuff, is when I read Adam Smiths the Wealth of Nations. It is something like 250 years old. I had read other new books about economic history and theories for this and that about the historical economic development of China, Africa etc. Surprisingly I found Smith treatment of the same much clearer, well reasoned and thought out. In short he offered a much better explanation than these newer books. But this isn't the first time. People like Ricardo and other economist have come after Smith and presumable proved him wrong only to have later economist discover that Smith was right all along.<br />
<br />
Perhaps talking of old and new as if it corresponded to bad and good is about as stupid as saying that derivation and integration is bad because now we have newer mathematical methods. A good idea is a good idea no matter how old it gets.<br />Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com1tag:blogger.com,1999:blog-21339754.post-56780124768070376952007-09-12T14:08:00.000-07:002007-09-12T14:14:52.225-07:00Why Objective-C is coolI've been asked to do an intro about Cocoa. So I thought about what would I tell people about Cocoa if I had some time. Sure I could throw up a quick tutorial on how to code a Cocoa app showing a bare minimum of how Objective-C works. But there are a million of tutorials like that and it doesn't really do the Cocoa justice. I want to give people an idea of why Cocoa or perhaps more specifically Objective-C is cool.
I think anybody who has played computer games to some extent know how the game Doom (1993) from id software revolutionized gaming on the PC platform. What few people know was the the game was actually not developed at the PC platform at all.
At the time Doom was developed in 1992, Windows 95 didn't exist yet and software development on PC's looked like this:
<img alt="" border="0" style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px;" src="http://upload.wikimedia.org/wikipedia/en/7/7a/Turbo_C%2B%2B_3.0.png">
Not very impressive. Instead it was developed on computers from NextStep, the company founded by Steven Jobs after he was kicked out of Apple. As <a href="http://rome.ro/2006/12/apple-next-merger-birthday.html">id developer John Romero says:</a>
<blockquote>Why do I care so much about NeXT computers? Because we at id Software developed the groundbreaking titles DOOM and Quake on the NeXTSTEP 3.3 OS running on a variety of hardware for about 4 years. I still remember the wonderful time I had coding DoomEd and QuakeEd in Objective-C; there was nothing like it before and there still is no environment quite like it even today.</blockquote>
To get an impression of how far ahead of its time Objective-C and what is now known as Cocoa was ahead of its time, consider this:
<blockquote>In fact, with the superpower of NeXTSTEP, one of the earliest incarnations of DoomEd had Carmack in his office, me in my office, DoomEd running on both our computers and both of us editing one map together at the same time. I could see John moving entities around on my screen as I drew new walls. Shared memory spaces and distributed objects. Pure magic.</blockquote>
<img alt="" border="0" style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px;" src="http://rome.ro/uploaded_images/qe_dev-726646.gif" border="0">
Consider how long time ago that was, CORBA didn't support C++ for remote method invokation until 1996. At the time id did this it was just in its infant stage. Not to mention this was long long before Java RMI made remote objects populare and mainstream. It isn't even common to do this kind of thing today, and yet this was something that was quite trivial to do in Objective-C and Cocoa back in 1992.
<p><b>Introducing Objective-C</b></p>
Every time someone introduce you to Objective-C they will tell you about what a small and simple language it is. How easy it is to learn etc. But that was not my impression when I first tried to learn it. At the time I knew C++. When I saw an example of how to declare a class:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">@interface</span> MyClass : <span style="color:#400080;">NSObject</span><br />
{<br />
<span style="color:#881350;">int</span> mSomeNumber;<br />
<span style="color:#400080;">NSString</span>* mSomeString;<br />
}<br />
<br />
- (<span style="color:#881350;">int</span>)<span style="color:#6c0540;">someNumber</span>;<br />
- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">setSomeNumber:</span>(<span style="color:#881350;">int</span>)aNum;<br />
<span style="color:#881350;">@end</span></div>
I didn't think it looked easy or simple at all. Especially when I saw some code examples:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">drawRect:</span>(<span style="color:#400080;">NSRect</span>)frameRect<br />
{<br />
<span style="color:#236e25;">// Fill whole background with white<br />
</span> [[<span style="color:#400080;">NSColor</span> <span style="color:#6c0540;">whiteColor</span>] <span style="color:#6c0540;">set</span>];<br />
[<span style="color:#400080;">NSBezierPath</span> <span style="color:#6c0540;">fillRect:</span>frameRect];<br />
<br />
<span style="color:#236e25;">// Construct rows of lines<br />
</span> <span style="color:#400080;">NSBezierPath</span> *gridLines = [[<span style="color:#400080;">NSBezierPath</span> <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">init</span>];<br />
<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">unsigned</span> row = <span style="color:#0000ff;">0</span>; row <= mNoRows; ++row) {<br />
[gridLines <span style="color:#6c0540;">moveToPoint:</span> <span style="color:#6c0540;">rowLeft</span>]; <br />
[gridLines <span style="color:#6c0540;">lineToPoint:</span> <span style="color:#6c0540;">rowRight</span>];<br />
rowLeft.y += rowHeight;<br />
rowRight.y += rowHeight; <br />
} <br />
<br />
<span style="color:#236e25;">// Set style to draw line in<br />
</span> [[<span style="color:#400080;">NSColor</span> <span style="color:#6c0540;">blackColor</span>] <span style="color:#6c0540;">set</span>];<br />
[gridLines <span style="color:#6c0540;">setLineCapStyle:</span><span style="color:#400080;">NSSquareLineCapStyle</span>];<br />
<br />
<span style="color:#881350;">float</span> pattern[<span style="color:#0000ff;">2</span>] = {<span style="color:#0000ff;">2.0f</span>, <span style="color:#0000ff;">5.0f</span>};<br />
[gridLines <span style="color:#6c0540;">setLineDash:</span>pattern <span style="color:#6c0540;">count:</span><span style="color:#0000ff;">2</span> <span style="color:#6c0540;">phase:</span><span style="color:#0000ff;">0.0f</span>];<br />
<br />
<span style="color:#236e25;">// Draw specified lines in given style<br />
</span> [gridLines <span style="color:#6c0540;">stroke</span>];<br />
[gridLines <span style="color:#ff0000;">release</span>]; <br />
}<br />
</div>
The code below is just to give an idea of what Objective-C syntax is like. What the code does is not important. It has been simplified a bit to understand better. It basically draws several rows with black lines on a white background. When I first saw this kind of code I didn't think it looked easy at all. And I thought whoever said that Objective-C was a much simpler language than C++ must have been smoking something that wasn't good for them.
The problem was that I was too caught up in the syntax. The syntax is indeed very unusual, but if one looks beyond that, the structure of the syntax is actually quite simple.
Perhaps the best way to understand the syntax is to understand, why it looks so strange.
<p><b>Background</b></p>
Unlike C++, Objective-C wasn't really a new language at all originally. There was no Objective-C compiler. Instead Objective-C was just plain old C, with an <a href="http://docs.mandragor.org/files/Programming_languages/Objective-C/Objective-C_Infrequently_Asked_Questions_en/q1.htm">added preprocessor</a>. As any C/C++ developer worth his/her salt should know all statements starting with # specifies a preprocessor directive in C/C++. E.g. <code>#define</code> and <code>#include</code>. The preprocessor runs before the compiler and replaces the directives with actual C/C++ code.
What the makers of Objective-C did was to add another preprocessor, but instead of marking the directives with #, they marked them with @. That way their special preprocessor could find all the new directives easily.
While this does make Objective-C look like some alien entity inside C, it does make it trivial to distinguish between pure C code and the add-ons from Objective-C. Unlike C++, where what is C and what is C++ is blurred. Actually C++ isn't a strict superset of C like Objective-C although it usually compiles C code. The reason being among other things that C++ reinterprets a lot of regular C code into new C++ concepts. E.g. a <i>struct</i> isn't just a <i>struct</i> anymore but actually a class in C++.
<p><b>Syntax inspiration from smalltalk</b></p>
The second stumble block in order to understand Objective-C syntax is to realize it is derived from Smalltalk, while C/C++ syntax is derived from Algol. Algol based languages separate arguments with comma, while smalltalk separates with name of argument and colon. Below is a code snippet that demonstrates setting the position and dimension of a rectangle object.
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Smalltalk <br />
</span>rectangle <span style="color:#6c0540;">setX:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">y:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">width:</span> <span style="color:#0000ff;">20</span> <span style="color:#6c0540;">height:</span> <span style="color:#0000ff;">20</span><br />
<br />
<span style="color:#236e25;">// Objective-C<br />
</span>[rectangle <span style="color:#6c0540;">setX:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">y:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">width:</span> <span style="color:#0000ff;">20</span> <span style="color:#6c0540;">height:</span> <span style="color:#6c0540;">20</span>];<br />
[rectangle <span style="color:#6c0540;">setX1:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">y1:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">x2:</span> <span style="color:#0000ff;">20</span> <span style="color:#6c0540;">y2:</span> <span style="color:#6c0540;">20</span>];<br />
<br />
<span style="color:#236e25;">// C++<br />
</span>rectangle-><span style="color:#003369;">set</span>(<span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">20</span>, <span style="color:#0000ff;">20</span>);</div>
The Smalltalk/Objective-C syntax improves readability of code. When specifying a rectangle some libraries use the start and end coordinates while others use start coordinates and size. With Smalltalk/Objective-C it is made quite clear what is done. While with C++ it is not clear whether the first or the second line of Objective-C code is used. Unlike Smalltalk Objective-C methods are called by enclosing call with []. This was originally to aid the preprocessor in extracting what was regular C code and what was Objective-C specific code.
<p><b>A small and simple language</b></p>
What do we exactly mean by saying that Objective-C is a small an simple language. C++ looks simpler syntax wise for the novice. However as said before syntax in deceiving. C++ add a host of new features to the C language: classes, virtual methods, non-virtual methods, constness, templates, friend classes, multiple inheritance, pure virtual functions, operator overloading, function overloading, private, public and protected members, constructors etc.
Objective-C on the other hand add very little, it is just classes, methods, categories and protocols. There is only one way to create classes and to inherit form them. You can specify whether inheritance is public or private e.g. There is only one kind of methods. There is no distinction between virtual and non-virtual. They can't be const or not const. The concept of constructors and destructors don't exist. Instead these are just normal methods.
As mentioned Objective-C is so simple they didn't even need to create a new compiler. A preprocessor was enough. This can make Objective-C seem overly simple. How can you do much with a language like that? The answer is the Objective-C runtime. The runtime is in fact what makes most of the Objective-C magic happen. Most of Objective-Cs features is provided at runtime and not done at compile time.
<p><b>Methods and Selectors</b></p>
Objective-C differs from C++ in that one distinguish between methods and selectors. To call a method on an Objective-C method you send a message. The method is not called directly. Instead Objective-C determines based on the selector which method to call.
Conceptually one can think of a message (selector) as simply a string with a list of arguments. The code below e.g. send the message <code>setX:y:width:height:</code> to object <code>rectangle</code>. This will invoke a method on rectangle if it understands the message.
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">[rectangle <span style="color:#6c0540;">setX:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">y:</span> <span style="color:#0000ff;">10</span> <span style="color:#6c0540;">width:</span> <span style="color:#0000ff;">20</span> <span style="color:#6c0540;">height:</span> <span style="color:#6c0540;">20</span>];<br />
</div>
If we think of Objective-C as just a preprocessor as it originally was, all message sending is replaced with a call to a C function:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">id</span> <span style="color:#003369;">objc_msgSend</span>(<span style="color:#881350;">id</span> theReceiver, <span style="color:#881350;">SEL</span> theSelector, ...)</div>
So when the preprocessor encounters our rectangle message it is translated into something like this:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#003369;">objc_msgSend</span>(rectangle, <span style="color:#760f15;">"setX:y:width:height:"</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">20</span>, <span style="color:#0000ff;">20</span>);</div>
<code>objc_msgSend</code> queries rectangle for its class and then queries the class for methods it contains. Then it finds out which method corresponds to the given selector. The method is nothing but a function pointer of the form:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">id</span><span style="color:#003369;"> </span>(*IMP)(<span style="color:#881350;">id</span>, <span style="color:#881350;">SEL</span>, ...)</div>
Of course this is a simplified explanation. In reality every selector is registered. Meaning we don't pass newly created strings to <code>objc_msgSend</code> each time we invoke a method but a pointer to a string. This pointer has to be unique. So we can't pass any string pointer.
So if we got å string we can find the unique pointer to this string by using the function:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#003369;">sel_registerName</span>(<span style="color:#881350;">const</span> <span style="color:#881350;">char</span> *str)</div>
Which returns the pointer if it exist or registers the selector as a unique string pointer if it doesn't exist. The benefit of this is that selectors can be looked up quickly and compared quickly by just comparing their pointer addresses rather than comparing each character of the string.
<p><b>Unique dynamic features of Objective-C</b></p>
I don't intend to go into every minor of the Objective-C runtime library but the example above should give an idea of how the dynamic features of Objective-C works.
<p><b>Classes</b></p>
In C++ classes don't exist passed compile time. Or at least in modern C++ compilers which support runtime type identification classes exist in a watered down sense in that one can query if two classes have a relationship.
In Objective-C it is almost opposite. Classes don't exist at compile time but rather are runtime entities. Classes are registered at runtime with function:
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">void</span> <span style="color:#003369;">objc_addClass</span>(<span style="color:#881350;">Class</span> myClass);</div>
Likewise methods and selectors are registered at runtime. So classes can in fact be modified at runtime. Of course users don't call <code> objc_addClass</code>. These methods along with the ones that registers methods are generated by the preprocessor from the class definition provided by the programmer.
But it is this fact that classes and methods exist as structures in memory at runtime that allows the programmer at runtime to query classes about their member variables and functions and whether they respond to a selector or not.
In fact an Objective-C developer could create an application which could let user specify classes to call and functions to call. User could just type in the name of a class. Then developer could use C function <b>NSClassFromString()</b> to get corresponding class. <b>NSSelectorFromString()</b> could be used to retrieve the selector. With this one could query class further about arguments existing for selector etc.
To learn more about Objective-C look at <a href="http://en.wikipedia.org/wiki/Objective-C">wikipedia</a>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com9tag:blogger.com,1999:blog-21339754.post-75787210283067457592007-09-08T15:13:00.000-07:002007-09-08T15:58:41.309-07:00The right tool for the job: C++ vs Objective-C<p>I read <a href="http://akosut.com/log/2004/05/19/c-vs-objective-c/">Alexei's blog entry about C++ vs Objective-C</a>. I agree fully with his statement that:</p>
<blockquote>The argument (which you can go and read yourself) boils down to “C++ can do everything Objective-C can” versus “sure it can, but not easily, usably or understandibly.” The usual argument comes down to object models: With Objective-C, you can send any object any message, and if it understands it, it will respond. C++ is restricted by its compile-time type system, so that even if you have arbitrary objects that all implement the member function foo, there is no way to call that method on a set of them unless they all inherit from the same base class. Except that you can, as exemplified boost::any and boost::variant.</blockquote>
<p>Actually I pretty much agrees with everything Alexei says and the comments put forth by different people. C++ templates is a wonderful thing while often complicated to understand and use. My major issue with them is that they can't be extended into runtime.</p>
<p>So anyway what I want to emphasis is that templates is not substitute for Objective-C's dynamic features. I work daily on a C++ application with +4 million lines of code. The application have a natural fit with OO design because it has a number of data objects which can be inspected, manipulated and visualized in many different sorts of views at the same time. E.g. one view can show the object in 3D while another can show an intersection in 2D. So Model-View-Controller is a natural fit for it.</p>
<p>The more I work on it the clearer Greenspuns Tenth Rule of programming becomes to me:</p><blockquote>Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.</blockquote >
<p>Bottom line is that the kind of structure a large application like this would typically have is what Objective-C was designed for. And thus when I look at the app, all we do in C++ is making a bug-ridden slow implementation of Objective-C. </p>
<p>Creation of views for data objects has to be abstracted because we might create new views we never thought about when the data objects were designed. Thus construction of views has to be abstracted. In Objective-C this is easy. Classes can be passed around like objects and instantiated. C++ on the other hand provides no abstraction of object creation so we ended up making this elaborate scheme with Class ids and factory classes.</p>
<p>We created reference counting, a system for sending messages between object for notifications etc. All stuff that Objective-C was designed for. We end up with huge amount of code for the overall architecture. And it is not even working that well.</p>
<p>But we are not the only ones. Every time I look at a large C++ framework or toolkit I realize that they are just re-implementing Objective-C features in a non standard and buggy way. When I say non-standard I mean that every toolkit has those features but they are done differently in each one and thus not compatible.</p>
<p>E.g. VTK (Visual Toolkit) has reference counting, message passing and abstraction of object creation. Open Inventor has their own reference counting etc. The Qt GUI toolkit has basically gone around the limitations of the C++ language for dynamic behavior and essentially created a library that allows for object introspection and runtime and dynamic dispatch. Essentially they have reimplemented Objective-C in C++. While they have don't quite a good job and I love the Qt toolkit it is still a kludge and non-standard.</p>
<p>So what exactly is my point? My point is that while people can argue from a theoretical foundation that C++ really is the better language due to all its features the power of templates etc, reality speaks for itself. In practice C++ very frequently fails as a language. It seems quite plain to me that when any large C++ toolkit seem to make a half bad implementation of Objective-C then something is wrong with the language.</p>
<p>Does this mean that I think Objective-C is a better language? No, far from it. I just think that one should use the best tool for the job. Frequently it seems like Objective-C would have been the best tool but C++ was selected instead. While C++ has an obvious advantage in implementing algorithms and high performance code, it doesn't seem like the mistake has been done as frequently at the Objective-C camp.</p>
<p>My speculation would be that this is because Objective-C's deficiencies are so obvious. While C++ deficiencies are not as clear. One can more easily kid oneself into thinking C++ will solve the problem without any hassle. And not at least because C++ is better known, a Objective-C developer will more likely consider using C++ for specific parts of the program than the other way around.</p>
<p>C++ and Objective-C complements each other very well in my opinion and they have different enough syntax from each other that it should be easy to keep the two apart in a program. But too often people think C++ is the silver bullet that can be used equally well for any task.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com3tag:blogger.com,1999:blog-21339754.post-1171811300310138182007-02-18T07:08:00.000-08:002009-12-02T11:52:54.117-08:00Wrapping C++ classes in Lua<p><a href="http://www.lua.org">Lua</a> is a script language frequently used in game development. Typically the game engine is written in C/C++ and the game itself is defined in Lua. Meaning setting up behaviour, placement of objects like powerups, doors, enemies etc is done in Lua. That way the game can be quickly tweaked without having to go through a time consuming re-compile. </p>
<p>But I am not going to go in depth about the different usage of Lua, nor explain the language itself. An <a href="http://www.lua.org/pil">online book</a> at the Lua web page does that very well already. I will here assume you already have some basic knowledge about Lua.</p>
<p>However finding any documentation on how to wrap a C++ class so that it can be used in Lua is difficult. One could of course use one of the ready made bridges. But here we are going to look at how to do it yourself. </p>
<p>The tricky part is deciding on how to do it. Because Lua is such a flexible language there is really a lot of ways you could achieve it. </p>
<p><b>The naive approach</b></p>
<p>First lets look at several options, what works and doesn't. My first approach was to <i>light user data</i> to store a pointer to my C++ class. I will use a Sprite class and related classes as examples here as that was the type of classes I was wrapping. </p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">static</span> <span style="color:#881350;">int</span> <span style="color:#003369;">newSprite</span>(lua_State *L) <br />
{<br />
<span style="color:#881350;">int</span> n = <span style="color:#003369;">lua_gettop</span>(L); <span style="color:#91dc93;">// Number of arguments<br />
</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(n != <span style="color:#0000ff;">4</span>) <br />
<span style="color:#881350;">return</span> <span style="color:#003369;">luaL_error</span>(L, <span style="color:#f0898f;">"Got %d arguments expected 4"</span>, n); <br />
<br />
<span style="color:#881350;">double</span> x = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">1</span>); <br />
<span style="color:#881350;">double</span> y = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">2</span>); <br />
<span style="color:#881350;">double</span> dir = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">3</span>); <br />
<span style="color:#881350;">double</span> speed = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">4</span>); <br />
<br />
Sprite *s = <span style="color:#881350;">new</span> <span style="color:#003369;">Sprite</span>(<span style="color:#003369;">Point2</span>(x, y), dir, speed);<br />
<br />
<span style="color:#003369;">lua_pushlightuserdata</span>(L, s);<br />
<br />
<span style="color:#881350;">return</span> <span style="color:#0000ff;">1</span>; <br />
}</div>
<p>The code snippet above shows a naive implementation of this approach. Unfortunately it doesn't work. The problem is that a <b>light user data</b> is just a simple pointer. Lua does not store any information with it. For instance a metatable which we could use to define the methods the class supports.</p>
<p><b>An approach with limited functionality</b></p>
<p>The next approach would be to use <b>user data</b>. Unlike light user data it can store a reference to a metatable.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">static</span> <span style="color:#881350;">int</span> <span style="color:#003369;">newSprite</span>(lua_State *L) <br />
{<br />
<span style="color:#881350;">int</span> n = <span style="color:#003369;">lua_gettop</span>(L); <span style="color:#236e25;">// Number of arguments<br />
</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(n != <span style="color:#0000ff;">4</span>) <br />
<span style="color:#881350;">return</span> <span style="color:#003369;">luaL_error</span>(L, <span style="color:#760f15;">"Got %d arguments expected 4"</span>, n); <br />
<br />
<span style="color:#236e25;">// Allocate memory for a pointer to to object<br />
</span> Sprite **s = (Sprite **)<span style="color:#003369;">lua_newuserdata</span>(L, <span style="color:#881350;">sizeof</span>(Sprite *)); <br />
<br />
<span style="color:#881350;">double</span> x = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">1</span>); <br />
<span style="color:#881350;">double</span> y = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">2</span>); <br />
<span style="color:#881350;">double</span> dir = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">3</span>); <br />
<span style="color:#881350;">double</span> speed = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">4</span>); <br />
<br />
*s = <span style="color:#881350;">new</span> <span style="color:#003369;">Sprite</span>(<span style="color:#003369;">Point2</span>(x, y), dir, speed);<br />
<br />
<span style="color:#003369;">lua_getglobal</span>(L, <span style="color:#760f15;">"Sprite"</span>); <span style="color:#236e25;">// Use global table 'Sprite' as metatable<br />
</span> <span style="color:#003369;">lua_setmetatable</span>(L, -<span style="color:#0000ff;">2</span>); <br />
<br />
<span style="color:#881350;">return</span> <span style="color:#0000ff;">1</span>; <br />
}<br />
</div>
<p>For us to be able to use sprite like this we need to register class first. Basically we need to create a table <i>Sprite</i> which contains all the methods that our <i>user data</i> should support. </p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Show registration of class<br />
</span><span style="color:#881350;">static</span> <span style="color:#881350;">const</span> luaL_Reg gSpriteFuncs[] = {<br />
<span style="color:#236e25;">// Creation<br />
</span> {<span style="color:#760f15;">"new"</span>, newSprite},<br />
{<span style="color:#760f15;">"position"</span>, position},<br />
{<span style="color:#760f15;">"nextPosition"</span>, nextPosition}, <br />
{<span style="color:#760f15;">"setPosition"</span>, setPosition}, <br />
{<span style="color:#760f15;">"render"</span>, render}, <br />
{<span style="color:#760f15;">"update"</span>, update}, <br />
{<span style="color:#760f15;">"collision"</span>, collision}, <br />
{<span style="color:#760f15;">"move"</span>, move}, <br />
{<span style="color:#760f15;">"accelerate"</span>, accelerate}, <br />
{<span style="color:#760f15;">"rotate"</span>, rotate}, <br />
{NULL, NULL}<br />
};<br />
<br />
<span style="color:#881350;">void</span> <span style="color:#003369;">registerSprite</span>(lua_State *L)<br />
{<br />
<span style="color:#003369;">luaL_register</span>(L, <span style="color:#760f15;">"Sprite"</span>, gSpriteFuncs); <br />
<span style="color:#003369;">lua_pushvalue</span>(L,-<span style="color:#0000ff;">1</span>);<br />
<span style="color:#003369;">lua_setfield</span>(L, -<span style="color:#0000ff;">2</span>, <span style="color:#760f15;">"__index"</span>); <br />
}<br />
</div>
<p>This will allow us to create instances of <i>Sprite</i> and call methods on it in Lua like this:</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">-- Create an instance of sprite an call some methods<br />
</span><span style="color:#881350;">local</span> sprite = Sprite.new(x, y, <span style="color:#400080;">dir</span>, speed)<br />
sprite:setPosition(x,y)<br />
sprite:render()</div></p>
<p><b>The final approach</b></p>
<p>In most cases this approach is sufficient but it has one major limitation. It does not support inheritance. You can change the methods of <i>Sprite</i> in Lua but that will change the behavior of all instances of Sprite. What you would want to do is to be able to change method on just the instance and then use that instance as a prototype for new Sprite instances, effectively creating a class inheritance system.</p>
<p>To do this we need to change the instance into being a table. How do we access our C++ object then? Simple, we just store the pointer to it as <i>user data</i> in one of the field of the table. You might think that this time <i>light user data</i> will be sufficient. However the problem is that only <i>user data</i> is informed of garbage collection, not tables or light user data. So if you want to delete the C++ object when corresponding lua table is garbage collected you need to use <i>user data</i>. </p>
<p>So we then arrive at our final solution. We will store a pointer to our C++ object as <b>user data</b> on the key <b>__self</b> in the table that represents our instance. <b>__self</b> is an arbitrary selected name. It could be anything. We will not register <i>Sprite</i> as as the metatable for our instance but instead register the first argument to the new function as it. This will allow us to support inheritance. Further the garbage collection function will be register on a separate table which will be used as metatable only for the <i>user data</i>. This is to allow it to be garbage collected.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">static</span> <span style="color:#881350;">int</span> <span style="color:#003369;">newSprite</span>(lua_State *L) <br />
{<br />
<span style="color:#881350;">int</span> n = <span style="color:#003369;">lua_gettop</span>(L); <span style="color:#236e25;">// Number of arguments<br />
</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(n != <span style="color:#0000ff;">5</span>) <br />
<span style="color:#881350;">return</span> <span style="color:#003369;">luaL_error</span>(L, <span style="color:#760f15;">"Got %d arguments expected 5 (class, x, y, dir, speed)"</span>, n); <br />
<span style="color:#236e25;">// First argument is now a table that represent the class to instantiate <br />
</span> <span style="color:#003369;">luaL_checktype</span>(L, <span style="color:#0000ff;">1</span>, LUA_TTABLE); <br />
<br />
<span style="color:#003369;">lua_newtable</span>(L); <span style="color:#236e25;">// Create table to represent instance<br />
</span><br />
<span style="color:#236e25;">// Set first argument of new to metatable of instance<br />
</span> <span style="color:#003369;">lua_pushvalue</span>(L,<span style="color:#0000ff;">1</span>); <br />
<span style="color:#003369;">lua_setmetatable</span>(L, -<span style="color:#0000ff;">2</span>);<br />
<br />
<span style="color:#236e25;">// Do function lookups in metatable<br />
</span> <span style="color:#003369;">lua_pushvalue</span>(L,<span style="color:#0000ff;">1</span>);<br />
<span style="color:#003369;">lua_setfield</span>(L, <span style="color:#0000ff;">1</span>, <span style="color:#760f15;">"__index"</span>); <br />
<br />
<span style="color:#236e25;">// Allocate memory for a pointer to to object<br />
</span> Sprite **s = (Sprite **)<span style="color:#003369;">lua_newuserdata</span>(L, <span style="color:#881350;">sizeof</span>(Sprite *)); <br />
<br />
<span style="color:#881350;">double</span> x = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">2</span>); <br />
<span style="color:#881350;">double</span> y = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">3</span>); <br />
<span style="color:#881350;">double</span> dir = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">4</span>); <br />
<span style="color:#881350;">double</span> speed = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">5</span>); <br />
<br />
*s = <span style="color:#881350;">new</span> <span style="color:#003369;">Sprite</span>(<span style="color:#003369;">Point2</span>(x, y), dir, speed);<br />
<br />
<span style="color:#236e25;">// Get metatable 'Lusion.Sprite' store in the registry<br />
</span> <span style="color:#003369;">luaL_getmetatable</span>(L, <span style="color:#760f15;">"Lusion.Sprite"</span>);<br />
<br />
<span style="color:#236e25;">// Set user data for Sprite to use this metatable<br />
</span> <span style="color:#003369;">lua_setmetatable</span>(L, -<span style="color:#0000ff;">2</span>); <br />
<br />
<span style="color:#236e25;">// Set field '__self' of instance table to the sprite user data<br />
</span> <span style="color:#003369;">lua_setfield</span>(L, -<span style="color:#0000ff;">2</span>, <span style="color:#760f15;">"__self"</span>); <br />
<br />
<span style="color:#881350;">return</span> <span style="color:#0000ff;">1</span>; <br />
}<br />
</div>
<p>We can now work with sprite instances in Lua like this:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">-- Instantiate from Sprite class<br />
</span><span style="color:#881350;">local</span> sprite = Sprite:new(x, y, <span style="color:#400080;">dir</span>, speed)<br />
sprite:setPosition(x,y)<br />
sprite:render()<br />
<br />
<span style="color:#236e25;">-- Add method to instance as use it as class<br />
</span><span style="color:#881350;">function</span> sprite:doSomething()<br />
<span style="color:#400080;">print</span>(<span style="color:#994500;">"do something"</span>)<br />
<span style="color:#881350;">end</span><br />
<br />
<span style="color:#881350;">local</span> derived = sprite:new(x, y, <span style="color:#400080;">dir</span>, speed)<br />
derived:render()<br />
derived:doSomething() <span style="color:#236e25;">-- This is now a legal operation</span></div>
<p>There are still a couple of loose ends. We haven't showed how the methods are registered with this new solution nor how we access C++ object pointer in methods. But this is fairly straight forward as I will show.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">void</span> <span style="color:#003369;">registerSprite</span>(lua_State *L)<br />
{ <br />
<span style="color:#236e25;">// Register metatable for user data in registry<br />
</span> <span style="color:#003369;">luaL_newmetatable</span>(L, <span style="color:#760f15;">"Lusion.Sprite"</span>);<br />
<span style="color:#003369;">luaL_register</span>(L, <span style="color:#0000ff;">0</span>, gDestroySpriteFuncs); <br />
<span style="color:#003369;">luaL_register</span>(L, <span style="color:#0000ff;">0</span>, gSpriteFuncs); <br />
<span style="color:#003369;">lua_pushvalue</span>(L,-<span style="color:#0000ff;">1</span>);<br />
<span style="color:#003369;">lua_setfield</span>(L,-<span style="color:#0000ff;">2</span>, <span style="color:#760f15;">"__index"</span>); <br />
<br />
<span style="color:#236e25;">// Register the base class for instances of Sprite<br />
</span> <span style="color:#003369;">luaL_register</span>(L, <span style="color:#760f15;">"Sprite"</span>, gSpriteFuncs); <br />
}<br />
</div>
<p>We can then implement a method of Sprite like this</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">static</span> <span style="color:#881350;">int</span> <span style="color:#003369;">setSpeed</span>(lua_State *L) <br />
{<br />
<span style="color:#881350;">int</span> n = <span style="color:#003369;">lua_gettop</span>(L); <span style="color:#236e25;">// Number of arguments<br />
</span> <br />
<span style="color:#881350;">if</span><span style="color:#003369;"> </span>(n == <span style="color:#0000ff;">2</span>) {<br />
Sprite* sprite = <span style="color:#003369;">checkSprite</span>(L);<br />
<span style="color:#003369;">assert</span>(sprite != <span style="color:#0000ff;">0</span>);<br />
real speed = <span style="color:#003369;">luaL_checknumber </span>(L, <span style="color:#0000ff;">2</span>); <br />
sprite-><span style="color:#003369;">setSpeed</span>(speed);<br />
}<br />
<span style="color:#881350;">else</span><br />
<span style="color:#003369;">luaL_error</span>(L, <span style="color:#760f15;">"Got %d arguments expected 2 (self, speed)"</span>, n); <br />
<br />
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;<br />
}<br />
</div>
<p>To extract the pointer to the C++ object and make sure it is of the correct type we use the following code:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Sprite* <span style="color:#003369;">checkSprite</span>(lua_State* L, <span style="color:#881350;">int</span> index)<br />
{<br />
<span style="color:#881350;">void</span>* ud = <span style="color:#0000ff;">0</span>;<br />
<span style="color:#003369;">luaL_checktype</span>(L, index, LUA_TTABLE); <br />
<span style="color:#003369;">lua_getfield</span>(L, index, <span style="color:#760f15;">"__self"</span>);<br />
ud = <span style="color:#003369;">luaL_checkudata</span>(L, index, <span style="color:#760f15;">"Lusion.Sprite"</span>);<br />
<span style="color:#003369;">luaL_argcheck</span>(L, ud != <span style="color:#0000ff;">0</span>, <span style="color:#760f15;">"`Lusion.Sprite' expected"</span>); <br />
<br />
<span style="color:#881350;">return</span> *((Sprite**)ud); <br />
}<br />
</div>
<p>The only thing left is dealing with garbage collection but I leave that as an exercise. You should already have the basic idea of how to deal with it. Please note that I have not tested the exact same code as written here so why the principles are correct there might be minor errors in the code. In my own code I have separated more of the code into separate functions since the code for creating an instance is almost identical for any class, as well as the code for extracting the <b>__self</b> pointer. </p>
<p><b>Conclusion</b></p>
<p>While using a bridge might be better for bigger projects I think it is good to know for yourself exactly what goes on under the hood and when you do it yourself you can more easily fine tune what you export and not and in which way. Typically you would want the Lua interface to be simpler and more limited than the C++ interface to your classes.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com6tag:blogger.com,1999:blog-21339754.post-1169293396724147612007-01-20T03:43:00.000-08:002007-01-20T03:43:16.756-08:00Intersection test assertion failure in CGALI have just started using the <a href="http://www.cgal.org/">CGAL</a> library. It is a library for doing computational geometry. It contains classes and functions for different geometric data structures like triangles, line segments, polygons and functions for testing for intersection, calculating convex hull and a lot more.
This week I fell into the first trap of computational geometry. I stored polygons in <code>Polygon_2</code> classes and then performed an intersection test with the <code>do_intersect</code> function. Unfortunately that too often lead to the program crashing with the assertion failure:
<code>
CGAL error: precondition violation!
Expr: Segment_assertions::_assert_is_point_on (p, cv, Has_exact_division()) && compare_xy(cv.left(), p) == SMALLER && compare_xy(cv.right(), p) == LARGER
File: /Users/Shared/CGAL-3.2.1/include/CGAL/Arr_segment_traits_2.h
Line: 730
</code>
or
<code>
CGAL error: assertion violation!
Expr: *slIter == curve
File: /Users/Shared/CGAL-3.2.1/include/CGAL/Basic_sweep_line_2.h
Line: 591
</code>
Eventually I narrowed it down to discover that it happened in specific degenerate cases. For instance when a corner of a polygon intersected an edge of another polygon. Which means two line segments are intersecting another line segment in the exact same spot. Now I know this would normally be a problem for line sweep algorithms but I also know that any computational geometry library with respect for itself should handle such degeneracies. So I could not understand why CGAL should fail.
Well it turns out that I didn't read this part of the CGAL manual:
<blockquote>
If it is crucial for you that the computation is reliable, the right choice is probably a number type that guarantees
exact computation. The Filtered kernel provides a way to apply filtering techniques [BBP01] to achieve a
kernel with exact and efficient predicates. Still other people will prefer the built-in type double, because they
need speed and can live with approximate results, or even algorithms that, from time to time, crash or compute
incorrect results due to accumulated rounding errors.
</blockquote>
I had expected this kind of problem. So I thought it was because I used float. I changed to double and got the same problem. It turns out I should have used <code>CGAL::Exact_predicates_exact_constructions_kernel</code>.Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1158581381772355672006-09-18T05:09:00.000-07:002009-12-02T11:49:44.846-08:00String manipulation: Qt vs Cocoa<p><b>Background</b></p>
<p>I am coding a raytracer program using C++ and Qt. It is program that supports many different algorithms and file formats. One of the algorithms had some bugs that I had problems finding, so I made a OpenGL visualizer of the scene with the hope to be able to visualize different steps in the algorithm to find the error.</p>
<p>Unfortunately the Qt OpenGL implementation proved extremely slow. Instead of looking all over the place to find a solution I decided to create the OpenGL viewer in Cocoa. It seems like Qt4 has several performance problems compared to Qt3. Qt Designer is quite slow when editing properties. Before the last update, it was so slow to type in a text field that I had to type in another window and then cut and paste the text in. Likewise Assistant was so slow I ended up using Safari instead to browse the help documentation. </p>
<p>The scene I read was written for the RenderBitch raytracer. The format is basically like this:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">lookat <span style="color:#0000ff;">0</span> <span style="color:#0000ff;">.15</span> -<span style="color:#0000ff;">4.0</span> ''<span style="color:#0000ff;">0</span> <span style="color:#0000ff;">0</span> <span style="color:#0000ff;">0</span> ''<span style="color:#0000ff;">0</span> <span style="color:#0000ff;">1</span> <span style="color:#0000ff;">0</span><br />
persp <span style="color:#0000ff;">40</span> <span style="color:#0000ff;">1</span> <span style="color:#0000ff;">4.3</span> <span style="color:#0000ff;">.0</span><br />
<br />
material cyan<br />
r_diff <span style="color:#0000ff;">.7</span> <span style="color:#0000ff;">1</span> <span style="color:#0000ff;">1</span><br />
r_refl <span style="color:#0000ff;">0</span> <span style="color:#0000ff;">0</span> <span style="color:#0000ff;">0</span><br />
<br />
<span style="color:#236e25;"># this adds vertices to the scene<br />
</span>vertex <span style="color:#0000ff;">1.25</span> <span style="color:#0000ff;">1</span> <span style="color:#0000ff;">1.3</span><br />
vertex <span style="color:#0000ff;">1.25</span> <span style="color:#0000ff;">1</span> -<span style="color:#0000ff;">3.3</span></div>
<p>That is it is a text based file format with some command at each line. So to read the file I had to do a bunch of string manipulation.</p>
<p>I had already done code to read this text in using Qt, so it proved an interesting comparison doing the same task in Cocoa</p>
<p><b>String classes in Qt and Cocoa</b></p>
<p>The approach used by Qt and Cocoa is quite different. When looking through Cocoa documentation it reminded me more of how I did string manipulation in script languages like Python and Ruby. Except the methods names were extremely long as is usual in Cocoa (One of the things I dislike the most about Cocoa).</p>
<p>In Qt you deal with strings using the <code>QString</code>, <code>QStringList</code>, <code>QTextStream</code> and <code>QRegExp</code> classes. In Cocoa the equivalents would more or less be: <code>NSString</code>, <code>NSArray</code> and <code>NSScanner</code>. Cocoa does not have an class for regular expressions.</p>
<p>The difference caused by Cocoa being dynamically typed shows itself in the use of <code>NSArray</code> instead of a special string list container. It is general purpose and can hold any kind of objects. Since objects in general can create string representations of itself <code>NSArray</code> also contains methods to joint elements into a string with a separator like <code>QStringList</code>.</p>
<p>One of the creates strength of Qt comes is visible when dealing with primitive types like <code>int</code> and <code>float</code>. By using templates and operator overloading these can be handled a lot easier than Cocoa. In Cocoa you must explicitly box primitive types.</p>
<p>So with Qt I could easily read in data from the text file using <code>QTextStream</code> and have operator overloading automatically take care of type conversion</p>
<p>The example below shows how I can read in e.g. a vertex line in the scene file. Placing the vertex keyword in the keyword variable and the coordinates in x, y and z:</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Reading in a line of text using Qt<br />
</span>QString keyword;<br />
<span style="color:#881350;">float</span> x, y, z;<br />
QTextStream <span style="color:#003369;">in</span>(line);<br />
in >> keyword >> x >> y >> z;<br />
</div></p>
<p>In Cocoa I would have to do it explicitly:</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Reading a line of text using Cocoa<br />
</span><span style="color:#400080;">NSString</span>* keyword;<br />
<span style="color:#881350;">float</span> x, y, z;<br />
<span style="color:#400080;">NSScanner</span>* scanner = [<span style="color:#400080;">NSScanner</span> <span style="color:#6c0540;">scannerWithString:</span>line];<br />
[scanner <span style="color:#6c0540;">scanUpToString:</span><span style="color:#760f15;">@" "</span> <span style="color:#6c0540;">intoString:</span>&keyword];<br />
[scanner <span style="color:#6c0540;">scanFloat:</span>&x];<br />
[scanner <span style="color:#6c0540;">scanFloat:</span>&y];<br />
[scanner <span style="color:#6c0540;">scanFloat:</span>&z];<br />
</div></p>
<p>However I didn't actually end up using the operator overloading in Qt. There was some benefits to doing in another way and perhaps it was influenced by my experience dealing with strings in script languages that don't have operator overloading and templates.</p>
<p>So instead I chopped the lines into pieces and converted each string separate like this:</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Split element on line separated by whitespace<br />
</span>QString keyword;<br />
<span style="color:#881350;">float</span> x, y, z;<br />
QRegExp <span style="color:#003369;">whitespace</span>(<span style="color:#760f15;">"\\s"</span>);<br />
QStringList list = line.<span style="color:#003369;">split</span>(whitespace, QString::SkipEmptyParts);<br />
keyword = line[<span style="color:#0000ff;">0</span>];<br />
x = line[<span style="color:#0000ff;">1</span>].<span style="color:#003369;">toFloat</span>();<br />
y = line[<span style="color:#0000ff;">2</span>].<span style="color:#003369;">toFloat</span>();<br />
z = line[<span style="color:#0000ff;">3</span>].<span style="color:#003369;">toFloat</span>();</div></p>
<p>This looks more complicated but it didn't turn out much different when considering that I read parameters in bulk and they were usually all floats or ints.</p>
<p>In Cocoa I could to similar with this code (although ironically I ended up using the stream approach there):</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// Split element on line separated by whitespace<br />
</span><span style="color:#400080;">NSString</span>* keyword;<br />
<span style="color:#881350;">float</span> x, y, z;<br />
<span style="color:#400080;">NSArray</span>* list = [line <span style="color:#6c0540;">componentsSeparatedByString:</span><span style="color:#760f15;">@" "</span>];<br />
keyword = [list <span style="color:#6c0540;">objectAtIndex:</span><span style="color:#0000ff;">0</span>];<br />
x = [[list <span style="color:#6c0540;">objectAtIndex:</span><span style="color:#0000ff;">1</span>] <span style="color:#6c0540;">floatValue</span>];<br />
y = [[list <span style="color:#6c0540;">objectAtIndex:</span><span style="color:#0000ff;">2</span>] <span style="color:#6c0540;">floatValue</span>];<br />
z = [[list <span style="color:#6c0540;">objectAtIndex:</span><span style="color:#0000ff;">3</span>] <span style="color:#6c0540;">floatValue</span>];</div></p>
<p>It is not entirely equal because it separates on single space and not on any whitespace. This is why I ended up with <code>NSScanner</code> because it has more options on separators.</p>
<p><b>Unicode</b></p>
<p>Both Qt and Cocoa uses unicode strings. However in Qt this is not something you have to think much about due to operator overloading. You can write <code>line[0] == '#'</code> and it works, despite <code>line[0]</code> actually representing a unicode character and <code>'#'</code> is not. But overloading of <code>==</code> takes care of the translation trasparently. </p>
<p>With Cocoa it is a different story, because of operator overloading.So to compare a unicode character to a range of other characters one would have to use the <code>NSCharacterSet</code> class.</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// line[0] == '#'<br />
</span><span style="color:#400080;">NSCharacterSet</span>* hash = [<span style="color:#400080;">NSCharacterSet</span> <span style="color:#6c0540;">characterSetWithCharactersInString:</span><span style="color:#760f15;">@"#"</span>];<br />
<span style="color:#881350;">if</span><span style="color:#003369;"> </span>([hash <span style="color:#6c0540;">characterIsMember:</span> [line <span style="color:#6c0540;">characterAtIndex:</span><span style="color:#0000ff;">0</span>]]) {<br />
''''<span style="color:#236e25;">// Do somehting...<br />
</span>}</div></p>
<p>Of course in this particular instance when we are just checking for a character at the start of the line there is a simpler solution:</p>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#236e25;">// line[0] == '#'<br />
</span><span style="color:#881350;">if</span><span style="color:#003369;"> </span>([line <span style="color:#6c0540;">hasPrefix:</span><span style="color:#760f15;">@"#"</span>]) {<br />
''''<span style="color:#236e25;">// Do somehting...<br />
</span>}</div></p>
<p><b>Experiences with the whole development cycle</b></p>
<p>It is of course hard to compare the development effort for both environments for this task given that I have much more experience with Qt and did the program in Qt first and then later in Cocoa. So it might even out in the sense that I didn't have to think that much about program design in Cocoa but I had to look up more documentation on how to deal with strings.</p>
<p>With debugging I think Cocoa overall was easier to deal with than Qt. Although I have noticed earlier that the weak typing used in C can cause from problems in Cocoa programs. A number of type mistakes are more easily caught using Qt because it uses C++ which has stronger typing than C.</p>
<p>Some problems with C was also enhanced by how Apple has chosen to let xCode build by default. It uses a Zero linking technique which I thought was great at first because it severely cuts down the compile and link process. However it is quite dangerous because it lets you compile and run programs with calls to functions that don't exist, or with the wrong number of parameters. Now one might argue that this is the case with most dynamic languages in general like script languages. However the combination of late binding and C is a very bad one. There are no proper error handlers when something goes wrong so you remain quite clueless to what went wrong.</p>
<p>However apart from that it is much easier to trace through and debug Objective-C programs in gdb than C++ programs. Almost all Objective-C classes give descriptions of themselves so they can easily be inspected. C functions and Objective-C methods are easier to call because they don't have problems with name mangling like C++ methods and functions.</p>
<p>For instance to debug Qt program I frequently had to make standalone methods that printed out specific objects I had to inspect.</p>
<p>Not an ideal solution because you have to add that code to every Qt program you debug and you have to make code for every class you want to inspect.</p>
<p>Since C++ methods are tricky to call, in debug mode it is hard to change their state through method calls in the debugger.</p>
<p><b>Conclusion</b></p>
<p>Although Qt and C++ have some distinct benefits like strong typing, templates and less verbose code, I found it quicker to develop my Cocoa solution because it was much faster to hunt down bugs in the program because debugging was easier.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1156797823920129862006-08-28T13:43:00.000-07:002009-12-02T11:50:06.566-08:00Graphics: Qt vs Cocoa<p>The logic of how things work in Cocoa is quite different from most other OO toolkits, especially when it comes to graphics. Here I will give a comparison to how one does some basic graphics operations in Cocoa as opposed to Qt.</p>
<p>I might have chosen to compare Cocoa to Java or .NET frameworks but I am most familiar with Qt. Qt graphics model is quite similar to that use in Java and .NET so the differences explained here should be of interest to Java and C# programers as well.</p>
<p><b>Introduction</b></p>
<p>Cocoa and Qt have similar image models for graphics. Both are able to describe images independent of output device (device on which image is displayed) and information about points in drawings (vertices) are retained in the model so that different transformations can be applied to it (e.g. affine transformations.)</p>
<p><strong>Programmin models</strong></p>
<p>However their programming models are different. A programming model can be statefull or stateless and procedure oriented or object oriented. E.g. OpenGL has a statefull and procedure oriented programming model while DirectX has a stateless and object oriented programming model.</p>
<p>In a statefull programming model all drawing operations are affected by the state of the graphics system. E.g. a command to draw a box might be <code>drawBox(x, y, width, height)</code>. This would draw a box with current color. The color is then a state in the graphics system. In a stateless system the command would be <code>drawBox(x, y, width, height, color).</code></p>
<p>The reason why many people find Cocoa confusing at first is that it uses a statefull and object oriented programming model. This is uncommon as Qt, Java and .NET all use a stateless object oriented programming model. In this respect Cocoa has a lot of similarities with OpenGL.</p>
<p><strong>Setting drawing target</strong></p>
<p>In Qt drawing commands change the image or graphics of a <code>QPaintDevice</code>. Typically one of the subclasses like <code>QWidget</code> is used. Below is an example on how one can draw a line of thickness 2 and blue color through three points p1, p2 and p3 on some arbitrary widget returned by <code>getCanvas()</code>.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">QWidget* w = <span style="color:#003369;">getCanvas</span>(); ''<span style="color:#236e25;">// Returns our drawing surface<br />
</span>QPainter <span style="color:#003369;">paint</span>(w);<br />
<br />
QPen <span style="color:#003369;">pen</span>(Qt::blue);<br />
pen.<span style="color:#003369;">setWidthF</span>(<span style="color:#0000ff;">2.0</span>);<br />
paint.<span style="color:#003369;">setPen</span>(pen);<br />
<br />
QPoint <span style="color:#003369;">p1</span>(x1, y1);<br />
QPoint <span style="color:#003369;">p2</span>(x2, y2);<br />
QPoint <span style="color:#003369;">p3</span>(x3, y3);<br />
<br />
paint.<span style="color:#003369;">drawLine</span>(p1, p2);<br />
paint.<span style="color:#003369;">drawLine</span>(p2, p3);</div>
<p>As one can see the surface to draw on is provided specifically to the <code>QPainter</code> object as well as the pen and color to use. The graphics system is not in any state. The result of the drawing operations is determined exclusively by the objects involved an their attributes.</p>
<p>Below is an example on how to do the same with Cocoa:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#400080;">NSView</span>* view = <span style="color:#003369;">getCanvas</span>(); <span style="color:#236e25;">// Returns our drawing surface<br />
</span><span style="color:#400080;">NSBezierPath</span>* path = [[<span style="color:#400080;">NSBezierPath</span> <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">init</span>];<br />
<br />
[[<span style="color:#400080;">NSColor</span>] <span style="color:#6c0540;">blueColor</span>] <span style="color:#6c0540;">set</span>];<br />
[path <span style="color:#6c0540;">setLineWidth:</span><span style="color:#0000ff;">2.0</span>];<br />
<br />
<span style="color:#400080;">NSPoint</span> p1 = <span style="color:#400080;">NSMakePoint</span>(x1, y1);<br />
<span style="color:#400080;">NSPoint</span> p2 = <span style="color:#400080;">NSMakePoint</span>(x2, y2);<br />
<span style="color:#400080;">NSPoint</span> p2 = <span style="color:#400080;">NSMakePoint</span>(x3, y3);<br />
<br />
[path <span style="color:#6c0540;">moveToPoint:</span>p1];<br />
[path <span style="color:#6c0540;">lineToPoint:</span>p2];<br />
[path <span style="color:#6c0540;">lineToPoint:</span>p3];<br />
<br />
[view <span style="color:#6c0540;">lockFocus</span>];<br />
[path <span style="color:#6c0540;">stroke</span>];<br />
[view <span style="color:#6c0540;">unlockFocus</span>];</div>
<p>As one can see there are some noticeable differences. There is no one object that represents the state of the graphics system like <code>QPainter</code>. <code>NSBezierPath</code> might look like it but it merely keeps track of points on a path and how the lines that connect the points are. No color information is passed to <code>NSBezierPath</code> in the form of a <code>NSColor</code> object nor is the surface on which to draw specified as in the Qt example.</p>
<p>Instead the state of the graphics system itself is changed. <code>[color set]</code> is used to change the color state of the graphics system. Likewise <code>[view lockFocus]</code> is used change the state for current drawing area in the graphics system. Usually when a views method to update its area is called, the state of the drawing area has already been set to that view. So most of the time the user does not have to call <code>lockFocus</code> himself.</p>
<p><strong>Drawing bitmaps</strong></p>
One are that is difficult to understand once one gets started with Cocoa graphics is how to deal with bitmaps. It is confusing precisely because when one is not used statefull programming models for graphics it is not obvious how one draws into a bitmap.
<p>Both Qt and Cocoa have two different image formats. However comparing them is difficult because there is no one to one correspondence in functionality. Qt has <code>QPixmap</code> and <code>QImage</code>. <code>QPixmap</code> is used for drawing images that will go on screen. Because graphics can be created in many different ways on screen individual pixel access is not possible. <code>QImage</code> on the other hand exists off screen in memory an allows individual pixel manipulation. However to put a <code>QImage</code> on screen it has to be converted to a <code>QPixmap</code></p>
<p>In Cocoa there is a similar situation. <code>NSImage</code> vaguely coresponds to <code>QPixmap</code>. You can not access its individual pixels but you can draw into it and display it on screen. <code>NSBitmapImageRep</code> corresponds roughly to <code>QImage</code>. You can access its pixels individually and set up exactly how pixels are stored, how many color components are used etc. However until recently you could not draw directly into it. Instead you would draw to a <code>NSImage</code>. The reason for this is that <code>NSImage</code> can represent an offscreen window, while <code>NSBitmapImageRep</code> is just a bunch of bytes in memory. Windows area can be drawn on by graphics hardware so they can be represented in any number of ways. They could exist in special graphics memory or on a remote machine. Thus accessing individual pixels on an <code>NSImage</code> makes no sense. However giving drawing commands does because the graphics hardware have drawing commands implemented to do it fast. Doing drawing on <code>NSBitmapImageRep</code> does not make sense because it is not accessible to graphics hardware and drawing can be done by graphics hardware.</p>
<p>Below is an example on how to manipulate the pixels in a <code>QPixmap</code></p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Pixmap pixmap = <span style="color:#003369;">getImage</span>(); <span style="color:#236e25;">// Get some image we want to manipulate<br />
</span>QImage img = pixmap.<span style="color:#003369;">toImage</span>(); <br />
<br />
<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> y=<span style="color:#0000ff;">0</span>; y < img.<span style="color:#003369;">height</span>(); ++y) {<br />
''''<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> x=<span style="color:#0000ff;">0</span>; x < img.<span style="color:#003369;">width</span>(); ++x) {<br />
''''''''QRgb pix = img.<span style="color:#003369;">pixel</span>(x,y);<br />
''''''''<span style="color:#003369;">doSomethingWithPixel</span>(&pix);<br />
''''''''img.<span style="color:#003369;">setPixel</span>(x,y,pix);<br />
''''}<br />
}<br />
</div>
<p>The code below shows how to do the same thing in Cocoa. That is, to manipulate the pixels in a <code>NSImage</code>. Notice how you must <code>lockFocus</code> on the <code>NSImage</code> to for the pixel grabbing to occur on that particular image.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#400080;">NSImage</span>* pixmap = <span style="color:#003369;">getImage</span>(); <span style="color:#236e25;">// Get some image we want to manipulate<br />
</span><span style="color:#400080;">NSRect</span> rect = <span style="color:#400080;">NSMakeRect</span>(<span style="color:#400080;">NSZeroPoint</span>, [pixmap <span style="color:#6c0540;">size</span>]);<br />
<br />
[pixmap <span style="color:#6c0540;">lockFocus</span>]; <span style="color:#236e25;">// Make pixmap target for drawing commands<br />
</span><span style="color:#400080;">NSBitmapImageRep</span>* img = [[<span style="color:#400080;">NSBitmapImageRep</span> <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">initWithFocusedViewRect:</span>rect];<br />
[pixmap <span style="color:#6c0540;">unlockFocus</span>];<br />
<br />
<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> y=<span style="color:#0000ff;">0</span>; y < [img <span style="color:#6c0540;">pixelsHigh</span>]; ++y) {<br />
''''<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> x=<span style="color:#0000ff;">0</span>; x < [img <span style="color:#6c0540;">pixelsWide</span>]; ++x) {<br />
''''''''<span style="color:#400080;">NSColor</span>* pix = [img <span style="color:#6c0540;">colorAtX:</span>x <span style="color:#6c0540;">y:</span>y];<br />
''''''''<span style="color:#003369;">doSomethingWithPixel</span>(pix);<br />
''''''''[img <span style="color:#6c0540;">setColor:</span>pix <span style="color:#6c0540;">atX:</span>x <span style="color:#6c0540;">y:</span>y];<br />
''''}<br />
}<br />
<br />
[pixmap <span style="color:#6c0540;">addRepresentation:</span>img];</div>
<p><b>Drawing images on screen</b></p>
To round I need to show how you draw images on screen, or more specifically in a window. A <code>QWidget</code> in Qt's case and a <code>NSView</code> in Cocoa's case. Below we can see how to draw rectangular area within an image given by <code>srcRect</code> inside a rectangular area inside a window (QWidget) given by <code>dstRect</code>
<p><div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">QWidget* w = <span style="color:#003369;">getCanvas</span>(); ''<span style="color:#236e25;">// Returns our drawing surface<br />
</span>Pixmap pixmap = <span style="color:#003369;">getImage</span>(); <span style="color:#236e25;">// Return some image we want to draw<br />
</span><br />
QPainter <span style="color:#003369;">paint</span>(w);<br />
<br />
QRectF <span style="color:#003369;">dstRect</span>(x1, y1, width1, height1) <span style="color:#236e25;">// Area of window to draw into<br />
</span>QRectF <span style="color:#003369;">srcRect</span>(x1, y1, width2, height2); <span style="color:#236e25;">// Area of image to draw<br />
</span><br />
paint.<span style="color:#003369;">drawPixmap</span>(dstRect, pixmap, srcRect);<br />
</div>
</p>
<p>Below we have the corresponding code for Cocoa. Notice that the drawing method is called on the <code>NSImage</code> itself. This does not however mean that drawing is performed inside the image as Qt, Java and C# programmers would easily assume. The target for drawing commands is always the surface/canvas which has focus.</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#400080;">NSView</span>* w = <span style="color:#003369;">getCanvas</span>(); ''<span style="color:#236e25;">// Returns our drawing surface<br />
</span><span style="color:#400080;">NSImage</span>* pixmap = <span style="color:#003369;">getImage</span>(); <span style="color:#236e25;">// Return some image we want to draw<br />
</span><br />
<span style="color:#400080;">NSRect</span> dstRect = <span style="color:#400080;">NSMakeRect</span>(x1, y1, width1, heigth1); <span style="color:#236e25;">// Area of window to draw into<br />
</span><span style="color:#400080;">NSRect</span> srcRect = <span style="color:#400080;">NSMakeRect</span>(x2, y2, width2, heigth2); <span style="color:#236e25;">// Area of image to draw<br />
</span><br />
[w <span style="color:#6c0540;">lockFocus</span>];<br />
[pixmap <span style="color:#6c0540;">drawInRect:</span>dstRect <span style="color:#6c0540;">fromRect:</span>srcRect <span style="color:#6c0540;">operation:</span><span style="color:#400080;">NSCompositeCopy</span> <span style="color:#6c0540;">fraction:</span><span style="color:#0000ff;">1.0</span>];<br />
[w <span style="color:#6c0540;">unlockFocus</span>];</div>
<p><b>Final thoughts</b></p>
It should be clear that drawing in Cocoa takes some time getting used to for Qt, Java or C# programmers. I have only scratched the surface in this post. From my own experience using both Java and Qt, it is a lot easier to get up to speed on graphics in Qt and Java at first.
<p>However as is typical with everything else with Cocoa it might not be fast to get started but when you dwell into more complicate things, that is when Cocoa start to shine. Likewise with graphics. My own impression from using it (although I am no wizard in Cocoa graphics) is that when it comes to more complicated graphics Cocoa is much more capable than Qt.</p>
<p>It is also my impression that for short examples as given here Qt and Java would usually require less code, but when the graphics is more complicated less code is required in Cocoa.</p>
<p>However I must say that dealing with bitmap graphics seems overly complicated at times in Cocoa. Probably owing a lot to the fact that NEXTStep had totally vector based graphics. The graphics system would output post script drawing commands to the window server and not individual pixels. Basically vector graphics was made easy at the expense of pixel manipulation and raster graphics.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com2tag:blogger.com,1999:blog-21339754.post-1156781857573101202006-08-28T09:14:00.000-07:002009-05-01T14:55:16.194-07:00Thoughts on Objective-C 2.0<p>One of the most exciting changes coming in Mac OS X Leopard, is the new xCode 3.0 with a major update of the Objective-C language. While I think Objective-C is a great programming language and Cocoa is a great framework, it is getting a little bit long in the tooth in a few places.
</p>
<p>This has attracted considerable bashing from different people. Nitesh Dhanjani of O'Reilly for instance started a <a href="http://www.oreillynet.com/onlamp/blog/2005/12/can_apple_do_better_than_objec.html">big debate</a> about whether Apple should offer more choices to developers.
</p>
<p>The basic argument is that Microsoft is way ahead of the curve with C# and the .NET framework. .NET allows developers to choose any language in theory, thus giving them more choice. This is not something apple offers at the moment.
</p>
<p>However with Objective-C 2.0 it seems like Apple has narrowed the gap to more modern languages like C# and Java. It is not official yet but it seems like Objective-C will offer:
</p>
<ul>
<li>Optional garbage collection. This was actually an option in Tiger, but toggling it didn't do anything.</li>
<li>for each statement. Which makes iterating through objects in containers easier. This is similar to the C# for each.</li>
<li>Properties. You don't need to explicitly use setter and getter methods. If you access or set a variable, access methods will be used if they are defined.</li>
<li>Method attributes. Not so sure about this but apparently it is similar to what is found in C#. </li>
</ul>
<p>Here are some examples of the new syntax. The for each syntax:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#400080;">NSArray</span> *arr = <span style="color:#003369;">randomListOfStrings</span>();<br />
<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#400080;">NSString</span> *s in arr) {<br />
<span style="color:#400080;">NSLog</span>(<span style="color:#760f15;">"%@\n"</span>, s);<br />
}</div>
<p>And here is an example of properties:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#881350;">@interface</span> MyClass : <span style="color:#400080;">NSObject</span> {<br />
<span style="color:#881350;">int</span> foo;<br />
<span style="color:#881350;">float</span> baz;<br />
}<br />
<br />
<span style="color:#881350;">@property</span> <span style="color:#881350;">int</span> foo;<br />
<span style="color:#881350;">@property</span>(<span style="color:#881350;">getter</span>=_getBaz,<span style="color:#881350;">setter</span>=_setBaz) <span style="color:#881350;">float</span> baz;<br />
<br />
<span style="color:#881350;">@end</span><br />
<br />
<span style="color:#881350;">@implementation</span> MyClass<br />
<br />
<span style="color:#881350;">@synthesize</span> foo;<br />
<br />
- (<span style="color:#881350;">int</span>)<span style="color:#6c0540;">_getBaz</span> { <span style="color:#881350;">return</span> baz; }<br />
- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">_setBaz:</span> (<span style="color:#881350;">float</span>)<span style="color:#6c0540;">newBaz</span> { baz = <span style="color:#003369;">abs</span>(newBaz); }<br />
<br />
<span style="color:#881350;">@end</span><br />
<br />
<span style="color:#881350;">int</span> <span style="color:#003369;">main</span>(<span style="color:#881350;">void</span>) {<br />
MyClass *m = [MyClass <span style="color:#ff0000;">new</span>];<br />
m.baz = <span style="color:#0000ff;">-5.0</span>;<br />
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"%f\n"</span>, m.baz); <span style="color:#236e25;">/* -&gt; 5.0000 */</span><br />
}</div>
<p>I think this means that Mac developers don't have to consider themselves second class citizens. Although I think it never was that bad. A lot of the advantages of .NET and C# are highly theoretical.
</p>
<p>Even though you can make any language that language is still limited in functionality to that of the IL code and the .NET framework. The Cocoa framework exploits a lot of the particular strengths of the Objective-C language. It would be difficult to make a framework similar to Cocoa within the .NET world.
</p>
<p>So far it seems like most .NET developers are using C#. The other major .NET languages offer little extra functionality or benefits.
</p>
Even if Apple does not have an IL type language on which to build other languages they are not really that trapped in language offerings. Because of the highly dynamic nature of Objective-C which is based on message passing instead of method invocation. Other dynamic languages can easily be made to interface with Objective-C classes. Some examples are <a href="http://rubycocoa.sourceforge.net/doc/">Ruby Cocoa</a> and <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, which is a Python bridge to Cocoa. One only needs to create a bridge or in other words code that translates the message passing in Objective-C to that of the other language. E.g. in Java there needs to be defined a type for each Objective-C class to maintain the bridge. Unknown Objective-C classes can not be used. For more in-depth look at the problems with bridging Objective-C and Java look at <a href="http://www.cocoadev.com/index.pl?JavaBridge">cocoadev</a>.
<p><b>Performance</b></p>
<p>Another important point is that I think Objective-C is simply a much more versatile language than C#. Although high performance can be squeezed out of C# it is not nearly as easy. "Easy" is a keyword here, because no language can really do anything another language can't. All popular programming languages today are Turing complete. The difference is then about with what flexibility, ease and performance one can do a certain task.
</p>
<p>In a lot of language you can create modules written in C for critical sections and call them from the "mother" language. You can do this in Java through Java native calls. In Ruby you can add your own classes written in C. In C# you can mix managed and unmanaged code. E.g. you can have certain sections written in plain C++.
</p>
<p>However the ease which one does this differs. Objective-C is a superset of C and interacts with it easily. Writing performance intensive parts of the program is thus easy and straight forward. One simply tries to use as much plain C as possible and as little object oriented features.
</p>
<p>The same can not be said about .NET. I have learned the hard way that mixing managed and unmanaged code is no trivial task. It looks easy in theory and doesn't seem that hard when one does it with small example code. But as soon as you have to deal with a large real world application, problems start piling up.
</p>
<p>This means that for performance critical tasks it will be much easier to fine tune a Objective-C application than a .NET application.
</p>
<p><b>Missing features from Objective-C</b></p>
<p>So if the rumors are right, what will be missing from Objective-C to make it competitive with C# and other modern languages? I could actually only thing of one thing and that is support for namespaces. At the moment this is solved by prefixing class and function names with two letters, to distinguish them from classes and functions with the same name.
</p>
<p>Some might argue templates or generics is missing but that is only something that is needed on a static language like C++, Java and C#. In Objective-C data types (except primitives) can be easily inserted and accessed from a container class. No up-casting is needed.
</p>
<p><b>Conclusion</b></p>
<p>Mac development is going to be better than ever. The only thing that I miss now is a better IDE. I think the debugger in xCode has a lot of problems. It should be improved and so should code completion. Code navigation and re-factoring support could also be improved. At the moment I use TextMate a lot for my coding because it is a lot faster to jump around in code. Apple+T brings up a window that does name completion of files so I can jump to a new file with some quick typing. it also does tabs.
</p>
<p>Perhaps Apple should simply improve the integration of the code editor with third party tools. For instance allows some hooks so that after a failed build one can jump to errors in an external code editor.
</p>
<p>Also they should do something to reduce the amount of windows that are open at any given time. I do like multiple windows because it allows you to organize your work more when you have a lot of screen real-estate. But at the moment it just seem to become too many windows to keep track of: debug window, code window, run window, console window, project window etc.
</p>
<p>If not reducing the number of windows at least provide a better way of managing them (although I guess Spaces might solve a lot of the problem).
</p>
<p>My last wish is that Cocoa method names was a bit shorter. Do we really need:
</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">[array <span style="color:#6c0540;">insertObject:</span>obj <span style="color:#6c0540;">atIndex:</span><span style="color:#0000ff;">2</span>];<br />
</div>
<p>Isn't it just as readable to simply write:</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">[array <span style="color:#6c0540;">insert:</span>obj <span style="color:#6c0540;">at:</span><span style="color:#0000ff;">2</span>];</div>
<p>Actually I find it almost hard to read Objective-C code at times because the Cocoa method names are so long that you can't actually glaze at them to see that they say. E.g. "thisIsAVeryLongMethodNameIndeed" takes some time to read. Of course this is all wishful thinking because it is too late to change the names not. But one can always dream.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1142893833575790902006-03-20T14:30:00.000-08:002006-03-20T14:30:33.856-08:00Cocoa Help SystemToday I wasted almost the whole day figuring out how to get the Cocoa help system working. I read guides and tutorials and every time I did I got more and more angry at the articles going on about how simple it was to get working, "I just worked like magic".
Well, I found other users didn't quite agree, and I am definitely one of them. One thing that can take some time getting used to with Cocoa is that naming convention is so important. If you don't give things the right name and are consistent, things easily get screwed up.
So to help others to avoid trouble here are the pitfalls I ran into:
<ol>
<li> The name of the menu item for help <b>HAS</b> to be the same as the name used in the <code>AppleTitle</code> metatags <code>content</code> attribute in your main html help file.</li>
<li><code>CFBundleName</code> key in the <code>info.plist</code> is not set automatically, so you need to do this. Set it to the same name as your executable.</li>
<li>And finally what took me longest time to discover. When you add your folder with help files, you need to select the <b>"Create Folder References for any added folders."</b> radio button when adding the files to project (use add existing files... under resource folder). If you have done correctly the folder should show up as blue. If it shows up as yellow then you did wrong.
</li>
</ol>
Anyway what really bugs me with this thing, is that there is no feedback on what went wrong when you miss one of the steps.
I will add a step by step guide on how to to it later. In the meantime feel free to post questions.
Oh and this <a href="http://developer.apple.com/qa/qa2001/qa1077.html">link</a> explains what the difference between copying added folders and using references is. Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com1tag:blogger.com,1999:blog-21339754.post-1140096371659457002006-02-16T05:26:00.000-08:002006-02-16T05:29:21.276-08:00The Dangers of Static initializationAs I have mentioned before C++ has a rich set of ways for you too shoot yourself in the foot. And that is what I did today. This time it was because of static initialization.
For those familiar with C++, you should know that one can declear variables like this:
<code>
<div style="text-align:left;color:#000000; background-color:#ffffff; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">static</span> MyClass myVariable;
</div>
</code>
I am writing a raytracer and this raytracer has a color class. For convenience I added some static variables for commonly used colors.
<code>
<div style="text-align:left;color:#000000; background-color:#ffffff; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Color Color::<span style="color:#003369;">Red</span>(<span style="color:#0000ff;">1.0f</span>, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">0.0f</span>);
Color Color::<span style="color:#003369;">Green</span>(<span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">1.0f</span>, <span style="color:#0000ff;">0.0f</span>);
Color Color::<span style="color:#003369;">Blue</span>(<span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">0.0f</span>, <span style="color:#0000ff;">1.0f</span>);</div>
</code>
In my code I have a singleton class, <code>AppSettings</code> used to access and store and access settings in the class. I used to create a default scene in my application, so a scene could be rendered when starting the app instead of having to read from file. This scene I was creating in constructor for my main window. For various reasons I decided to move the creation of the default scene to <code>AppSettings</code>.
Now suddenly nothing was rendered anymore. I spent a lot of time figuring out what was wrong. Until it hit me that the <code>AppSettings</code> singleton was allocated statically. Using the debugger I discovered the problem: The <code>AppSettings</code> object was constructed before the static color objects. Since my static color objects were used to create the scene they were set to <code>0.0f, 0.0f, 0.0f</code> when being used by <code>AppSettings</code>.
It also occurred to me that I have had this problem before. But I had forgotten about the dangers of static initialization.
So my own advice on this is. Avoid using static initialization for complex objects because you never know when the initialization code will actually be executed and in which sequence. Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1139329365702956402006-02-07T08:22:00.000-08:002009-05-01T15:59:27.432-07:00Cracking Software on OS X<p>You might be curious on how people actually do it, or you work with securing software and want to know what methods the other side use so you can better figure out how to protect you software. If so, read on. </p>
<p>I am by now means an expert in the area, but I recently did crack a piece of software to learn about it. </p>
<p>To learn about this I suggest you find a small shareware application or similar to practice on. They usually have copy protection that is weak enough to be cracked by a novice. </p>
<p><b>Tools</b><br />
To be able to crack you need a number of different tools. Most of them will already be installed if you have installed Apple’s development tools.<br />
<ul><br />
<li>Interface Builder. You will use this application to poke around in the user interface of the program you wish to crack.</li><br />
<li>otool. With this command line tool you can disassemble a file, or print<br />
out other useful information about the executable. For instance you can use it to find out where in memory your program will be loaded.</li><br />
<li><a href="http://www.macupdate.com/info.php/id/14250">class-dump</a>. Command line utility you have to download. It has some of the same functionality as otool, but allows you to see Objective-C class definitions as they normally appear in the header file.</li><br />
<li>gdb. This is our most important tool. The <span class="caps">GNU</span> Debugger. You will use it to trace through the program you wish to crack to find where int he code the copy protection resides</li><br />
<li>HexEditor. To be able to patch your changes into an executable you need an editor that can edit the raw data on a file.</li><br />
</ul></p>
<p><b>Knowledge</b><br />
Before you can learn and do cracking you have to know some basics first.</p>
<ul>
<li><a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html#//apple_ref/doc/uid/TP30001163">Objective-C</a>. You do not need to be a wiz, but some basic knowledge of Objective-C is needed to crack a OS X program since most are written using it. At least most of the little shareware programs you find</li>
<li>Interface Builder. You should have basic knowledge about how to work with interface builder.</li>
<li>Assembly programming. Again no need to be great at it, or know PowerPC assembly. But you will need to get a <a href="http://www-3.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF778525699600719DF2">reference</a> over the instruction set. <span class="caps">IBM</span> also has a nice <a href="http://www-128.ibm.com/developerworks/library/l-ppc/">intro</a> about <span class="caps">PPC</span> assembly.</li>
<li>Know about the Mac OS X binary application interface (<span class="caps">ABI</span>). This you can read about in the Apple developer documentation. Info about the format of OS X executable can be found <a href="http://developer.apple.com/documentation/Performance/Conceptual/CodeFootprint/Articles/MachOOverview.html#//apple_ref/doc/uid/20001860">here</a> and <a href="http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/index.html#//apple_ref/doc/uid/TP40000895">here</a>. </li>
<li>Most important though is to make yourself familiar with <a href="http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/Articles/32bitPowerPC.html#//apple_ref/doc/uid/TP40002438-SW20">calling conventions</a> for functions on OS X. This will tell you what the different registers are typically used for, which helps reading and understanding assembly code.</li>
</ul>
<p><b><span class="caps">GDB</span> crash course </b><br />
To run a program through the gdb debugger. write:<br />
<code>gdb nameOfProgram.app/Contents/MacOS/nameOfProgram</code><br />
The program has now been loaded into memory. </p>
<p><i>Commands in <span class="caps">GDB</span></i><br />
gdb has a lot of commands and they all have long descriptive names. You can get help about a command by writing <code>help commandname</code>. However to save you typing, gdb allows you to write abbrivations for the names. You can write the first letter and gdb will choose the first command that match these letters. E.g. instead of writing <code>print</code> you can write <code>p</code>.</p>
<p>A lot of the commands will take a line number, function name, or address in memory as argument. Usually you just write the function name as it is. gdb supports command completion with tab if you don’t remember exact name. Line number can also be written directly. If you write an address you have to prefix it with <code>*</code>. Example <code>*0x00fbc</code>.</p>
<p>Useful commands:<br />
<ul><br />
<li><code>run</code> Starts running the loaded program</li><br />
<li><code>info fun</code> Gets information about a given function. Address it is located at etc.</li><br />
<li><code>info sym</code> Dumps information about which segment and function the address or symbol is in. Example: <br />
<code> | (gdb) info sym 0x50fd0
| [StateController validState] in section LC_SEGMENT.__TEXT.__text
</code><br />
Says that address 0×50fd0 is in segment __TEXT and section __text.<br />
</li><br />
<li><code>break</code> Set a breakpoint at given function, line number or address.Short hand <code>b</code><br />
</li><br />
<li><code>rbreak</code> Set breakpoint at all functions with matching regular expression</li><br />
<li><code>delete</code> Delete all breakpoints or breakpoint given by an index number</li><br />
<li><code>disassemble</code> Disassemble code in current function</li><br />
<li><code>backtrace</code> Show a stack backtrace of all functions that were called up to current function.</li><br />
<li><code>stepi</code> Step one instruction at a time in code. Short hand <code>si</code><br />
</li><br />
<li><code>where</code> Print out current location in code. Where the program counter is.</li><br />
<li><code>print</code> Print the value of a memory location, expression or register. Short hand <code>p</code><br />
</li><br />
<li><code>po</code> Interpret expression as the address of a Objective-C object and print out result one would get from asString. This is used to e.g. print Objective-C strings. Since print does not allow you to do that.</li><br />
<li><code>x</code> Examine memory by giving and address or register.</li><br />
<li><code>disp</code> Set expression to display each time program is stopped. For instance after each break or step instruction. Use it to display registers or memory you want to follow the changes in.</li><br />
</ul></p>
<p>You can look up more detailed explanation of each command in the gdb online help. One thing that is useful to know though is that most command that give information about memory or registers let you choose format. E.g. <code>x/x *0x00fb</code> dumps content of address <code>0x00fb</code> as hexadecimal number. <code>x/s *0x00fb</code> dumps contents as a c string. </p>
<p>Supported format types:<br />
<ul><br />
<li><code>o</code> octal</li>
<li><code>x</code> hex</li>
<li><code>d</code> decimal</li>
<li><code>u</code> unsigned</li>
<li><code>t</code> binary</li>
<li><code>a</code> address</li>
<li><code>i</code> instruction. Lets you interpret location in memory as PowerPC assembly instructions</li><br />
<li><code>o</code> octal</li>
<li><code>c</code> char</li>
<li><code>s</code> string</li>
<li><code>T</code> <span class="caps">OST</span>ype</li>
</ul></p>
<p>Showing contenst of registers:<br />
The PowerPC <span class="caps">CPU</span> has a lot of register. The most used general purpose registers are numbered from 0 to 31. To show contents of register you specify a register by writing <code>$</code> and then its name. E.g. <code>$r1</code> denotes the second general purpose register. <code>$pc</code> is the program counter. </p>
<p><code>info register $r3</code> Dumps contents of register 3 and contents of memory it points to.<br />
<code>p $r3</code> Dump contents of memory pointed to by register 3.<br />
<code>x/x $r3</code> Dump contents of memory pointed to by register 3. as hexadecimal format.</p>
<p>Setting value of register:<br />
You can set the value of a register using <code>set</code> command. E.g. <code>set $r3=0x44</code></p>
<p><b>otool</b><br />
As with gdb you have to specify the path to the executable. E.g. <code>otool -l nameOfProgram.app/Contents/MacOS/nameOfProgram</code>. otool has several options, you can read about them all by writing <code>man otool</code> in Termina.app. However there are a few options that are of most use to us.</p>
<p><ul><br />
<li><code>-l</code> Display load commands. This will give you information about where the different segments in the code get loaded into memory. And at which offset in your executable this segment can be found. E.g. if you look at the output you will see e.g.: <br />
<code>
Section
sectname __text
segname __TEXT
addr 0x00002884
size 0x0009149c
offset 6276
align 2^2 (4)
reloff 0
nreloc 0
flags 0x80000400
reserved1 0
reserved2 0
</code><br />
This tells us that this section can be found at offset (decimal) 6276 in executable. When this program is loaded into memory that section is placed at address (in hex) 0×00002884.</p>
<p></li>
<li><code>-tVv</code> Disassembles the give executable and dumps to standard output.</li>
</ul></p>
<p><b>class-dump</b><br />
This is easy to use just type <code>class-dump nameOfProgram.app/Contents/MacOS/nameOfProgram</code> to dump out definition of Objective-C classes in executable. When you download the file though it doesn’t come with an installer so you need to put it in your search path. E.g. I copied it directly to <code>/usr/bin</code> but you have to be root user to do that. So do a <code>sudo cp class-dump /usr/bin</code> e.g.</p>
<p><b>Lets get started</b><br />
First thing to do is to go into the application bundle by right clicking and select Show Package Contents. Then you navigate to e.g <code>Contens/Resources/English.lproj</code>. Here there will be .nib files for the user interface. I opened <code>MainMenu.nib</code>. Here I found the registration sheet. I selected the register… button. Pressing <code>shift+apple+i</code> brings up the inspector panel. Here I looked at the <code>target</code> attribute. It shows which method the button click is connected to. Say it is called <code>registerAction:</code> and exist in the <code>AppController</code>. Try looking for something similar. Then you will have a starting point to start looking for the registration code.</p>
<p>An alternative is to use <code>class-dump</code> on the executable and search for method names called something with register, serial number etc or a class nammed RegHandler, Reg or something similar. E.g. <code>class-dump executable | grep register</code>.</p>
<p>Now that we found a method to inspect, load the executable in gdb. Type <br />
<code>break registerA</code> and hit tab to complete our method. Then we have set a break point. Type <code>run</code> and out application should start running and then stop executing when it reaches our breakpoint. </p>
<p>When you trace through the code using <code>stepi</code> you will often see lines like this: <br />
<code>00010070 bl 0x12e10 ; symbol stub for: _objc_msgSend</code></p>
<p>What this is a dispatching of a message to an Objective-C object. This is how methods are called in Objective-C. r3 contains pointer to object that will receive message. You can find out what kind of object it is by typing <code>po $r3</code>, this will give a description of object. If object is a <code>NSString</code> it will display the string. r4 contains the message selector. This basically says which method to call. The good thing about this is that the message selector also happens to be a pointer to the c string name of the method. So by typing <code>x/s $r4</code> you can find out what the name is of the objective-c method being called. r5, r6.. contains the first, second… arguments of the method call. The result of the method call is returned in r3. </p>
<p>r1 and r30 typically points to the start of the stack. So <code>stw</code> and <code>lwz</code> using these registers usually access local variables in the method.</p>
<p><b>How copy protection works</b><br />
Copy protection can work in many ways. But the way it worked in my case is that I register the product with a name. I then get back a serial number for that registration name from the application maker. So say I register the application on “Erik” then there will be a unique serial number associated with that name. </p>
<p>When the program loads it reads my registered name from a file and then performs a calculation on it. This will produce a serial number or seed that is compared with the serial number I have put into my application. </p>
<p>What I managed to do was to eventually locate the method that did generation of serial number or seed. I then put a break at that method and rerun the application. Then I did a <code>backtrace</code> to find out which method called the serial number generator at startup. </p>
<p>From there on I was able to find the place where the serial number method was called and the code that compared the results afterwards. </p>
<p>What the code did was doing a comparison with a fixed number against the returned seed (the registered code and provided serial number are used together to produce some unique seed numbers). </p>
<p>In the debugger I used <code>set</code> to set the registered that were compared to the exact same number they were compared against. Then I typed <code>continue</code> to continue execution of code. The program now ran as registered. So it worked.</p>
<p><b>Patching</b><br />
The last step to do when you have found out how to circumvent the copy protection is to change the executable (patch it). This you can do with HexEdit. The problem is to find out where in the file to make changes. </p>
<p>There are a number of things that are handy to know when doing this:<br />
<ul><br />
<li>Since PowerPC is a <span class="caps">RISC</span> processor every instruction has the same length, which is 32 bits (4 bytes). Thus you can easily calculate the number of bytes a chunk of code takes. Just multiply number of instructions with 4. This also means you can easily jump over an instruction when tracing by writing <code>jump *($pc+4)</code><br />
</li>
<li>As described before you can use <code>otool -l</code> to get information about code or data segments. Using this information you can find the location of an address in memory in the file as follows: <code>location_in_file = location_in_memory - segment_address - segment_offset </code>
</li>
</ul></p>
<p>So to find the location in the executable to change, you use <code>info sym</code> as mentioned before to figure out in which segment and section it is in. Then use <code>otool -l</code> to find the coresponding segment and section. Then read of the offset and address to calculate location of code in the executable. </p>
<p>To find out what hex codes corresponds to instructions you can write a simple assembly program as below. You can just fill in the instructions you need the hex codes for.<br />
<code>
.text
.globl _main
_main:
li r0,3241
stw r0,84(r30)
lwz r0,84(r30)
</code></p>
<p>Name the file with the filename extension <code>.s</code>. The file can then be compiled with e.g.: <code>gcc newfunc.s -o temp</code> if your file is named newfunc.s. </p>
<p>You can now load your app into gdb and look at the hex code for your application by writing <code>x/24xb main</code>. Which will list the 24 first bytes starting from main as hex values. 24 bytes corresponds to 24/4 = 6 instructions.</p>
<p>If you have any questions or comments please let me know. I will update this post later.</p>
<p><b>References</b><br />
Here you can find more information:<br />
<a href="http://www.phrack.org/">www.phrack.org</a><br />
<a href="http://www.phrack.org/phrack/63/p63-0x10_PowerPC_Cracking_on_OSX_with_GDB.txt">PowerPC cracking with <span class="caps">GDB</span></a></p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com3tag:blogger.com,1999:blog-21339754.post-1139094401833675332006-02-04T15:06:00.000-08:002006-02-16T06:15:36.343-08:00Object Oriented programming in CObject Oriented programming in C has been covered by many other before and actually whole frameworks are based on it. E.g. the Gtk (API on Gnome) and <a href="http://developer.apple.com/documentation/CoreFoundation/index.html#//apple_ref/doc/uid/TP30000421">Core Foundation</a> (API on OS X).
So I am not going to show how all aspects of Object Oriented Programming (OOP) can be done. Just a few to demonstrate that it is in fact possible to do OOP in C. Perhaps then it will be clearer what the benefits of OOP is and that it is more a matter of style and a way of thinking than language syntax. Of course C++ and Java wasn't created for no reason. They have syntax that assist one in thinking and designing programs around Object Oriented (OO) principles. But that is all they do, assist. They do not magically make your program OO.
<b>Procedural example</b>
To understand the benfits of OOP lets take an example of one piece of code written in a procedural way, and then later in a OOP way.
Our task is simple. We want to display a button on the screen that the user can click. When he/she clicks the button the program exits. We here assume we have the needed functions and skip initialing graphics hardware etc.
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">void</span> <span style="color:#003369;">draw_box</span>(<span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y, <span style="color:#881350;">int</span> width, <span style="color:#881350;">int</span> height);
<span style="color:#881350;">void</span> <span style="color:#003369;">draw_string</span>(<span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y, <span style="color:#881350;">char</span> *text);
<span style="color:#881350;">int</span> <span style="color:#003369;">is_mouse_button_down</span>(<span style="color:#881350;">int</span>* x, <span style="color:#881350;">int</span>* y);
<span style="color:#881350;">int</span> <span style="color:#003369;">is_inside</span>(<span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y, <span style="color:#881350;">int</span> x1, <span style="color:#881350;">int</span> y2, <span style="color:#881350;">int</span> width, <span style="color:#881350;">int</span> height);
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
<span style="color:#003369;">draw_box</span>(<span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>);
<span style="color:#003369;">draw_string</span>(<span style="color:#0000ff;">12</span>, <span style="color:#0000ff;">12</span>, <span style="color:#760f15;">"Quit"</span>);
<span style="color:#881350;">int</span> done = <span style="color:#0000ff;">0</span>;
<span style="color:#881350;">while</span> <span style="color:#003369;"> </span>(!done) {
<span style="color:#881350;">int</span> x, y;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(!<span style="color:#003369;">is_mouse_button_down</span>(x, y))
<span style="color:#881350;">continue</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">is_inside</span>(x, y, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>))
done = <span style="color:#0000ff;">1</span>;
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
} </div>
</code>
We assume here that the functions listed above <code>main()</code> exists and do as their name implies. The simple program just draws a box that represent our button on screen and then draws a string on top of that so the button gets a label/caption.
It is obvious that if we want to create more than one button we it is tedious to keep passing almost to same coordinates to draw the string on the button. So we create a new function <code>draw_button()</code>, which gives us the following code:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
<span style="color:#003369;">draw_button</span>(<span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">30</span>, <span style="color:#0000ff;">60</span>, <span style="color:#760f15;">"Quit"</span>);
<span style="color:#003369;">draw_button</span>(<span style="color:#0000ff;">50</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">30</span>, <span style="color:#0000ff;">60</span>, <span style="color:#760f15;">"Button1"</span>);
<span style="color:#881350;">int</span> done = <span style="color:#0000ff;">0</span>;
<span style="color:#881350;">while</span> <span style="color:#003369;"> </span>(!done) {
<span style="color:#881350;">int</span> x, y;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(!<span style="color:#003369;">is_mouse_button_down</span>(x, y))
<span style="color:#881350;">continue</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">is_inside</span>(x, y, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>))
done = <span style="color:#0000ff;">1</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">is_inside</span>(x, y, <span style="color:#0000ff;">50</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>))
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Clicked Button1"</span>);
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
However let us say that each time one clicks Button1 we want it to move 5 pixels to down and to the right. We can change as follows:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
<span style="color:#881350;">int</span> bx = <span style="color:#0000ff;">50</span>, by = <span style="color:#0000ff;">10</span>;
<span style="color:#003369;">draw_button</span>(<span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">30</span>, <span style="color:#0000ff;">60</span>, <span style="color:#760f15;">"Quit"</span>);
<span style="color:#003369;">draw_button</span>(bx, by, <span style="color:#0000ff;">30</span>, <span style="color:#0000ff;">60</span>, <span style="color:#760f15;">"Button1"</span>);
<span style="color:#881350;">int</span> done = <span style="color:#0000ff;">0</span>;
<span style="color:#881350;">while</span> <span style="color:#003369;"> </span>(!done) {
<span style="color:#881350;">int</span> x, y;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(!<span style="color:#003369;">is_mouse_button_down</span>(x, y))
<span style="color:#881350;">continue</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">is_inside</span>(x, y, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>))
done = <span style="color:#0000ff;">1</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">is_inside</span>(x, y, bx, by, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>)) {
bx += <span style="color:#0000ff;">5</span>;
by += <span style="color:#0000ff;">5</span>;
<span style="color:#003369;">button_draw</span>(bx, by <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">30</span>, <span style="color:#760f15;">"Button1"</span>);
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Clicked Button1"</span>);
}
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
Now there is a number of problems with this code.
<ol>
<li>There is no forced relationship between the drawing of the
button and checking for clicks in it. It is possible
to change drawing position of button and forget to update
the mouse code check code as well.</li>
<li>If we start making more buttons, say 20 more buttons, which
each will move in different direction when clicked, it becomes
a nightmare to keep track of where each button is.</li>
</ol>
<b>Object Oriented Approach</b>
What needs to be done is instead of thinking of the problem as that of actions. That is what needs to be drawn, where etc. One should focus on the button as an entity or unit in the program. That is we should move to a more data centric way of thinking.
Instead of thinking about drawing, checking for mouse clicks, etc, we think about creating, placing and moving button objects.
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">typedef</span> <span style="color:#881350;">struct</span> {
<span style="color:#881350;">int</span> x, y;
<span style="color:#881350;">int</span> width, height;
<span style="color:#881350;">char</span> *caption;
} Button;
Button* <span style="color:#003369;">button_create</span>(<span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y, <span style="color:#881350;">int</span> width, <span style="color:#881350;">int</span> height)
{
Button* b = <span style="color:#003369;">malloc</span>(<span style="color:#881350;">sizeof</span>(Button));
b.x = x;
b.y = y;
b.width = width;
b.height = height;
<span style="color:#003369;">button_draw</span>(b);
}
<span style="color:#881350;">void</span> <span style="color:#003369;">button_draw</span>(Button* <span style="color:#881350;">this</span>)
{
<span style="color:#003369;">draw_button</span>(<span style="color:#881350;">this</span>->x, <span style="color:#881350;">this</span>->y, <span style="color:#881350;">this</span>->width, <span style="color:#881350;">this</span>->height, <span style="color:#881350;">this</span>->caption);
}
<span style="color:#881350;">int</span> <span style="color:#003369;">button_is_inside</span>(Button* <span style="color:#881350;">this</span>, x, y)
{
<span style="color:#881350;">return</span> <span style="color:#003369;">is_inside</span>(x, y, <span style="color:#881350;">this</span>->x, <span style="color:#881350;">this</span>->y, <span style="color:#881350;">this</span>->width, <span style="color:#881350;">this</span>->height);
}
<span style="color:#881350;">void</span> <span style="color:#003369;">button_move</span>(Button* <span style="color:#881350;">this</span>, <span style="color:#881350;">int</span> dx, <span style="color:#881350;">int</span> dy)
{
<span style="color:#881350;">this</span>->x += dx; <span style="color:#881350;">this</span>->y += dy;
<span style="color:#003369;">button_draw</span>(<span style="color:#881350;">this</span>);
}
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
Button* quit = <span style="color:#003369;">button_create</span>(<span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">20</span>, <span style="color:#760f15;">"Quit"</span>);
Button* b1 = <span style="color:#003369;">button_create</span>(<span style="color:#0000ff;">50</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">60</span>, <span style="color:#0000ff;">20</span>, <span style="color:#760f15;">"Button1"</span>);
<span style="color:#881350;">int</span> done = <span style="color:#0000ff;">0</span>;
<span style="color:#881350;">while</span> <span style="color:#003369;"> </span>(!done) {
<span style="color:#881350;">int</span> x, y;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(!<span style="color:#003369;">is_mouse_button_down</span>(x, y))
<span style="color:#881350;">continue</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">button_is_inside</span>(quit, x, y))
done = <span style="color:#0000ff;">1</span>;
<span style="color:#881350;">if</span> <span style="color:#003369;"> </span>(<span style="color:#003369;">button_is_inside</span>(b1, x, y)) {
<span style="color:#003369;">button_move</span>(<span style="color:#0000ff;">5</span>, <span style="color:#0000ff;">5</span>)
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Clicked Button1"</span>);
}
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
As one can see, we can now easily create lots of buttons and move them around without loosing track.
But this is not all there is to OOP. So far I have made it look like it is just about collecting all variables in a struct so it is easier to see which variables that belong to each other. To show another important aspect of OOP I will give another code example. Here we are creating an <code>Array</code> object. The point of this object is that unlike regular C arrays it keeps track of its size, so we can query it in the rest of the program.
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">typedef</span> <span style="color:#881350;">struct</span> {
<span style="color:#881350;">int</span> *data;
<span style="color:#881350;">int</span> size;
} Array;
Array* <span style="color:#003369;">array_create</span>(<span style="color:#881350;">int</span> size)
{
<span style="color:#881350;">int</span>* a = <span style="color:#003369;">malloc</span>(size*<span style="color:#881350;">sizeof</span>(<span style="color:#881350;">int</span>));
Array* array = <span style="color:#003369;">malloc</span>(<span style="color:#881350;">sizeof</span>(Array));
array->data = a;
array->size = size;
}
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
Array* a = <span style="color:#003369;">array_create</span>(<span style="color:#0000ff;">10</span>);
<span style="color:#236e25;">/* Fill array with values */</span>
<span style="color:#881350;">for</span> <span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> i=<span style="color:#0000ff;">0</span>; a->size; i++) {
a->data[i] = i;
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
Now this is all fine. But lets consider that we want to iterate over the whole array using pointers. So we decide that in order to do this it would be better to change the data structure for the Array. Instead of storing size of array we will store a pointer to the end of the array.
So we change the data structure to the following:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">typedef</span> <span style="color:#881350;">struct</span> {
<span style="color:#881350;">int</span> *begin;
<span style="color:#881350;">int</span> *end;
} Array;
</div>
</code>
Except there is one problem with this. Suddenly our code in <code>main()</code> is broken. We have to change <code>a->size</code> to <code>a->end - a->begin</code>. That is of course quick for us to do in this example. But what if we had written 10000 lines of code and iterated over the array loads of places? We would have to go through all that source code and made changes!
<b>Data encapsulation</b>
Data encapsulation is the solution to this problem. And this is one of the cornerstones of OOP. Instead of letting users of our Array object access its data directly we hide the internal representation of the object by requiring the users of it to access its properties through function calls.
The code below shows this approach:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Array* <span style="color:#003369;">array_create</span>(<span style="color:#881350;">int</span> size)
{
<span style="color:#881350;">int</span>* a = <span style="color:#003369;">malloc</span>((size+<span style="color:#0000ff;">1</span>)*<span style="color:#881350;">sizeof</span>(<span style="color:#881350;">int</span>));
Array* array = <span style="color:#003369;">malloc</span>(<span style="color:#881350;">sizeof</span>(Array));
array->begin = a;
array->end = a+size;
}
<span style="color:#881350;">int</span> <span style="color:#003369;">array_size</span>(Array* <span style="color:#881350;">this</span>)
{
<span style="color:#881350;">return</span> <span style="color:#881350;">this</span>->end - <span style="color:#881350;">this</span>->begin;
}
<span style="color:#881350;">int</span>* <span style="color:#003369;">array_begin</span>(Array* <span style="color:#881350;">this</span>)
{
<span style="color:#881350;">return</span> <span style="color:#881350;">this</span>->begin;
}
<span style="color:#881350;">int</span>* <span style="color:#003369;">array_end</span>(Array* <span style="color:#881350;">this</span>)
{
<span style="color:#881350;">return</span> <span style="color:#881350;">this</span>->end;
}
<span style="color:#881350;">void</span> <span style="color:#003369;">array_set_at</span>(Array* <span style="color:#881350;">this</span>, <span style="color:#881350;">int</span> index, <span style="color:#881350;">int</span> value)
{
<span style="color:#881350;">this</span>->begin[index] = value;
}
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
Array* a = <span style="color:#003369;">array_create</span>(<span style="color:#0000ff;">10</span>);
<span style="color:#236e25;">/* Fill array with values */</span>
<span style="color:#881350;">for</span> <span style="color:#003369;"> </span>(<span style="color:#881350;">int</span> i=<span style="color:#0000ff;">0</span>; <span style="color:#003369;">array_size</span>(a); i++) {
<span style="color:#003369;">array_set_at</span>(a, i, i);
}
<span style="color:#236e25;">/* Print values to screen */</span>
<span style="color:#881350;">for</span> <span style="color:#003369;"> </span>(<span style="color:#881350;">int</span>* it = <span style="color:#003369;">array_begin</span>(a); it != <span style="color:#003369;">array_end</span>(a); it++) {
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"%d"</span>, *it);
}
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
<b>Inheritance</b>
Another important aspect of OOP in inheritance. This is a mechanism of style of programming that allows one to reuse a lot of code. Of course the code examples I show here are rather small so they don't show fully how much code there is to save having to rewrite. But it doesn't require too much imagination to see that this can be a big benefit when writing larger programs.
To illustrate the usage of inheritance I will use geometric primitives. E.g. a point has a location is space. We can imagine functions to move the point in space relative to current position or set it at an absolute position.
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">typedef</span> <span style="color:#881350;">struct</span> {
<span style="color:#881350;">int</span> x, y;
} Point;
Point* <span style="color:#003369;">point_alloc</span>() {
<span style="color:#881350;">return</span> <span style="color:#003369;">malloc</span>(<span style="color:#881350;">sizeof</span>(Point));
}
Point* <span style="color:#003369;">point_init</span>(Point* <span style="color:#881350;">this</span>, <span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y) {
<span style="color:#881350;">this</span>->x = x; <span style="color:#881350;">this</span>->y = y;
}
<span style="color:#881350;">void</span> <span style="color:#003369;">point_move</span>(Point* <span style="color:#881350;">this</span>, <span style="color:#881350;">int</span> dx, <span style="color:#881350;">int</span> dy) {
<span style="color:#881350;">this</span>->x += dx; <span style="color:#881350;">this</span>->y += dy;
}
<span style="color:#881350;">float</span> <span style="color:#003369;">point_distance_to_origo</span>(Point* <span style="color:#881350;">this</span>) {
<span style="color:#881350;">return</span> <span style="color:#003369;">sqrt</span>(<span style="color:#881350;">this</span>->x*<span style="color:#881350;">this</span>->x + <span style="color:#881350;">this</span>->y*<span style="color:#881350;">this</span>->y);
}
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
Point* p = <span style="color:#003369;">point_init</span>(<span style="color:#003369;">point_alloc</span>(), <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>);
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Distance to origo: %f"</span>, <span style="color:#003369;">point_distance_to_origo</span>(p));
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
Now we want to define a <code>Circle</code> object but we do not want to have to rewrite the code for moving the center of the circle since it is basically the same as moving a point. How can we reuse the <code>point_move()</code> function with <code>Circle</code>?
All we need to do is to make it look like for the <code>point_move()</code> function as if it is dealing with a Point data structure as its first argument.
This is actually not as hard as is seems:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">
<span style="color:#881350;">typedef</span> <span style="color:#881350;">struct</span> {
Point position;
<span style="color:#881350;">int</span> radius
} Circle;
</div>
</code>
If we have a pointer to a circle structure we can now access the x and y coordinates like this:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Circle *c;
<span style="color:#881350;">int</span> x = c->position->x;
</div>
</code>
However this is not very interesting for us since it does not allow us to treat a Circle as a point. What is interesting is that we can do the following:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Circle *c;
Point* p = (Point*)c;
<span style="color:#881350;">int</span> x = p->x;
</div>
</code>
How is this possible!? The reason why this works has to do with how C deals with structs. When you define a struct, the C compiler will store offset values for each variable in the struct. Here is how it works.
When you write <code>int x = p->x</code> the C compiler will compile this into machine code that takes the start address of the struct pointed to by p and add a offset value representing the x to this address. This will create a new address which is used to locate the x member variable.
Addresses are typically given on int boundaries (32 bit). So in the case of the Point data structure. Let us say that p is located in memory location 11. Then since x is the first variable defined it is located at offest 0, which means it is at 11 too. y on the other hand is the second variable so it has offset 1. Meaning it is on location 11+1=12. p->y would thus be converted to address 12. Bottom line is that offsets are given relative to top of struct definition.
Thus if we put Point at the top of the Circle definition, the offset values x and y will still be valid for the Circle struct. Of course the C compiler doesn't know that, so we must trick it by doing a cast to <code>Point*</code>. Otherwise it will complain that Circle does not have any members named x and y.
So to demonstrate inheritance, here is a short program again:
<code>
<div style="text-align:left;color:#000000; background-color:#eeeeee; border:solid grey 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">Circle* <span style="color:#003369;">circle_alloc</span>() {
<span style="color:#881350;">return</span> <span style="color:#003369;">malloc</span>(<span style="color:#881350;">sizeof</span>(Circle));
}
Circle* <span style="color:#003369;">circle_init</span>(Circle* <span style="color:#881350;">this</span>, <span style="color:#881350;">int</span> x, <span style="color:#881350;">int</span> y, <span style="color:#881350;">int</span> radius) {
<span style="color:#003369;">point_init</span>((Point*)<span style="color:#881350;">this</span>, x, y);
<span style="color:#881350;">this</span>->radius = radius;
}
<span style="color:#881350;">float</span> <span style="color:#003369;">circle_area</span>(Circle* <span style="color:#881350;">this</span>) {
<span style="color:#881350;">return</span> <span style="color:#881350;">this</span>->radius*<span style="color:#881350;">this</span>->radius*M_PI;
}
<span style="color:#881350;">int</span> <span style="color:#003369;">main </span>(<span style="color:#881350;">int</span> argc, <span style="color:#881350;">char</span> <span style="color:#881350;">const</span>* argv[])
{
Cricle* c = <span style="color:#003369;">cricle_init</span>(<span style="color:#003369;">circle_alloc</span>(), <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">10</span>, <span style="color:#0000ff;">5</span>);
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Distance to origo: %f"</span>, <span style="color:#003369;">point_distance_to_origo</span>((Point*)c));
<span style="color:#003369;">printf</span>(<span style="color:#760f15;">"Area of circle: %f"</span>, <span style="color:#003369;">circle_area</span>(c));
<span style="color:#881350;">return</span> <span style="color:#0000ff;">0</span>;
}
</div>
</code>
There is of course a lot more to OOP, but several other people on the web have written much more extensive explanations, even books on doing OOP in C. My intention here was just to give an introduction.
So you might wonder what is the point of using a dedicated OOP language like C++ or Java? First of all is the syntax sugar.
Instead of writing <code>point_move(p, x, y)</code> you can write <code>p->move(x, y)</code> in C++. C++ makes p the first argument to move but by putting it in front it makes it clear that p is the object of focus. The other important aspect is that one is not required to prefix the function names with say <code>point_</code> to avoid clutter in the namespace. C++ makes sure that each function called <code>move()</code> is associated with a type. And then there is the case of e.g. polymorphism that would just get very ugly looking in C.
I do of course not advice anybody to do OO in C. There is no need for that when we have so many nice OOP language to choose from. Doing OOP in C however does make it clear that OOP is a paradigm and not a language feature.
To look at it from the other side: One could choose to program procedure oriented in Java or C++ for instance by:
<ol>
<li>Declaring member variables public and not use accessor methods (thus breaking the OO principle of data encapsulation and abstraction).</li>
<li>One could use switch case statements instead of polymorphism.</li>
<li>Don't do code reuse by using inheritance. Make all methods static</li>
</ol>
Personally I believe OOP thinking is best understood by either learning it through a procedure oriented language or a OOP language that takes OO to the extreme like Smalltalk. E.g. Smalltalk just force the user to think more about OO than C++ or Java does. Java and C++ lets programmers easily slip into procedure oriented ways of thinking. And I believe the reason for this is that both C++ and Java are procedure oriented at the low level. Instances of primitive types like <code>int, float and char</code> are not objects. <code>if, while</code> and <code>for</code> statements have procedure like semantics. And these are the statements and objects the newbie programmers are exposed to first, thus locking them into a procedure oriented way of thinking early on. Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com1tag:blogger.com,1999:blog-21339754.post-1139088343347032552006-02-04T13:25:00.000-08:002006-02-04T13:25:43.390-08:00Teaching Object Oriented programmingToday I decided I want to talk about one of the topics that constantly bug me: Object Oriented programming and people's conception of what it is about. Basically what I wish is that Universities and tech school started teaching Object Oriented programming in a different way. Why?
Because I believe that most students today totally miss the point about Object Oriented programming. Even at Master level they still don't understand what it is about. And I blame the universities for this.
Take my University College. I think it is the prime example of how to teach students Object Oriented programming in such a way that they just don't get it. I know that they didn't get it because I was the TA in the programming class and I helped the students with their programming assignments and corrected their hand inns.
The first half of the year the University College taught C and the second half they taught Java. In the C classes they learned procedure oriented programming. In the Java classes they learned object oriented programming and GUI programming. Or so was the intention at least.
Most of the hand ins I got were still procedure oriented. And that is my issue with Universities teaching object oriented programming to students using object oriented languages like Java. Students think that because they use an object oriented language their program automatically becomes object oriented!
I remember having a discussion this year with a fellow student at the University of Utrecht. Unlike me he had been taught Java as the main programming language in University. My main programming language was C++. Now he was forced to learn C++, because of the project he was doing. He was going on about how much more powerful and modern java was than C++. I think it goes without saying that I think that is rubbish, but I am going to address that some other time. One thing he complained about in C++ was that it was not fully object oriented. As if being fully object oriented is some sort of quality stamp. The reason he gave was that not everything was classes. One could write functions that were not part of a class.
Very sure of himself he said that in Java everything was object oriented. I said no, and that one can very well write programs in Java that are not object oriented. He protested loudly saying that was simply not possible, since everything had to belong to a class, there was no way you could write a program that was not object oriented.
Which made it clear to me once again that even at Master level in Computer Science students don't understand that object oriented programming and procedure oriented programming are not programming language features but paradigms. Some languages have special support for certain paradigms but they do not force you to use that paradigm.
All Java does by forcing you to declare your functions as methods in a class is to force your methods to reside in a namespace. It does not force you to follow the object oriented paradigm.
To deal with these broad misconceptions I suggest that one starts teaching object oriented programming in a procedure oriented language like C. So that students will see clearly that Object Oriented programming is not a language feature but a style of programming and a way of thinking about programming problems.
Then after students are familiar with object oriented programming in a procedure oriented language they should be taught a object oriented language so that it is made clear that these languages are merely there to make it easier to do object oriented programming because they have special language constructs that support the paradigm, not because they force you to think object oriented.
Now you might already have been shaking your head for a long time, wondering what I am talking about. What is this guy talking about you might think. Object oriented programming in C!? That is not possible! Well I used to think that myself. Not at least owing to the fact that I wasn't taught what object oriented programming is properly at University either. But in the next post I will show how object oriented programming can be done in C.
Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1137938749877181122006-01-22T06:05:00.000-08:002009-05-01T15:09:28.379-07:00Pointers, References and Objects in C++<p>What I notice is that almost all C++ beginners have problems with the way objects are passed and handled in C++. Especially people coming from a Java background. Java basically just have one way of dealing with objects and that is through references (well that is a white lie, basic types are not). C++ on the other hand has 3! Not only that but they can be mixed in all kinds of confusing ways. So this post will be geared towards you Java programmers.</p>
<p><b>Objects</b><br />
Dealing with objects directly instead of having pointers or references to them is the easy way. For primitive types like int, float, double etc Java and C++ are the same. </p>
<p>You declare a object like this:</p>
<p><code>int number;</code></p>
<p>Then you can assign values:</p>
<p><code>number = 20;</code></p>
<p>Okay I am not going to dwell on this. It is not a newbie tutorial. What is important to notice is that in C++ even classes can be declared as objects this way. They are then allocated on the stack and not the heap like objects allocated using <code>new</code>. We’ll talk more about the stack and head later.</p>
<p><code>MyClass obj;<br />
MyClass otherObj;</p>
<p>obj = otherObj;</code></p>
<p>Unlike Java the last statement copies the whole contents of <code>otherObj</code> to obj. So no <code>clone()</code> or <code>copy()</code> type of statement is needed. You also do not need to create the object using <code>new</code>. So you might wonder. How does one pass arguments to the constructor?</p>
<p>In Java it would be something like this:</p>
<p><code>MyClass obj = new MyClass("Hello world", 12);</code></p>
<p>In C++ this can be done like:</p>
<p><code>MyClass obj("Hello world", 12);</code></p>
<p><b>Pointers</b><br />
Let us first start with the pointers. They resemble references in Java or other OO languages the most. </p>
<p><code>int* ptr; // Declares a variable that is a pointer to a int value
ptr = new int; // allocates memory for int and sets ptr to point to value
int number; // Creates a int value, allocated on stack
number = 20; // Assigns it a value of 20
*ptr = 30; // Sets the value pointed to by ptr to 30
number = *ptr; // Assigns 30 to the number variable
number = ptr; // Assign the address of ptr points to to number.
number = 40;
ptr = &number; // Makes ptr point to number variable
number = 26; // ptr now points to the value 26</code></p>
<p>The code snippet above shows the many ways of using pointers. </p>
<p><b>References</b><br />
References lets you write your code as if were dealing with objects direcly as allocated on a stack. The reason why C++ lets you do this is because it is a sort of safe pointer. Little will go wrong by treating the the reference as the object itself. A reference is however little more than a <code>const</code>. This put some limitations on its usage. Once a reference has been set to point to an object it can not be changed to point to another object. So in practice it is almost like an alias. </p>
<p><code>int number;
int value = 20;
int& ref = number; // Sets ref to reference (point to) number
ref = 30; // Changes the value of number to 30
ref = value; // Changes the value of number to 20. </code></p>
<p>As can be seen in last line, when reference has first been assigned it is not changed. <code>ref = value</code> will not make ref reference the value variable. </p>
<p><code>*, &, .</code>
*, & and . are symbols that easily have their meaning mixed up in peoples head. Perhaps because what they mean depends on the context.</p>
<p>E.g. here are some different meaning of &:</p>
<p><code>int var2 = 3;
int var3 = 5;
int var1 = var2 & var3; // Binary and var2 and var3, Result = 7
bool b1 = false, b2 = true;
bool b = b1 && b2; // Logical and b1 and b2, Result = false
int* ptr = &value; // Get the address of value
int& ref = value; // Declare ref as a reference</code></p>
<p>Likewise * can have many meanings:</p>
<p><code>var1 = var2*var3; // Multiply var2 and var3
int* ptr; // Declear a int pointer
ptr = &var3;
*ptr = var2; // Set value of variable pointed to by ptr to var2
</code></p>
<p>And the situation doesn’t exactly get easier by the fact that these symbols can be combined.</p>
<p><code>int*& ptrRef = ptr; // Declares a reference to a pointer
int** ptrPtr = &ptr; // Declare a pointer to a pointer</code></p>
<p><b>Pointer arithmetic and arrays</b><br />
So what Java programmers probably wonder, is what is it one can do with pointers that one can’t do with references. What is the point with pointers anyway?</p>
<p>Pointers allow you to do operations on the pointers themselves and not just the values they point to. References do not allow this (C++ or Java).</p>
<p><code>int array[20]; // Creates and array with 20 int elements
array[2] = 4; // Assign 4 to the 3rd element in the array
int* ptr = array; // Makes ptr point to first element in array;
*ptr = 3; // Assign 3 to the first element of array
ptr[2] = 5; // Assign 5 to the 3rd element in the array
ptr = ptr+2; // Address pointer points to increased by 2
*ptr = 3; // Assign 3 to the 3rd element of array
ptr[2] = 5; // Assign 5 to the 5th (3+2) element of array</code></p>
<p>In C one can even set a pointer to point directly to an arbitrary memory location.</p>
<p><code>int *ptr = 40; // Set ptr to point to address 40</code></p>
<p>Back in the day this was actually how one manipulated graphics. Typically one would set a pointer to point to the location in memory where the screen buffer was located. Then the pixels on the screen could be changed by changing the value the pointer was pointing to. </p>
<p>Anyway I think I am getting into nitpicking. If somebody wants me to elaborate more on this I will. Otherwise I will write about other things.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0tag:blogger.com,1999:blog-21339754.post-1137934930215301052006-01-22T04:54:00.000-08:002009-05-01T15:11:55.182-07:00About Load Code<p>My intention with this blog is to write about programming for beginners to professionals. Mostly I will focus on intermediate. I am also very opinionated about programming languages and practices and will also comment on that as well as make language comparisons. I love to try out new tools and programming languages. Well maybe not so much anymore. </p>
<p>Anyway I been a C/C++ programmer for many years, both on hobby basis, as a student and as a professional. It is a language that probably has the riches selections of ways to shoot yourself in the foot. Still I can’t seem to get away from it. There is just so much flexibility, libraries and tools available for the language. And when it comes to performance it is hard to match. It is a very handy language to know but I find that a lot of Computer Science students these days have a lot of trouble understanding the language. So I thought I’d write some tutorials and tips and tricks on the things that are difficult for beginners to understand. </p>
<p>Apart from that I would also like to write about what is more like my passion. Languages like Ruby, Python and Smalltalk. A lot of these languages I like just because of their elegance, power and simplicity, but unfortunately don’t use that much because they are simply not practical. So I will also write some about Script languages and how you can benefit from them even if you are a C++ programmer. </p>
<p>And lastly I’d like to give introduction to more advance topics. A sort of algorithms for dummies. Well…. I always have a lot of ideas. We’ll see what I will actually get around to writing about.</p>Adam Smithhttp://www.blogger.com/profile/05862497628924727350noreply@blogger.com0