13 #ifndef XACC_HETEROGENEOUS_HPP_ 14 #define XACC_HETEROGENEOUS_HPP_ 19 #include <unordered_map> 22 #include <experimental/type_traits> 25 #include "variant.hpp" 33 class HeterogeneousMap;
57 template <
typename T>
void loop_pairs(T value) {
58 insert(value.first, value.second);
61 template <
typename First,
typename... Rest>
62 void loop_pairs(First firstValue, Rest... rest) {
63 loop_pairs(firstValue);
67 template <
typename... TYPES,
69 std::remove_cv_t<std::remove_reference_t<TYPES>>...>::value>>
74 template <
typename... Ts>
void print(std::ostream &os)
const {
75 _internal_print_visitor<Ts...> v(os);
79 template <
class T>
void insert(
const std::string key,
const T &_t) {
80 if (items.count(key)) {
83 items.insert({key, _t});
87 template <
typename T>
const T
get(
const std::string key)
const {
89 return std::any_cast<T>(items.at(key));
90 }
catch (std::exception &e) {
91 XACCLogger::instance()->error(
"HeterogeneousMap::get() error - Invalid type or key (" + key +
97 template <
class T>
const T get_with_throw(
const std::string key)
const {
98 return std::any_cast<T>(items.at(key));
101 bool stringExists(
const std::string key)
const {
102 if (keyExists<const char *>(key)) {
105 if (keyExists<std::string>(key)) {
111 const std::string getString(
const std::string key)
const {
112 if (keyExists<const char *>(key)) {
113 return get<const char *>(key);
114 }
else if (keyExists<std::string>(key)) {
115 return get<std::string>(key);
117 XACCLogger::instance()->error(
"No string-like value at provided key (" +
124 template <
typename T>
bool pointerLikeExists(
const std::string key)
const {
125 if (keyExists<T *>(key)) {
127 }
else if (keyExists<std::shared_ptr<T>>(key)) {
133 template <
typename T> T *getPointerLike(
const std::string key)
const {
134 if (keyExists<T *>(key)) {
135 return get<T *>(key);
136 }
else if (keyExists<std::shared_ptr<T>>(key)) {
137 return get<std::shared_ptr<T>>(key).
get();
139 XACCLogger::instance()->error(
"No pointer-like value at provided key (" +
145 void clear() { items.clear(); }
147 template <
class T>
size_t number_of()
const {
148 _internal_number_of_visitor<T> v;
153 bool key_exists_any_type(
const std::string key)
const {
154 return items.count(key);
157 template <
typename T>
bool keyExists(
const std::string key)
const {
158 if (items.count(key)) {
161 std::any_cast<T>(items.at(key));
162 }
catch (std::exception &e) {
170 size_t size()
const {
return items.size(); }
174 template <
class T>
void visit(T &&visitor)
const {
175 visit_impl(visitor,
typename std::decay_t<T>::types{});
179 std::map<std::string, std::any> items;
181 template <
typename T>
182 class _internal_number_of_visitor :
public visitor_base<T> {
185 void operator()(
const std::string &key,
const T &t) { count++; }
188 template <
typename... Ts>
189 class _internal_print_visitor :
public visitor_base<Ts...> {
194 _internal_print_visitor(std::ostream &s) : ss(s) {}
196 template <
typename T>
void operator()(
const std::string &key,
const T &t) {
197 ss << key <<
": " << t <<
"\n";
201 template <
class T,
class HEAD,
class... TAIL>
struct try_visit {
202 template <
class U>
static void apply(T &visitor, U &&element) {
204 visitor(element.first, std::any_cast<HEAD>(element.second));
206 try_visit<T, TAIL...>::apply(visitor, element);
210 template <
class T,
class HEAD>
struct try_visit<T, HEAD> {
211 template <
class U>
static void apply(T &visitor, U &&element) {
213 visitor(element.first, std::any_cast<HEAD>(element.second));
218 template <
class T,
class... U>
220 for (
auto &&element : items) {
221 try_visit<std::decay_t<T>, U...>::apply(visitor, element);
227 template const bool HeterogeneousMap::get<bool>(
const std::string key)
const;
228 template const int HeterogeneousMap::get<int>(
const std::string key)
const;
229 template const double 230 HeterogeneousMap::get<double>(
const std::string key)
const;
231 template const std::vector<std::complex<double>>
232 HeterogeneousMap::get<std::vector<std::complex<double>>>(
233 const std::string key)
const;
234 template const std::vector<double>
235 HeterogeneousMap::get<std::vector<double>>(
const std::string key)
const;
236 template const std::vector<double>
237 HeterogeneousMap::get_with_throw<std::vector<double>>(
238 const std::string key)
const;
240 template <
typename... Types>
class Variant :
public mpark::variant<Types...> {
243 std::string originalExpression =
"";
245 class ToStringVisitor {
247 template <
typename T> std::string operator()(
const T &t)
const {
248 std::stringstream ss;
254 class IsArithmeticVisitor {
256 template <
typename T>
bool operator()(
const T &t)
const {
257 return std::is_arithmetic<T>::value;
261 template <
typename To,
typename From>
class CastVisitor {
263 To operator()(
const From &t)
const {
return (To)t; }
267 Variant() : mpark::variant<Types...>() {}
268 template <
typename T>
269 Variant(T &element) : mpark::variant<Types...>(element) {}
270 template <
typename T>
271 Variant(T &&element) : mpark::variant<Types...>(element) {}
273 : mpark::variant<Types...>(element),
274 originalExpression(element.originalExpression) {}
276 template <
typename T> T as()
const {
279 return mpark::get<T>(*this);
280 }
catch (std::exception &e) {
282 s <<
"InstructionParameter::this->toString() = " << toString() <<
"\n";
283 s <<
"This InstructionParameter type id is " << this->which() <<
"\n";
284 XACCLogger::instance()->error(
"Cannot cast Variant: " + s.str());
291 template <
typename T> T as_no_error()
const {
293 return mpark::get<T>(*this);
296 int which()
const {
return this->index(); }
298 bool isNumeric()
const {
299 IsArithmeticVisitor v;
300 return mpark::visit(v, *
this);
302 void storeOriginalExpression(std::string expr) { originalExpression = expr; }
303 void storeOriginalExpression() { originalExpression = toString(); }
304 std::string getOriginalExpression() {
return originalExpression; }
305 bool isVariable()
const {
307 mpark::get<std::string>(*this);
308 }
catch (std::exception &e) {
314 const std::string toString()
const {
316 return mpark::visit(vis, *
this);
320 return v.toString() == toString();
Definition: Accelerator.hpp:25
Definition: heterogeneous.hpp:41
Definition: heterogeneous.hpp:39
Definition: heterogeneous.hpp:35
Definition: heterogeneous.hpp:45
Definition: heterogeneous.hpp:37
Definition: heterogeneous.hpp:240