Samchon Framework for CPP  1.0.0
EntityGroup.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 #include <samchon/protocol/IEntityGroup.hpp>
4 
5 namespace samchon
6 {
7 namespace protocol
8 {
51  template <typename _Container, typename _ETy = Entity, typename T = _Container::value_type>
53  : public _Container,
54  public virtual Entity, //CLASS
55  public virtual IEntityGroup //INTERFACE
56  {
57  public:
58  typedef _Container container_type;
59  typedef T value_type;
60  typedef _ETy entity_type;
61 
62  public:
63  /* ------------------------------------------------------------------------------------
64  CONSTRUCTORS
65  ------------------------------------------------------------------------------------ */
70  : _Container(), Entity(),
71  IEntityGroup()
72  {
73  };
74  virtual ~EntityGroup() = default;
75 
90  virtual void construct(std::shared_ptr<library::XML> xml)
91  {
92  clear();
93  if (xml->has(CHILD_TAG()) == false)
94  return;
95 
96  std::shared_ptr<library::XMLList> &xmlList = xml->get(CHILD_TAG());
97 
98  if (std::is_same<_Container, std::vector<_Container::value_type, _Container::allocator_type>>::value == true)
99  {
100  //FOR RESERVE
101  assign(xmlList->size(), nullptr);
102  erase(begin(), end());
103  }
104 
105  for (size_t i = 0; i < xmlList->size(); i++)
106  {
107  std::shared_ptr<library::XML> &xmlElement = xmlList->at(i);
108 
109  entity_type *entity = createChild(xmlElement);
110  if (entity != nullptr)
111  {
112  entity->construct(xmlList->at(i));
113  emplace_back(entity);
114  }
115  }
116  };
117 
118  protected:
129  virtual auto createChild(std::shared_ptr<library::XML>)->entity_type* = 0;
130 
131  public:
132  /* ------------------------------------------------------------------------------------
133  MODIFIERS
134  ------------------------------------------------------------------------------------ */
135  using _Container::erase;
136 
137  void erase(const std::string &key)
138  {
139  for (_Container::iterator it = begin(); it != end();)
140  if ((*it)->key() == key)
141  it = erase(it);
142  else
143  it++;
144  };
145 
146  /* ------------------------------------------------------------------------------------
147  GETTERS
148  ------------------------------------------------------------------------------------ */
155  auto has(const std::string &key) const -> bool
156  {
157  for (auto it = begin(); it != end(); it++)
158  if ((*it)->key() == key)
159  return true;
160 
161  return false;
162  };
163 
170  auto get(const std::string &key) -> value_type&
171  {
172  for (auto it = begin(); it != end(); it++)
173  if ((*it)->key() == key)
174  return *it;
175 
176  throw std::exception("out of range");
177  };
178 
185  auto get(const std::string &key) const -> const value_type&
186  {
187  for (auto it = begin(); it != end(); it++)
188  if ((*it)->key() == key)
189  return *it;
190 
191  throw std::exception("out of range");
192  };
193 
194  /* ------------------------------------------------------------------------------------
195  EXPORTERS
196  ------------------------------------------------------------------------------------ */
212  virtual auto toXML() const -> std::shared_ptr<library::XML>
213  {
214  std::shared_ptr<library::XML> &xml = Entity::toXML();
215 
216  std::shared_ptr<library::XMLList> xmlList(new library::XMLList());
217  xmlList->reserve(this->size());
218 
219  for (auto it = begin(); it != end(); it++)
220  xmlList->push_back((*it)->toXML());
221 
222  xml->set(CHILD_TAG(), xmlList);
223  return xml;
224  };
225  };
226 };
227 };
An entity, a standard data class.
Definition: Entity.hpp:48
std::vector< std::shared_ptr< XML > > XMLList
A list of XML, tags are same.
Definition: XMLList.hpp:19
auto has(const std::string &key) const -> bool
Indicates whether a container has an object having the specified identifier.
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the Entity.
Definition: Entity.cpp:30
An iternface for entity group.
Definition: RWMutex.hpp:4
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:52
virtual auto toXML() const -> std::shared_ptr< library::XML >
Get an XML object represents the EntityGroup.
virtual auto key() const -> std::string
Get a key that can identify the Entity uniquely.
Definition: Entity.cpp:15
EntityGroup()
Default Constructor.
Definition: EntityGroup.hpp:69
virtual void construct(std::shared_ptr< library::XML > xml)
Construct data of the Entity from an XML object.
Definition: EntityGroup.hpp:90
virtual auto CHILD_TAG() const -> std::string=0
A tag name of children.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
virtual auto createChild(std::shared_ptr< library::XML >) -> entity_type *=0
Factory method of a child Entity.