Page MenuHomec4science

named_parameter_pack.hh
No OneTemporary

File Metadata

Created
Wed, Nov 6, 00:13

named_parameter_pack.hh

/**
* @file named_parameter_pack.hh
*
* @author Till Junge <till.junge@altermail.ch>
*
* @date 20 Jan 2019
*
* @brief Provides a structure for named parameters of disparate types that
transparently maps onto Python dicts.
*
* Copyright © 2019 Till Junge
*
* µSpectre is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3, or (at
* your option) any later version.
*
* µSpectre is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with µSpectre; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with proprietary FFT implementations or numerical libraries, containing parts
* covered by the terms of those libraries' licenses, the licensors of this
* Program grant you additional permission to convey the resulting work.
*/
#ifndef SRC_COMMON_NAMED_PARAMETER_PACK_HH_
#define SRC_COMMON_NAMED_PARAMETER_PACK_HH_
#include "common/common.hh"
#include <Eigen/Dense>
#include "external/variant.hpp"
#include <map>
#include <memory>
#include <string>
namespace muSpectre {
/**
* forward declaration
*/
class NamedParameterPack;
enum class ParamTag { Bool, Real, RealMatrix, Pack };
/* ---------------------------------------------------------------------- */
std::ostream & operator<<(std::ostream & os, ParamTag tag);
template <ParamTag Tag>
struct TagToType {};
template <>
struct TagToType<ParamTag::Bool> {
using type = bool;
};
template <>
struct TagToType<ParamTag::Real> {
using type = Real;
};
template <>
struct TagToType<ParamTag::RealMatrix> {
using type = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
};
template <>
struct TagToType<ParamTag::Pack> {
using type = std::shared_ptr<NamedParameterPack>;
};
template <ParamTag Tag>
using TagToType_t = typename TagToType<Tag>::type;
class ParameterValue {
public:
using RealMat_t = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
//! Default constructor
ParameterValue() = delete;
//! Copy constructor
ParameterValue(const ParameterValue & other) = default;
//! constructor for a boolean parameter
ParameterValue(const bool & boolean); // NOLINT(runtime/explicit)
//! constructor for a real scalar parameter
ParameterValue(const Real & real_number); // NOLINT(runtime/explicit)
//! constructor for a real matrix parameter
ParameterValue(const Eigen::Ref<const RealMat_t> &
real_matrix); // NOLINT(runtime/explicit)
//! constructor for a nested parameter pack
ParameterValue(
const NamedParameterPack & pack); // NOLINT(runtime/explicit)
//! Move constructor
ParameterValue(ParameterValue && other) = default;
//! Destructor
virtual ~ParameterValue() = default;
//! Copy assignment operator
ParameterValue & operator=(const ParameterValue & other) = default;
//! Move assignment operator
ParameterValue & operator=(ParameterValue && other) = default;
//! get tag (e.g., for runtime type checking
const ParamTag & get_tag() const;
template <ParamTag Tag>
TagToType_t<Tag> get_value() const;
protected:
mpark::variant<bool, Real, RealMat_t, std::shared_ptr<NamedParameterPack>>
data;
ParamTag tag;
};
class NamedParameterPack : public std::map<std::string, ParameterValue> {
public:
using Parent = std::map<std::string, ParameterValue>;
//! Default constructor
NamedParameterPack() = default;
//! use the std::map's constructors
using Parent::map;
//! Copy constructor
NamedParameterPack(const NamedParameterPack & other) = default;
//! Move constructor
NamedParameterPack(NamedParameterPack && other) = default;
//! Destructor
virtual ~NamedParameterPack() = default;
//! Copy assignment operator
NamedParameterPack & operator=(const NamedParameterPack & other) = default;
//! Move assignment operator
NamedParameterPack & operator=(NamedParameterPack && other) = default;
//! check the type of a parameter
const ParamTag & get_tag(const std::string & name) const;
//! get the value
template <ParamTag Tag>
TagToType_t<Tag> get_value(const std::string & name);
//! get the unevaluated node
ParameterValue & get(const std::string & name);
};
} // namespace muSpectre
#endif // SRC_COMMON_NAMED_PARAMETER_PACK_HH_

Event Timeline