Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
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 
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(
130  RCP<const ParameterEntryValidator> const& validator
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 
227 private:
228 
230  void reset();
231 
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 
262 template<typename T>
263 inline T& getValue( const ParameterEntry &entry )
264 {
265  return entry.getValue(static_cast<T*>(0));
266 }
267 
273 template<typename T>
275 {
276  return entry->getValue(static_cast<T*>(0));
277 }
278 
282 inline 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 
295 inline bool operator!=(const ParameterEntry& e1, const ParameterEntry& e2)
296 {
297  return !( e1 == e2 );
298 }
299 
303 inline 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 
313 template<typename T>
314 inline
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 
329 // Set Methods
330 
331 template<typename T>
332 inline
334  T value_in, bool isDefault_in, const std::string &docString_in,
335  RCP<const ParameterEntryValidator> const& validator_in
336  )
337 {
338  val_ = value_in;
339  isDefault_ = isDefault_in;
340  if(docString_in.length())
341  docString_ = docString_in;
342  if(validator_in.get())
343  validator_ = validator_in;
344 }
345 
346 // Get Methods
347 
348 template<typename T>
349 inline
350 T& ParameterEntry::getValue(T * /*ptr*/) const
351 {
352  isUsed_ = true;
353  return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
354 }
355 
356 inline
357 any& ParameterEntry::getAny(bool activeQry)
358 {
359  if (activeQry == true) {
360  isUsed_ = true;
361  }
362  return val_;
363 }
364 
365 inline
366 const any& ParameterEntry::getAny(bool activeQry) const
367 {
368  if (activeQry == true) {
369  isUsed_ = true;
370  }
371  return val_;
372 }
373 
374 // Attribute Methods
375 
376 inline
378 { return isUsed_; }
379 
380 template <typename T>
381 inline
383 { return val_.type() == typeid(T); }
384 
385 inline
387 { return isDefault_; }
388 
389 inline
390 std::string ParameterEntry::docString() const
391 { return docString_; }
392 
393 inline
396 { return validator_; }
397 
398 
399 } // namespace Teuchos
400 
401 
402 #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.
bool isDefault_
Was this parameter a default value assigned by a "get" function?
This object is held as the "value" 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...
Modified boost::any class, which is a container for a templated value.
Definition: Teuchos_any.hpp:86
T * get() const
Get the raw C++ pointer to the underlying object.
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.
std::ostream & leftshift(std::ostream &os, bool printFlags=true) const
Output a non-list parameter to the given output stream.
T & getValue(RCP< const ParameterEntry > entry)
A templated helper function for returning the value of type T held in the ParameterEntry object...
bool isDefault() const
Indicate whether this entry takes on the default value.
std::string docString_
Optional documentation field.
any val_
Templated Datatype.
bool isList() const
Return whether or not the value itself is a list.
RCP< const ParameterEntryValidator > validator() const
Return the (optional) validator object.
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
T & getValue(T *ptr) const
Templated get method that uses the input pointer type to determine the type of parameter to return...
bool isType() const
Test the type of the data being contained.
const std::type_info & type() const
Return the type of value being stored.
any & getAny(bool activeQry=true)
Direct access to the Teuchos::any data value underlying this object. The bool argument activeQry (def...
bool isUsed() const
Return whether or not the value has been used; i.e., whether or not the value has been retrieved via ...
std::string docString() const
Return the (optional) documentation std::string.
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.
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 "get" function?