variant.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef _SQL_VARIANT_H_
  31. #define _SQL_VARIANT_H_
  32. #include <string>
  33. #include <map>
  34. #include <list>
  35. #include <algorithm>
  36. #include <typeinfo>
  37. #include "build_config.h"
  38. #include "sqlstring.h"
  39. #include "exception.h"
  40. namespace sql
  41. {
  42. class BaseVariantImpl
  43. {
  44. public:
  45. BaseVariantImpl (void *ptr, sql::SQLString vtype) :
  46. cvptr(ptr),
  47. vTypeName(vtype)
  48. {}
  49. virtual ~BaseVariantImpl() {
  50. cvptr=NULL;
  51. }
  52. virtual BaseVariantImpl* Clone()=0;
  53. template<class T>
  54. T* get() const {
  55. if (typeid(T).name() == typeid(void).name()) {
  56. return static_cast< T * > (cvptr);
  57. }
  58. if ((vTypeName == typeid(std::string).name() &&
  59. typeid(T).name() == typeid(sql::SQLString).name()) ||
  60. (vTypeName == typeid(sql::SQLString).name() &&
  61. typeid(T).name() == typeid(std::string).name()) ||
  62. (vTypeName == typeid(std::map< std::string, std::string >).name() &&
  63. typeid(T).name() ==
  64. typeid(std::map< sql::SQLString, sql::SQLString >).name()) ||
  65. (vTypeName ==
  66. typeid(std::map< sql::SQLString, sql::SQLString >).name() &&
  67. typeid(T).name() ==
  68. typeid(std::map< std::string, std::string >).name()) ||
  69. (vTypeName == typeid(std::list< std::string >).name() &&
  70. typeid(T).name() ==
  71. typeid(std::list< sql::SQLString >).name()) ||
  72. (vTypeName ==
  73. typeid(std::list< sql::SQLString >).name() &&
  74. typeid(T).name() ==
  75. typeid(std::list< std::string >).name()))
  76. {
  77. return static_cast< T * > (cvptr);
  78. }
  79. if (typeid(T).name() != vTypeName) {
  80. throw sql::InvalidArgumentException("Variant type doesn't match.");
  81. }
  82. return static_cast< T * > (cvptr);
  83. }
  84. protected:
  85. void *cvptr;
  86. sql::SQLString vTypeName;
  87. };
  88. template<class T>
  89. class VariantImpl : public BaseVariantImpl
  90. {
  91. public:
  92. VariantImpl(T i) : BaseVariantImpl(new T(i), typeid(i).name()) {}
  93. ~VariantImpl() {
  94. destroy_content();
  95. }
  96. VariantImpl(VariantImpl& that) : BaseVariantImpl(that) {
  97. copy_content(that);
  98. }
  99. VariantImpl& operator=(VariantImpl& that) {
  100. if (this != &that) {
  101. destroy_content();
  102. if (cvptr == NULL) {
  103. copy_content(that);
  104. }
  105. }
  106. return *this;
  107. }
  108. virtual VariantImpl* Clone() {
  109. return new VariantImpl(*this);
  110. }
  111. private:
  112. void destroy_content() {
  113. T *tmp=static_cast< T * >(cvptr);
  114. if (tmp) {
  115. delete tmp;
  116. cvptr=NULL;
  117. }
  118. }
  119. void copy_content(BaseVariantImpl& that) {
  120. cvptr=new T (*(static_cast< T * > (that.get< void >())));
  121. }
  122. };
  123. template<class T>
  124. class VariantMap : public BaseVariantImpl
  125. {
  126. public:
  127. VariantMap(T i) : BaseVariantImpl(new T(i), typeid(i).name()) {}
  128. ~VariantMap() {
  129. destroy_content();
  130. }
  131. VariantMap(VariantMap& that) : BaseVariantImpl(that) {
  132. if (this != &that) {
  133. copy_content(that);
  134. }
  135. }
  136. VariantMap& operator=(VariantMap& that) {
  137. if (this != &that) {
  138. destroy_content();
  139. copy_content(that);
  140. }
  141. return *this;
  142. }
  143. virtual VariantMap* Clone() {
  144. return new VariantMap(*this);
  145. }
  146. private:
  147. void destroy_content() {
  148. T *tmp=static_cast< T *> (cvptr);
  149. if (tmp) {
  150. tmp->clear();
  151. delete tmp;
  152. cvptr=NULL;
  153. }
  154. }
  155. void copy_content(VariantMap& var) {
  156. T *tmp=static_cast< T *> (var.cvptr);
  157. if (tmp) {
  158. cvptr=new T();
  159. typename T::const_iterator cit=tmp->begin();
  160. while(cit != tmp->end()) {
  161. (static_cast< T * >(cvptr))->insert(
  162. std::make_pair(sql::SQLString(cit->first),
  163. sql::SQLString(cit->second)));
  164. ++cit;
  165. }
  166. }
  167. }
  168. };
  169. template<class T>
  170. class VariantList : public BaseVariantImpl
  171. {
  172. public:
  173. VariantList(T i) : BaseVariantImpl(new T(i), typeid(i).name()) {}
  174. ~VariantList() {
  175. destroy_content();
  176. }
  177. VariantList(VariantList& that) : BaseVariantImpl(that) {
  178. if (this != &that) {
  179. copy_content(that);
  180. }
  181. }
  182. VariantList& operator=(VariantList& that) {
  183. if (this != &that) {
  184. destroy_content();
  185. copy_content(that);
  186. }
  187. return *this;
  188. }
  189. virtual VariantList* Clone() {
  190. return new VariantList(*this);
  191. }
  192. private:
  193. void destroy_content()
  194. {
  195. T *tmp=static_cast< T *> (cvptr);
  196. if (tmp) {
  197. tmp->clear();
  198. delete tmp;
  199. cvptr=NULL;
  200. }
  201. }
  202. void copy_content(VariantList& var)
  203. {
  204. T *tmp=static_cast< T *> (var.cvptr);
  205. if (tmp) {
  206. cvptr=new T();
  207. typename T::const_iterator cit=tmp->begin();
  208. while(cit != tmp->end()) {
  209. (static_cast< T * >(cvptr))->push_back(sql::SQLString(*cit));
  210. ++cit;
  211. }
  212. }
  213. }
  214. };
  215. class CPPCONN_PUBLIC_FUNC Variant
  216. {
  217. public:
  218. Variant(const int &i=0) :
  219. variant(new VariantImpl< int >(i)) {}
  220. Variant(const double &i) :
  221. variant(new VariantImpl< double >(i)) {}
  222. Variant(const bool &i) :
  223. variant(new VariantImpl< bool >(i)) {}
  224. Variant(const char* i) :
  225. variant(new VariantImpl< sql::SQLString >(i)) {}
  226. Variant(const std::string &i) :
  227. variant(new VariantImpl< sql::SQLString >(i)) {}
  228. Variant(const sql::SQLString &i) :
  229. variant(new VariantImpl< sql::SQLString >(i)) {}
  230. Variant(const std::list< std::string > &i) :
  231. variant(new VariantList< std::list < std::string > >(i)) {}
  232. Variant(const std::list< sql::SQLString > &i) :
  233. variant(new VariantList< std::list < sql::SQLString > >(i)) {}
  234. Variant(const std::map< std::string, std::string > &i) :
  235. variant(new VariantMap< std::map< std::string, std::string > >(i)) {}
  236. Variant(const std::map< sql::SQLString, sql::SQLString > &i) :
  237. variant(new VariantMap< std::map< sql::SQLString, sql::SQLString > >(i)) {}
  238. ~Variant() {
  239. if (variant) {
  240. delete variant;
  241. variant=0;
  242. }
  243. }
  244. Variant(const Variant& that) {
  245. if (this != &that) {
  246. variant=that.variant->Clone();
  247. }
  248. }
  249. Variant& operator=(const Variant& that) {
  250. if (this != &that) {
  251. delete variant;
  252. variant=that.variant->Clone();
  253. }
  254. return *this;
  255. }
  256. template<class T>
  257. T* get() const {
  258. return variant->get<T>();
  259. }
  260. private:
  261. BaseVariantImpl *variant;
  262. };
  263. } /* namespace sql */
  264. #endif /* _SQL_VARIANT_H_ */