Value.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file Value.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2016 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef COMMON_VALUE
  26. #define COMMON_VALUE 1
  27. #include <boost/serialization/serialization.hpp>
  28. #include <boost/serialization/array.hpp>
  29. #include <cstring>
  30. #include <typeinfo>
  31. #include <iostream>
  32. namespace paradevs { namespace common {
  33. class Value
  34. {
  35. public:
  36. Value() : _content(nullptr), _size(0)
  37. { }
  38. template < typename T >
  39. Value(T value)
  40. { assign(&value, sizeof(T), typeid(T).hash_code()); }
  41. Value(void* content, size_t size)
  42. { assign(content, size, typeid(void*).hash_code()); }
  43. Value(const char* value, unsigned int size)
  44. { assign(value, size * sizeof(char), typeid(char*).hash_code()); }
  45. Value(const Value& value)
  46. {
  47. if (value._content) {
  48. assign(value._content, value._size, value._type_id);
  49. } else {
  50. _content = nullptr;
  51. _size = 0;
  52. _type_id = 0;
  53. }
  54. }
  55. virtual ~Value()
  56. { if (_content) delete _content; }
  57. bool empty() const
  58. { return _content == nullptr; }
  59. template < typename T >
  60. T get_content() const
  61. {
  62. assert(_type_id == typeid(T).hash_code());
  63. return *(T*)(_content);
  64. }
  65. void operator=(const Value& value)
  66. {
  67. if (_content) {
  68. delete _content;
  69. }
  70. if (value._content) {
  71. assign(value._content, value._size, value._type_id);
  72. } else {
  73. _content = nullptr;
  74. _size = 0;
  75. _type_id = 0;
  76. }
  77. }
  78. std::string to_string() const
  79. {
  80. return std::string();
  81. }
  82. private:
  83. void assign(const void* content, size_t size, size_t type_id)
  84. {
  85. _content = new char[size];
  86. std::memcpy(_content, content, size);
  87. _size = size;
  88. _type_id = type_id;
  89. }
  90. friend class boost::serialization::access;
  91. template<class Archive>
  92. void serialize(Archive & ar, const unsigned int version)
  93. {
  94. (void) version;
  95. ar & _size;
  96. if (Archive::is_loading::value) {
  97. assert(_content == nullptr);
  98. _content = new char[_size];
  99. }
  100. ar & boost::serialization::make_array < char >(_content, _size);
  101. ar & _type_id;
  102. }
  103. char* _content;
  104. size_t _size;
  105. size_t _type_id;
  106. };
  107. } } // namespace paradevs common
  108. #endif