Created
March 23, 2011 08:43
-
-
Save nomadalex/882802 to your computer and use it in GitHub Desktop.
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 <stdlib.h> | |
#include "c_style_OO_1.h" | |
struct _A { | |
Base *base; | |
int a; | |
}; | |
void A_create(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = malloc(sizeof(struct _A)); | |
a->base = base; | |
base->this = (void*) a; | |
} | |
void A_destroy(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(base->this); | |
free(base); | |
} | |
int A_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
return a->a; | |
} | |
void A_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
a->a = v; | |
} | |
struct _B { | |
Base *base; | |
int b; | |
}; | |
void B_create(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = malloc(sizeof(struct _B)); | |
b->base = base; | |
base->this = (void*) b; | |
} | |
void B_destroy(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(base->this); | |
free(base); | |
} | |
int B_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
return b->b; | |
} | |
void B_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
b->b = v; | |
} | |
typedef void (*XXX_create)(Base *base); | |
const XXX_create create_funcs[] = | |
{ | |
A_create, | |
B_create, | |
}; | |
typedef void (*XXX_destroy)(Base *base); | |
const XXX_destroy destroy_funcs[] = | |
{ | |
A_destroy, | |
B_destroy, | |
}; | |
typedef int (*XXX_get)(void *this); | |
const XXX_get get_funcs[] = | |
{ | |
A_get, | |
B_get, | |
}; | |
typedef void (*XXX_set)(void *this, int v); | |
const XXX_set set_funcs[] = | |
{ | |
A_set, | |
B_set, | |
}; | |
Base* create(int type) | |
{ | |
printf("%s\n", __FUNCTION__); | |
Base *b = malloc(sizeof(Base)); | |
b->type = type; | |
create_funcs[type](b); | |
return b; | |
} | |
void destroy(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
destroy_funcs[base->type](base); | |
} | |
int get(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
return get_funcs[base->type](base->this); | |
} | |
void set(Base *base, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
return set_funcs[base->type](base->this, v); | |
} |
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
#ifndef _BASE_H_ | |
#define _BASE_H_ | |
enum { | |
TYPE_A =0, | |
TYPE_B, | |
}; | |
typedef struct _base { | |
int type; | |
void* this; | |
} Base; | |
Base* create(int type); | |
int get(Base *base); | |
void set(Base *base, int v); | |
void destroy(Base *base); | |
#endif |
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 "c_style_OO_2.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct _A { | |
int a; | |
}; | |
void* A_create() | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = malloc(sizeof(struct _A)); | |
return (void*) a; | |
} | |
void A_destroy(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(this); | |
} | |
int A_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
return a->a; | |
} | |
void A_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
a->a = v; | |
} | |
struct _B { | |
int b; | |
}; | |
void* B_create() | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = malloc(sizeof(struct _B)); | |
return (void*) b; | |
} | |
void B_destroy(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(this); | |
} | |
int B_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
return b->b; | |
} | |
void B_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
b->b = v; | |
} | |
const XXX_create create_funcs[] = | |
{ | |
A_create, | |
B_create, | |
}; | |
const XXX_destroy destroy_funcs[] = | |
{ | |
A_destroy, | |
B_destroy, | |
}; | |
const XXX_get get_funcs[] = | |
{ | |
A_get, | |
B_get, | |
}; | |
const XXX_set set_funcs[] = | |
{ | |
A_set, | |
B_set, | |
}; |
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
#ifndef _BASE_H_ | |
#define _BASE_H_ | |
enum { | |
TYPE_A =0, | |
TYPE_B, | |
}; | |
typedef void* (*XXX_create)(); | |
extern const XXX_create create_funcs[]; | |
typedef void (*XXX_destroy)(void *this); | |
extern const XXX_destroy destroy_funcs[]; | |
typedef int (*XXX_get)(void *this); | |
extern const XXX_get get_funcs[]; | |
typedef void (*XXX_set)(void *this, int v); | |
extern const XXX_set set_funcs[]; | |
#endif |
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 "c_style_OO_3.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct _A { | |
Base *base; | |
int a; | |
}; | |
void A_create(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = malloc(sizeof(struct _A)); | |
a->base = base; | |
base->this = (void*) a; | |
} | |
void A_destroy(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(base->this); | |
free(base); | |
} | |
int A_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
return a->a; | |
} | |
void A_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _A *a = (struct _A*) this; | |
a->a = v; | |
} | |
void A_register(struct _funcs *funcs, int type) | |
{ | |
funcs->create_funcs[type] = A_create; | |
funcs->destroy_funcs[type] = A_destroy; | |
funcs->set_funcs[type] = A_set; | |
funcs->get_funcs[type] = A_get; | |
} | |
struct _B { | |
Base *base; | |
int b; | |
}; | |
void B_create(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = malloc(sizeof(struct _B)); | |
b->base = base; | |
base->this = (void*) b; | |
} | |
void B_destroy(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
free(base->this); | |
free(base); | |
} | |
int B_get(void* this) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
return b->b; | |
} | |
void B_set(void* this, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
struct _B *b = (struct _B*) this; | |
b->b = v; | |
} | |
void B_register(struct _funcs *funcs, int type) | |
{ | |
funcs->create_funcs[type] = B_create; | |
funcs->destroy_funcs[type] = B_destroy; | |
funcs->set_funcs[type] = B_set; | |
funcs->get_funcs[type] = B_get; | |
} | |
XXX_create *create_funcs; | |
XXX_destroy *destroy_funcs; | |
XXX_get *get_funcs; | |
XXX_set *set_funcs; | |
void init_for_register() | |
{ | |
static unsigned char uninited = 1; | |
if(uninited) | |
{ | |
struct _funcs funcs; | |
uninited = 0; | |
create_funcs = malloc(TYPE_ALL * sizeof(XXX_create)); | |
destroy_funcs = malloc(TYPE_ALL * sizeof(XXX_destroy)); | |
set_funcs = malloc(TYPE_ALL * sizeof(XXX_set)); | |
get_funcs = malloc(TYPE_ALL * sizeof(XXX_get)); | |
funcs.create_funcs = create_funcs; | |
funcs.destroy_funcs = destroy_funcs; | |
funcs.set_funcs = set_funcs; | |
funcs.get_funcs = get_funcs; | |
A_register(&funcs, TYPE_A); | |
B_register(&funcs, TYPE_B); | |
} | |
} | |
Base* create(int type) | |
{ | |
printf("%s\n", __FUNCTION__); | |
Base *b = malloc(sizeof(Base)); | |
b->type = type; | |
create_funcs[type](b); | |
return b; | |
} | |
void destroy(Base *base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
destroy_funcs[base->type](base); | |
} | |
int get(Base* base) | |
{ | |
printf("%s\n", __FUNCTION__); | |
return get_funcs[base->type](base->this); | |
} | |
void set(Base *base, int v) | |
{ | |
printf("%s\n", __FUNCTION__); | |
return set_funcs[base->type](base->this, v); | |
} |
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
#ifndef _BASE_H_ | |
#define _BASE_H_ | |
enum { | |
TYPE_A =0, | |
TYPE_B, | |
TYPE_ALL, | |
}; | |
typedef struct _base { | |
int type; | |
void* this; | |
} Base; | |
void init_for_register(); | |
Base* create(int type); | |
int get(Base *base); | |
void set(Base *base, int v); | |
void destroy(Base *base); | |
// for child class | |
typedef void (*XXX_create)(Base *base); | |
typedef void (*XXX_destroy)(Base *base); | |
typedef int (*XXX_get)(void *this); | |
typedef void (*XXX_set)(void *this, int v); | |
struct _funcs { | |
XXX_create *create_funcs; | |
XXX_destroy *destroy_funcs; | |
XXX_set *set_funcs; | |
XXX_get *get_funcs; | |
}; | |
#endif |
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 "c_style_OO_1.h" | |
#include <stdio.h> | |
//// main | |
int main() | |
{ | |
Base* a = create(TYPE_A); | |
set(a, 10); | |
printf("%d\n", get(a)); | |
destroy(a); | |
a = create(TYPE_B); | |
set(a, 15); | |
printf("%d\n", get(a)); | |
destroy(a); | |
} |
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 "c_style_OO_2.h" | |
//// main | |
int main() | |
{ | |
int type = TYPE_A; | |
void* a = create_funcs[type](); | |
set_funcs[type](a, 10); | |
printf("%d\n", get_funcs[type](a)); | |
destroy_funcs[type](a); | |
type = TYPE_B; | |
a = create_funcs[type](); | |
set_funcs[type](a, 15); | |
printf("%d\n", get_funcs[type](a)); | |
destroy_funcs[type](a); | |
} |
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 "c_style_OO_3.h" | |
//// main | |
int main() | |
{ | |
init_for_register(); | |
Base* a = create(TYPE_A); | |
set(a, 10); | |
printf("%d\n", get(a)); | |
destroy(a); | |
a = create(TYPE_B); | |
set(a, 15); | |
printf("%d\n", get(a)); | |
destroy(a); | |
} |
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
### Makefile --- | |
## Author: Kun Wang <ifreedom.cn@gmail.com> | |
## Version: $Id: Makefile,v 0.0 2011/03/23 10:57:42 ifreedom Exp $ | |
## Keywords: | |
## X-URL: | |
all: src c1 c2 c3 | |
src: | |
gcc -c *.c | |
c1: | |
gcc -o c1 c_style_OO_1.o main1.o | |
c2: | |
gcc -o c2 c_style_OO_2.o main2.o | |
c3: | |
gcc -o c3 c_style_OO_3.o main3.o | |
clean: | |
rm -f *.o c? | |
### Makefile ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment