YAPOG
0.0.1
Yet Another Pokemon Online Game
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
ObjectFactory.cpp
Go to the documentation of this file.
1
#include "
YAPOG/Game/Factory/ObjectFactory.hpp
"
2
#include "
YAPOG/System/Error/Exception.hpp
"
3
#include "
YAPOG/Game/Factory/IObjectIDLoader.hpp
"
4
#include "
YAPOG/Game/Factory/IObjectLoader.hpp
"
5
#include "
YAPOG/System/StringHelper.hpp
"
6
7
namespace
yap
8
{
9
const
UInt64
ObjectFactory::INITIAL_ID
= 0;
10
UInt64
ObjectFactory::currentID_
= INITIAL_ID;
11
12
ObjectFactory::ObjectFactory
()
13
: objectIDLoaders_ ()
14
, objectLoaders_ ()
15
, types_ ()
16
, ids_ ()
17
{
18
}
19
20
ObjectFactory::~ObjectFactory
()
21
{
22
for
(
const
auto
& it :
objectIDLoaders_
)
23
delete
it.second;
24
25
for
(
const
auto
& it :
objectLoaders_
)
26
delete
it.second;
27
}
28
29
ObjectFactory
&
ObjectFactory::Instance
()
30
{
31
static
ObjectFactory
instance;
32
33
return
instance;
34
}
35
36
void
ObjectFactory::RegisterLoader
(
37
const
String
& typeName,
38
IObjectIDLoader
* loader)
39
{
40
if
(
ContainsLoader
(typeName))
41
YAPOG_THROW
(
42
"Loader `"
+ typeName +
"' already added to the factory."
);
43
44
objectIDLoaders_
.
Add
(typeName, loader);
45
46
AddType
(
ID
(
currentID_
++), typeName);
47
}
48
49
void
ObjectFactory::RegisterLoader
(
50
const
String
& typeName,
51
IObjectLoader
* loader)
52
{
53
if
(
ContainsLoader
(typeName))
54
YAPOG_THROW
(
55
"Loader `"
+ typeName +
"' already added to the factory."
);
56
57
objectLoaders_
.
Add
(typeName, loader);
58
59
AddType
(
ID
(
currentID_
++), typeName);
60
}
61
62
void
ObjectFactory::AddType
(
const
ID
&
id
,
const
String
& type)
63
{
64
if
(!
types_
.
Contains
(
id
))
65
{
66
types_
.
Add
(
id
, type);
67
ids_
.
Add
(type,
id
);
68
}
69
else
70
{
71
types_
[id] = type;
72
ids_
[type] = id;
73
}
74
}
75
76
const
String
&
ObjectFactory::GetType
(
const
ID
&
id
)
const
77
{
78
if
(!
types_
.
Contains
(
id
))
79
YAPOG_THROW
(
80
"No type for ID `"
+
81
StringHelper::ToString
(
id
.GetValue ())
82
+
"'."
);
83
84
return
types_
[id];
85
}
86
87
const
ID
&
ObjectFactory::GetID
(
const
String
& type)
const
88
{
89
if
(!
ids_
.
Contains
(type))
90
YAPOG_THROW
(
"No ID for type `"
+ type +
"'."
);
91
92
return
ids_
[type];
93
}
94
95
const
collection::Map<ID, String>
&
ObjectFactory::GetTypes
()
const
96
{
97
return
types_
;
98
}
99
100
bool
ObjectFactory::ContainsLoader
(
const
String
& typeName)
const
101
{
102
return
103
objectLoaders_
.
Contains
(typeName) ||
104
objectIDLoaders_
.
Contains
(typeName);
105
}
106
}
// namespace yap
YAPOG
src
YAPOG
Game
Factory
ObjectFactory.cpp
Generated on Mon Sep 17 2012 22:24:24 for YAPOG by
1.8.1.1