Branch data TLA Line data Source code
1 : : // Copyright (c) 2017-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #include <xlnt/utils/datetime.hpp>
26 : : #include <xlnt/utils/variant.hpp>
27 : :
28 : : namespace xlnt {
29 : :
30 :CBC 1 : variant::variant()
31 : 1 : : type_(type::null)
32 : : {
33 : 1 : }
34 : :
35 : 4129 : variant::variant(const std::string &value)
36 : 4129 : : type_(type::lpstr),
37 [ + ]: 4129 : lpstr_value_(value)
38 : : {
39 : 4129 : }
40 : :
41 : 2126 : variant::variant(const char *value)
42 [ + + ]: 2126 : : variant(std::string(value))
43 : : {
44 : 2126 : }
45 : :
46 : 923 : variant::variant(int32_t value)
47 : 923 : : type_(type::i4),
48 : 923 : i4_value_(value)
49 : : {
50 : 923 : }
51 : :
52 : 1024 : variant::variant(bool value)
53 : 1024 : : type_(type::boolean),
54 [ - + ]: 1024 : i4_value_(value ? 1 : 0)
55 : : {
56 : 1024 : }
57 : :
58 : 512 : variant::variant(const datetime &value)
59 : 512 : : type_(type::date),
60 [ + ]: 512 : lpstr_value_(value.to_iso_string())
61 : : {
62 : 512 : }
63 : :
64 : : template<typename T>
65 : 1304 : void variant::construct_vector_internal(const T &vec)
66 : : {
67 : 1304 : vector_value_.reserve(vec.size());
68 [ + + ]: 3412 : for (const auto &v : vec)
69 : : {
70 [ + ]: 2108 : vector_value_.emplace_back(v);
71 : : }
72 : 1304 : }
73 : :
74 : 1 : variant::variant(const std::initializer_list<std::int32_t> &value)
75 : 1 : : type_(type::vector)
76 : : {
77 [ + ]: 1 : construct_vector_internal(value);
78 : 1 : }
79 : :
80 : 1 : variant::variant(const std::vector<int> &value)
81 : 1 : : type_(type::vector)
82 : : {
83 [ + ]: 1 : construct_vector_internal(value);
84 : 1 : }
85 : :
86 : 256 : variant::variant(const std::initializer_list<const char *> &value)
87 : 256 : : type_(type::vector)
88 : : {
89 [ + ]: 256 : construct_vector_internal(value);
90 : 256 : }
91 : :
92 :UBC 0 : variant::variant(const std::vector<const char *> &value)
93 : 0 : : type_(type::vector)
94 : : {
95 [ # ]: 0 : construct_vector_internal(value);
96 : 0 : }
97 : :
98 :CBC 1 : variant::variant(const std::initializer_list<std::string> &value)
99 : 1 : : type_(type::vector)
100 : : {
101 [ + ]: 1 : construct_vector_internal(value);
102 : 1 : }
103 : :
104 : 328 : variant::variant(const std::vector<std::string> &value)
105 : 328 : : type_(type::vector)
106 : : {
107 [ + ]: 328 : construct_vector_internal(value);
108 : 328 : }
109 : :
110 : 717 : variant::variant(const std::vector<variant> &value)
111 : 717 : : type_(type::vector)
112 : : {
113 [ + ]: 717 : construct_vector_internal(value);
114 : 717 : }
115 : :
116 : 118 : bool variant::operator==(const variant &rhs) const
117 : : {
118 [ - + ]: 118 : if (type_ != rhs.type_)
119 : : {
120 :UBC 0 : return false;
121 : : }
122 [ + + + - :CBC 118 : switch (type_)
- ]
123 : : {
124 : 6 : case type::vector:
125 : 6 : return vector_value_ == rhs.vector_value_;
126 : 18 : case type::i4:
127 : : case type::boolean:
128 : 18 : return i4_value_ == rhs.i4_value_;
129 : 94 : case type::date:
130 : : case type::lpstr:
131 : 94 : return lpstr_value_ == rhs.lpstr_value_;
132 :UBC 0 : case type::null:
133 : 0 : return true;
134 : : }
135 : 0 : return false;
136 : : }
137 : :
138 : 0 : bool variant::operator!=(const variant &rhs) const
139 : : {
140 : 0 : return !(*this == rhs);
141 : : }
142 : :
143 :CBC 6 : bool variant::is(type t) const
144 : : {
145 : 6 : return type_ == t;
146 : : }
147 : :
148 : : template <>
149 : 517 : std::string variant::get() const
150 : : {
151 : 517 : return lpstr_value_;
152 : : }
153 : :
154 : : template <>
155 : 56 : bool variant::get() const
156 : : {
157 : 56 : return i4_value_ != 0;
158 : : }
159 : :
160 : : template <>
161 : 68 : std::int32_t variant::get() const
162 : : {
163 : 68 : return i4_value_;
164 : : }
165 : :
166 : : template <>
167 : 20 : datetime variant::get() const
168 : : {
169 : 20 : return datetime::from_iso_string(lpstr_value_);
170 : : }
171 : :
172 : : template <>
173 : 78 : std::vector<variant> variant::get() const
174 : : {
175 : 78 : return vector_value_;
176 : : }
177 : :
178 : : template<typename T>
179 : 4 : std::vector<T> variant::get_vector_internal() const
180 : : {
181 : : // According to the specification, "Vector contents shall be of uniform type"
182 : 4 : std::vector<T> vec;
183 [ + ]: 4 : vec.reserve(vector_value_.size());
184 [ + + ]: 24 : for (const variant &var : vector_value_)
185 : : {
186 [ + + ]: 20 : vec.emplace_back(var.get<T>());
187 : : }
188 : 4 : return vec;
189 :UBC 0 : }
190 : :
191 : : template <>
192 : 0 : std::vector<bool> variant::get() const
193 : : {
194 : 0 : return get_vector_internal<bool>();
195 : : }
196 : :
197 : : template <>
198 :CBC 2 : std::vector<std::int32_t> variant::get() const
199 : : {
200 : 2 : return get_vector_internal<std::int32_t>();
201 : : }
202 : :
203 : : template <>
204 : 2 : std::vector<std::string> variant::get() const
205 : : {
206 : 2 : return get_vector_internal<std::string>();
207 : : }
208 : :
209 : : template <>
210 :UBC 0 : std::vector<datetime> variant::get() const
211 : : {
212 : 0 : return get_vector_internal<datetime>();
213 : : }
214 : :
215 :CBC 891 : variant::type variant::value_type() const
216 : : {
217 : 891 : return type_;
218 : : }
219 : :
220 : : } // namespace xlnt
|