Skip to content
Snippets Groups Projects
Commit 24135ee7 authored by Riccardo Maria Bianchi's avatar Riccardo Maria Bianchi :sunny: Committed by Johannes Junggeburth
Browse files

New helpers to handle variant data

parent 00a5a247
No related branches found
No related tags found
1 merge request!327New schema for the GeoModel SQLite database and updated I/O
// 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment