Created
May 31, 2010 04:31
-
-
Save jonsterling/419532 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define $(obj,method) obj->method(obj) | |
| #define $$(obj,method,args) obj->method(obj,args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PString * str = initializePString(); | |
| str.setValue(str, "a value"); | |
| printf("string: %s", str.value(str)); | |
| str.destroy(str); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PString *str = initializePString(); | |
| $$(str, setValue, "a value"); | |
| printf("string: %s", $(str, value)); | |
| $(str, destroy); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PString * initializePString() { | |
| PString *str=(PString*)malloc(sizeof(PString)); | |
| str->chars = (char *)malloc(sizeof(char) * 1); | |
| str->chars[0] = ''; | |
| str->length = &length; | |
| str->characterAtIndex = &characterAtIndex; | |
| str->value = &value; | |
| str->setValue = &setValue; | |
| str->destroy = &destroy; | |
| return str; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct pstring_t { | |
| /* | |
| instance variables | |
| */ | |
| char * chars; | |
| /* | |
| instance methods | |
| */ | |
| int (* length)(struct pstring_t *); | |
| char (* characterAtIndex)(struct pstring_t *, int); | |
| char * (* value)(struct pstring_t *); | |
| void (* setValue)(struct pstring_t *, char *); | |
| void (* destroy)(struct pstring_t *); | |
| } PString; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int length(PString * self) { | |
| return strlen(self->chars); | |
| } | |
| char characterAtIndex(PString * self, int i) { | |
| return self->chars[i]; | |
| } | |
| char * value(PString * self) { | |
| return self->chars; | |
| } | |
| void setValue(PString * self, char * chars) { | |
| free(self->chars); | |
| self->chars = (char *)malloc(sizeof(char) * (strlen(chars) + 1)); | |
| strcpy(self->chars, chars); | |
| } | |
| void destroy(PString * self) { | |
| free(self->chars); | |
| free(self); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PString * initializePString(); | |
| int length(PString * self); | |
| char characterAtIndex(PString * self, int i); | |
| char * value (PString * self); | |
| void setValue(PString * self, char * chars); | |
| void destroy(PString * self); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment