oop 지원되는 언어 쓰면 편하긴 한데... c 는 자작하는 손맛이 있음. 이렇게 위로.

다른 언어 쓰고 싶은데 호환성 생각하면 c 를 쓸 수 밖에 없음.

그래서 c 를 하두 많이 쓰다보니 급기야는 oop 흉내를 ㅋㅋ

tian_object_new (sizeof (StructType));

이런 식으로 하면 될 거 같기도 함.


#ifndef __TIAN_OBJECT_H__

#define __TIAN_OBJECT_H__


#ifdef  __cplusplus

extern "C" {

#endif


typedef struct _TianObject TianObject;

struct _TianObject

{

  int ref_count;

  TianObject * (* ref)      (TianObject *object);

  void         (* unref)    (TianObject *object);

  void         (* finalize) (TianObject *object);

};


void tian_object_ref   (TianObject *object);

void tian_object_unref (TianObject *object);


#ifdef  __cplusplus

}

#endif


#endif /* __TIAN_OBJECT_H__ */




#include

#include "tian-object.h"

#include


void

tian_object_ref (TianObject *object)

{

  object->ref_count++;

}


void

tian_object_unref (TianObject *object)

{

  object->ref_count--;


  if (object->ref_count == 0)

  {

    if (object->finalize)

      object->finalize (object);


    free (object);

  }

}