Page MenuHomec4science

configuration.cpp
No OneTemporary

File Metadata

Created
Sat, Nov 16, 23:53

configuration.cpp

/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#include "configuration.hpp"
#include "utils/io/safe_config.hpp"
namespace specmicp {
namespace io {
#define S_UNITS_A_LENGTH "length"
#define S_UNITS_A_MASS "mass"
units::UnitsSet configure_units(const YAML::Node& units_conf)
{
units::UnitsSet the_units;
the_units.length = configure_length_unit(units_conf);
the_units.mass = configure_mass_unit(units_conf);
return the_units;
}
units::LengthUnit configure_length_unit(const YAML::Node& conf)
{
units::LengthUnit length = units::LengthUnit::meter;
if (conf[S_UNITS_A_LENGTH])
{
const std::string length_u = conf[S_UNITS_A_LENGTH].as<std::string>();
if (length_u == "centimeter")
length = units::LengthUnit::centimeter;
else if (length_u == "decimeter")
length = units::LengthUnit::decimeter;
else if (length_u == "meter")
length = units::LengthUnit::meter;
else
throw std::invalid_argument("Unknown length unit : '"+length_u+"'.\n" +
"Valid options : 'centimeter', 'decimeter', 'meter'.");
}
return length;
}
units::MassUnit configure_mass_unit(const YAML::Node& conf)
{
units::MassUnit mass = units::MassUnit::kilogram;
if (conf[S_UNITS_A_MASS])
{
const std::string mass_u = conf[S_UNITS_A_MASS].as<std::string>();
if (mass_u == "kilogram")
mass = units::MassUnit::kilogram;
else if (mass_u == "gram")
mass = units::MassUnit::gram;
else
throw std::invalid_argument("Unknown mass unit : '"+mass_u+"'.\n" +
"Valid options : 'gram', 'kilogram'.");
}
return mass;
}
units::UnitsSet configure_units(YAMLConfigHandle&& units_conf)
{
units::UnitsSet the_units;
the_units.length = configure_length_unit(units_conf);
the_units.mass = configure_mass_unit(units_conf);
return the_units;
}
units::LengthUnit configure_length_unit(YAMLConfigHandle& units_conf)
{
// acceptable values
static const char* centimeter[] {"centimeter", "cm"};
static const char* decimeter[] {"decimeter" , "dm"};
static const char* meter[] {"meter" , " m"};
units::LengthUnit length = units::LengthUnit::meter;
// no conf
if (not units_conf.has_attribute(S_UNITS_A_LENGTH))
{
goto exit;
}
else
{
// conf
// => test all possible values
auto conf_value = units_conf.get_attribute<std::string>(S_UNITS_A_LENGTH);
for (auto test: centimeter)
{
if (conf_value == test)
{
length = units::LengthUnit::centimeter;
goto exit;
}
}
for (auto test: decimeter)
{
if (conf_value == test)
{
length = units::LengthUnit::decimeter;
goto exit;
}
}
for (auto test: meter)
{
if (conf_value == test)
{
length = units::LengthUnit::meter;
goto exit;
}
}
// throw an error
units_conf.report_error(YAMLConfigError::InvalidArgument,
"Unknown mass unit : '"+conf_value+"'.\n" +
"Valid options : "
"'centimeter'"
", 'decimeter'"
", 'meter'."
);
}
exit:
return length;
}
units::MassUnit configure_mass_unit(YAMLConfigHandle& units_conf)
{
// acceptable values
static const char* kilogram[] {"kilogram", "kg"};
static const char* gram[] { "gram" ," g"};
units::MassUnit mass = units::MassUnit::kilogram;
// no conf
if (not units_conf.has_attribute(S_UNITS_A_MASS))
{
goto exit;
}
// conf
// => test all possible values
else
{
auto conf_value = units_conf.get_attribute<std::string>(S_UNITS_A_MASS);
for (auto test: kilogram)
{
if (conf_value == test)
{
mass = units::MassUnit::kilogram;
goto exit;
}
}
for (auto test: gram)
{
if (conf_value == test)
{
mass = units::MassUnit::gram;
goto exit;
}
}
// throw an error
units_conf.report_error(YAMLConfigError::InvalidArgument,
"Unknown length unit : '" + conf_value + "'.\n" +
"Valid options :"
"'kilogram'"
"', gram'."
);
}
exit:
return mass;
}
} //end namespace io
} //end namespace specmicp

Event Timeline