Samchon Framework for CPP  1.0.0
StaticEntityGroup.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 #include <samchon/protocol/EntityGroupBase.hpp>
4 
5 #include <algorithm>
6 
7 namespace samchon
8 {
9 namespace protocol
10 {
11  template <typename Container, typename Key = std::string>
12  class StaticEntityGroup
13  : public Container, // CONTAINS CHILD ENTITY ELEMENTS
14  public virtual Entity<Key>, // I AM A TYPE OF ENTITY TOO
15  public EntityGroupBase // INTERFACE
16  {
17  public:
18  typedef Container container_type;
19 
20  public:
21  /* ------------------------------------------------------------------------------------
22  CONSTRUCTORS
23  ------------------------------------------------------------------------------------ */
24  using container_type::container_type;
25 
26  virtual void construct(std::shared_ptr<library::XML> xml)
27  {
28  clear();
29  if (xml->has(CHILD_TAG()) == false)
30  return;
31 
32  std::shared_ptr<library::XMLList> &xml_list = xml->get(CHILD_TAG());
33  size_t i = 0;
34 
35  assign(xml_list->size(), container_type::value_type());
36  for (auto it = begin(); it != end(); it++)
37  it->construct(xml_list->at(i++));
38  };
39 
40  /* ------------------------------------------------------------------------------------
41  ACCESSORS
42  ------------------------------------------------------------------------------------ */
43  using container_type::erase;
44 
45  void erase(const typename container_type::value_type::key_type &key)
46  {
47  for (auto it = begin(); it != end(); )
48  if (it->key() == key)
49  it = erase(it);
50  else
51  it++;
52  };
53 
71  auto find(const typename container_type::value_type::key_type &key) -> typename container_type::iterator
72  {
73  return std::find_if
74  (
75  begin(), end(),
76  [key](const container_type::value_type &entity) -> bool
77  {
78  return entity.key() == key;
79  }
80  );
81  };
82 
100  auto find(const typename container_type::value_type::key_type &key) const -> typename container_type::const_iterator
101  {
102  return std::find_if
103  (
104  begin(), end(),
105  [key](const container_type::value_type &entity) -> bool
106  {
107  return entity.key() == key;
108  }
109  );
110  };
111 
118  auto has(const typename container_type::value_type::key_type &key) const -> bool
119  {
120  return std::any_of
121  (
122  begin(), end(),
123  [key](const container_type::value_type &entity) -> bool
124  {
125  return entity.key() == key;
126  }
127  );
128  };
129 
136  auto count(const typename container_type::value_type::key_type &key) const -> size_t
137  {
138  return std::count_if
139  (
140  begin(), end(),
141  [key](const container_type::value_type &entity) -> bool
142  {
143  return entity.key() == key;
144  }
145  );
146  };
147 
154  auto get(const typename container_type::value_type::key_type &key) -> typename container_type::value_type&
155  {
156  auto it = std::find_if
157  (
158  begin(), end(),
159  [key](const container_type::value_type &entity) -> bool
160  {
161  return entity.key() == key;
162  }
163  );
164 
165  if (it == end())
166  throw std::out_of_range("out of range");
167 
168  return *it;
169  };
170 
177  auto get(const typename container_type::value_type::key_type &key) const -> const typename container_type::value_type&
178  {
179  auto it = std::find_if
180  (
181  begin(), end(),
182  [key](const container_type::value_type &entity) -> bool
183  {
184  return entity.key() == key;
185  }
186  );
187 
188  if (it == end())
189  throw std::out_of_range("out of range");
190 
191  return *it;
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 };
virtual auto CHILD_TAG() const -> std::string=0
A tag name of children.