Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct {
- int (*open)(void *self, char *fspec);
- int (*close)(void *self);
- int (*read)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
- int (*write)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
- // And data goes here.
- } tCommClass;
- tCommClass commRs232;
- commRs232.open = &rs232Open;
- : :
- commRs232.write = &rs232Write;
- tCommClass commTcp;
- commTcp.open = &tcpOpen;
- : :
- commTcp.write = &tcpWrite;
- int stat = (commTcp.open)(commTcp, "bigiron.box.com:5000");
- #include <stdio.h>
- // The top-level class.
- typedef struct _tCommClass {
- int (*open)(struct _tCommClass *self, char *fspec);
- } tCommClass;
- // Function for the TCP 'class'.
- static int tcpOpen (tCommClass *tcp, char *fspec) {
- printf ("Opening TCP: %sn", fspec);
- return 0;
- }
- static int tcpInit (tCommClass *tcp) {
- tcp->open = &tcpOpen;
- return 0;
- }
- // Function for the HTTP 'class'.
- static int httpOpen (tCommClass *http, char *fspec) {
- printf ("Opening HTTP: %sn", fspec);
- return 0;
- }
- static int httpInit (tCommClass *http) {
- http->open = &httpOpen;
- return 0;
- }
- // Test program.
- int main (void) {
- int status;
- tCommClass commTcp, commHttp;
- // Same 'base' class but initialised to different sub-classes.
- tcpInit (&commTcp);
- httpInit (&commHttp);
- // Called in exactly the same manner.
- status = (commTcp.open)(&commTcp, "bigiron.box.com:5000");
- status = (commHttp.open)(&commHttp, "http://www.microsoft.com");
- return 0;
- }
- Opening TCP: bigiron.box.com:5000
- Opening HTTP: http://www.microsoft.com
- stack_push(thing *)
- stack::push(thing *)
- class stack {
- public:
- stack();
- void push(thing *);
- thing * pop();
- static int this_is_here_as_an_example_only;
- private:
- ...
- };
- struct stack {
- struct stack_type * my_type;
- // put the stuff that you put after private: here
- };
- struct stack_type {
- void (* construct)(struct stack * this); // this takes uninitialized memory
- struct stack * (* operator_new)(); // this allocates a new struct, passes it to construct, and then returns it
- void (*push)(struct stack * this, thing * t); // pushing t onto this stack
- thing * (*pop)(struct stack * this); // pops the top thing off the stack and returns it
- int this_is_here_as_an_example_only;
- }Stack = {
- .construct = stack_construct,
- .operator_new = stack_operator_new,
- .push = stack_push,
- .pop = stack_pop
- };
- // all of these functions are assumed to be defined somewhere else
- struct stack * st = Stack.operator_new(); // make a new stack
- if (!st) {
- // do something about it
- } else {
- // you can use the stack
- stack_push(st, thing0); // This is a non-virtual call
- Stack.push(st, thing1); // This is like casting *st to a Stack (which it already is) and doing the push
- st->my_type.push(st, thing2); // This is a virtual call
- }
- struct Animal_Vtable{
- typedef void (*Walk_Fun)(struct Animal *a_This);
- typedef struct Animal * (*Dtor_Fun)(struct Animal *a_This);
- Walk_Fun Walk;
- Dtor_Fun Dtor;
- };
- struct Animal{
- Animal_Vtable vtable;
- char *Name;
- };
- struct Dog{
- Animal_Vtable vtable;
- char *Name; // mirror member variables for easy access
- char *Type;
- };
- void Animal_Walk(struct Animal *a_This){
- printf("Animal (%s) walkingn", a_This->Name);
- }
- struct Animal* Animal_Dtor(struct Animal *a_This){
- printf("animal::dtorn");
- return a_This;
- }
- Animal *Animal_Alloc(){
- return (Animal*)malloc(sizeof(Animal));
- }
- Animal *Animal_New(Animal *a_Animal){
- a_Animal->vtable.Walk = Animal_Walk;
- a_Animal->vtable.Dtor = Animal_Dtor;
- a_Animal->Name = "Anonymous";
- return a_Animal;
- }
- void Animal_Free(Animal *a_This){
- a_This->vtable.Dtor(a_This);
- free(a_This);
- }
- void Dog_Walk(struct Dog *a_This){
- printf("Dog walking %s (%s)n", a_This->Type, a_This->Name);
- }
- Dog* Dog_Dtor(struct Dog *a_This){
- // explicit call to parent destructor
- Animal_Dtor((Animal*)a_This);
- printf("dog::dtorn");
- return a_This;
- }
- Dog *Dog_Alloc(){
- return (Dog*)malloc(sizeof(Dog));
- }
- Dog *Dog_New(Dog *a_Dog){
- // explict call to parent constructor
- Animal_New((Animal*)a_Dog);
- a_Dog->Type = "Dog type";
- a_Dog->vtable.Walk = (Animal_Vtable::Walk_Fun) Dog_Walk;
- a_Dog->vtable.Dtor = (Animal_Vtable::Dtor_Fun) Dog_Dtor;
- return a_Dog;
- }
- int main(int argc, char **argv){
- /*
- base class:
- Animal *a_Animal = Animal_New(Animal_Alloc());
- */
- Animal *a_Animal = (Animal*)Dog_New(Dog_Alloc());
- a_Animal->vtable.Walk(a_Animal);
- Animal_Free(a_Animal);
- }
- #ifndef FOO_H_
- #define FOO_H_
- ...
- typedef struct FOO_type FOO_type; /* That's all the rest of the program knows about FOO */
- /* Declaration of accessors, functions */
- FOO_type *FOO_new(void);
- void FOO_free(FOO_type *this);
- ...
- void FOO_dosomething(FOO_type *this, param ...):
- char *FOO_getName(FOO_type *this, etc);
- #endif
- #include <stdlib.h>
- ...
- #include "FOO.h"
- struct FOO_type {
- whatever...
- };
- FOO_type *FOO_new(void)
- {
- FOO_type *this = calloc(1, sizeof (FOO_type));
- ...
- FOO_dosomething(this, );
- return this;
- }
- #include<stdio.h>
- struct foobarbaz{
- int one;
- int two;
- int three;
- int (*exampleMethod)(int, int);
- };
- int addTwoNumbers(int a, int b){
- return a+b;
- }
- int main()
- {
- //define the function pointer
- int (*pointerToFunction)(int, int) = addTwoNumbers;
- //lets make sure we can call the pointer
- int test = (*pointerToFunction)(12,12);
- printf ("test: %u n", test);
- //now, define an instance of our struct
- //and add some default values
- struct foobarbaz fbb;
- fbb.one = 1;
- fbb.two = 2;
- fbb.three = 3;
- //now add a "method"
- fbb.exampleMethod = addTwoNumbers;
- //try calling the method
- int test2 = fbb.exampleMethod(13,36);
- printf ("test2: %u n", test2);
- printf("nDonen");
- return 0;
- }
- #include <stdio.h>
- struct Node {
- int somevar;
- };
- void print() {
- printf("Hello from Object-Oriented C method!");
- };
- struct Tree {
- struct Node * NIL;
- void (*FPprint)(void);
- struct Node *root;
- struct Node NIL_t;
- } TreeA = {&TreeA.NIL_t,print};
- int main()
- {
- struct Tree TreeB;
- TreeB = TreeA;
- TreeB.FPprint();
- return 0;
- }
- #include "OOStd.h"
- CLASS(Animal){
- char *name;
- STATIC(Animal);
- vFn talk;
- };
- static int Animal_load(Animal *THIS,void *name){
- THIS->name=name;
- return 0;
- }
- ASM(Animal,Animal_load,NULL,NULL,NULL)
- CLASS_EX(Cat,Animal){
- STATIC_EX(Cat,Animal);
- };
- static void Meow(Animal *THIS){
- printf("Meow!My name is %s!n",THIS->name);
- }
- static int Cat_loadSt(StAnimal *THIS,void *PARAM){
- THIS->talk=(void *)Meow;
- return 0;
- }
- ASM_EX(Cat,Animal,NULL,NULL,Cat_loadSt,NULL)
- CLASS_EX(Dog,Animal){
- STATIC_EX(Dog,Animal);
- };
- static void Woof(Animal *THIS){
- printf("Woof!My name is %s!n",THIS->name);
- }
- static int Dog_loadSt(StAnimal *THIS,void *PARAM){
- THIS->talk=(void *)Woof;
- return 0;
- }
- ASM_EX(Dog,Animal,NULL,NULL,Dog_loadSt,NULL)
- int main(){
- Animal *animals[4000];
- StAnimal *f;
- int i=0;
- for(i=0;i<4000;i++)
- {
- if(i%2==0) animals[i]=NEW(Dog,"Jack");
- else animals[i]=NEW(Cat,"Lily");
- };
- f=ST(animals[0]);
- for(i=0;i<4000;++i){
- f->talk(animals[i]);
- }
- for(i=0;i<4000;++i){
- DELETE0(animals[i]);
- }
- return 0;
- }
- typedef struct _peeker
- {
- log_t *log;
- symbols_t *sym;
- scanner_t scan; // inherited instance
- peek_t pk;
- int trace;
- void (*push) ( SELF *d, symbol_t *symbol );
- short (*peek) ( SELF *d, int level );
- short (*get) ( SELF *d );
- int (*get_line_number) ( SELF *d );
- } peeker_t, SlkToken;
- #define push(self,a) (*self).push(self, a)
- #define peek(self,a) (*self).peek(self, a)
- #define get(self) (*self).get(self)
- #define get_line_number(self) (*self).get_line_number(self)
- INSTANCE_METHOD
- int
- (get_line_number) ( peeker_t *d )
- {
- return d->scan.line_number;
- }
- PUBLIC
- void
- InitializePeeker ( peeker_t *peeker,
- int trace,
- symbols_t *symbols,
- log_t *log,
- list_t *list )
- {
- InitializeScanner ( &peeker->scan, trace, symbols, log, list );
- peeker->log = log;
- peeker->sym = symbols;
- peeker->pk.current = peeker->pk.buffer;
- peeker->pk.count = 0;
- peeker->trace = trace;
- peeker->get_line_number = get_line_number;
- peeker->push = push;
- peeker->get = get;
- peeker->peek = peek;
- }
- #include "triangle.h"
- #include "rectangle.h"
- #include "polygon.h"
- #include <stdio.h>
- int main()
- {
- Triangle tr1= CTriangle->new();
- Rectangle rc1= CRectangle->new();
- tr1->width= rc1->width= 3.2;
- tr1->height= rc1->height= 4.1;
- CPolygon->printArea((Polygon)tr1);
- printf("n");
- CPolygon->printArea((Polygon)rc1);
- }
- /*output:
- 6.56
- 13.12
- */
- /* this is pure C, no macros preprocessing. it has innheritance, polymorphism,
- data encapsulation (including private data). it not has equivalent protected
- qualifier, wich means private data it is private down the innheritance chain too.
- I hope you enjoy it. So my response, based on my work and on what
- i show you here, yes, it is possible. it requieres well structured code,
- work around it, think, try and try, at least in my case. Finally you got it.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement