From 24135ee7a4e705435336365bf28473874cad2628 Mon Sep 17 00:00:00 2001 From: Riccardo Maria Bianchi <riccardo.maria.bianchi@cern.ch> Date: Sat, 11 May 2024 02:59:18 +0200 Subject: [PATCH] New helpers to handle variant data --- .../GeoModelHelpers/variantHelpers.h | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 GeoModelCore/GeoModelHelpers/GeoModelHelpers/variantHelpers.h diff --git a/GeoModelCore/GeoModelHelpers/GeoModelHelpers/variantHelpers.h b/GeoModelCore/GeoModelHelpers/GeoModelHelpers/variantHelpers.h new file mode 100644 index 000000000..c601b7193 --- /dev/null +++ b/GeoModelCore/GeoModelHelpers/GeoModelHelpers/variantHelpers.h @@ -0,0 +1,124 @@ +// Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration + +/* + * This header file provides helper C++ functions used in GeoModel IO code. + * + * Author: Riccardo Maria BIANCHI @ CERN + * Created on: April, 2024 + * + */ + + +#ifndef GEOMODELHELPERS_VARIANTHELPERS_H +#define GEOMODELHELPERS_VARIANTHELPERS_H + +#include <iostream> +#include <string> +#include <vector> +#include <variant> +#include <type_traits> + + +namespace GeoModelHelpers { + + class variantHelper + { + public: + + static void printStdVectorVariants(const std::vector<std::variant<int, long, float, double, std::string>> vec) + { + for (const auto &item : vec) + { + if (std::holds_alternative<int>(item)) + std::cout << std::get<int>(item); + else if (std::holds_alternative<long>(item)) + std::cout << std::get<long>(item); + else if (std::holds_alternative<float>(item)) + std::cout << std::get<float>(item); + else if (std::holds_alternative<double>(item)) + std::cout << std::get<double>(item); + else if (std::holds_alternative<std::string>(item)) + std::cout << std::get<std::string>(item); + + std::cout << ", "; + } + std::cout << std::endl; + } + + static std::string getFromVariant_String(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "") + { + std::string_view type{"string"}; + std::string ret; + try + { + ret = std::get<std::string>(record); + } + catch (std::bad_variant_access const &ex) + { + std::cout << ex.what() << ": '" << logMsg << "' is not a 'string'!\n"; + } + return ret; + } + static int getFromVariant_Int(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "") + { + std::string_view type{"int"}; + int ret; + + try + { + ret = std::get<int>(record); + } + catch (std::bad_variant_access const &ex) + { + std::cout << ex.what() << ": '" << logMsg << "' is not a '" << type << "'!\n"; + } + return ret; + } + static int getFromVariant_Double(const std::variant<int, long, float, double, std::string> &record, std::string_view logMsg = "") + { + std::string_view type{"double"}; + double ret; + try + { + ret = std::get<double>(record); + } + catch (std::bad_variant_access const &ex) + { + std::cout << ex.what() << ": '" << logMsg << "' is not a '" << type << "'!\n"; + } + return ret; + } + static std::string getFromVariant_Type(const std::variant<int, long, float, double, std::string> &record) + { + std::string type; + if (std::holds_alternative<int>(record)) + { + type = "int"; + } + else if (std::holds_alternative<long>(record)) + { + type = "long"; + } + else if (std::holds_alternative<float>(record)) + { + type = "float"; + } + else if (std::holds_alternative<double>(record)) + { + type = "double"; + } + else if (std::holds_alternative<std::string>(record)) + { + type = "string"; + } else { + type = "UNKOWN"; + } + std::cout << "Variant is of type '" << type << "'" << std::endl; + return type; + } + }; +} // end of namespace + + +#endif + -- GitLab