NEML2 1.4.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
51enum class FType : int8_t
52{
53 NONE,
54 INPUT,
55 OUTPUT,
57 BUFFER
58};
59
60namespace details
61{
73template <typename P>
74void _print_helper(std::ostream & os, const P *);
75template <typename P>
76void _print_helper(std::ostream & os, const std::vector<P> *);
77template <typename P>
78void _print_helper(std::ostream & os, const std::vector<std::vector<P>> *);
80template <>
81void _print_helper(std::ostream & os, const char *);
83template <>
84void _print_helper(std::ostream & os, const unsigned char *);
86}
87
88bool options_compatible(const OptionSet & opts, const OptionSet & additional_opts);
89
90// Streaming operators
91std::ostream & operator<<(std::ostream & os, FType);
92std::ostream & operator<<(std::ostream & os, const OptionSet & p);
93
100{
101public:
102 OptionSet() = default;
103
105 OptionSet(const OptionSet &);
106
107 virtual ~OptionSet() = default;
108
111
117 void operator+=(const OptionSet & source);
118
120 const std::string & name() const { return _metadata.name; }
122 std::string & name() { return _metadata.name; }
124 const std::string & type() const { return _metadata.type; }
126 std::string & type() { return _metadata.type; }
128 const std::string & path() const { return _metadata.path; }
130 std::string & path() { return _metadata.path; }
132 const std::string & doc() const { return _metadata.doc; }
134 std::string & doc() { return _metadata.doc; }
136 const std::string & section() const { return _metadata.section; }
138 std::string & section() { return _metadata.section; }
139
144 template <typename T>
145 bool contains(const std::string &) const;
146
152 bool contains(const std::string &) const;
153
155 std::size_t size() const { return _values.size(); }
156
158 virtual void clear();
159
161 void print(std::ostream & os = std::cout) const;
162
167 {
168 public:
169 virtual ~OptionBase() = default;
170
172 virtual bool operator==(const OptionBase & other) const = 0;
173
175 virtual bool operator!=(const OptionBase & other) const = 0;
176
178 const std::string & name() const { return _metadata.name; }
179
181 const std::string & type() const { return _metadata.type; }
182
184 const FType & ftype() const { return _metadata.ftype; }
185
187 FType & ftype() { return _metadata.ftype; }
188
190 const std::string & doc() const { return _metadata.doc; }
191
193 std::string & doc() { return _metadata.doc; }
194
196 const bool & suppressed() const { return _metadata.suppressed; }
197
199 bool & suppressed() { return _metadata.suppressed; }
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;
273
274 bool operator==(const Metadata & other) const
275 {
276 return name == other.name && type == other.type && ftype == other.ftype &&
277 doc == other.doc && suppressed == other.suppressed;
278 }
279
280 bool operator!=(const Metadata & other) const { return !(*this == other); }
281
283 };
284
289 template <typename T>
290 class Option : public OptionBase
291 {
292 public:
293 Option(const std::string & name)
294 : _value()
295 {
296 _metadata.name = name;
297 _metadata.type = utils::demangle(typeid(T).name());
298 }
299
300 virtual bool operator==(const OptionBase & other) const override;
301
302 virtual bool operator!=(const OptionBase & other) const override;
303
307 const T & get() const { return _value; }
308
312 T & set() { return _value; }
313
314 virtual void print(std::ostream &) const override;
315
316 virtual std::unique_ptr<OptionBase> clone() const override;
317
318 private:
320 T _value;
321 };
322
327 template <typename T>
328 const T & get(const std::string &) const;
329
330 const OptionBase & get(const std::string &) const;
331
333
338 template <typename T, FType f = FType::NONE>
339 T & set(const std::string &);
340 OptionBase & set(const std::string &);
342
344 LabeledAxisAccessor & set_input(const std::string &);
346 LabeledAxisAccessor & set_output(const std::string &);
348 template <typename T>
349 T & set_parameter(const std::string &);
351 template <typename T>
352 T & set_buffer(const std::string &);
353
355 typedef std::map<std::string, std::unique_ptr<OptionBase>, std::less<>> map_type;
356
358 typedef map_type::iterator iterator;
359
361 typedef map_type::const_iterator const_iterator;
362
364 iterator begin();
365
367 const_iterator begin() const;
368
370 iterator end();
371
373 const_iterator end() const;
374
375protected:
379 struct Metadata
380 {
394 std::string name = "";
409 std::string type = "";
428 std::string path = "";
439 std::string doc = "";
447 std::string section = "";
449
452};
453
454template <typename T>
455bool
457{
458 const auto other_ptr = dynamic_cast<const Option<T> *>(&other);
459 if (!other_ptr)
460 return false;
461
462 return (_metadata == other_ptr->_metadata) && (other_ptr->get() == this->get());
463}
464
465template <typename T>
466bool
468{
469 return !(*this == other);
470}
471
472// LCOV_EXCL_START
473template <typename T>
474void
475OptionSet::Option<T>::print(std::ostream & os) const
476{
477 details::_print_helper(os, static_cast<const T *>(&_value));
478}
479// LCOV_EXCL_STOP
480
481template <typename T>
482std::unique_ptr<OptionSet::OptionBase>
484{
485 auto copy = std::make_unique<Option<T>>(this->name());
486 copy->_value = this->_value;
487 copy->_metadata = this->_metadata;
488 return copy;
489}
490
491template <typename T>
492bool
493OptionSet::contains(const std::string & name) const
494{
496 if (it != _values.end())
497 if (dynamic_cast<const Option<T> *>(it->second.get()))
498 return true;
499 return false;
500}
501
502template <typename T>
503const T &
504OptionSet::get(const std::string & name) const
505{
507 "ERROR: no option named \"",
508 name,
509 "\" found.\n\nKnown options:\n",
510 *this);
511
512 auto ptr = dynamic_cast<Option<T> *>(_values.at(name).get());
513 return ptr->get();
514}
515
516template <typename T, FType F>
517T &
518OptionSet::set(const std::string & name)
519{
520 if (!this->contains<T>(name))
521 _values[name] = std::make_unique<Option<T>>(name);
522 auto ptr = dynamic_cast<Option<T> *>(_values[name].get());
523 ptr->ftype() = F;
524 return ptr->set();
525}
526
527template <typename T>
528T &
529OptionSet::set_parameter(const std::string & name)
530{
532}
533
534template <typename T>
535T &
536OptionSet::set_buffer(const std::string & name)
537{
539}
540
541namespace details
542{
543// LCOV_EXCL_START
544template <typename P>
545void
546_print_helper(std::ostream & os, const P * option)
547{
548 os << *option;
549}
550
551template <>
552inline void
553_print_helper(std::ostream & os, const char * option)
554{
555 os << static_cast<int>(*option);
556}
557
558template <>
559inline void
560_print_helper(std::ostream & os, const unsigned char * option)
561{
562 os << static_cast<int>(*option);
563}
564
565template <typename P>
566void
567_print_helper(std::ostream & os, const std::vector<P> * option)
568{
569 for (const auto & p : *option)
570 os << p << " ";
571}
572
573template <typename P>
574void
575_print_helper(std::ostream & os, const std::vector<std::vector<P>> * option)
576{
577 for (const auto & pv : *option)
578 for (const auto & p : pv)
579 os << p << " ";
580}
581} // namespace details
582// LCOV_EXCL_STOP
583} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:47
Definition OptionSet.h:167
virtual void print(std::ostream &) const =0
const std::string & name() const
A readonly reference to the option's name.
Definition OptionSet.h:178
FType & ftype()
A writable reference to the option's ftype.
Definition OptionSet.h:187
const std::string & type() const
A readonly reference to the option's type.
Definition OptionSet.h:181
virtual ~OptionBase()=default
const bool & suppressed() const
A readonly reference to the option's suppression status.
Definition OptionSet.h:196
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:190
virtual std::unique_ptr< OptionBase > clone() const =0
std::string & doc()
A writable reference to the option's docstring.
Definition OptionSet.h:193
bool & suppressed()
A writable reference to the option's suppression status.
Definition OptionSet.h:199
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:184
Definition OptionSet.h:291
virtual void print(std::ostream &) const override
Definition OptionSet.h:475
T & set()
Definition OptionSet.h:312
virtual std::unique_ptr< OptionBase > clone() const override
Definition OptionSet.h:483
virtual bool operator==(const OptionBase &other) const override
Test for option equality.
Definition OptionSet.h:456
virtual bool operator!=(const OptionBase &other) const override
Test for option inequality.
Definition OptionSet.h:467
Option(const std::string &name)
Definition OptionSet.h:293
const T & get() const
Definition OptionSet.h:307
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:100
map_type _values
Data structure to map names with values.
Definition OptionSet.h:451
T & set_buffer(const std::string &)
Convenient method to request a buffer.
Definition OptionSet.h:536
virtual ~OptionSet()=default
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:120
OptionSet()=default
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:124
LabeledAxisAccessor & set_output(const std::string &)
Definition OptionSet.cxx:80
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:122
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:126
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:132
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:361
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:136
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:355
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:529
iterator begin()
Iterator pointing to the beginning of the set of options.
Definition OptionSet.cxx:172
iterator end()
Iterator pointing to the end of the set of options.
Definition OptionSet.cxx:184
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:128
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:134
void operator+=(const OptionSet &source)
Definition OptionSet.cxx:101
const T & get(const std::string &) const
Definition OptionSet.h:504
struct neml2::OptionSet::Metadata _metadata
virtual 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:130
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:138
std::size_t size() const
Definition OptionSet.h:155
T & set(const std::string &)
Definition OptionSet.h:518
void print(std::ostream &os=std::cout) const
Print the contents.
Definition OptionSet.cxx:111
OptionSet & operator=(const OptionSet &source)
Assignment operator. Deep copy.
Definition OptionSet.cxx:92
bool contains(const std::string &) const
Definition OptionSet.h:493
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:358
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:33
Definition CrossRef.cxx:30
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.cxx:31
FType
Role in a function definition.
Definition OptionSet.h:52
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:380
std::string path
Path to the option set.
Definition OptionSet.h:428
std::string type
Type of the option set.
Definition OptionSet.h:409
std::string name
Name of the option set.
Definition OptionSet.h:394
std::string section
Which NEML2 input file section this object belongs to.
Definition OptionSet.h:447
std::string doc
Option set's doc string.
Definition OptionSet.h:439
Definition OptionSet.h:218
bool operator!=(const Metadata &other) const
Definition OptionSet.h:280
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:274