Samchon Framework for CPP  1.0.0
samchon::example::entity Namespace Reference

An example using entity module. More...

Detailed Description

An example using entity module.

Example Source
1 #include <iostream>
2 
3 #include <samchon/protocol/Entity.hpp>
4 #include <samchon/protocol/IHTMLEntity.hpp>
5 #include <samchon/protocol/SharedEntityArray.hpp>
6 
7 #include <samchon/library/XML.hpp>
8 
9 #ifdef _WIN64
10 # ifdef _DEBUG
11 # pragma comment(lib, "x64/Debug/SamchonFramework.lib")
12 # else
13 # pragma comment(lib, "x64/Release/SamchonFramework.lib")
14 # endif
15 #else
16 # ifdef _DEBUG
17 # pragma comment(lib, "Debug/SamchonFramework.lib")
18 # else
19 # pragma comment(lib, "Release/SamchonFramework.lib")
20 # endif
21 #endif
22 
23 using namespace std;
24 using namespace samchon::library;
25 using namespace samchon::protocol;
26 
27 class Member
28  : public Entity, public virtual IHTMLEntity
29 {
30 protected:
31  typedef Entity super;
32 
33  string id;
34  string name;
35  int age;
36  int grade;
37 
38 public:
39  /* ---------------------------------------------------------------------
40  CONSTRUCTORS
41  --------------------------------------------------------------------- */
42  Member()
43  : super(), IHTMLEntity()
44  {
45  };
46  Member(const string &id, const string &name, int age, int grade)
47  : super(), IHTMLEntity()
48  {
49  this->id = id;
50  this->name = name;
51  this->age = age;
52  this->grade = grade;
53  };
54  virtual ~Member() = default;
55 
56  virtual void construct(shared_ptr<XML> xml) override
57  {
58  this->id = xml->getProperty("id");
59  this->name = xml->getProperty("name");
60  this->age = xml->getProperty<int>("age");
61  this->grade = xml->getProperty<int>("grade");
62  };
63 
64  /* ---------------------------------------------------------------------
65  GETTERSs
66  --------------------------------------------------------------------- */
67  virtual auto key() const -> std::string override
68  {
69  return this->id;
70  };
71 
72  /* ---------------------------------------------------------------------
73  XML EXPORTERS
74  --------------------------------------------------------------------- */
75  virtual auto TAG() const -> string override
76  {
77  return "member";
78  };
79  virtual auto toXML() const -> shared_ptr<XML>
80  {
81  shared_ptr<XML> &xml = super::toXML();
82  xml->setProperty("id", id);
83  xml->setProperty("name", name);
84  xml->setProperty("age", age);
85  xml->setProperty("grade", grade);
86 
87  return move(xml);
88  };
89  virtual auto toHTML() const -> string
90  {
91  return toTR(id, name, age, grade);
92  };
93 };
94 
95 class MemberArray
96  : public SharedEntityArray<Member>,
97  public virtual IHTMLEntity
98 {
99 protected:
100  typedef SharedEntityArray<Member> super;
101 
102  string application;
103  int department;
104  Member *chief;
105 
106 public:
107  /* ---------------------------------------------------------------------
108  CONSTRUCTORS
109  --------------------------------------------------------------------- */
110  MemberArray()
111  : super(), IHTMLEntity()
112  {
113  this->chief = nullptr;
114  };
115  virtual ~MemberArray() = default;
116 
117  // You don't need to consider children(Member) objects
118  // Just concentrate on constructing MemberArray's own member variables
119  virtual void construct(shared_ptr<XML> xml) override
120  {
121  super::construct(xml);
122 
123  this->application = xml->getProperty("application");
124  this->department = xml->getProperty<int>("department");
125 
126  if(xml->hasProperty("chief") == true && this->has( xml->getProperty("chief") ) == true)
127  this->chief = this->get( xml->getProperty("cheif") ).get();
128  };
129 
130 protected:
131  //FACTORY METHOD FOR MEMBER
132  virtual auto createChild(shared_ptr<XML> = nullptr) -> Member* override
133  {
134  return new Member();
135  };
136 
137  /* ---------------------------------------------------------------------
138  XML EXPORTERS
139  --------------------------------------------------------------------- */
140 public:
141  virtual auto TAG() const -> string override
142  {
143  return "memberArray";
144  };
145  virtual auto CHILD_TAG() const -> string override
146  {
147  return "member";
148  };
149 
150  // You don't need to consider children(Member) objects
151  // Just concentrate on expressing MemberArray's own member variables
152  virtual auto toXML() const -> shared_ptr<XML>
153  {
154  shared_ptr<XML> &xml = super::toXML();
155  xml->setProperty("application", application);
156  xml->setProperty("department", department);
157 
158  if(chief != nullptr)
159  xml->setProperty("cheif", chief->key());
160 
161  return move(xml);
162  };
163  virtual auto toHTML() const -> string
164  {
165  string html = "<table>\n";
166  html += toTH("ID", "Name", "Age", "Grade") + "\n";
167 
168  for (size_t i = 0; i < 2; i++)
169  html += at(i)->toHTML() + "\n";
170 
171  html += "</table>";
172  return move(html);
173  };
174 };
175 
176 void main()
177 {
178  string str = string("") +
179  "<memberArray application='framework' department='7' cheif='samchon'>\n" +
180  " <member id='samchon' name='Jeongho Nam' age='27' grade='5' />" +
181  " <member id='submaster' name='No Name' age='100' grade='4' />" +
182  " <member id='john' name='John Doe' age='33' grade='2' />" +
183  " <member id='bad_man' name='Bad Man' age='44' grade='-1' />" +
184  " <member id='guest' name='Guest' age='0' grade='0' />" +
185  "</memberArray>";
186  shared_ptr<XML> xml(new XML(str));
187 
188  MemberArray memberArray;
189  memberArray.construct(xml);
190 
191  memberArray.emplace_back(new Member("freshman", "A fresh man", 20, 2));
192  memberArray.emplace_back(new Member("senior", "A senior", 70, 2));
193 
194  cout << memberArray.toXML()->toString() << endl << endl;
195  cout << memberArray.toHTML() << endl;
196  system("pause");
197 }
An entity, a standard data class.
Definition: Entity.hpp:48
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:52
Package of network protocol and libraries.
Definition: protocol.hpp:185
XML is a class representing xml object.
Definition: XML.hpp:72
An interface supporting conversion to html.
Definition: IHTMLEntity.hpp:41
Result of the example
example_entity_result.png
Author
Jeongho Nam http://samchon.org