Created
April 1, 2011 23:48
-
-
Save skahack/899068 to your computer and use it in GitHub Desktop.
CでOO
This file contains 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
#include <stdio.h> | |
#include "Object.c" | |
#include "String.c" | |
int main(void) { | |
int len; | |
struct String *s = new(String, "hello"); | |
len = s->length(s); | |
printf("length : %d\n", len); | |
s->set(s, "hello, world"); | |
len = s->length(s); | |
printf("length : %d\n", len); | |
s->set(s, "test")->puts(s) | |
->set(s, "Hi!")->puts(s); | |
s->destroy(s); | |
return 0; | |
} |
This file contains 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
#pragma once | |
#include <stdlib.h> | |
#include <stdarg.h> | |
typedef struct _Class * Class; | |
struct _Class { | |
size_t size; | |
void * (*new)(void *self, va_list *args); | |
}; | |
// | |
// Class Object | |
// | |
typedef struct Object * _Object; | |
struct Object { | |
void *self; | |
Class super; | |
void (*class)(_Object self); | |
}; | |
void object_class(_Object self) { | |
printf("Class : Object\n"); | |
} | |
void* object_new(void *_self, va_list *args) { | |
_Object self = _self; | |
self->class = &object_class; | |
return self; | |
} | |
static const struct _Class __Object = { | |
sizeof(struct Object), | |
&object_new | |
}; | |
const void *Object = &__Object; | |
// | |
// Class Util | |
// | |
void* new(const void *_class, ...) { | |
const struct _Class *obj = _class; | |
va_list ap; | |
va_start(ap, _class); | |
void *p = calloc(1, obj->size); | |
*(const struct _Class **)p = obj; | |
p = (obj->new)(p, &ap); | |
va_end(ap); | |
return p; | |
} | |
This file contains 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
#pragma once | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#include "Object.c" | |
typedef struct String * _String; | |
struct String { | |
void *self; | |
_Object super; | |
char *_str; | |
int _length; | |
void (*initialize)(_String self, va_list *args); | |
void (*destroy)(_String self); | |
_String (*set)(_String self, char *str); | |
int (*length)(_String self); | |
_String (*puts)(_String self); | |
}; | |
void string_initialize(_String self, va_list *args) { | |
char *text = va_arg(*args, char *); | |
(self->set)(self, text); | |
} | |
void string_destroy(_String self) { | |
free(self->_str); | |
self->_str = 0; | |
free(self); | |
} | |
_String string_set(_String self, char *str) { | |
if (self == NULL) return NULL; | |
free(self->_str); | |
self->_str = malloc(strlen(str) + 1); | |
strcpy(self->_str, str); | |
self->_length = strlen(str); | |
return self; | |
} | |
_String string_puts(_String self) { | |
if (self == NULL) return NULL; | |
printf("%s\n", self->_str); | |
return self; | |
} | |
int string_length(_String self) { | |
if (self == NULL) return 0; | |
return self->_length; | |
} | |
void* string_new(void *_self, va_list *args) { | |
_String self = _self; | |
self->initialize = &string_initialize; | |
self->destroy = &string_destroy; | |
self->set = &string_set; | |
self->length = &string_length; | |
self->puts= &string_puts; | |
(self->initialize)(self, args); | |
return self; | |
} | |
static const struct _Class __String = { | |
sizeof(struct String), | |
&string_new | |
}; | |
const void *String = &__String; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment