Samchon Framework for CPP  1.0.0
XML.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <samchon/HashMap.hpp>
5 #include <samchon/library/XMLList.hpp>
6 
7 #include <sstream>
8 #include <string>
9 #include <samchon/WeakString.hpp>
10 
11 namespace std
12 {
13  template<class T, class _Alloc> class list;
14 };
15 namespace samchon
16 {
17 namespace library
18 {
72  class SAMCHON_FRAMEWORK_API XML
73  : public HashMap<std::string, std::shared_ptr<XMLList>>
74  {
75  private:
77 
85  std::string tag;
86 
94  std::string value;
95 
107 
108  public:
109  /* -----------------------------------------------------------
110  CONSTRUCTORS
111  ----------------------------------------------------------- */
117  XML();
118 
126  XML(const XML &);
127 
131  XML(XML &&);
132 
142  XML(WeakString);
143 
144  private:
155  XML(XML*, WeakString &);
156 
157  void construct(WeakString &);
158  void constructKey(WeakString &);
159  void constructProperty(WeakString &);
160  auto constructValue(WeakString &) -> bool;
161  void constructChildren(WeakString &);
162 
163  public:
169  void push_back(const WeakString &);
170 
176  void push_back(const std::shared_ptr<XML>);
177 
187  void addAllProperty(const std::shared_ptr<XML>);
188 
189  /* -----------------------------------------------------------
190  SETTERS
191  ----------------------------------------------------------- */
197  void setTag(const std::string &);
198 
228  template <typename T>
229  void setValue(const T &val)
230  {
231  std::stringstream sstream;
232  sstream << val;
233 
234  this->value = std::move(sstream.str());
235  };
236  template<> void setValue(const std::string &val)
237  {
238  this->value = val;
239  };
240  template<> void setValue(const WeakString &val)
241  {
242  this->value = val.str();
243  };
244 
245  template <typename T>
246  void insertValue(const std::string &tag, const T &val)
247  {
248  std::shared_ptr<XML> xml(new XML());
249  xml->setTag(tag);
250  xml->setValue(val);
251 
252  push_back(xml);
253  };
254 
258  template<typename T>
259  void setProperty(const std::string &key, const T &val)
260  {
261  std::stringstream sstream;
262  sstream << val;
263 
264  propertyMap.set(key, sstream.str());
265  };
266  template<> void setProperty(const std::string &key, const std::string &val)
267  {
268  propertyMap.set(key, val);
269  };
270  template<> void setProperty(const std::string &key, const WeakString &val)
271  {
272  propertyMap.set(key, val.str());
273  };
274 
281  void eraseProperty(const std::string&);
282 
286  void clearProperties();
287 
288  /* -----------------------------------------------------------
289  GETTERS
290  ----------------------------------------------------------- */
291  public:
298  auto getTag() const->std::string;
299 
303  template<class T = std::string> auto getValue() const -> T
304  {
305  std::stringstream sstream;
306  sstream << this->value;
307 
308  T val;
309  sstream >> val;
310 
311  return std::move(val);
312  };
313 
314  template<> auto getValue() const -> std::string
315  {
316  return value;
317  };
318  template<> auto getValue() const -> WeakString
319  {
320  return value;
321  };
322 
326  template<class T = std::string> auto getProperty(const std::string &key) const -> T
327  {
328  std::stringstream sstream;
329  sstream << propertyMap.get(key);
330 
331  T val;
332  sstream >> val;
333 
334  return std::move(val);
335  };
336 
337  template<> auto getProperty(const std::string &key) const -> std::string
338  {
339  return propertyMap.get(key);
340  };
341  template<> auto getProperty(const std::string &key) const -> WeakString
342  {
343  return propertyMap.get(key);
344  };
345 
349  auto hasProperty(const std::string &) const -> bool;
350 
354  auto getPropertyMap() const -> const HashMap<std::string, std::string>&;
355 
356  /* -----------------------------------------------------------
357  FILTERS
358  ----------------------------------------------------------- */
359  private:
360  auto calcMinIndex(const std::vector<size_t>&) const->size_t;
361 
362  auto encodeValue(const WeakString &) const->std::string;
363  auto decodeValue(const WeakString &) const->std::string;
364  auto encodeProperty(const WeakString &) const->std::string;
365  auto decodeProperty(const WeakString &) const->std::string;
366 
367  /* -----------------------------------------------------------
368  EXPORTERS
369  ----------------------------------------------------------- */
370  public:
377  auto toString(size_t level = 0) const->std::string;
378  };
379 
380  SAMCHON_FRAMEWORK_EXTERN template class SAMCHON_FRAMEWORK_API std::shared_ptr<XML>;
381  SAMCHON_FRAMEWORK_EXTERN template class SAMCHON_FRAMEWORK_API HashMap<std::string, std::shared_ptr<XMLList>>;
382  SAMCHON_FRAMEWORK_EXTERN template class SAMCHON_FRAMEWORK_API HashMap<std::string, std::string>;
383 };
384 };
std::string value
Value of the XML.
Definition: XML.hpp:94
Definition: RWMutex.hpp:4
auto str() const -> std::string
Get the string content.
Definition: WeakString.cpp:546
std::string tag
Tag name.
Definition: XML.hpp:85
auto get(const Key &key) -> T &
Get element.
Definition: HashMap.hpp:146
HashMap< std::string, std::string > propertyMap
Properties belongs to the XML.
Definition: XML.hpp:106
void setProperty(const std::string &key, const T &val)
Set a property with its key.
Definition: XML.hpp:259
auto getValue() const -> T
Get value of the XML.
Definition: XML.hpp:303
XML is a class representing xml object.
Definition: XML.hpp:72
auto getProperty(const std::string &key) const -> T
Get property.
Definition: XML.hpp:326
void set(const Key &key, const T &val)
Set element.
Definition: HashMap.hpp:167
void setValue(const T &val)
Set value of the XML.
Definition: XML.hpp:229
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7
Customized std::unordered_map.
Definition: HashMap.hpp:105
A string class only references characeters, reference only.
Definition: WeakString.hpp:32