fork download
  1. #include <map>
  2. #include <vector>
  3. #include <string>
  4. #include <iostream>
  5. #include <cstring>
  6. #include <utility>
  7. #include <functional>
  8.  
  9. namespace json {
  10.  
  11. class value;
  12.  
  13. typedef std::vector<value> array;
  14. typedef std::map<std::string, value> object;
  15. typedef std::string string;
  16.  
  17. enum class type
  18. {
  19. null, // it is not realy a type but a value
  20. boolean,
  21. integer,
  22. number,
  23. string,
  24. object,
  25. array
  26. };
  27.  
  28. class value
  29. {
  30. public:
  31. value();
  32. ~value();
  33.  
  34. value(double number);
  35. value(int integer);
  36. value(bool b);
  37. value(char const* str);
  38. value(json::string const& str);
  39. value(json::array const& arr);
  40. value(json::object const& obj);
  41.  
  42. value(json::string&& str);
  43. value(json::array&& arr);
  44. value(json::object&& obj);
  45.  
  46. value(value const& val);
  47. value(value&& val);
  48.  
  49. // assignment operators
  50. value& operator=(double number);
  51. value& operator=(int integer);
  52. value& operator=(bool b);
  53. value& operator=(json::string const& str);
  54. value& operator=(json::array const& arr);
  55. value& operator=(json::object const& obj);
  56.  
  57. value& operator=(json::string&& str);
  58. value& operator=(json::array&& arr);
  59. value& operator=(json::object&& obj);
  60.  
  61. value& operator=(value const& val);
  62. value& operator=(value&& val);
  63.  
  64. void clear();
  65.  
  66. public:
  67. json::type type;
  68. union
  69. {
  70. int integer;
  71. double number;
  72. bool boolean;
  73. json::string* str;
  74. json::array* arr;
  75. json::object* obj;
  76. };
  77. };
  78.  
  79. }
  80.  
  81.  
  82. namespace json {
  83.  
  84.  
  85. value::value() : type(json::type::null)
  86. {
  87. std::cerr << "default constructor null" << '\n';
  88. }
  89.  
  90. value::~value()
  91. {
  92. std::cerr << "destructor" << '\n';
  93. clear();
  94. }
  95.  
  96. value::value(double number) : type(json::type::number), number(number)
  97. {
  98. std::cerr << "copy constructor number" << '\n';
  99. }
  100.  
  101. value::value(int integer) : type(json::type::integer), integer(integer)
  102. {
  103. std::cerr << "copy constructor integer" << '\n';
  104. }
  105.  
  106. value::value(bool b) : type(json::type::boolean), boolean(b)
  107. {
  108. std::cerr << "copy constructor boolean" << '\n';
  109. }
  110.  
  111. value::value(char const* str) : value(json::string(str))
  112. {
  113. }
  114.  
  115. value::value(json::string const& str) : type(json::type::string), str(new json::string(str))
  116. {
  117. std::cerr << "copy constructor string" << '\n';
  118. }
  119.  
  120. value::value(json::array const& arr) : type(json::type::array), arr(new json::array(arr))
  121. {
  122. std::cerr << "copy constructor array" << '\n';
  123. }
  124.  
  125. value::value(json::object const& obj) : type(json::type::object), obj(new json::object(obj))
  126. {
  127. std::cerr << "copy constructor object" << '\n';
  128. }
  129.  
  130. value::value(value const& val)
  131. {
  132. std::cerr << "copy constructor value" << '\n';
  133. switch (val.type)
  134. {
  135. case json::type::array:
  136. {
  137. arr = new json::array(*val.arr);
  138. type = json::type::array;
  139. }
  140. break;
  141. case json::type::string:
  142. {
  143. str = new json::string(*val.str);
  144. type = json::type::string;
  145. }
  146. break;
  147. case json::type::object:
  148. {
  149. obj = new json::object(*val.obj);
  150. type = json::type::object;
  151. }
  152. break;
  153. default:
  154. std::memcpy(this, &val, sizeof(val));
  155. }
  156. }
  157.  
  158. value::value(json::string && str)
  159. : type(json::type::string)
  160. , str(new json::string(std::move(str)))
  161. {
  162. std::cerr << "move constructor string" << '\n';
  163. }
  164.  
  165. value::value(json::array && arr)
  166. : type(json::type::array)
  167. , arr(new json::array(std::move(arr)))
  168. {
  169. std::cerr << "move constructor array" << '\n';
  170. }
  171.  
  172. value::value(json::object && obj)
  173. : type(json::type::object)
  174. , obj(new json::object(std::move(obj)))
  175. {
  176. std::cerr << "move constructor object" << '\n';
  177. }
  178.  
  179. value::value(value && val)
  180. {
  181. std::cerr << "move constructor value" << '\n';
  182. std::memcpy(this, &val, sizeof(val));
  183. val.type = json::type::null;
  184. }
  185.  
  186. value& value::operator=(double number)
  187. {
  188. std::cerr << "assignment number" << '\n';
  189. clear();
  190. type = json::type::number;
  191. this->number = number;
  192. return *this;
  193. }
  194. value& value::operator=(int integer)
  195. {
  196. std::cerr << "assignment integer" << '\n';
  197. clear();
  198. type = json::type::integer;
  199. this->integer = integer;
  200. return *this;
  201. }
  202.  
  203. value& value::operator=(bool b)
  204. {
  205. std::cerr << "assignment bool" << '\n';
  206. clear();
  207. type = json::type::integer;
  208. this->integer = integer;
  209. return *this;
  210. }
  211.  
  212. value& value::operator=(json::string const& str)
  213. {
  214. std::cerr << "assignment string" << '\n';
  215. clear();
  216. this->str = new json::string(str);
  217. type = json::type::string;
  218. return *this;
  219. }
  220.  
  221. value& value::operator=(json::array const& arr)
  222. {
  223. std::cerr << "assignment array" << '\n';
  224. clear();
  225. this->arr = new json::array(arr);
  226. type = json::type::array;
  227. return *this;
  228. }
  229.  
  230. value& value::operator=(json::object const& obj)
  231. {
  232. std::cerr << "assignment object" << '\n';
  233. clear();
  234. this->obj = new json::object(obj);
  235. type = json::type::object;
  236. return *this;
  237. }
  238.  
  239. value& value::operator=(value const& val)
  240. {
  241. std::cerr << "assignment value" << '\n';
  242. clear();
  243. switch (val.type)
  244. {
  245. case json::type::array:
  246. {
  247. arr = new json::array(*val.arr);
  248. type = json::type::array;
  249. }
  250. break;
  251. case json::type::string:
  252. {
  253. str = new json::string(*val.str);
  254. type = json::type::string;
  255. }
  256. break;
  257. case json::type::object:
  258. {
  259. obj = new json::object(*val.obj);
  260. type = json::type::object;
  261. }
  262. break;
  263. default:
  264. std::memcpy(this, &val, sizeof(val));
  265. }
  266. return *this;
  267. }
  268.  
  269. value& value::operator=(json::string&& str)
  270. {
  271. std::cerr << "move assignment string" << '\n';
  272. clear();
  273. this->str = new json::string(std::move(str));
  274. type = json::type::string;
  275. return *this;
  276. }
  277.  
  278. value& value::operator=(json::array&& arr)
  279. {
  280. std::cerr << "move assignment array" << '\n';
  281. clear();
  282. this->arr = new json::array(std::move(arr));
  283. type = json::type::array;
  284. return *this;
  285. }
  286.  
  287. value& value::operator=(json::object&& obj)
  288. {
  289. std::cerr << "move assignment object" << '\n';
  290. clear();
  291. this->obj = new json::object(std::move(obj));
  292. type = json::type::object;
  293. return *this;
  294. }
  295.  
  296. value& value::operator=(value && val)
  297. {
  298. std::cerr << "move assignment value" << '\n';
  299. clear();
  300. std::memcpy(this, &val, sizeof(val));
  301. val.type = json::type::null;
  302. return *this;
  303. }
  304.  
  305. void value::clear()
  306. {
  307. switch (type)
  308. {
  309. case json::type::array:
  310. {
  311. delete arr;
  312. }
  313. break;
  314. case json::type::string:
  315. {
  316. delete str;
  317. }
  318. break;
  319. case json::type::object:
  320. {
  321. delete obj;
  322. }
  323. break;
  324. }
  325. type = json::type::null;
  326. }
  327.  
  328.  
  329. }
  330.  
  331. int main(int argc, char* argv [])
  332. {
  333. json::string s = "HelloWorld";
  334. json::value v_str = std::move(s);
  335. json::value v_arr = std::move(std::vector<json::value>{std::cref(v_str)});
  336.  
  337. return 0;
  338. }
Success #stdin #stdout #stderr 0s 3036KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
move constructor string
copy constructor value
copy constructor value
move constructor array
destructor
destructor
destructor
destructor