惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
V
Visual Studio Blog
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
I
Intezer
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
腾讯CDC
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
Project Zero
Project Zero
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
量子位
M
MIT News - Artificial intelligence
美团技术团队
C
Cisco Blogs
S
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
Google Developers Blog
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
The Hacker News
The Hacker News
H
Help Net Security
S
Secure Thoughts
Scott Helme
Scott Helme
SecWiki News
SecWiki News
T
Troy Hunt's Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
O
OpenAI News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 司徒正美
T
Tenable Blog

博客园 - 稻草人的麦田

[导入]显卡对于mipmap选择的研究 [导入]光照优缺点 [导入]蜗牛的笔试题目 [导入]一个开发休闲游戏的程序组人与分配方案 [导入]运动模糊的制作 [导入]格里高里的数学题 [导入]水彩化 [导入]伪 HDR/Blow [导入]描边,只是换了个过滤器而已 [导入]锐化模糊 [导入]卷积积分(转) [导入]再贴个圆形马赛克 [导入]马赛克 [导入]hlsl的浮雕效果 [导入]游戏出了一个demo [导入]图形文件的格式(转载) [导入] 一些色彩方面的知识 [导入]流浪歌手 [导入][转载]游戏引擎列表
[导入]仔细研究了lithtech中的根据类名创建对象
稻草人的麦田 · 2008-01-02 · via 博客园 - 稻草人的麦田

上班闲下来后,仔细研究了lithtech中的根据类名创建对象的方法,于是自己也写了一个出来。只是我写的是一个通用缩水版,没有和游戏具体结合。如果用于具体游戏,请根据需要添加内容,发出来:

/********************************************************************
 created: 2008/01/02
 created: 2:1:2008   16:41
 filename:  d:\My Documents\Visual Studio Projects\test\test.cpp
 file path: d:\My Documents\Visual Studio Projects\test
 file base: test
 file ext: cpp
 author:  宋晓宇 
 E-mail:  songxiaoyu8@163.com
 
 purpose: 用于类自动管理,包含此文件后,只需要调用
    Declare(A,NULL)
    Declare(B,CC)   就可以对类进行根据字符串自动创建
    example:
    classMgr cm;
    A* ap0 = (A*)cm.GetClassByName("A");
    则此时ap0对象就是所需,并分配给了相应的空间。
*********************************************************************/

#include "stdafx.h"
using namespace std;

class ClassDef;
class __ClassDefiner;

typedef void (*ConstructObjectFn)(void *pObject);
typedef void (*DestructObjectFn)(void *pObject);

//自己重载new运算符
inline void * __cdecl operator new(unsigned int size,void* ptr,int le,char ch)
{
 return ptr;
};

#define DEFINE_HANDLE_TYPE(name) \
 typedef struct TT{int __nothing;} name##_t, *name;

//类的构造函数和析构函数
#define DO_DEFAULT_FUNCTIONS(_ClassName) \
 void Default##_ClassName##Constructor(void *ptr)\
{\
 new(ptr,(int)0,(char)0) _ClassName;\
};\
 void Default##_ClassName##Destructor(void *ptr)\
{\
 _ClassName *thePtr = (_ClassName*)ptr;\
 thePtr->~_ClassName();\
};\

#define DO_AUTO_CLASSLIST(name) \
 static __ClassDefiner __##name##_definer(&_##name##_Class__);

//
#define CLASS_SYMBOL(name, parentSymbol, construct_fn, destruct_fn) \
 ClassDef _##name##_Class__ = { \
 #name, #parentSymbol, \
 construct_fn, destruct_fn, \
 sizeof(name) ,\
 }; \
 DO_AUTO_CLASSLIST(name) \
 __int32 __impl_instance_##name##__;                 


class ClassDef
{
public:

 char *m_ClassName;
 char *m_ParentClass;

 //构造函数和析构函数
 ConstructObjectFn m_ConstructFn;
 DestructObjectFn m_DestructFn;

 // How big an object of this class is (set automatically).
 long m_ClassObjectSize;
};

__ClassDefiner *__g_ClassDefinerHead = NULL;

class __ClassDefiner
{
public:

 __ClassDefiner(ClassDef *pDef)
 {
  m_pClass = pDef;
  m_pNext = __g_ClassDefinerHead;
  __g_ClassDefinerHead = this;
 }

 ClassDef        *m_pClass;
 __ClassDefiner  *m_pNext;

};
#define Declare(CLASSC,CLASSP)  \
 DO_DEFAULT_FUNCTIONS(CLASSC)\
 CLASS_SYMBOL(CLASSC,CLASSP,Default##CLASSC##Constructor,Default##CLASSC##Destructor)

class classMgr
{
public:
 void* GetClassByName(char* _name)
 {
  __ClassDefiner* cd = __g_ClassDefinerHead;
  while (cd)
  {
   if ( strcmp(cd->m_pClass->m_ClassName ,_name) == 0  )
   {
    void *p = malloc(cd->m_pClass->m_ClassObjectSize);
    cd->m_pClass->m_ConstructFn(p); 
    return p;
   }
   cd = cd->m_pNext;
  }
 return NULL;
 }
};

//////////////////for test//////////////////////////////////////
////////////////////////////////////////////////////////////
#define Name(K)\
  public: char* getName(){return #K;}

class CC
{

public:
 int a[4];
 CC()
 {
  int k=0;
  k++;
  cout<<"cc"<<endl;
 }
 ~CC(){
  int k=0;
  k++;
  cout<<"!CC"<<endl;

 }


 Name(CC)
};

class  A:public CC
{
public:
 A(){
  a[0]=10000;
  int k=0;
  cout<<"aaaaaaaaaA"<<endl;
 }
 ~A(){
  cout<<"~~aaaaaaaaaA"<<endl;
  int k=0;
 }


 Name(A);
};

class  B:public CC
{
public:
 B()
 {
  cout<<"bbbbbbbbB"<<endl;
 }
 ~B(){}

 Name(B);
};


Declare(A,NULL)
Declare(B,CC)

main()
{

classMgr cm;
A* ap0 = (A*)cm.GetClassByName("A");
cout<<ap0->getName()<<endl;

B* ab0 = (B*)cm.GetClassByName("B");
delete ab0;

}

文章来源:http://songxiaoyu8.blog.163.com/blog/static/2081812820080245015991