Samchon Framework for CPP  1.0.0
IHTMLEntity.hpp
1 #pragma once
2 
3 #include <string>
4 #include <sstream>
5 
6 namespace samchon
7 {
8  class WeakString;
9 };
10 
11 namespace samchon
12 {
13 namespace protocol
14 {
40  {
41  public:
42  /* ------------------------------------------------------------------------------------
43  CONSTRUCTORS
44  ------------------------------------------------------------------------------------ */
45  virtual ~IHTMLEntity() = default;
46 
47  /* ------------------------------------------------------------------------------------
48  EXPORTERS
49  ------------------------------------------------------------------------------------ */
82  template <typename T, typename ... _Args>
83  static auto toTH(const T &val, const _Args& ... args) -> std::string
84  {
85  std::string str;
86  size_t i = 0;
87  size_t size = sizeof...(args)+1;
88 
89  fetchTX("th", str, i, size, val, args...);
90  return str;
91  };
92  template <typename T>
93  static auto toTH(const T &val) -> std::string
94  {
95  std::string str =
96  "\t<th>\n" +
97  "\t\t" + toTD(val) + "\n" +
98  "\t</th>";
99 
100  return str;
101  };
102 
135  template <typename T, typename ... _Args>
136  static auto toTR(const T &val, const _Args& ... args) -> std::string
137  {
138  std::string str;
139  size_t i = 0;
140  size_t size = sizeof...(args)+1;
141 
142  fetchTX("tr", str, i, size, val, args...);
143  return str;
144  };
145  template <typename T>
146  static auto toTR(const T &val) -> std::string
147  {
148  std::string str =
149  "\t<tr>\n" +
150  "\t\t" + toTD(val) + "\n" +
151  "\t</tr>";
152 
153  return str;
154  };
155 
161  virtual auto toHTML() const->std::string = 0;
162 
163  protected:
164  template <typename T, typename ... _Args>
165  static void fetchTX(const std::string &tag, std::string &str, size_t &index, size_t size, const T &val, const _Args& ... args)
166  {
167  fetchTX(tag, str, index, size, val);
168 
169  index++;
170  fetchTX(tag, str, index, size, args...);
171  };
172  template <typename T>
173  static void fetchTX(const std::string &tag, std::string &str, size_t &index, size_t size, const T &val)
174  {
175  if (index == 0)
176  str += "\t<" + tag + ">\n";
177 
178  str += "\t\t" + toTD(val) + "\n";
179 
180  if (index == size - 1)
181  str += "\t</" + tag + ">";
182  };
183 
184  template <typename T>
185  static auto toTD(const T &val) -> std::string
186  {
187  std::stringstream ss;
188 
189  ss << "<td>" << val << "</td>";
190  return ss.str();
191  };
192  template<> static auto toTD(const WeakString &)->std::string;
193  };
194 };
195 };
static auto toTR(const T &val, const _Args &...args) -> std::string
Get a string represents a <tr> tag.
static auto toTH(const T &val, const _Args &...args) -> std::string
Get a string represents a <th> tag.
Definition: IHTMLEntity.hpp:83
virtual auto toHTML() const -> std::string=0
Get an html string represents the Entity.
An interface supporting conversion to html.
Definition: IHTMLEntity.hpp:39
A string class only references characeters, reference only.
Definition: WeakString.hpp:35