Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ParameterEntry.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42
43#ifndef TEUCHOS_PARAMETER_ENTRY_H
44#define TEUCHOS_PARAMETER_ENTRY_H
45
49
51#include "Teuchos_any.hpp"
52#include "Teuchos_RCP.hpp"
53#include "Teuchos_ParameterEntryValidator.hpp"
54
55namespace Teuchos {
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58class ParameterList; // another parameter type (forward declaration)
59#endif
60
67class TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT ParameterEntry {
68
69public:
70
73
75 typedef unsigned int ParameterEntryID;
76
78
80
81
84
86 ParameterEntry(const ParameterEntry& source);
87
90
92 template <typename T, typename = std::enable_if_t< ! std::is_same_v<std::decay_t<T>, ParameterEntry>>>
93 explicit ParameterEntry(
94 T&& value, bool isDefault = false, bool isList = false,
95 const std::string &docString = "",
97 );
98
100
102
103
106
109
117 template<typename T>
118 void setValue(
119 T value, bool isDefault = false,
120 const std::string &docString = "",
122 );
123
130 void setAnyValue(
131 const any &value, bool isDefault = false
132 );
133
135 void setValidator(
137 );
138
140 void setDocString(const std::string &docString);
141
144 bool isDefault = false,
145 const std::string &docString = ""
146 );
147
149
151
152
158 template<typename T>
159 inline
160 T& getValue(T *ptr) const;
161
166 inline
167 any& getAny(bool activeQry = true);
168
173 inline
174 const any& getAny(bool activeQry = true) const;
175
177
179
180
182 inline
183 bool isUsed() const;
184
186 bool isList() const;
187
189 template <typename T>
190 inline
191 bool isType() const;
192
194 bool isArray() const;
195 //
197 bool isTwoDArray() const;
198
200 inline
201 bool isDefault() const;
202
204 inline
205 std::string docString() const;
206
208 inline
210
212
214
215
221 std::ostream& leftshift(std::ostream& os, bool printFlags = true) const;
222
226 static const std::string& getTagName(){
227 static const std::string tagName = "Parameter";
228 return tagName;
229 }
230
232
233private:
234
236 void reset();
237
239 any val_;
240
242 mutable bool isUsed_;
243
245 mutable bool isDefault_;
246
248 std::string docString_;
249
251//use pragmas to disable some false positive warnings for windows sharedlib export
252#ifdef _MSC_VER
253#pragma warning(push)
254#pragma warning(disable:4251)
255#endif
257#ifdef _MSC_VER
258#pragma warning(pop)
259#endif
260
261};
262
268template<typename T>
269inline T& getValue( const ParameterEntry &entry )
270{
271 return entry.getValue(static_cast<T*>(0));
272}
273
279template<typename T>
281{
282 return entry->getValue(static_cast<T*>(0));
283}
284
288inline bool operator==(const ParameterEntry& e1, const ParameterEntry& e2)
289{
290 return (
291 e1.getAny() == e2.getAny()
292 && e1.isList()== e2.isList()
293 && e1.isUsed() == e2.isUsed()
294 && e1.isDefault() == e2.isDefault()
295 );
296}
297
301inline bool operator!=(const ParameterEntry& e1, const ParameterEntry& e2)
302{
303 return !( e1 == e2 );
304}
305
309inline std::ostream& operator<<(std::ostream& os, const ParameterEntry& e)
310{
311 return e.leftshift(os);
312}
313
314// ///////////////////////////////////////////
315// Inline and Template Function Definitions
316
317// Constructor/Destructor
318
319template<typename T, typename>
320inline
322 T&& value_in,
323 bool isDefault_in,
324 bool /*isList_in*/, // 2007/11/26: rabartl: ToDo: This arg is ignored and should be removed!
325 const std::string &docString_in,
326 RCP<const ParameterEntryValidator> const& validator_in
327 )
328 : val_(std::forward<T>(value_in)),
329 isUsed_(false),
330 isDefault_(isDefault_in),
331 docString_(docString_in),
332 validator_(validator_in)
333{
334 static_assert(std::is_same<typename Teuchos::is_comparable<T>::type, std::true_type>::value &&
335 std::is_same<typename Teuchos::is_printable<T>::type, std::true_type>::value,
336 "ParameterList values must be comparable and printable!");
337}
338
339// Set Methods
340
341template<typename T>
342inline
344 T value_in, bool isDefault_in, const std::string &docString_in,
345 RCP<const ParameterEntryValidator> const& validator_in
346 )
347{
348 static_assert(std::is_same<typename Teuchos::is_comparable<T>::type, std::true_type>::value &&
349 std::is_same<typename Teuchos::is_printable<T>::type, std::true_type>::value,
350 "ParameterList values must be comparable and printable!");
351 val_ = value_in;
352 isDefault_ = isDefault_in;
353 if(docString_in.length())
354 docString_ = docString_in;
355 if(validator_in.get())
356 validator_ = validator_in;
357}
358
359// Get Methods
360
361template<typename T>
362inline
363T& ParameterEntry::getValue(T * /*ptr*/) const
364{
365 isUsed_ = true;
366 return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
367}
368
369inline
371{
372 if (activeQry == true) {
373 isUsed_ = true;
374 }
375 return val_;
376}
377
378inline
379const any& ParameterEntry::getAny(bool activeQry) const
380{
381 if (activeQry == true) {
382 isUsed_ = true;
383 }
384 return val_;
385}
386
387// Attribute Methods
388
389inline
391{ return isUsed_; }
392
393template <typename T>
394inline
396{ return val_.type() == typeid(T); }
397
398inline
400{ return isDefault_; }
401
402inline
403std::string ParameterEntry::docString() const
404{ return docString_; }
405
406inline
409{ return validator_; }
410
411
412} // namespace Teuchos
413
414
415#endif
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Reference-counted pointer class and non-member templated function implementations.
Modified boost::any class for holding a templated value.
void setValidator(RCP< const ParameterEntryValidator > const &validator)
Set the validator.
bool isType() const
Test the type of the data being contained.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
T & getValue(T *ptr) const
Templated get method that uses the input pointer type to determine the type of parameter to return.
ParameterList & setList(bool isDefault=false, const std::string &docString="")
Create a parameter entry that is an empty list.
ParameterEntry & operator=(const ParameterEntry &source)
Replace the current parameter entry with source.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
ParameterEntry()
Default Constructor.
bool isTwoDArray() const
Test if the type of data being contained is a Teuchos::TwoDArray.
void setValue(T value, bool isDefault=false, const std::string &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method that uses the input value type to determine the type of parameter.
T & getValue(const ParameterEntry &entry)
A templated helper function for returning the value of type T held in the ParameterEntry object,...
bool operator==(const ParameterEntry &e1, const ParameterEntry &e2)
Returns true if two ParameterEntry objects are equal.
bool isArray() const
Test if the type of data being contained is a Teuchos::Array.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
void setDocString(const std::string &docString)
Set the documentation std::string.
std::string docString() const
Return the (optional) documentation std::string.
bool isList() const
Return whether or not the value itself is a list.
bool operator!=(const ParameterEntry &e1, const ParameterEntry &e2)
Returns true if two ParameterEntry objects are not equal.
void setAnyValue(const any &value, bool isDefault=false)
Set the value as an any object.
T & getValue(RCP< const ParameterEntry > entry)
A templated helper function for returning the value of type T held in the ParameterEntry object,...
std::ostream & operator<<(std::ostream &os, const ParameterEntry &e)
Output stream operator for handling the printing of parameter entries.
static const std::string & getTagName()
Get the string that should be used as the tag name for all parameters when they are serialized to xml...
bool isDefault() const
Indicate whether this entry takes on the default value.
A list of parameters of arbitrary type.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Smart reference counting pointer class for automatic garbage collection.
T * get() const
Get the raw C++ pointer to the underlying object.
Modified boost::any class, which is a container for a templated value.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
const std::type_info & type() const
Return the type of value being stored.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...