NEML2 2.0.0
Loading...
Searching...
No Matches
OptionSet.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 "neml2/misc/utils.h"
28
29#include <map>
30#include <sstream>
31#include <string>
32#include <typeinfo>
33#include <vector>
34#include <memory>
35
36namespace neml2
37{
38// Forward decl
39class OptionSet;
40class LabeledAxisAccessor;
41
42namespace details
43{
55template <typename P>
56void _print_helper(std::ostream & os, const P *);
57template <typename P>
58void _print_helper(std::ostream & os, const std::vector<P> *);
60template <>
61void _print_helper(std::ostream & os, const std::vector<bool> *);
62template <typename P>
63void _print_helper(std::ostream & os, const std::vector<std::vector<P>> *);
65template <>
66void _print_helper(std::ostream & os, const char *);
68template <>
69void _print_helper(std::ostream & os, const unsigned char *);
71}
72
73bool options_compatible(const OptionSet & opts, const OptionSet & additional_opts);
74
75// Streaming operators
76std::ostream & operator<<(std::ostream & os, FType);
77std::ostream & operator<<(std::ostream & os, const OptionSet & p);
78
85{
86public:
87 OptionSet() = default;
88
89 OptionSet(const OptionSet &);
94
96
103 void operator+=(const OptionSet & source);
104 void operator+=(OptionSet && source);
106
108 const std::string & name() const { return _metadata.name; }
110 std::string & name() { return _metadata.name; }
112 const std::string & type() const { return _metadata.type; }
114 std::string & type() { return _metadata.type; }
116 const std::string & path() const { return _metadata.path; }
118 std::string & path() { return _metadata.path; }
120 const std::string & doc() const { return _metadata.doc; }
122 std::string & doc() { return _metadata.doc; }
124 const std::string & section() const { return _metadata.section; }
126 std::string & section() { return _metadata.section; }
127
132 template <typename T>
133 bool contains(const std::string &) const;
134
140 bool contains(const std::string &) const;
141
143 std::size_t size() const { return _values.size(); }
144
146 void clear();
147
149 void print(std::ostream & os = std::cout) const;
150
155 {
156 public:
157 OptionBase() = default;
158
159 OptionBase(OptionBase &&) = delete;
160 OptionBase(const OptionBase &) = delete;
161 OptionBase & operator=(const OptionBase &) = delete;
163 virtual ~OptionBase() = default;
164
166 virtual bool operator==(const OptionBase & other) const = 0;
167
169 virtual bool operator!=(const OptionBase & other) const = 0;
170
172 const std::string & name() const { return _metadata.name; }
173
175 const std::string & type() const { return _metadata.type; }
176
178 const FType & ftype() const { return _metadata.ftype; }
179
181 FType & ftype() { return _metadata.ftype; }
182
184 const std::string & doc() const { return _metadata.doc; }
185
187 std::string & doc() { return _metadata.doc; }
188
190 const bool & suppressed() const { return _metadata.suppressed; }
191
193 bool & suppressed() { return _metadata.suppressed; }
194
196 const bool & user_specified() const { return _metadata.user_specified; }
197
199 bool & user_specified() { return _metadata.user_specified; }
200
205 virtual void print(std::ostream &) const = 0;
206
211 virtual std::unique_ptr<OptionBase> clone() const = 0;
212
213 protected:
217 struct Metadata
218 {
231 std::string name = "";
241 std::string type = "";
262 std::string doc = "";
272 bool suppressed = false;
281 bool user_specified = false;
282
283 bool operator==(const Metadata & other) const
284 {
285 return name == other.name && type == other.type && ftype == other.ftype &&
286 doc == other.doc && suppressed == other.suppressed &&
287 user_specified == other.user_specified;
288 }
289
290 bool operator!=(const Metadata & other) const { return !(*this == other); }
291
293 };
294
299 template <typename T>
300 class Option : public OptionBase
301 {
302 public:
303 Option(const std::string & name)
304 : _value()
305 {
306 _metadata.name = name;
307 _metadata.type = utils::demangle(typeid(T).name());
308 }
309
310 bool operator==(const OptionBase & other) const override;
311
312 bool operator!=(const OptionBase & other) const override;
313
317 const T & get() const { return _value; }
318
322 T & set() { return _value; }
323
324 void print(std::ostream &) const override;
325
326 std::unique_ptr<OptionBase> clone() const override;
327
328 private:
330 T _value;
331 };
332
337 template <typename T>
338 const T & get(const std::string &) const;
339
340 const OptionBase & get(const std::string &) const;
341
343
348 template <typename T, FType f = FType::NONE>
349 T & set(const std::string &);
350 OptionBase & set(const std::string &);
352
354 LabeledAxisAccessor & set_input(const std::string &);
356 LabeledAxisAccessor & set_output(const std::string &);
358 template <typename T>
359 T & set_parameter(const std::string &);
361 template <typename T>
362 T & set_buffer(const std::string &);
363
365 using map_type = std::map<std::string, std::unique_ptr<OptionBase>, std::less<>>;
367 using iterator = map_type::iterator;
369 using const_iterator = map_type::const_iterator;
370
372 iterator begin();
374 const_iterator begin() const;
376 iterator end();
378 const_iterator end() const;
379
380protected:
384 struct Metadata
385 {
399 std::string name = "";
414 std::string type = "";
433 std::string path = "";
444 std::string doc = "";
452 std::string section = "";
454
457};
458
459template <typename T>
460bool
462{
463 const auto other_ptr = dynamic_cast<const Option<T> *>(&other);
464 if (!other_ptr)
465 return false;
466
467 return (_metadata == other_ptr->_metadata) && (other_ptr->get() == this->get());
468}
469
470template <typename T>
471bool
473{
474 return !(*this == other);
475}
476
477// LCOV_EXCL_START
478template <typename T>
479void
480OptionSet::Option<T>::print(std::ostream & os) const
481{
482 details::_print_helper(os, static_cast<const T *>(&_value));
483}
484// LCOV_EXCL_STOP
485
486template <typename T>
487std::unique_ptr<OptionSet::OptionBase>
489{
490 auto copy = std::make_unique<Option<T>>(this->name());
491 copy->_value = this->_value;
492 copy->_metadata = this->_metadata;
493 return copy;
494}
495
496template <typename T>
497bool
498OptionSet::contains(const std::string & name) const
499{
501 if (it != _values.end())
502 if (dynamic_cast<const Option<T> *>(it->second.get()))
503 return true;
504 return false;
505}
506
507template <typename T>
508const T &
509OptionSet::get(const std::string & name) const
510{
512 "ERROR: no option named \"",
513 name,
514 "\" found.\n\nKnown options:\n",
515 *this);
516
517 auto ptr = dynamic_cast<Option<T> *>(_values.at(name).get());
518 return ptr->get();
519}
520
521template <typename T, FType F>
522T &
523OptionSet::set(const std::string & name)
524{
525 if (!this->contains<T>(name))
526 _values[name] = std::make_unique<Option<T>>(name);
527 auto ptr = dynamic_cast<Option<T> *>(_values[name].get());
528 ptr->ftype() = F;
529 return ptr->set();
530}
531
532template <typename T>
533T &
534OptionSet::set_parameter(const std::string & name)
535{
537}
538
539template <typename T>
540T &
541OptionSet::set_buffer(const std::string & name)
542{
544}
545
546namespace details
547{
548// LCOV_EXCL_START
549template <typename P>
550void
551_print_helper(std::ostream & os, const P * option)
552{
553 os << *option;
554}
555
556template <>
557inline void
558_print_helper(std::ostream & os, const char * option)
559{
560 os << static_cast<int>(*option);
561}
562
563template <>
564inline void
565_print_helper(std::ostream & os, const unsigned char * option)
566{
567 os << static_cast<int>(*option);
568}
569
570template <typename P>
571void
572_print_helper(std::ostream & os, const std::vector<P> * option)
573{
574 for (const auto & p : *option)
575 os << p << " ";
576}
577
578template <>
579inline void
580_print_helper(std::ostream & os, const std::vector<bool> * option)
581{
582 for (const auto p : *option)
583 os << static_cast<bool>(p) << " ";
584}
585
586template <typename P>
587void
588_print_helper(std::ostream & os, const std::vector<std::vector<P>> * option)
589{
590 for (const auto & pv : *option)
591 _print_helper(os, &pv);
592}
593} // namespace details
594// LCOV_EXCL_STOP
595} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:58
Definition OptionSet.h:155
OptionBase & operator=(const OptionBase &)=delete
virtual void print(std::ostream &) const =0
const std::string & name() const
A readonly reference to the option's name.
Definition OptionSet.h:172
FType & ftype()
A writable reference to the option's ftype.
Definition OptionSet.h:181
bool & user_specified()
A writable reference to the option's user_specified status.
Definition OptionSet.h:199
OptionBase(OptionBase &&)=delete
const std::string & type() const
A readonly reference to the option's type.
Definition OptionSet.h:175
virtual ~OptionBase()=default
const bool & user_specified() const
A readonly reference to the option's user_specified status.
Definition OptionSet.h:196
const bool & suppressed() const
A readonly reference to the option's suppression status.
Definition OptionSet.h:190
struct neml2::OptionSet::OptionBase::Metadata _metadata
virtual bool operator==(const OptionBase &other) const =0
Test for option equality.
const std::string & doc() const
A readonly reference to the option's docstring.
Definition OptionSet.h:184
virtual std::unique_ptr< OptionBase > clone() const =0
OptionBase(const OptionBase &)=delete
std::string & doc()
A writable reference to the option's docstring.
Definition OptionSet.h:187
OptionBase & operator=(OptionBase &&)=delete
bool & suppressed()
A writable reference to the option's suppression status.
Definition OptionSet.h:193
virtual bool operator!=(const OptionBase &other) const =0
Test for option inequality.
const FType & ftype() const
A readonly reference to the option's ftype.
Definition OptionSet.h:178
Definition OptionSet.h:301
void print(std::ostream &) const override
Definition OptionSet.h:480
T & set()
Definition OptionSet.h:322
std::unique_ptr< OptionBase > clone() const override
Definition OptionSet.h:488
bool operator==(const OptionBase &other) const override
Test for option equality.
Definition OptionSet.h:461
bool operator!=(const OptionBase &other) const override
Test for option inequality.
Definition OptionSet.h:472
Option(const std::string &name)
Definition OptionSet.h:303
const T & get() const
Definition OptionSet.h:317
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:85
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:367
map_type _values
Data structure to map names with values.
Definition OptionSet.h:456
T & set_buffer(const std::string &)
Convenient method to request a buffer.
Definition OptionSet.h:541
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:108
OptionSet()=default
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:112
LabeledAxisAccessor & set_output(const std::string &)
Definition OptionSet.cxx:80
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:369
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:365
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:110
LabeledAxisAccessor & set_input(const std::string &)
Definition OptionSet.cxx:74
std::string & type()
A writable reference to the option set's type.
Definition OptionSet.h:114
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:120
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:124
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:534
iterator begin()
Iterator pointing to the beginning of the set of options.
Definition OptionSet.cxx:190
iterator end()
Iterator pointing to the end of the set of options.
Definition OptionSet.cxx:202
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:116
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:122
const T & get(const std::string &) const
Definition OptionSet.h:509
struct neml2::OptionSet::Metadata _metadata
void clear()
Clear internal data structures & frees any allocated memory.
Definition OptionSet.cxx:86
std::string & path()
A writable reference to the option set's path.
Definition OptionSet.h:118
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:126
std::size_t size() const
Definition OptionSet.h:143
T & set(const std::string &)
Definition OptionSet.h:523
void print(std::ostream &os=std::cout) const
Print the contents.
Definition OptionSet.cxx:129
bool contains(const std::string &) const
Definition OptionSet.h:498
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:32
Definition CrossRef.cxx:31
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.cxx:31
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
FType
Role in a function definition.
Definition types.h:119
std::ostream & operator<<(std::ostream &os, const EnumSelection &es)
Definition EnumSelection.cxx:31
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64
Definition OptionSet.h:385
std::string path
Path to the option set.
Definition OptionSet.h:433
std::string type
Type of the option set.
Definition OptionSet.h:414
std::string name
Name of the option set.
Definition OptionSet.h:399
std::string section
Which NEML2 input file section this object belongs to.
Definition OptionSet.h:452
std::string doc
Option set's doc string.
Definition OptionSet.h:444
Definition OptionSet.h:218
bool operator!=(const Metadata &other) const
Definition OptionSet.h:290
bool suppressed
Whether this option is suppressed.
Definition OptionSet.h:272
std::string type
Type of the option.
Definition OptionSet.h:241
FType ftype
Option's role in defining the function.
Definition OptionSet.h:251
std::string name
Name of the option.
Definition OptionSet.h:231
std::string doc
Option's doc string.
Definition OptionSet.h:262
bool operator==(const Metadata &other) const
Definition OptionSet.h:283
bool user_specified
Whether this option has been specified by the user from the input file.
Definition OptionSet.h:281