Samchon Framework for CPP  1.0.0
samchon::protocol::IHTMLEntity Class Referenceabstract

An interface supporting conversion to html. More...

#include <IHTMLEntity.hpp>

Public Member Functions

 IHTMLEntity ()
 Default Constructor. More...
 
virtual auto toHTML () const -> std::string=0
 Get an html string represents the Entity. More...
 

Static Public Member Functions

template<typename T , typename... _Args>
static auto toTH (const T &val, const _Args &...args) -> std::string
 Get a string represents a <th> tag. More...
 
template<typename T , typename... _Args>
static auto toTR (const T &val, const _Args &...args) -> std::string
 Get a string represents a <tr> tag. More...
 

Detailed Description

An interface supporting conversion to html.

IHTMEntity is an interface supporting conversion method to html tags. The IHTMLEntity is used to documentation or printing data of the entity to web-screen; In C++, documentation is main reason to implementing the IHTMLEntity and in JS, printing on web-screen is main reason.

IHTMLEntity provides abstract method toHTML() and varadic template methods for constituting a table. I'm planning to support lots of utility methods for html tags but I've not exprienced developing the homepage or html service. If you're familiar with the html, please contribute to the IHTMLEntity interface.

protocol_entity.png
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
static auto toTR(const T &val, const _Args &...args) -> std::string
Get a string represents a <tr> tag.
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:52
static auto toTH(const T &val, const _Args &...args) -> std::string
Get a string represents a <th> tag.
Definition: IHTMLEntity.hpp:89
Package of network protocol and libraries.
Definition: protocol.hpp:185
virtual auto toHTML() const -> std::string=0
Get an html string represents the Entity.
An interface supporting conversion to html.
Definition: IHTMLEntity.hpp:41

Result of the example

example_entity.png
Author
Jeongho Nam http://samchon.org

Definition at line 41 of file IHTMLEntity.hpp.

Constructor & Destructor Documentation

IHTMLEntity::IHTMLEntity ( )

Default Constructor.

Definition at line 9 of file IHTMLEntity.cpp.

References samchon::WeakString::str().

Here is the call graph for this function:

Member Function Documentation

template<typename T , typename... _Args>
static auto samchon::protocol::IHTMLEntity::toTH ( const T &  val,
const _Args &...  args 
) -> std::string
inlinestatic

Get a string represents a <th> tag.

Template Parameters
_TyType of an argument to be contained with a <td> tag.
_ArgsLeft varadic template arguments' type

Returns a string represents a title columns with <th> tag by varadic template method.

Supported parameters: that can converted to string

  • number
    • (unsigned) short
    • (unsigned) long
    • (unsigned) long long
    • (unsigned) int
    • float
    • double
    • long double
  • string
Parameters
valAn element to be contained with <td> a tag.
argsLeft argument to be contained with <td> tags.
Returns
A string of <th> tag represents title columns.

Definition at line 89 of file IHTMLEntity.hpp.

template<typename T , typename... _Args>
static auto samchon::protocol::IHTMLEntity::toTR ( const T &  val,
const _Args &...  args 
) -> std::string
inlinestatic

Get a string represents a <tr> tag.

Template Parameters
_TyType of an argument to be contained with a <tr> tag.
_ArgsLeft varadic template arguments' type

Returns a string represents a data columns of a row with <tr> tag by varadic template method.

Supported parameters: that can converted to string

  • number
    • (unsigned) short
    • (unsigned) long
    • (unsigned) long long
    • (unsigned) int
    • float
    • double
    • long double
  • string
Parameters
valAn element to be contained with <tr> a tag.
argsLeft argument to be contained with <tr> tags.
Returns
A string of <th> tag represents data columns of a row.

Definition at line 142 of file IHTMLEntity.hpp.

virtual auto samchon::protocol::IHTMLEntity::toHTML ( ) const -> std::string
pure virtual

Get an html string represents the Entity.

Returns
A string represents the Entity by html tags.

The documentation for this class was generated from the following files: