OOC-Embeded_C语言模拟对象的应用
其实还是在折腾如何用C语言模拟对象的机制,
现在这个版本个人该觉还是不错的,可以用GCC以及KEIL,IAR等附带的编译器来编译
另外还在此基础上实现了常用的数据结构,包括下面这些,
链表[包括单链表,双链表,循环链表],堆栈,队列,集合,
哈希表[包括链式和开放地址式],二差树[普通二叉树,平衡二叉树],
堆/优先队列,图......
项目地址:
http://code.google.com/p/ooc-embeded/
提供win下编译好的DLL,源代码可通过SVN获取
核心代码如下,
/**
* OOC-Embeded
* version : see the log file
* status : beta
* author : Jesse Meng
* Blog : http://www.pingf.me
* E-mail : pingf0@gmail.com
*/
#ifndef __JC_OOP__
#define __JC_OOP__
#include <stdio.h>
#include <stdlib.h>
//
#ifdef BUILD_DLL
/* DLL export */
#define EXPORT __declspec(dllexport)
#elif defined BUILD_WITH_DLL
/* EXE import */
#define EXPORT __declspec(dllimport)
#else
#define EXPORT
#endif
//
#define USE_HEAP_FOR_CLASS
//
#ifdef USE_CALLOC_FOR_CLASS
#define _CLASS_ALLOC(this,Type) \
this=(struct _##Type *)malloc(sizeof(struct _##Type));
#else
#define _CLASS_ALLOC(this,Type) \
this=(struct _##Type *)calloc(1,sizeof(struct _##Type));
#endif
/////////////////////////////////////////////////////////////
#ifdef USE_HEAP_FOR_CLASS
#define _CLASS(Type) \
typedef struct _##Type Type; \
EXPORT int init##Type(struct _##Type * this,void *param); \
EXPORT int fin##Type(struct _##Type *this,void *param); \
EXPORT struct _##Type * new##Type(void *param); \
EXPORT int del##Type(struct _##Type **this,void *param); \
struct _##Type \
{
//////////////////////////////////////
#define _CTOR(Type) \
EXPORT struct _##Type * new##Type(void *param) \
{ \
struct _##Type *this; \
int initRet=0; \
_CLASS_ALLOC(this,Type); \
if( NULL==this ) { \
return NULL; \
} \
initRet=init##Type(this,param); \
if(-1==initRet) { \
free(this); \
return NULL; \
} \
return this; \
} \
EXPORT int init##Type(struct _##Type * this,void *param) \
{
//
#define _DTOR(Type) \
EXPORT int del##Type(struct _##Type **this,void *param) \
{ \
int finRet=0; \
if(NULL==(this)) { \
return -1; \
} \
finRet=fin##Type(*(this),param); \
if(-1==finRet) {\
return -1; \
} \
free(*(this)); \
(*(this))=NULL; \
return 0; \
} \
EXPORT int fin##Type(struct _##Type *this,void *param) \
{
//
#else
#define _CLASS(Type) \
typedef struct _##Type Type; \
EXPORT int init##Type(struct _##Type * this,void *param); \
EXPORT int fin##Type(struct _##Type *this,void *param); \
struct _##Type \
{
//////////////////////////////////////
#define _CTOR(Type) \
EXPORT int init##Type(struct _##Type * this,void *param) \
{
//
#define _DTOR(Type) \
EXPORT int fin##Type(struct _##Type *this,void *param) \
{
//
#endif
/////////////////////
#define _END_CLASS };
//
#define _END_CTOR \
return 0; \
}
//
#define _END_DTOR \
return 0; \
}
//
#define _HAVES(Type,name) \
static struct _##Type##S s; \
//
#define _AUTOS(Type,name,sparam) \
do{ \
init##Type##S(&s,sparam); \
this->name=&s; \
}while(0);
//
#define _HAVEP(Type) \
static struct _##Type##P *p=NULL;
//
#define _CASTP(Type) \
do{ \
p=(struct _##Type##P *)param; \
}while(0);
/////////////////////////////////////////////////////////////
#define CLASS(Type) _CLASS(Type)
#define END_CLASS _END_CLASS
//
#define CTOR(Type) _CTOR(Type)
#define END_CTOR _END_CTOR
//
#define DTOR(Type) _DTOR(Type)
#define END_DTOR _END_DTOR
//
#define HAVES(Type,name) _HAVES(Type,name)
//
#define AUTOS(Type,name,sparam) _AUTOS(Type,name,sparam)
//
#define HAVEP(Type) _HAVEP(Type)
//
#define CASTP(Type) _CASTP(Type)
//
#endif
Comments (0)