YAPOG  0.0.1
Yet Another Pokemon Online Game
ObjectFactory.cpp
Go to the documentation of this file.
6 
7 namespace yap
8 {
10  UInt64 ObjectFactory::currentID_ = INITIAL_ID;
11 
13  : objectIDLoaders_ ()
14  , objectLoaders_ ()
15  , types_ ()
16  , ids_ ()
17  {
18  }
19 
21  {
22  for (const auto& it : objectIDLoaders_)
23  delete it.second;
24 
25  for (const auto& it : objectLoaders_)
26  delete it.second;
27  }
28 
30  {
31  static ObjectFactory instance;
32 
33  return instance;
34  }
35 
37  const String& typeName,
38  IObjectIDLoader* loader)
39  {
40  if (ContainsLoader (typeName))
42  "Loader `" + typeName + "' already added to the factory.");
43 
44  objectIDLoaders_.Add (typeName, loader);
45 
46  AddType (ID (currentID_++), typeName);
47  }
48 
50  const String& typeName,
51  IObjectLoader* loader)
52  {
53  if (ContainsLoader (typeName))
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))
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 
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