Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
33
Task/Abstract-type/C/abstract-type-1.c
Normal file
33
Task/Abstract-type/C/abstract-type-1.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef INTERFACE_ABS
|
||||
#define INTERFACE_ABS
|
||||
|
||||
typedef struct sAbstractCls *AbsCls;
|
||||
|
||||
typedef struct sAbstractMethods {
|
||||
int (*method1)(AbsCls c, int a);
|
||||
const char *(*method2)(AbsCls c, int b);
|
||||
void (*method3)(AbsCls c, double d);
|
||||
} *AbstractMethods, sAbsMethods;
|
||||
|
||||
struct sAbstractCls {
|
||||
AbstractMethods klass;
|
||||
void *instData;
|
||||
};
|
||||
|
||||
#define ABSTRACT_METHODS( cName, m1, m2, m3 ) \
|
||||
static sAbsMethods cName ## _Iface = { &m1, &m2, &m3 }; \
|
||||
AbsCls cName ## _Instance( void *clInst) { \
|
||||
AbsCls ac = malloc(sizeof(struct sAbstractCls)); \
|
||||
if (ac) { \
|
||||
ac->klass = &cName ## _Iface; \
|
||||
ac->instData = clInst; \
|
||||
}\
|
||||
return ac; }
|
||||
|
||||
#define Abs_Method1( c, a) (c)->klass->method1(c, a)
|
||||
#define Abs_Method2( c, b) (c)->klass->method2(c, b)
|
||||
#define Abs_Method3( c, d) (c)->klass->method3(c, d)
|
||||
#define Abs_Free(c) \
|
||||
do { if (c) { free((c)->instData); free(c); } } while(0);
|
||||
|
||||
#endif
|
||||
10
Task/Abstract-type/C/abstract-type-2.c
Normal file
10
Task/Abstract-type/C/abstract-type-2.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef SILLY_H
|
||||
#define SILLY_H
|
||||
#include "intefaceAbs.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct sillyStruct *Silly;
|
||||
extern Silly NewSilly( double, const char *);
|
||||
extern AbsCls Silly_Instance(void *);
|
||||
|
||||
#endif
|
||||
41
Task/Abstract-type/C/abstract-type-3.c
Normal file
41
Task/Abstract-type/C/abstract-type-3.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include "silly.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct sillyStruct {
|
||||
double v1;
|
||||
char str[32];
|
||||
};
|
||||
|
||||
Silly NewSilly(double vInit, const char *strInit)
|
||||
{
|
||||
Silly sily = malloc(sizeof( struct sillyStruct ));
|
||||
sily->v1 = vInit;
|
||||
sily->str[0] = '\0';
|
||||
strncat(sily->str, strInit, 31);
|
||||
return sily;
|
||||
}
|
||||
|
||||
static
|
||||
int MyMethod1( AbsCls c, int a)
|
||||
{
|
||||
Silly s = (Silly)(c->instData);
|
||||
return a+strlen(s->str);
|
||||
}
|
||||
|
||||
static
|
||||
const char *MyMethod2(AbsCls c, int b)
|
||||
{
|
||||
Silly s = (Silly)(c->instData);
|
||||
sprintf(s->str, "%d", b);
|
||||
return s->str;
|
||||
}
|
||||
|
||||
static
|
||||
void MyMethod3(AbsCls c, double d)
|
||||
{
|
||||
Silly s = (Silly)(c->instData);
|
||||
printf("InMyMethod3, %f\n",s->v1 * d);
|
||||
}
|
||||
|
||||
ABSTRACT_METHODS( Silly, MyMethod1, MyMethod2, MyMethod3)
|
||||
13
Task/Abstract-type/C/abstract-type-4.c
Normal file
13
Task/Abstract-type/C/abstract-type-4.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
#include "silly.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
AbsCls abster = Silly_Instance(NewSilly( 10.1, "Green Tomato"));
|
||||
|
||||
printf("AbsMethod1: %d\n", Abs_Method1(abster, 5));
|
||||
printf("AbsMethod2: %s\n", Abs_Method2(abster, 4));
|
||||
Abs_Method3(abster, 21.55);
|
||||
Abs_Free(abster);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue