reflect.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. // vi:set ts=4 sts=4 sw=4 noet :
  3. //
  4. // Copyright 2010, 2011 wkhtmltopdf authors
  5. //
  6. // This file is part of wkhtmltopdf.
  7. //
  8. // wkhtmltopdf is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // wkhtmltopdf is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.
  20. #ifndef __REFLECT_HH__
  21. #define __REFLECT_HH__
  22. #if defined(_MSC_VER) && _MSC_VER>=1600
  23. #define typeof decltype
  24. #endif
  25. #include "loadsettings.hh"
  26. #include "websettings.hh"
  27. #include <QStringList>
  28. #include <cstring>
  29. #include "dllbegin.inc"
  30. namespace wkhtmltopdf {
  31. namespace settings {
  32. #define WKHTMLTOPDF_REFLECT(name) ReflectClass::add(#name, new ReflectImpl<typeof(c.name)> (c.name));
  33. class DLL_LOCAL Reflect {
  34. public:
  35. virtual QString get(const char * name) = 0;
  36. virtual bool set(const char * name, const QString & value) = 0;
  37. virtual ~Reflect() {};
  38. };
  39. class DLL_LOCAL ReflectSimple: public Reflect {
  40. public:
  41. virtual QString get() = 0;
  42. virtual void set(const QString & value, bool * ok) = 0;
  43. virtual QString get(const char * name) {return name[0]=='\0'?get():QString();}
  44. virtual bool set(const char * name, const QString & value);
  45. };
  46. class DLL_LOCAL ReflectClass: public Reflect {
  47. public:
  48. QMap<QString, Reflect *> elms;
  49. void add(const char * name, Reflect * r) {elms[name] = r;}
  50. QString get(const char * name);
  51. bool set(const char * name, const QString & value);
  52. ~ReflectClass();
  53. };
  54. template <typename X>
  55. class DLL_LOCAL ReflectImpl {
  56. private:
  57. ReflectImpl();
  58. };
  59. template<>
  60. struct DLL_LOCAL ReflectImpl<bool>: public ReflectSimple {
  61. bool & b;
  62. ReflectImpl(bool & _): b(_) {}
  63. QString get() {return b?"true":"false";}
  64. void set(const QString & value, bool * ok) {
  65. if (value == "true") b=true;
  66. else if (value == "false") b=false;
  67. else *ok=false;
  68. *ok=true;
  69. }
  70. };
  71. template<>
  72. struct DLL_LOCAL ReflectImpl<QString>: public ReflectSimple {
  73. QString & s;
  74. ReflectImpl(QString & _): s(_) {}
  75. QString get() {return s;}
  76. void set(const QString & value, bool * ok) {s=value; *ok=true;}
  77. };
  78. template<>
  79. struct DLL_LOCAL ReflectImpl<int>: public ReflectSimple {
  80. int & i;
  81. ReflectImpl(int & _): i(_) {}
  82. QString get() {return QString::number(i);}
  83. void set(const QString & value, bool * ok) {i = value.toInt(ok);}
  84. };
  85. template<>
  86. struct DLL_LOCAL ReflectImpl<float>: public ReflectSimple {
  87. float & f;
  88. ReflectImpl(float & _): f(_) {}
  89. QString get() {return QString::number(f);}
  90. void set(const QString & value, bool * ok) {f = value.toDouble(ok);}
  91. };
  92. template<>
  93. struct DLL_LOCAL ReflectImpl< QPair<QString, QString> >: public ReflectSimple {
  94. QPair<QString, QString> & p;
  95. ReflectImpl(QPair<QString, QString> & _): p(_) {};
  96. QString get() {
  97. return p.first + "\n" + p.second;
  98. }
  99. void set(const QString & value, bool * ok) {
  100. QStringList l = value.split("\n");
  101. if (l.size() != 2) {*ok=false; return;}
  102. *ok = true;
  103. p.first = l[0];
  104. p.second = l[1];
  105. }
  106. };
  107. template<typename X>
  108. struct DLL_LOCAL ReflectImpl< QList< X> >: public Reflect {
  109. QList<X> & l;
  110. ReflectImpl(QList<X> & _): l(_) {}
  111. bool parse(const char * name, int & parmsize, int & next, int & elm) {
  112. elm=-1;
  113. parmsize = -1;
  114. if (name[0] == '[') {
  115. next = 0;
  116. while (name[next] != '\0' && name[next] != ']') ++next;
  117. bool ok=true;
  118. elm = QString::fromLocal8Bit(name+1,next-1).toInt(&ok);
  119. while (name[next] == ']' || name[next] == '.') ++next;
  120. return ok;
  121. }
  122. parmsize = 0;
  123. while (name[parmsize] != '\0' && name[parmsize] != '.' && name[parmsize] != '[') ++parmsize;
  124. next = parmsize;
  125. if (name[next] == '.') ++next;
  126. return true;
  127. }
  128. virtual QString get(const char * name) {
  129. int ps, next, elm;
  130. if (!strcmp(name,"size") || !strcmp(name,"length") || !strcmp(name,"count")) return QString::number(l.size());
  131. if (!parse(name, ps, next, elm)) return QString();
  132. if (ps > 0 && !l.isEmpty() && !strncmp(name, "first", ps)) elm = 0;
  133. if (ps > 0 && !l.isEmpty() && !strncmp(name, "last", ps)) elm = l.size() - 1;
  134. if (elm < 0 || elm >= l.size()) return QString();
  135. ReflectImpl<X> impl(l[elm]);
  136. return static_cast<Reflect*>(&impl)->get(name+next);
  137. };
  138. virtual bool set(const char * name, const QString & value) {
  139. int ps, next, elm;
  140. if (!strcmp(name,"clear"))
  141. l.clear();
  142. else if (!strcmp(name,"append"))
  143. l.append(X());
  144. else if (!strcmp(name,"prepend"))
  145. l.prepend(X());
  146. else if (!strcmp(name,"delete")) {
  147. bool ok = true;
  148. int idx = value.toInt(&ok);
  149. if (ok && idx >= 0 && idx < l.size()) {
  150. l.removeAt(idx);
  151. return true;
  152. }
  153. return false;
  154. }
  155. else {
  156. if (!parse(name, ps, next, elm)) return false;
  157. if (ps > 0 && !l.isEmpty() && !strncmp(name, "first", ps)) elm = 0;
  158. if (ps > 0 && !l.isEmpty() && !strncmp(name, "last", ps)) elm = l.size() - 1;
  159. ReflectImpl<X> impl(l[elm]);
  160. return static_cast<Reflect *>(&impl)->set(name+next, value);
  161. }
  162. return true;
  163. }
  164. };
  165. template<>
  166. struct DLL_LOCAL ReflectImpl<LoadPage::LoadErrorHandling>: public ReflectSimple {
  167. LoadPage::LoadErrorHandling & l;
  168. ReflectImpl(LoadPage::LoadErrorHandling & _): l(_) {}
  169. QString get() {return loadErrorHandlingToStr(l);}
  170. void set(const QString & value, bool * ok) {l = strToLoadErrorHandling(value.toUtf8().constData(), ok);}
  171. };
  172. template<>
  173. struct DLL_LOCAL ReflectImpl<Proxy>: public ReflectSimple {
  174. Proxy & p;
  175. ReflectImpl(Proxy & _): p(_) {}
  176. QString get() {return proxyToStr(p);}
  177. void set(const QString & value, bool * ok) {p = strToProxy(value.toUtf8().constData(), ok);}
  178. };
  179. template<>
  180. struct DLL_LOCAL ReflectImpl<PostItem>: public ReflectClass {
  181. ReflectImpl(PostItem & c) {
  182. WKHTMLTOPDF_REFLECT(name);
  183. WKHTMLTOPDF_REFLECT(value);
  184. WKHTMLTOPDF_REFLECT(file);
  185. }
  186. };
  187. template <>
  188. struct DLL_LOCAL ReflectImpl<LoadGlobal>: public ReflectClass {
  189. ReflectImpl(LoadGlobal & c);
  190. };
  191. template <>
  192. struct DLL_LOCAL ReflectImpl<LoadPage>: public ReflectClass {
  193. ReflectImpl(LoadPage & c);
  194. };
  195. template <>
  196. struct DLL_LOCAL ReflectImpl<Web>: public ReflectClass {
  197. ReflectImpl(Web & c);
  198. };
  199. }
  200. }
  201. #include "dllend.inc"
  202. #endif //__REFLECT_HH__