variant.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright (c) 2017-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/utils/datetime.hpp>
  24. #include <xlnt/utils/variant.hpp>
  25. namespace xlnt {
  26. variant::variant()
  27. : type_(type::null)
  28. {
  29. }
  30. variant::variant(const std::string &value)
  31. : type_(type::lpstr),
  32. lpstr_value_(value)
  33. {
  34. }
  35. variant::variant(const char *value)
  36. : variant(std::string(value))
  37. {
  38. }
  39. variant::variant(int32_t value)
  40. : type_(type::i4),
  41. i4_value_(value)
  42. {
  43. }
  44. variant::variant(bool value)
  45. : type_(type::boolean),
  46. i4_value_(value ? 1 : 0)
  47. {
  48. }
  49. variant::variant(const datetime &value)
  50. : type_(type::date),
  51. lpstr_value_(value.to_iso_string())
  52. {
  53. }
  54. variant::variant(const std::initializer_list<int> &value)
  55. : type_(type::vector)
  56. {
  57. for (const auto &v : value)
  58. {
  59. vector_value_.emplace_back(v);
  60. }
  61. }
  62. variant::variant(const std::vector<int> &value)
  63. : type_(type::vector)
  64. {
  65. for (const auto &v : value)
  66. {
  67. vector_value_.emplace_back(v);
  68. }
  69. }
  70. variant::variant(const std::initializer_list<const char *> &value)
  71. : type_(type::vector)
  72. {
  73. for (const auto &v : value)
  74. {
  75. vector_value_.emplace_back(v);
  76. }
  77. }
  78. variant::variant(const std::vector<const char *> &value)
  79. : type_(type::vector)
  80. {
  81. for (const auto &v : value)
  82. {
  83. vector_value_.emplace_back(v);
  84. }
  85. }
  86. variant::variant(const std::initializer_list<std::string> &value)
  87. : type_(type::vector)
  88. {
  89. for (const auto &v : value)
  90. {
  91. vector_value_.emplace_back(v);
  92. }
  93. }
  94. variant::variant(const std::vector<std::string> &value)
  95. : type_(type::vector)
  96. {
  97. for (const auto &v : value)
  98. {
  99. vector_value_.emplace_back(v);
  100. }
  101. }
  102. variant::variant(const std::vector<variant> &value)
  103. : type_(type::vector)
  104. {
  105. for (const auto &v : value)
  106. {
  107. vector_value_.emplace_back(v);
  108. }
  109. }
  110. bool variant::operator==(const variant &rhs) const
  111. {
  112. if (type_ != rhs.type_)
  113. {
  114. return false;
  115. }
  116. switch (type_)
  117. {
  118. case type::vector:
  119. return vector_value_ == rhs.vector_value_;
  120. case type::i4:
  121. case type::boolean:
  122. return i4_value_ == rhs.i4_value_;
  123. case type::date:
  124. case type::lpstr:
  125. return lpstr_value_ == rhs.lpstr_value_;
  126. case type::null:
  127. return true;
  128. }
  129. return false;
  130. }
  131. bool variant::is(type t) const
  132. {
  133. return type_ == t;
  134. }
  135. template <>
  136. XLNT_API std::string variant::get() const
  137. {
  138. return lpstr_value_;
  139. }
  140. template <>
  141. XLNT_API std::vector<variant> variant::get() const
  142. {
  143. return vector_value_;
  144. }
  145. template <>
  146. XLNT_API bool variant::get() const
  147. {
  148. return i4_value_ != 0;
  149. }
  150. template <>
  151. XLNT_API std::int32_t variant::get() const
  152. {
  153. return i4_value_;
  154. }
  155. template <>
  156. XLNT_API datetime variant::get() const
  157. {
  158. return datetime::from_iso_string(lpstr_value_);
  159. }
  160. variant::type variant::value_type() const
  161. {
  162. return type_;
  163. }
  164. } // namespace xlnt