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
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
89 template<typename T>
90 explicit ParameterEntry(
91 T value, bool isDefault = false, bool isList = false,
92 const std::string &docString = "",
93 RCP<const ParameterEntryValidator> const& validator = null
94 );
95
97
99
100
102 ParameterEntry& operator=(const ParameterEntry& source);
103
111 template<typename T>
112 void setValue(
113 T value, bool isDefault = false,
114 const std::string &docString = "",
115 RCP<const ParameterEntryValidator> const& validator = null
116 );
117
124 void setAnyValue(
125 const any &value, bool isDefault = false
126 );
127
129 void setValidator(
131 );
132
134 void setDocString(const std::string &docString);
135
137 ParameterList& setList(
138 bool isDefault = false,
139 const std::string &docString = ""
140 );
141
143
145
146
152 template<typename T>
153 inline
154 T& getValue(T *ptr) const;
155
160 inline
161 any& getAny(bool activeQry = true);
162
167 inline
168 const any& getAny(bool activeQry = true) const;
169
171
173
174
176 inline
177 bool isUsed() const;
178
180 bool isList() const;
181
183 template <typename T>
184 inline
185 bool isType() const;
186
188 bool isArray() const;
189 //
191 bool isTwoDArray() const;
192
194 inline
195 bool isDefault() const;
196
198 inline
199 std::string docString() const;
200
202 inline
203 RCP<const ParameterEntryValidator> validator() const;
204
206
208
209
215 std::ostream& leftshift(std::ostream& os, bool printFlags = true) const;
216
220 static const std::string& getTagName(){
221 static const std::string tagName = "Parameter";
222 return tagName;
223 }
224
226
227private:
228
230 void reset();
231
233 any val_;
234
236 mutable bool isUsed_;
237
239 mutable bool isDefault_;
240
242 std::string docString_;
243
245//use pragmas to disable some false positive warnings for windows sharedlib export
246#ifdef _MSC_VER
247#pragma warning(push)
248#pragma warning(disable:4251)
249#endif
251#ifdef _MSC_VER
252#pragma warning(pop)
253#endif
254
255};
256
262template<typename T>
263inline T& getValue( const ParameterEntry &entry )
264{
265 return entry.getValue(static_cast<T*>(0));
266}
267
273template<typename T>
275{
276 return entry->getValue(static_cast<T*>(0));
277}
278
282inline bool operator==(const ParameterEntry& e1, const ParameterEntry& e2)
283{
284 return (
285 e1.getAny() == e2.getAny()
286 && e1.isList()== e2.isList()
287 && e1.isUsed() == e2.isUsed()
288 && e1.isDefault() == e2.isDefault()
289 );
290}
291
295inline bool operator!=(const ParameterEntry& e1, const ParameterEntry& e2)
296{
297 return !( e1 == e2 );
298}
299
303inline std::ostream& operator<<(std::ostream& os, const ParameterEntry& e)
304{
305 return e.leftshift(os);
306}
307
308// ///////////////////////////////////////////
309// Inline and Template Function Definitions
310
311// Constructor/Destructor
312
313template<typename T>
314inline
316 T value_in,
317 bool isDefault_in,
318 bool /*isList_in*/, // 2007/11/26: rabartl: ToDo: This arg is ignored and should be removed!
319 const std::string &docString_in,
320 RCP<const ParameterEntryValidator> const& validator_in
321 )
322 : val_(value_in),
323 isUsed_(false),
324 isDefault_(isDefault_in),
325 docString_(docString_in),
326 validator_(validator_in)
327{
328 static_assert(std::is_same<typename Teuchos::is_comparable<T>::type, std::true_type>::value &&
329 std::is_same<typename Teuchos::is_printable<T>::type, std::true_type>::value,
330 "ParameterList values must be comparable and printable!");
331}
332
333// Set Methods
334
335template<typename T>
336inline
338 T value_in, bool isDefault_in, const std::string &docString_in,
339 RCP<const ParameterEntryValidator> const& validator_in
340 )
341{
342 static_assert(std::is_same<typename Teuchos::is_comparable<T>::type, std::true_type>::value &&
343 std::is_same<typename Teuchos::is_printable<T>::type, std::true_type>::value,
344 "ParameterList values must be comparable and printable!");
345 val_ = value_in;
346 isDefault_ = isDefault_in;
347 if(docString_in.length())
348 docString_ = docString_in;
349 if(validator_in.get())
350 validator_ = validator_in;
351}
352
353// Get Methods
354
355template<typename T>
356inline
357T& ParameterEntry::getValue(T * /*ptr*/) const
358{
359 isUsed_ = true;
360 return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
361}
362
363inline
365{
366 if (activeQry == true) {
367 isUsed_ = true;
368 }
369 return val_;
370}
371
372inline
373const any& ParameterEntry::getAny(bool activeQry) const
374{
375 if (activeQry == true) {
376 isUsed_ = true;
377 }
378 return val_;
379}
380
381// Attribute Methods
382
383inline
385{ return isUsed_; }
386
387template <typename T>
388inline
390{ return val_.type() == typeid(T); }
391
392inline
394{ return isDefault_; }
395
396inline
397std::string ParameterEntry::docString() const
398{ return docString_; }
399
400inline
403{ return validator_; }
404
405
406} // namespace Teuchos
407
408
409#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.
This object is held as the "value" in the Teuchos::ParameterList std::map.
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.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
ParameterEntry()
Default Constructor.
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.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
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.
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.
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.
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,...