Value.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file artis/context/Value.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2017 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU 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. * This program 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 General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARTIS_CONTEXT_VALUE_HPP
  22. #define __ARTIS_CONTEXT_VALUE_HPP
  23. #include <boost/serialization/serialization.hpp>
  24. #include <boost/serialization/array.hpp>
  25. #include <algorithm>
  26. #include <typeinfo>
  27. #include <cstring>
  28. #if WIN32
  29. #include <string>
  30. #endif
  31. namespace artis { namespace context {
  32. class Value
  33. {
  34. public:
  35. Value() : _content(nullptr), _size(0)
  36. { }
  37. template < typename T >
  38. Value(T value) : _content(nullptr), _size(0)
  39. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  40. Value(void* content, size_t size) : _content(nullptr), _size(0)
  41. { assign(content, size, typeid(void*).hash_code()); }
  42. Value(const char* value, unsigned int size) : _content(nullptr), _size(0)
  43. { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
  44. Value(const Value& value) : _content(nullptr), _size(0)
  45. {
  46. if (value._content) {
  47. assign(value._content, value._size, value._type_id);
  48. }
  49. }
  50. virtual ~Value()
  51. { if (_content) delete[] _content; }
  52. bool empty() const
  53. { return _content == nullptr; }
  54. template < typename T >
  55. void get_content(T& value) const
  56. {
  57. assert(_type_id == typeid(T).hash_code());
  58. value = *(T*)(_content);
  59. }
  60. const Value& operator=(const Value& value)
  61. {
  62. if (_content) delete[] _content;
  63. _content = nullptr;
  64. _size = 0;
  65. if (value._content) {
  66. assign(value._content, value._size, value._type_id);
  67. }
  68. return *this;
  69. }
  70. std::string to_string() const
  71. {
  72. if (is_double()) {
  73. double v;
  74. get_content(v);
  75. return std::to_string(v);
  76. } else if (is_int()) {
  77. int v;
  78. get_content(v);
  79. return std::to_string(v);
  80. } else if (is_bool()) {
  81. bool v;
  82. get_content(v);
  83. return v ? "true" : "false";
  84. } else {
  85. assert(false);
  86. }
  87. }
  88. bool is_bool() const
  89. { return _type_id == typeid(bool).hash_code(); }
  90. bool is_int() const
  91. { return _type_id == typeid(int).hash_code(); }
  92. bool is_double() const
  93. { return _type_id == typeid(double).hash_code(); }
  94. private:
  95. void assign(const void* content, size_t size, size_t type_id)
  96. {
  97. _content = new char[size < 16 ? 16 : size];
  98. std::memcpy(_content, content, size);
  99. _size = size;
  100. _type_id = type_id;
  101. }
  102. friend class boost::serialization::access;
  103. template < class Archive >
  104. void serialize(Archive & ar, const unsigned int version)
  105. {
  106. (void) version;
  107. ar & _size;
  108. if (Archive::is_loading::value) {
  109. assert(_content == nullptr);
  110. _content = new char[_size];
  111. }
  112. ar & boost::serialization::make_array < char >(_content, _size);
  113. ar & _type_id;
  114. }
  115. char* _content;
  116. size_t _size;
  117. size_t _type_id;
  118. };
  119. } }
  120. #endif