Samchon Framework for CPP  1.0.0
InvokeParameter.hpp
1 #pragma once
2 #include <samchon/protocol/Entity.hpp>
3 
4 #include <sstream>
5 #include <samchon/ByteArray.hpp>
6 #include <samchon/WeakString.hpp>
7 
8 namespace samchon
9 {
10 namespace protocol
11 {
12  class Invoke;
13 
48  : public virtual Entity<>
49  {
50  friend class Invoke;
51 
52  protected:
53  typedef Entity<std::string> super;
54 
58  std::string name;
59 
70  std::string type;
71 
75  std::string str;
76 
80  std::shared_ptr<library::XML> xml;
81 
86 
87  public:
88  /* ----------------------------------------------------------
89  CONSTRUCTORS
90  ---------------------------------------------------------- */
95  {
96  };
97 
112  InvokeParameter(const std::string &name, const std::string &type, const std::string &val)
113  {
114  this->name = name;
115  this->type = type;
116  this->str = val;
117  };
118 
152  template <typename T>
153  InvokeParameter(const std::string &name, const T &val)
154  {
155  this->name = name;
156 
157  construct_by_varadic_template(val);
158  };
159 
160  InvokeParameter(const std::string &name, const char *ptr)
161  {
162  this->name = name;
163  this->type = "string";
164 
165  this->str = ptr;
166  };
167 
168  //MOVE CONSTRUCTORS
172  InvokeParameter(const std::string &name, std::string &&str)
173  {
174  this->name = name;
175  this->type = "string";
176 
177  this->str = move(str);
178  };
179 
183  InvokeParameter(const std::string &name, ByteArray &&byte_array)
184  {
185  this->name = name;
186  this->type = "ByteArray";
187 
188  this->byte_array = move(byte_array);
189  };
190 
191  virtual ~InvokeParameter() = default;
192 
193  virtual void construct(std::shared_ptr<library::XML> xml) override
194  {
195  if (xml->hasProperty("name") == true)
196  this->name = xml->getProperty("name");
197  else
198  this->name = "";
199 
200  this->type = xml->getProperty("type");
201 
202  if (type == "XML")
203  {
204  if (xml->empty())
205  this->xml = nullptr;
206  else
207  this->xml = xml->begin()->second->at(0);
208  }
209  else if (type == "ByteArray")
210  {
211  size_t size = xml->getValue<size_t>();
212 
213  byte_array.reserve(size);
214  }
215  else
216  this->str = xml->getValue();
217  };
218 
219  auto byteArrayCapacity() const -> size_t
220  {
221  return byte_array.capacity();
222  };
223 
224  void setByteArray(ByteArray &&ba)
225  {
226  byte_array = move(ba);
227  };
228 
229  protected:
230  template <typename T>
231  void construct_by_varadic_template(const T &val)
232  {
233  this->type = "number";
234 
235  std::stringstream sstream;
236  sstream << val;
237 
238  this->str = move(sstream.str());
239  };
240  template<> void construct_by_varadic_template(const std::string &str)
241  {
242  this->type = "string";
243  this->str = str;
244  };
245  template<> void construct_by_varadic_template(const WeakString &wstr)
246  {
247  this->type = "string";
248  this->str = wstr.str();
249  };
250  template<> void construct_by_varadic_template(const ByteArray &byte_array)
251  {
252  this->type = "ByteArray";
253  this->byte_array = byte_array;
254  };
255 
256  template<> void construct_by_varadic_template(const std::shared_ptr<library::XML> &xml)
257  {
258  this->type = "XML";
259  this->xml = xml;
260  };
261 
262  public:
263  /* ----------------------------------------------------------
264  GETTERS
265  ---------------------------------------------------------- */
266  virtual auto key() const -> std::string override
267  {
268  return name;
269  };
270 
274  auto getName() const->std::string
275  {
276  return name;
277  };
278 
282  auto getType() const->std::string
283  {
284  return type;
285  };
286 
292  template<typename T> auto getValue() const -> T
293  {
294  double val = std::stod(str);
295 
296  return (T)val;
297  };
298  template<> auto getValue() const -> std::string
299  {
300  return str;
301  };
302  template<> auto getValue() const -> WeakString
303  {
304  return str;
305  };
306  template<> auto getValue() const -> std::shared_ptr<library::XML>
307  {
308  return xml;
309  };
310  template<> auto getValue() const -> ByteArray
311  {
312  return byte_array;
313  };
314 
319  auto getValueAsXML() const -> std::shared_ptr<library::XML>
320  {
321  return xml;
322  };
323 
329  template <typename T> auto referValue() const -> const T&;
330  template<> auto referValue() const -> const std::string&
331  {
332  return str;
333  };
334  template<> auto referValue() const -> const ByteArray&
335  {
336  return byte_array;
337  };
338 
344  template <typename T> auto moveValue() -> T;
345  template<> auto moveValue() -> std::string
346  {
347  return move(str);
348  };
349  template<> auto moveValue() -> ByteArray
350  {
351  return move(byte_array);
352  };
353 
354  public:
355  /* ----------------------------------------------------------
356  EXPORTERS
357  ---------------------------------------------------------- */
358  virtual auto TAG() const -> std::string override
359  {
360  return "parameter";
361  };
362 
363  virtual auto toXML() const -> std::shared_ptr<library::XML> override
364  {
365  std::shared_ptr<library::XML> &xml = super::toXML();
366 
367  if (name.empty() == false)
368  xml->setProperty("name", name);
369  xml->setProperty("type", type);
370 
371  if (type == "XML")
372  xml->push_back(this->xml);
373  else if (type == "ByteArray")
374  xml->setValue(byte_array.size());
375  else
376  xml->setValue(str);
377 
378  return xml;
379  };
380  };
381 };
382 };
An entity, a standard data class.
Definition: Entity.hpp:115
auto str() const -> std::string
Get the string content.
Definition: WeakString.hpp:926
std::string str
A string value if the type is "string" or "number".
std::string name
A name can represent the parameter.
InvokeParameter(const std::string &name, const std::string &type, const std::string &val)
Construct from arguments.
InvokeParameter(const std::string &name, const T &val)
Construct with its name and a value.
auto referValue() const -> const T &
Reference value.
auto getType() const -> std::string
Get type.
std::shared_ptr< library::XML > xml
An XML object if the type is "XML".
virtual auto key() const -> std::string override
Get a key that can identify the Entity uniquely.
auto moveValue() -> T
Move value.
auto getValueAsXML() const -> std::shared_ptr< library::XML >
Get value as XML object.
auto getName() const -> std::string
Get name.
Standard message of network I/O.
Definition: Invoke.hpp:35
Binary data class.
Definition: ByteArray.hpp:29
InvokeParameter()
Default Constructor.
ByteArray byte_array
A binary value if the type is "ByteArray".
std::string type
Type of the parameter.
InvokeParameter(const std::string &name, std::string &&str)
Construct from the name and a moved string.
A parameter of an Invoke.
auto getValue() const -> T
Get value.
InvokeParameter(const std::string &name, ByteArray &&byte_array)
Construct from name and a moved ByteArray.
A string class only references characeters, reference only.
Definition: WeakString.hpp:35