-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccspring.cpp
More file actions
125 lines (117 loc) · 2.29 KB
/
ccspring.cpp
File metadata and controls
125 lines (117 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "ccspring.h"
#include "stdio.h"
#include "ccxml.h"
namespace ccs{
/**
* 获取由Bean工厂产生的对象
*/
void *getVoidBean(const char *name)
{
return ObjectFactory::instance()->getBean(name);
}
void putBean(const char *name, void *bean)
{
ObjectFactory::instance()->putBean(name, bean);
}
void *getVoidBean(const char *name, void*buffer, size_t size)
{
return ObjectFactory::instance()->getBean(name, buffer, size);
}
void initFrom(const char *filename)
{
ObjectFactory::instance()->init(filename);
}
}
/**
* 从配置文件中加载Bean
*/
void ObjectFactory::init(const char *filename)
{
_initCreators();
XML xml;
xml.imp(); // 设置默认实现
if (xml->initFrom(filename))
{
INode * node = xml->firstNode("bean");
while (node)
{
BeanInfo info;
info.name = node->getStr("name");
info.type = node->get<int>("creatortype");
info.creator = _imps[node->getStr("class")];
if (info.creator)
{
info.creator->configure(node);
_beans[info.name] = info;
}
node = node->nextNode("bean");
}
}
}
/**
* 获取Bean
*/
void *ObjectFactory::getBean(const char *name)
{
auto it = _beans.find(name);
if (it != _beans.end())
{
if (it->second.type == BeanInfo::SINGLETON)
{
if (it->second.creator)
return it->second.creator->self();
}
else if (it->second.type == BeanInfo::CLONE)
{
auto free_bean_it = _free_beans.find(name);
if ( free_bean_it != _free_beans.end())
{
if (free_bean_it->second.size())
{
void * p = free_bean_it->second.back();
free_bean_it->second.pop_back();
return p;
}
}
if (it->second.creator)
return it->second.creator->clone();
}
}
return 0;
}
/**
* 获取Bean 通过clone 的方式
*/
void *ObjectFactory::getBean(const char *name, void*buffer, size_t size)
{
auto it = _beans.find(name);
if (it != _beans.end())
{
if (it->second.creator)
return it->second.creator->clone_buffer(buffer, size);
}
return 0;
}
/**
* 释放Bean
*/
void ObjectFactory::putBean(const char *name,void *bean)
{
auto it = _beans.find(name);
if (it != _beans.end())
{
if (it->second.type == BeanInfo::CLONE)
{
_free_beans[name].push_back(bean);
}
}
}
ICreator * ObjectFactory::creator = 0;
void ObjectFactory::_initCreators()
{
while (creator)
{
_imps[creator->name] = creator;
creator = creator->next;
}
}