Teuchos_ParameterEntry.hpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 
00030 #ifndef TEUCHOS_PARAMETER_ENTRY_H
00031 #define TEUCHOS_PARAMETER_ENTRY_H
00032 
00037 #include "Teuchos_ConfigDefs.hpp"
00038 #include "Teuchos_any.hpp"
00039 #include "Teuchos_RCP.hpp"
00040 #include "Teuchos_ParameterEntryValidator.hpp"
00041 
00042 namespace Teuchos {
00043 
00044 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00045 class ParameterList; // another parameter type (forward declaration)
00046 #endif
00047 
00054 class ParameterEntry {
00055 public:
00056 
00058 
00059 
00061   ParameterEntry();
00062   
00064   ParameterEntry(const ParameterEntry& source);
00065 
00067   template<typename T>
00068   explicit ParameterEntry(
00069     T value, bool isDefault = false, bool isList = false,
00070     const std::string &docString = "",
00071     RCP<const ParameterEntryValidator> const& validator = null
00072     );
00073 
00075   ~ParameterEntry();
00076 
00078 
00080 
00081 
00083   ParameterEntry& operator=(const ParameterEntry& source);
00084 
00092   template<typename T>
00093   void setValue(
00094     T value, bool isDefault = false,
00095     const std::string &docString = "",
00096     RCP<const ParameterEntryValidator> const& validator = null
00097     );
00098 
00105   void setAnyValue(
00106     const any &value, bool isDefault = false
00107     );
00108 
00110   void setValidator(
00111     RCP<const ParameterEntryValidator> const& validator
00112     );
00113 
00115   void setDocString(const std::string &docString);
00116 
00118   ParameterList& setList(
00119     bool isDefault = false,
00120     const std::string &docString = ""
00121     );
00122 
00124 
00126 
00127    
00133   template<typename T>
00134   T& getValue(T *ptr) const;
00135 
00140   any& getAny(bool activeQry = true);
00141 
00146   const any& getAny(bool activeQry = true) const;
00147 
00149 
00151 
00152   
00154   bool isUsed() const;
00155 
00157   bool isList() const;
00158 
00160   template <typename T>
00161   bool isType() const;
00162 
00164   bool isDefault() const;
00165 
00167   std::string docString() const;
00168 
00170   RCP<const ParameterEntryValidator> validator() const;
00171 
00173 
00175 
00182   std::ostream& leftshift(std::ostream& os, bool printFlags = true) const;
00183 
00185 
00186 private:
00187 
00189   void reset();
00190   
00192   any val_;
00193 
00195   mutable bool isUsed_;
00196 
00198   mutable bool isDefault_;
00199 
00201   std::string  docString_;
00202 
00204   RCP<const ParameterEntryValidator> validator_;
00205 
00206 };
00207 
00213 template<typename T>
00214 inline T& getValue( const ParameterEntry &entry )
00215 {
00216   return entry.getValue((T*)NULL);
00217 }
00218 
00222 inline bool operator==(const ParameterEntry& e1, const ParameterEntry& e2) 
00223 { 
00224   return (
00225     e1.getAny() == e2.getAny()
00226     && e1.isList()== e2.isList()
00227     && e1.isUsed() == e2.isUsed()
00228     && e1.isDefault() == e2.isDefault()
00229     );
00230 }
00231 
00235 inline bool operator!=(const ParameterEntry& e1, const ParameterEntry& e2) 
00236 { 
00237   return !( e1 == e2 );
00238 }
00239 
00243 inline std::ostream& operator<<(std::ostream& os, const ParameterEntry& e) 
00244 { 
00245   return e.leftshift(os);
00246 }
00247 
00248 // ///////////////////////////////////////////
00249 // Inline and Template Function Definitions
00250 
00251 // Constructor/Destructor
00252 
00253 template<typename T>
00254 inline
00255 ParameterEntry::ParameterEntry(
00256   T value_in,
00257   bool isDefault_in,
00258   bool /*isList_in*/, // 2007/11/26: rabartl: ToDo: This arg is ignored and should be removed!
00259   const std::string &docString_in,
00260   RCP<const ParameterEntryValidator> const& validator_in
00261   )
00262   : val_(value_in),
00263     isUsed_(false),
00264     isDefault_(isDefault_in),
00265     docString_(docString_in),
00266     validator_(validator_in)
00267 {}
00268 
00269 inline
00270 ParameterEntry::~ParameterEntry()
00271 {}
00272 
00273 // Set Methods
00274 
00275 template<typename T>
00276 inline
00277 void ParameterEntry::setValue(
00278   T value_in, bool isDefault_in, const std::string &docString_in,
00279   RCP<const ParameterEntryValidator> const& validator_in
00280   )
00281 {
00282   val_ = value_in;
00283   isDefault_ = isDefault_in;
00284   if(docString_in.length())
00285     docString_ = docString_in;
00286   if(validator_in.get())
00287     validator_ = validator_in;
00288 }
00289 
00290 // Get Methods
00291 
00292 template<typename T>
00293 inline
00294 T& ParameterEntry::getValue(T * /*ptr*/) const
00295 {
00296   isUsed_ = true;
00297   return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
00298 }
00299 
00300 inline
00301 any& ParameterEntry::getAny(bool activeQry)
00302 { 
00303   if (activeQry == true) {
00304     isUsed_ = true;
00305   }
00306   return val_; 
00307 }
00308 
00309 inline
00310 const any& ParameterEntry::getAny(bool activeQry) const
00311 { 
00312   if (activeQry == true) {
00313     isUsed_ = true;
00314   }
00315   return val_; 
00316 }
00317 
00318 // Attribute Methods
00319 
00320 inline
00321 bool ParameterEntry::isUsed() const
00322 { return isUsed_; }
00323 
00324 template <typename T>
00325 inline
00326 bool ParameterEntry::isType() const
00327 { return val_.type() == typeid(T); }
00328 
00329 inline
00330 bool ParameterEntry::isDefault() const
00331 { return isDefault_; }
00332 
00333 inline
00334 std::string ParameterEntry::docString() const
00335 { return docString_; }
00336 
00337 inline
00338 RCP<const ParameterEntryValidator>
00339 ParameterEntry::validator() const
00340 { return validator_; }
00341 
00342 } // namespace Teuchos
00343 
00344 #endif

Generated on Wed May 12 21:40:32 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7