Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
50 #include "Teuchos_ConfigDefs.hpp"
51 #include "Teuchos_any.hpp"
52 #include "Teuchos_RCP.hpp"
54 
55 namespace Teuchos {
56 
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 class ParameterList; // another parameter type (forward declaration)
59 #endif
60 
68 
69 public:
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 = "",
96  RCP<const ParameterEntryValidator> const& validator = null
97  );
98 
100 
102 
103 
105  ParameterEntry& operator=(const ParameterEntry& source);
106 
108  ParameterEntry& operator=(ParameterEntry&&);
109 
117  template<typename T>
118  void setValue(
119  T value, bool isDefault = false,
120  const std::string &docString = "",
121  RCP<const ParameterEntryValidator> const& validator = null
122  );
123 
130  void setAnyValue(
131  const any &value, bool isDefault = false
132  );
133 
135  void setValidator(
136  RCP<const ParameterEntryValidator> const& validator
137  );
138 
140  void setDocString(const std::string &docString);
141 
143  ParameterList& setList(
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
209  RCP<const ParameterEntryValidator> validator() const;
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 
233 private:
234 
236  void reset();
237 
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 
268 template<typename T>
269 inline T& getValue( const ParameterEntry &entry )
270 {
271  return entry.getValue(static_cast<T*>(0));
272 }
273 
279 template<typename T>
281 {
282  return entry->getValue(static_cast<T*>(0));
283 }
284 
288 inline 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 
301 inline bool operator!=(const ParameterEntry& e1, const ParameterEntry& e2)
302 {
303  return !( e1 == e2 );
304 }
305 
309 inline 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 
319 template<typename T, typename>
320 inline
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 
341 template<typename T>
342 inline
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 
361 template<typename T>
362 inline
363 T& ParameterEntry::getValue(T * /*ptr*/) const
364 {
365  isUsed_ = true;
366  return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
367 }
368 
369 inline
370 any& ParameterEntry::getAny(bool activeQry)
371 {
372  if (activeQry == true) {
373  isUsed_ = true;
374  }
375  return val_;
376 }
377 
378 inline
379 const any& ParameterEntry::getAny(bool activeQry) const
380 {
381  if (activeQry == true) {
382  isUsed_ = true;
383  }
384  return val_;
385 }
386 
387 // Attribute Methods
388 
389 inline
391 { return isUsed_; }
392 
393 template <typename T>
394 inline
396 { return val_.type() == typeid(T); }
397 
398 inline
400 { return isDefault_; }
401 
402 inline
403 std::string ParameterEntry::docString() const
404 { return docString_; }
405 
406 inline
409 { return validator_; }
410 
411 
412 } // namespace Teuchos
413 
414 
415 #endif
Modified boost::any class for holding a templated value.
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.
ParameterEntry()
Default Constructor.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
bool isDefault_
Was this parameter a default value assigned by a &quot;get&quot; function?
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
This object is held as the &quot;value&quot; in the Teuchos::ParameterList std::map.
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...
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
bool isType() const
Test the type of the data being contained.
T * get() const
Get the raw C++ pointer to the underlying object.
decltype(test< T >(0)) type
Definition: Teuchos_any.hpp:95
bool isDefault() const
Indicate whether this entry takes on the default value.
Modified boost::any class, which is a container for a templated value.
T & getValue(T *ptr) const
Templated get method that uses the input pointer type to determine the type of parameter to return...
bool operator==(const ParameterEntry &e1, const ParameterEntry &e2)
Returns true if two ParameterEntry objects are equal.
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::string docString_
Optional documentation field.
any val_
Templated Datatype.
bool isList() const
Return whether or not the value itself is a list.
A list of parameters of arbitrary type.
RCP< const ParameterEntryValidator > validator_
Optional validator object.
T & getValue(const ParameterEntry &entry)
A templated helper function for returning the value of type T held in the ParameterEntry object...
#define TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
Smart reference counting pointer class for automatic garbage collection.
const std::type_info & type() const
Return the type of value being stored.
std::string docString() const
Return the (optional) documentation std::string.
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
Reference-counted pointer class and non-member templated function implementations.
decltype(test< T >(0)) type
std::ostream & operator<<(std::ostream &os, const ParameterEntry &e)
Output stream operator for handling the printing of parameter entries.
bool isUsed_
Has this parameter been accessed by a &quot;get&quot; function?