Samchon Framework for CPP  1.0.0
ExternalSystemArray.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/protocol/SharedEntityDeque.hpp>
5 # include <samchon/templates/external/ExternalSystem.hpp>
6 #include <samchon/templates/external/base/ExternalSystemArrayBase.hpp>
7 #include <samchon/protocol/IProtocol.hpp>
8 
9 #include <vector>
10 #include <thread>
11 
12 namespace samchon
13 {
14 namespace templates
15 {
22 namespace external
23 {
67  template <class System = ExternalSystem>
69  : public protocol::SharedEntityDeque<System>,
70  public base::ExternalSystemArrayBase,
71  public protocol::IProtocol
72  {
73  private:
75 
76  public:
77  /* ---------------------------------------------------------
78  CONSTRUCTORS
79  --------------------------------------------------------- */
84  : super()
85  {
86  };
87 
91  virtual ~ExternalSystemArray() = default;
92 
93  public:
94  /* ---------------------------------------------------------
95  ACCESSORS
96  --------------------------------------------------------- */
104  auto hasRole(const std::string &key) const -> bool
105  {
106  for (size_t i = 0; i < size(); i++)
107  for (size_t j = 0; j < at(i)->size(); j++)
108  if (at(i)->at(j)->key() == key)
109  return true;
110 
111  return false;
112  };
113 
121  auto getRole(const std::string &key) const -> std::shared_ptr<ExternalSystemRole>
122  {
123  for (size_t i = 0; i < size(); i++)
124  for (size_t j = 0; j < at(i)->size(); j++)
125  if (at(i)->at(j)->key() == key)
126  return at(i)->at(j);
127 
128  throw std::out_of_range("No such key.");
129  };
130 
131  virtual auto _Get_children() const -> std::vector<std::shared_ptr<ExternalSystem>>
132  {
133  std::vector<std::shared_ptr<ExternalSystem>> children(begin(), end());
134  return children;
135  };
136 
137  /* ---------------------------------------------------------
138  MESSAGE CHAIN
139  --------------------------------------------------------- */
145  virtual void sendData(std::shared_ptr<protocol::Invoke> invoke)
146  {
147  std::vector<std::thread> threads;
148  library::UniqueReadLock uk(getMutex());
149 
150  threads.reserve(this->size());
151  for (size_t i = 0; i < size(); i++)
152  threads.emplace_back(&ExternalSystem::sendData, at(i).get(), invoke);
153 
154  uk.unlock();
155  for (auto it = threads.begin(); it != threads.end(); it++)
156  it->join();
157  };
158 
164  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) = 0;
165 
166  /* ---------------------------------------------------------
167  EXPORTERS
168  --------------------------------------------------------- */
169  virtual auto TAG() const -> std::string override
170  {
171  return "systemArray";
172  };
173  virtual auto CHILD_TAG() const -> std::string override
174  {
175  return "system";
176  };
177  };
178 };
179 };
180 };
virtual void replyData(std::shared_ptr< protocol::Invoke > invoke)=0
auto hasRole(const std::string &key) const -> bool
virtual auto CHILD_TAG() const -> std::string override
A tag name of children.
void unlock() const
Unlock of read.
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:40
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke)
Unique lock for reading.
auto getRole(const std::string &key) const -> std::shared_ptr< ExternalSystemRole >
virtual auto key() const -> Key
Get a key that can identify the Entity uniquely.
Definition: Entity.hpp:133
virtual void sendData(std::shared_ptr< protocol::Invoke > invoke) override