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) {
93 mSize = other.mSize;
94 variant_helper<const char*>::set(&mStore, other.get<const char*>());
95 return;
97 mSize = other.mSize;
98 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
99 return;
101 mSize = other.mSize;
102 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
103 return;
105 mSize = other.mSize;
106 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
107 return;
109 mSize = other.mSize;
110 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
111 return;
113 mSize = other.mSize;
114 variant_helper<std::vector<std::string>>::set(&mStore, other.get<std::vector<std::string>>());
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) {
133 *reinterpret_cast<void**>(&(other.mStore)) = nullptr;
134 return;
135 default:
136 return;
137 }
138}
139
141{
142 // In case we allocated an array, we
143 // should delete it.
144 switch (mType) {
150 if (reinterpret_cast<void**>(&mStore) != nullptr) {
151 free(*reinterpret_cast<void**>(&mStore));
152 }
153 return;
154 }
156 // Allocated with placement new. Nothing to delete.
157 return;
158 }
159 default:
160 return;
161 }
162}
163
165{
166 mSize = other.mSize;
167 mType = other.mType;
168 switch (mType) {
170 variant_helper<const char*>::set(&mStore, other.get<const char*>());
171 return *this;
173 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
174 return *this;
176 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
177 return *this;
179 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
180 return *this;
182 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
183 return *this;
185 variant_helper<std::vector<std::string>>::set(&mStore, other.get<std::vector<std::string>>());
186 return *this;
187 default:
188 mStore = other.mStore;
189 return *this;
190 }
191}
192
194{
195 mSize = other.mSize;
196 mType = other.mType;
197 switch (mType) {
199 variant_helper<const char*>::set(&mStore, other.get<const char*>());
200 *reinterpret_cast<char**>(&(other.mStore)) = nullptr;
201 return *this;
203 variant_helper<int*>::set(&mStore, other.get<int*>(), mSize);
204 *reinterpret_cast<int**>(&(other.mStore)) = nullptr;
205 return *this;
207 variant_helper<float*>::set(&mStore, other.get<float*>(), mSize);
208 *reinterpret_cast<float**>(&(other.mStore)) = nullptr;
209 return *this;
211 variant_helper<double*>::set(&mStore, other.get<double*>(), mSize);
212 *reinterpret_cast<double**>(&(other.mStore)) = nullptr;
213 return *this;
215 variant_helper<bool*>::set(&mStore, other.get<bool*>(), mSize);
216 *reinterpret_cast<bool**>(&(other.mStore)) = nullptr;
217 return *this;
219 variant_helper<std::vector<std::string>>::set(&mStore, other.get<std::vector<std::string>>());
220 *reinterpret_cast<std::vector<std::string>**>(&(other.mStore)) = nullptr;
221 return *this;
222 default:
223 mStore = other.mStore;
224 return *this;
225 }
226}
227
228std::pair<std::vector<std::string>, std::vector<std::string>> extractLabels(boost::property_tree::ptree const& tree)
229{
230 std::vector<std::string> labels_rows;
231 std::vector<std::string> labels_cols;
232 auto lrc = tree.get_child_optional(labels_rows_str);
233 if (lrc) {
234 labels_rows = basicVectorFromBranch<std::string>(lrc.value());
235 }
236 auto lcc = tree.get_child_optional(labels_cols_str);
237 if (lcc) {
238 labels_cols = basicVectorFromBranch<std::string>(lcc.value());
239 }
240 return std::make_pair(labels_rows, labels_cols);
241}
242
243} // 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:318
std::string asString() const
Definition Variant.cxx:80
void set(T value)
Definition Variant.h:385
Variant & operator=(const Variant &other)
Definition Variant.cxx:164
Variant(VariantType type=VariantType::Unknown)
Definition Variant.h:320
GLuint GLfloat * val
Definition glcorearb.h:1582
Defining PrimaryVertex explicitly as messageable.
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:228
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()))