Project
Loading...
Searching...
No Matches
Variant.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#include "Framework/Variant.h"
14#include <iostream>
15#include <sstream>
16
17namespace o2::framework
18{
19std::ostream& operator<<(std::ostream& oss, Variant const& val)
20{
21 switch (val.type()) {
23 oss << val.get<int>();
24 break;
26 oss << (int)val.get<int8_t>();
27 break;
29 oss << (int)val.get<int16_t>();
30 break;
32 oss << (int)val.get<uint8_t>();
33 break;
35 oss << (int)val.get<uint16_t>();
36 break;
38 oss << val.get<uint32_t>();
39 break;
41 oss << val.get<uint64_t>();
42 break;
44 oss << val.get<int64_t>();
45 break;
47 oss << val.get<float>();
48 break;
50 oss << val.get<double>();
51 break;
53 oss << val.get<const char*>();
54 break;
56 oss << val.get<bool>();
57 break;
67 break;
69 break;
71 oss << "{}";
72 break;
73 default:
74 oss << "undefined";
75 break;
76 };
77 return oss;
78}
79
80std::string Variant::asString() const
81{
82 std::stringstream ss;
83 ss << *this;
84 return ss.str();
85}
86
87Variant::Variant(const Variant& other) : mType(other.mType)
88{
89 // In case this is an array we need to duplicate it to avoid
90 // double deletion.
91 switch (mType) {
92 case variant_trait_v<const char*>:
93 mSize = other.mSize;
94 variant_helper<const char*>::set(&mStore, other.get<const char*>());
95 return;
96 case variant_trait_v<int*>:
97 mSize = other.mSize;
98 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
99 return;
100 case variant_trait_v<float*>:
101 mSize = other.mSize;
102 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
103 return;
104 case variant_trait_v<double*>:
105 mSize = other.mSize;
106 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
107 return;
108 case variant_trait_v<bool*>:
109 mSize = other.mSize;
110 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
111 return;
112 case variant_trait_v<std::string*>:
113 mSize = other.mSize;
114 variant_helper<std::string*>::set(&mStore, other.get<std::string*>(), mSize);
115 return;
116 default:
117 mStore = other.mStore;
118 mSize = other.mSize;
119 }
120}
121
122Variant::Variant(Variant&& other) noexcept : mType(other.mType)
123{
124 mStore = other.mStore;
125 mSize = other.mSize;
126 switch (mType) {
127 case variant_trait_v<const char*>:
128 *reinterpret_cast<char**>(&(other.mStore)) = nullptr;
129 return;
130 case variant_trait_v<int*>:
131 *reinterpret_cast<int**>(&(other.mStore)) = nullptr;
132 return;
133 case variant_trait_v<float*>:
134 *reinterpret_cast<float**>(&(other.mStore)) = nullptr;
135 return;
136 case variant_trait_v<double*>:
137 *reinterpret_cast<double**>(&(other.mStore)) = nullptr;
138 return;
139 case variant_trait_v<bool*>:
140 *reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
141 return;
142 case variant_trait_v<std::string*>:
143 *reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
144 default:
145 return;
146 }
147}
148
150{
151 // In case we allocated an array, we
152 // should delete it.
153 switch (mType) {
154 case variant_trait_v<const char*>:
155 case variant_trait_v<int*>:
156 case variant_trait_v<float*>:
157 case variant_trait_v<double*>:
158 case variant_trait_v<bool*>:
159 case variant_trait_v<std::string*>:
160 if (reinterpret_cast<void**>(&mStore) != nullptr) {
161 free(*reinterpret_cast<void**>(&mStore));
162 }
163 return;
164 default:
165 return;
166 }
167}
168
170{
171 mSize = other.mSize;
172 mType = other.mType;
173 switch (mType) {
174 case variant_trait_v<const char*>:
175 variant_helper<const char*>::set(&mStore, other.get<const char*>());
176 return *this;
177 case variant_trait_v<int*>:
178 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
179 return *this;
180 case variant_trait_v<float*>:
181 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
182 return *this;
183 case variant_trait_v<double*>:
184 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
185 return *this;
186 case variant_trait_v<bool*>:
187 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
188 return *this;
189 case variant_trait_v<std::string*>:
190 variant_helper<std::string*>::set(&mStore, other.get<std::string*>(), mSize);
191 return *this;
192 default:
193 mStore = other.mStore;
194 return *this;
195 }
196}
197
199{
200 mSize = other.mSize;
201 mType = other.mType;
202 switch (mType) {
203 case variant_trait_v<const char*>:
204 variant_helper<const char*>::set(&mStore, other.get<const char*>());
205 *reinterpret_cast<char**>(&(other.mStore)) = nullptr;
206 return *this;
207 case variant_trait_v<int*>:
208 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
209 *reinterpret_cast<int**>(&(other.mStore)) = nullptr;
210 return *this;
211 case variant_trait_v<float*>:
212 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
213 *reinterpret_cast<float**>(&(other.mStore)) = nullptr;
214 return *this;
215 case variant_trait_v<double*>:
216 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
217 *reinterpret_cast<double**>(&(other.mStore)) = nullptr;
218 return *this;
219 case variant_trait_v<bool*>:
220 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
221 *reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
222 return *this;
223 case variant_trait_v<std::string*>:
224 variant_helper<std::string*>::set(&mStore, other.get<std::string*>(), mSize);
225 *reinterpret_cast<std::string**>(&(other.mStore)) = nullptr;
226 return *this;
227 default:
228 mStore = other.mStore;
229 return *this;
230 }
231}
232
233std::pair<std::vector<std::string>, std::vector<std::string>> extractLabels(boost::property_tree::ptree const& tree)
234{
235 std::vector<std::string> labels_rows;
236 std::vector<std::string> labels_cols;
237 auto lrc = tree.get_child_optional(labels_rows_str);
238 if (lrc) {
239 labels_rows = basicVectorFromBranch<std::string>(lrc.value());
240 }
241 auto lcc = tree.get_child_optional(labels_cols_str);
242 if (lcc) {
243 labels_cols = basicVectorFromBranch<std::string>(lcc.value());
244 }
245 return std::make_pair(labels_rows, labels_cols);
246}
247
248} // namespace o2::framework
std::vector< std::string > labels_rows
std::vector< std::string > labels_cols
Variant for configuration parameter storage. Owns stored data.
Definition Variant.h:307
std::string asString() const
Definition Variant.cxx:80
Variant & operator=(const Variant &other)
Definition Variant.cxx:169
Variant(VariantType type=VariantType::Unknown)
Definition Variant.h:309
GLuint GLfloat * val
Definition glcorearb.h:1582
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::ostream & operator<<(std::ostream &s, ChannelType const &type)
Stream operators so that we can use ChannelType with Boost.Test.
std::pair< std::vector< std::string >, std::vector< std::string > > extractLabels(boost::property_tree::ptree const &tree)
Definition Variant.cxx:233
static void write(std::ostream &o, Variant const &v)
static void set(void *store, T value)
Definition Variant.h:259
VectorOfTObjectPtrs other
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))