JCOOP入门指南[04]
OOC-Embeded_C语言模拟对象的应用

C语言模拟OO机制的再研究

pingf posted @ Fri, 27 Aug 2010 06:00:38 -1100 in C语言 with tags 创作 研发 , 3789 readers

因为过一段日子可能又要写些单片机上的代码,所以难免还是离不开C.

以前研究过GObject,也自己用宏写过一些模拟OO的东西,不过这些用在单片机上就不那么顺了.....

于是就想把OO的思想进一步简化,

最和新的东西无非就是类的声明(设计),构造与析构,

所以浓缩了下面的代码,并保证在Keil这类工具上0警告编译通过....

/**
 * OOC-Embeded [Jcoop elite version]
 * version : 0.2.0827elite
 * 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 * z); \
EXPORT int fin##Type(struct _##Type *z); \
EXPORT struct _##Type * new##Type(void); \
EXPORT int del##Type(struct _##Type **z); \
struct _##Type \
{
//
#define _END_CLASS };
//////////////////////////////////////
#define _CTOR(Type) \
EXPORT struct _##Type * new##Type(void) \
{ \
	struct _##Type *this; \
	int initRet=0; \
	_CLASS_ALLOC(this,Type); \
	if( NULL==this ) { \
        return NULL; \
    } \
	initRet=init##Type(this); \
	if(initRet<0) { \
        return NULL; \
    } \
	return this; \
} \
EXPORT int init##Type(struct _##Type * z) \
{  \
	struct _##Type * this=NULL;\
	this=(struct _##Type *)z; \
	if( NULL==this ) { \
        return -1; \
    }  
        
//
#define _END_CTOR \
	return  1; \
} 
//
#define _DTOR(Type) \
EXPORT int del##Type(struct _##Type **z) \
{ \
	struct _##Type **this = NULL; \
	int finRet=0; \
	this=(struct _##Type **)z;  \
	if(NULL==this) { \
        return -1; \
    } \
	finRet=fin##Type(*this); \
	if(finRet<0) {\
        return -1; \
    } \
	free(*this); \
	(*this)=NULL; \
	return 1; \
} \
EXPORT int fin##Type(struct _##Type *z) \
{ \
	struct _##Type * this=NULL; \
	this=(struct _##Type *)z; \
	if( NULL==this ) { \
        return -1; \
    } 
//  
#define _END_DTOR \
return 1; \
}
#else
#define _CLASS(Type) \
typedef struct _##Type Type; \
EXPORT int init##Type(struct _##Type * z); \
EXPORT int fin##Type(struct _##Type *z); \
struct _##Type \
{
//
#define _END_CLASS };
//////////////////////////////////////
#define _CTOR(Type) \
EXPORT int init##Type(struct _##Type * z) \
{  \
	struct _##Type * this=NULL;\
	this=(struct _##Type *)z; \
	if( NULL==this ) { \
        return -1; \
    }  
        
//
#define _END_CTOR \
	return  1; \
} 
//
#define _DTOR(Type) \
EXPORT int fin##Type(struct _##Type *z) \
{ \
	struct _##Type * this=NULL; \
	this=(struct _##Type *)z; \
	if( NULL==this ) { \
        return -1; \
    } 
//  
#define _END_DTOR \
return 1; \
}
#endif 
 
/////////////////////////////////////////////////////////////
#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
//
#endif

因为定义的东西少[也就100多行吧],所以使用起来就容易,省去了很多要记忆的东西,

下面是用例测试

#include <stdio.h>
#include "jc_oop.h"


CLASS(Test)
    int a;
    int b;
END_CLASS

CTOR(Test)
    printf("Test constructor");
    //this->a=1;
    //this->b=2;
END_CTOR
//
DTOR(Test)
    printf("Test destructor");
    this->a=this->b=0;
END_DTOR

int main (int argc, char *argv[])
{
    Test stk;
    Test * heap=NULL;
    heap=newTest();
    delTest(&heap);
    initTest(&stk);
    finTest(&stk);
    return(0);
}

别看这么简单,但是像继承和组合的关系还是可以表示清楚的

比如下面测试了组合关系

 

#include <stdio.h>
#include "jc_oop.h"


CLASS(Test)
    int a;
    int b;
END_CLASS

CTOR(Test)
    printf("Test constructor\n");
    //this->a=1;
    //this->b=2;
END_CTOR
//
DTOR(Test)
    printf("Test destructor\n");
    this->a=this->b=0;
END_DTOR

CLASS(Test2)
    Test *test;
END_CLASS

CTOR(Test2)
    printf("Test2 constructor\n");
    this->test=newTest();
END_CTOR
//
DTOR(Test2)
    printf("Test2 destructor,actually this is after Test\n");
    delTest(&(this->test));
END_DTOR

int main (int argc, char *argv[])
{
    Test stk;
    Test2 * heap=NULL;
    heap=newTest2();
    delTest2(&heap);
    return(0);
}

至于像方法之类的东西,自然还是用函数指针来模拟,

为了在嵌入式系统上使用方便,不再用宏来定义

Avatar_small
纵横天下 said:
Sat, 30 Oct 2010 05:53:46 -1100

牛人,不过好像没有成员函数

Avatar_small
pingf said:
Sat, 13 Nov 2010 13:55:08 -1100

成员函数用函数指针模拟,
另外关于这这样的宏我后来又做了一些其他的尝试,比如完成了一些常用的数据结构[比如哈希表神马的]
比如像C++那样也整一个虚表[当然还是用函数指针模拟,不过指向堆空间罢了],这样的话在使用时可能麻烦了些,
但是在类的继承以及类型转换时会有些额外的好处,
这段时间要复习考研,以后有时间在传到网上

Avatar_small
pingf said:
Tue, 16 Nov 2010 05:46:15 -1100

@纵横天下:
最近还是更新了下,舍弃了"虚表"的方法,还是因为用起来不方便
有兴趣的话看下下面的v0.6那个
http://code.google.com/p/ooc-embeded/downloads/list

xiaoao360 said:
Sun, 01 Jan 2012 04:23:21 -1100

http://code.google.com/p/ooc-embeded/downloads/list
这个网址我上不去,请问怎么能看到这里的代码?

pingf said:
Sun, 01 Jan 2012 15:35:49 -1100

http://code.google.com/p/ooc-lite/downloads/list

NCERT Economics Samp said:
Wed, 28 Sep 2022 19:50:33 -1100

Every Secondary candidates can download NCERT 10th Class Economics Sample Paper 2023 with learning and study material to know the new exam scheme or question pattern for all formats of SA1, SA2, FA1, FA2, FA3, FA4 and Assignments exams held under Term-1 & Term-2 of the Course. By downloading and practicing these NCERT STD-10 Economics model papers 2023 every student may get an analysis on the question paper style, NCERT Economics Sample Paper Class 10 important questions and the way of asking.Every Secondary candidates can download NCERT 10th Class Economics Sample Paper 2023 with learning and study material to know the new exam scheme or question pattern for all formats of SA1, SA2, FA1, FA2, FA3, FA4 and Assignments exams held under Term-1 & Term-2 of the Course. By downloading and practicing these NCERT STD-10 Economics model papers 2023 every student may get an analysis on the question paper style.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter