• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xlnt-community / xlnt / 677b76fc-3901-4722-b4c8-ce9367a0b564

26 Apr 2025 06:05PM UTC coverage: 81.8% (-0.5%) from 82.27%
677b76fc-3901-4722-b4c8-ce9367a0b564

Pull #79

circleci

m7913d
Disabled samples and benchmarks while generating coverage report

Coverage report should be based on the real unit tests.
Pull Request #79: Publishing coverage to github pages

14023 of 18604 branches covered (75.38%)

11488 of 14044 relevant lines covered (81.8%)

10384.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.77
./source/utils/variant.cpp
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
variant::variant()
1✔
31
    : type_(type::null)
1✔
32
{
33
}
1✔
34

35
variant::variant(const std::string &value)
3,898✔
36
    : type_(type::lpstr),
3,898✔
37
      lpstr_value_(value)
3,898✔
38
{
39
}
3,898✔
40

41
variant::variant(const char *value)
2,008✔
42
    : variant(std::string(value))
2,008✔
43
{
44
}
2,008✔
45

46
variant::variant(int32_t value)
864✔
47
    : type_(type::i4),
864✔
48
      i4_value_(value)
864✔
49
{
50
}
864✔
51

52
variant::variant(bool value)
968✔
53
    : type_(type::boolean),
968✔
54
      i4_value_(value ? 1 : 0)
968!
55
{
56
}
968✔
57

58
variant::variant(const datetime &value)
484✔
59
    : type_(type::date),
484✔
60
      lpstr_value_(value.to_iso_string())
484✔
61
{
62
}
484✔
63

64
template<typename T>
65
void variant::construct_vector_internal(const T &vec)
1,228✔
66
{
67
    vector_value_.reserve(vec.size());
1,228✔
68
    for (const auto &v : vec)
3,195✔
69
    {
70
        vector_value_.emplace_back(v);
1,967✔
71
    }
72
}
1,228✔
73

74
variant::variant(const std::initializer_list<int> &value)
×
75
    : type_(type::vector)
×
76
{
77
    construct_vector_internal(value);
×
78
}
×
79

80
variant::variant(const std::vector<int> &value)
1✔
81
    : type_(type::vector)
1✔
82
{
83
    construct_vector_internal(value);
1✔
84
}
1✔
85

86
variant::variant(const std::initializer_list<const char *> &value)
242✔
87
    : type_(type::vector)
242✔
88
{
89
    construct_vector_internal(value);
242✔
90
}
242✔
91

92
variant::variant(const std::vector<const char *> &value)
×
93
    : type_(type::vector)
×
94
{
95
    construct_vector_internal(value);
×
96
}
×
97

98
variant::variant(const std::initializer_list<std::string> &value)
×
99
    : type_(type::vector)
×
100
{
101
    construct_vector_internal(value);
×
102
}
×
103

104
variant::variant(const std::vector<std::string> &value)
308✔
105
    : type_(type::vector)
308✔
106
{
107
    construct_vector_internal(value);
308✔
108
}
308✔
109

110
variant::variant(const std::vector<variant> &value)
677✔
111
    : type_(type::vector)
677✔
112
{
113
    construct_vector_internal(value);
677✔
114
}
677✔
115

116
bool variant::operator==(const variant &rhs) const
106✔
117
{
118
    if (type_ != rhs.type_)
106!
119
    {
120
        return false;
×
121
    }
122
    switch (type_)
106!
123
    {
124
    case type::vector:
4✔
125
        return vector_value_ == rhs.vector_value_;
4✔
126
    case type::i4:
12✔
127
    case type::boolean:
128
        return i4_value_ == rhs.i4_value_;
12✔
129
    case type::date:
90✔
130
    case type::lpstr:
131
        return lpstr_value_ == rhs.lpstr_value_;
90✔
132
    case type::null:
×
133
        return true;
×
134
    }
135
    return false;
×
136
}
137

138
bool variant::operator!=(const variant &rhs) const
×
139
{
140
    return !(*this == rhs);
×
141
}
142

143
bool variant::is(type t) const
6✔
144
{
145
    return type_ == t;
6✔
146
}
147

148
template <>
149
std::string variant::get() const
499✔
150
{
151
    return lpstr_value_;
499✔
152
}
153

154
template <>
155
bool variant::get() const
48✔
156
{
157
    return i4_value_ != 0;
48✔
158
}
159

160
template <>
161
std::int32_t variant::get() const
56✔
162
{
163
    return i4_value_;
56✔
164
}
165

166
template <>
167
datetime variant::get() const
16✔
168
{
169
    return datetime::from_iso_string(lpstr_value_);
16✔
170
}
171

172
template <>
173
std::vector<variant> variant::get() const
74✔
174
{
175
    return vector_value_;
74✔
176
}
177

178
template<typename T>
179
std::vector<T> variant::get_vector_internal() const
2✔
180
{
181
    // According to the specification, "Vector contents shall be of uniform type"
182
    std::vector<T> vec;
2✔
183
    vec.reserve(vector_value_.size());
2✔
184
    for (const variant &var : vector_value_)
10✔
185
    {
186
        vec.emplace_back(var.get<T>());
8✔
187
    }
188
    return vec;
2✔
189
}
×
190

191
template <>
192
std::vector<bool> variant::get() const
×
193
{
194
    return get_vector_internal<bool>();
×
195
}
196

197
template <>
198
std::vector<std::int32_t> variant::get() const
1✔
199
{
200
    return get_vector_internal<std::int32_t>();
1✔
201
}
202

203
template <>
204
std::vector<std::string> variant::get() const
1✔
205
{
206
    return get_vector_internal<std::string>();
1✔
207
}
208

209
template <>
210
std::vector<datetime> variant::get() const
×
211
{
212
    return get_vector_internal<datetime>();
×
213
}
214

215
variant::type variant::value_type() const
849✔
216
{
217
    return type_;
849✔
218
}
219

220
} // namespace xlnt
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc