NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Option.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <vector>
28
29#include "neml2/base/OptionBase.h"
30
31namespace neml2
32{
33namespace details
34{
46template <typename P>
47void _print_helper(std::ostream & os, const P *);
48template <typename P>
49void _print_helper(std::ostream & os, const std::vector<P> *);
50template <typename P>
51void _print_helper(std::ostream & os, const std::vector<std::vector<P>> *);
53template <>
54void _print_helper(std::ostream & os, const std::vector<bool> *);
56template <>
57void _print_helper(std::ostream & os, const char *);
59template <>
60void _print_helper(std::ostream & os, const unsigned char *);
62}
63
68template <typename T>
69class Option : public OptionBase
70{
71public:
72 Option(const std::string & name);
73
74 bool operator==(const OptionBase & other) const override;
75
76 bool operator!=(const OptionBase & other) const override;
77
81 const T & get() const { return _value; }
82
86 T & set() { return _value; }
87
88 void print(std::ostream &) const override;
89
90 std::unique_ptr<OptionBase> clone() const override;
91
92private:
94 T _value;
95};
96
97namespace details
98{
99// LCOV_EXCL_START
100template <typename P>
101void
102_print_helper(std::ostream & os, const P * option)
103{
104 os << *option;
105}
106
107template <typename P>
108void
109_print_helper(std::ostream & os, const std::vector<P> * option)
110{
111 for (const auto & p : *option)
112 os << p << " ";
113}
114
115template <typename P>
116void
117_print_helper(std::ostream & os, const std::vector<std::vector<P>> * option)
118{
119 for (const auto & pv : *option)
120 _print_helper(os, &pv);
121}
122} // namespace details
123// LCOV_EXCL_STOP
124} // namespace neml2
const std::string & name() const
A readonly reference to the option's name.
Definition OptionBase.h:61
OptionBase()=default
void print(std::ostream &) const override
Definition Option.cxx:75
T & set()
Definition Option.h:86
bool operator==(const OptionBase &other) const override
Test for option equality.
Definition Option.cxx:56
bool operator!=(const OptionBase &other) const override
Test for option inequality.
Definition Option.cxx:67
Option(const std::string &name)
Definition Option.cxx:39
std::unique_ptr< OptionBase > clone() const override
Definition Option.cxx:83
const T & get() const
Definition Option.h:81
Definition DiagnosticsInterface.cxx:30