Advertisement
Guest User

Untitled

a guest
May 7th, 2014
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. typedef struct {
  2. int (*open)(void *self, char *fspec);
  3. int (*close)(void *self);
  4. int (*read)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
  5. int (*write)(void *self, void *buff, size_t max_sz, size_t *p_act_sz);
  6. // And data goes here.
  7. } tCommClass;
  8.  
  9. tCommClass commRs232;
  10. commRs232.open = &rs232Open;
  11. : :
  12. commRs232.write = &rs232Write;
  13.  
  14. tCommClass commTcp;
  15. commTcp.open = &tcpOpen;
  16. : :
  17. commTcp.write = &tcpWrite;
  18.  
  19. int stat = (commTcp.open)(commTcp, "bigiron.box.com:5000");
  20.  
  21. #include <stdio.h>
  22.  
  23. // The top-level class.
  24.  
  25. typedef struct _tCommClass {
  26. int (*open)(struct _tCommClass *self, char *fspec);
  27. } tCommClass;
  28.  
  29. // Function for the TCP 'class'.
  30.  
  31. static int tcpOpen (tCommClass *tcp, char *fspec) {
  32. printf ("Opening TCP: %sn", fspec);
  33. return 0;
  34. }
  35. static int tcpInit (tCommClass *tcp) {
  36. tcp->open = &tcpOpen;
  37. return 0;
  38. }
  39.  
  40. // Function for the HTTP 'class'.
  41.  
  42. static int httpOpen (tCommClass *http, char *fspec) {
  43. printf ("Opening HTTP: %sn", fspec);
  44. return 0;
  45. }
  46. static int httpInit (tCommClass *http) {
  47. http->open = &httpOpen;
  48. return 0;
  49. }
  50.  
  51. // Test program.
  52.  
  53. int main (void) {
  54. int status;
  55. tCommClass commTcp, commHttp;
  56.  
  57. // Same 'base' class but initialised to different sub-classes.
  58.  
  59. tcpInit (&commTcp);
  60. httpInit (&commHttp);
  61.  
  62. // Called in exactly the same manner.
  63.  
  64. status = (commTcp.open)(&commTcp, "bigiron.box.com:5000");
  65. status = (commHttp.open)(&commHttp, "http://www.microsoft.com");
  66.  
  67. return 0;
  68. }
  69.  
  70. Opening TCP: bigiron.box.com:5000
  71. Opening HTTP: http://www.microsoft.com
  72.  
  73. stack_push(thing *)
  74.  
  75. stack::push(thing *)
  76.  
  77. class stack {
  78. public:
  79. stack();
  80. void push(thing *);
  81. thing * pop();
  82. static int this_is_here_as_an_example_only;
  83. private:
  84. ...
  85. };
  86.  
  87. struct stack {
  88. struct stack_type * my_type;
  89. // put the stuff that you put after private: here
  90. };
  91. struct stack_type {
  92. void (* construct)(struct stack * this); // this takes uninitialized memory
  93. struct stack * (* operator_new)(); // this allocates a new struct, passes it to construct, and then returns it
  94. void (*push)(struct stack * this, thing * t); // pushing t onto this stack
  95. thing * (*pop)(struct stack * this); // pops the top thing off the stack and returns it
  96. int this_is_here_as_an_example_only;
  97. }Stack = {
  98. .construct = stack_construct,
  99. .operator_new = stack_operator_new,
  100. .push = stack_push,
  101. .pop = stack_pop
  102. };
  103. // all of these functions are assumed to be defined somewhere else
  104.  
  105. struct stack * st = Stack.operator_new(); // make a new stack
  106. if (!st) {
  107. // do something about it
  108. } else {
  109. // you can use the stack
  110. stack_push(st, thing0); // This is a non-virtual call
  111. Stack.push(st, thing1); // This is like casting *st to a Stack (which it already is) and doing the push
  112. st->my_type.push(st, thing2); // This is a virtual call
  113. }
  114.  
  115. struct Animal_Vtable{
  116. typedef void (*Walk_Fun)(struct Animal *a_This);
  117. typedef struct Animal * (*Dtor_Fun)(struct Animal *a_This);
  118.  
  119. Walk_Fun Walk;
  120. Dtor_Fun Dtor;
  121. };
  122.  
  123. struct Animal{
  124. Animal_Vtable vtable;
  125.  
  126. char *Name;
  127. };
  128.  
  129. struct Dog{
  130. Animal_Vtable vtable;
  131.  
  132. char *Name; // mirror member variables for easy access
  133. char *Type;
  134. };
  135.  
  136. void Animal_Walk(struct Animal *a_This){
  137. printf("Animal (%s) walkingn", a_This->Name);
  138. }
  139.  
  140. struct Animal* Animal_Dtor(struct Animal *a_This){
  141. printf("animal::dtorn");
  142. return a_This;
  143. }
  144.  
  145. Animal *Animal_Alloc(){
  146. return (Animal*)malloc(sizeof(Animal));
  147. }
  148.  
  149. Animal *Animal_New(Animal *a_Animal){
  150. a_Animal->vtable.Walk = Animal_Walk;
  151. a_Animal->vtable.Dtor = Animal_Dtor;
  152. a_Animal->Name = "Anonymous";
  153. return a_Animal;
  154. }
  155.  
  156. void Animal_Free(Animal *a_This){
  157. a_This->vtable.Dtor(a_This);
  158.  
  159. free(a_This);
  160. }
  161.  
  162. void Dog_Walk(struct Dog *a_This){
  163. printf("Dog walking %s (%s)n", a_This->Type, a_This->Name);
  164. }
  165.  
  166. Dog* Dog_Dtor(struct Dog *a_This){
  167. // explicit call to parent destructor
  168. Animal_Dtor((Animal*)a_This);
  169.  
  170. printf("dog::dtorn");
  171.  
  172. return a_This;
  173. }
  174.  
  175. Dog *Dog_Alloc(){
  176. return (Dog*)malloc(sizeof(Dog));
  177. }
  178.  
  179. Dog *Dog_New(Dog *a_Dog){
  180. // explict call to parent constructor
  181. Animal_New((Animal*)a_Dog);
  182.  
  183. a_Dog->Type = "Dog type";
  184. a_Dog->vtable.Walk = (Animal_Vtable::Walk_Fun) Dog_Walk;
  185. a_Dog->vtable.Dtor = (Animal_Vtable::Dtor_Fun) Dog_Dtor;
  186.  
  187. return a_Dog;
  188. }
  189.  
  190. int main(int argc, char **argv){
  191. /*
  192. base class:
  193. Animal *a_Animal = Animal_New(Animal_Alloc());
  194. */
  195. Animal *a_Animal = (Animal*)Dog_New(Dog_Alloc());
  196.  
  197. a_Animal->vtable.Walk(a_Animal);
  198.  
  199. Animal_Free(a_Animal);
  200. }
  201.  
  202. #ifndef FOO_H_
  203. #define FOO_H_
  204.  
  205. ...
  206. typedef struct FOO_type FOO_type; /* That's all the rest of the program knows about FOO */
  207.  
  208. /* Declaration of accessors, functions */
  209. FOO_type *FOO_new(void);
  210. void FOO_free(FOO_type *this);
  211. ...
  212. void FOO_dosomething(FOO_type *this, param ...):
  213. char *FOO_getName(FOO_type *this, etc);
  214. #endif
  215.  
  216. #include <stdlib.h>
  217. ...
  218. #include "FOO.h"
  219.  
  220. struct FOO_type {
  221. whatever...
  222. };
  223.  
  224.  
  225. FOO_type *FOO_new(void)
  226. {
  227. FOO_type *this = calloc(1, sizeof (FOO_type));
  228.  
  229. ...
  230. FOO_dosomething(this, );
  231. return this;
  232. }
  233.  
  234. #include<stdio.h>
  235.  
  236. struct foobarbaz{
  237. int one;
  238. int two;
  239. int three;
  240. int (*exampleMethod)(int, int);
  241. };
  242.  
  243. int addTwoNumbers(int a, int b){
  244. return a+b;
  245. }
  246.  
  247. int main()
  248. {
  249. //define the function pointer
  250. int (*pointerToFunction)(int, int) = addTwoNumbers;
  251.  
  252. //lets make sure we can call the pointer
  253. int test = (*pointerToFunction)(12,12);
  254. printf ("test: %u n", test);
  255.  
  256. //now, define an instance of our struct
  257. //and add some default values
  258. struct foobarbaz fbb;
  259. fbb.one = 1;
  260. fbb.two = 2;
  261. fbb.three = 3;
  262.  
  263. //now add a "method"
  264. fbb.exampleMethod = addTwoNumbers;
  265.  
  266. //try calling the method
  267. int test2 = fbb.exampleMethod(13,36);
  268. printf ("test2: %u n", test2);
  269.  
  270. printf("nDonen");
  271. return 0;
  272. }
  273.  
  274. #include <stdio.h>
  275.  
  276. struct Node {
  277. int somevar;
  278. };
  279.  
  280. void print() {
  281. printf("Hello from Object-Oriented C method!");
  282. };
  283.  
  284. struct Tree {
  285. struct Node * NIL;
  286. void (*FPprint)(void);
  287. struct Node *root;
  288. struct Node NIL_t;
  289. } TreeA = {&TreeA.NIL_t,print};
  290.  
  291.  
  292.  
  293.  
  294. int main()
  295. {
  296.  
  297. struct Tree TreeB;
  298. TreeB = TreeA;
  299. TreeB.FPprint();
  300. return 0;
  301. }
  302.  
  303. #include "OOStd.h"
  304.  
  305. CLASS(Animal){
  306. char *name;
  307. STATIC(Animal);
  308. vFn talk;
  309. };
  310. static int Animal_load(Animal *THIS,void *name){
  311. THIS->name=name;
  312. return 0;
  313. }
  314. ASM(Animal,Animal_load,NULL,NULL,NULL)
  315.  
  316. CLASS_EX(Cat,Animal){
  317. STATIC_EX(Cat,Animal);
  318. };
  319. static void Meow(Animal *THIS){
  320. printf("Meow!My name is %s!n",THIS->name);
  321. }
  322.  
  323. static int Cat_loadSt(StAnimal *THIS,void *PARAM){
  324. THIS->talk=(void *)Meow;
  325. return 0;
  326. }
  327. ASM_EX(Cat,Animal,NULL,NULL,Cat_loadSt,NULL)
  328.  
  329.  
  330. CLASS_EX(Dog,Animal){
  331. STATIC_EX(Dog,Animal);
  332. };
  333. static void Woof(Animal *THIS){
  334. printf("Woof!My name is %s!n",THIS->name);
  335. }
  336. static int Dog_loadSt(StAnimal *THIS,void *PARAM){
  337. THIS->talk=(void *)Woof;
  338. return 0;
  339. }
  340. ASM_EX(Dog,Animal,NULL,NULL,Dog_loadSt,NULL)
  341.  
  342. int main(){
  343. Animal *animals[4000];
  344. StAnimal *f;
  345. int i=0;
  346. for(i=0;i<4000;i++)
  347. {
  348. if(i%2==0) animals[i]=NEW(Dog,"Jack");
  349. else animals[i]=NEW(Cat,"Lily");
  350. };
  351. f=ST(animals[0]);
  352. for(i=0;i<4000;++i){
  353. f->talk(animals[i]);
  354. }
  355. for(i=0;i<4000;++i){
  356. DELETE0(animals[i]);
  357. }
  358. return 0;
  359. }
  360.  
  361. typedef struct _peeker
  362. {
  363. log_t *log;
  364. symbols_t *sym;
  365. scanner_t scan; // inherited instance
  366. peek_t pk;
  367. int trace;
  368.  
  369. void (*push) ( SELF *d, symbol_t *symbol );
  370. short (*peek) ( SELF *d, int level );
  371. short (*get) ( SELF *d );
  372. int (*get_line_number) ( SELF *d );
  373.  
  374. } peeker_t, SlkToken;
  375.  
  376. #define push(self,a) (*self).push(self, a)
  377. #define peek(self,a) (*self).peek(self, a)
  378. #define get(self) (*self).get(self)
  379. #define get_line_number(self) (*self).get_line_number(self)
  380.  
  381. INSTANCE_METHOD
  382. int
  383. (get_line_number) ( peeker_t *d )
  384. {
  385. return d->scan.line_number;
  386. }
  387.  
  388. PUBLIC
  389. void
  390. InitializePeeker ( peeker_t *peeker,
  391. int trace,
  392. symbols_t *symbols,
  393. log_t *log,
  394. list_t *list )
  395. {
  396. InitializeScanner ( &peeker->scan, trace, symbols, log, list );
  397. peeker->log = log;
  398. peeker->sym = symbols;
  399. peeker->pk.current = peeker->pk.buffer;
  400. peeker->pk.count = 0;
  401. peeker->trace = trace;
  402.  
  403. peeker->get_line_number = get_line_number;
  404. peeker->push = push;
  405. peeker->get = get;
  406. peeker->peek = peek;
  407. }
  408.  
  409. #include "triangle.h"
  410. #include "rectangle.h"
  411. #include "polygon.h"
  412.  
  413. #include <stdio.h>
  414.  
  415. int main()
  416. {
  417. Triangle tr1= CTriangle->new();
  418. Rectangle rc1= CRectangle->new();
  419.  
  420. tr1->width= rc1->width= 3.2;
  421. tr1->height= rc1->height= 4.1;
  422.  
  423. CPolygon->printArea((Polygon)tr1);
  424.  
  425. printf("n");
  426.  
  427. CPolygon->printArea((Polygon)rc1);
  428. }
  429.  
  430. /*output:
  431. 6.56
  432. 13.12
  433. */
  434.  
  435. /* this is pure C, no macros preprocessing. it has innheritance, polymorphism,
  436. data encapsulation (including private data). it not has equivalent protected
  437. qualifier, wich means private data it is private down the innheritance chain too.
  438. I hope you enjoy it. So my response, based on my work and on what
  439. i show you here, yes, it is possible. it requieres well structured code,
  440. work around it, think, try and try, at least in my case. Finally you got it.
  441. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement