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, bool isDefault, bool isList
00257   ,const std::string &docString
00258   ,RCP<const ParameterEntryValidator> const& validator
00259   )
00260   : val_(value),
00261     isUsed_(false),
00262     isDefault_(isDefault),
00263     docString_(docString),
00264     validator_(validator)
00265 {}
00266 
00267 inline
00268 ParameterEntry::~ParameterEntry()
00269 {}
00270 
00271 // Set Methods
00272 
00273 template<typename T>
00274 inline
00275 void ParameterEntry::setValue(
00276   T value, bool isDefault, const std::string &docString
00277   ,RCP<const ParameterEntryValidator> const& validator
00278   )
00279 {
00280   val_ = value;
00281   isDefault_ = isDefault;
00282   if(docString.length())
00283     docString_ = docString;
00284   if(validator.get())
00285     validator_ = validator;
00286 }
00287 
00288 // Get Methods
00289 
00290 template<typename T>
00291 inline
00292 T& ParameterEntry::getValue(T *ptr) const
00293 {
00294   isUsed_ = true;
00295   return const_cast<T&>(Teuchos::any_cast<T>( val_ ));
00296 }
00297 
00298 inline
00299 any& ParameterEntry::getAny(bool activeQry)
00300 { 
00301   if (activeQry == true) {
00302     isUsed_ = true;
00303   }
00304   return val_; 
00305 }
00306 
00307 inline
00308 const any& ParameterEntry::getAny(bool activeQry) const
00309 { 
00310   if (activeQry == true) {
00311     isUsed_ = true;
00312   }
00313   return val_; 
00314 }
00315 
00316 // Attribute Methods
00317 
00318 inline
00319 bool ParameterEntry::isUsed() const
00320 { return isUsed_; }
00321 
00322 template <typename T>
00323 inline
00324 bool ParameterEntry::isType() const
00325 {
00326   bool match = ( val_.type() == typeid(T) );
00327 #ifdef HAVE_SHARED
00328   // For shared libraries, the above may resolve as false because one
00329   // of the types is not fully qualified.  The following check will
00330   // compensate for that.
00331   if ( !match )
00332     match = ( strcmp(val_.type().name(), typeid(T).name()) == 0 );
00333 #endif
00334   return match;
00335 }
00336 
00337 inline
00338 bool ParameterEntry::isDefault() const
00339 { return isDefault_; }
00340 
00341 inline
00342 std::string ParameterEntry::docString() const
00343 { return docString_; }
00344 
00345 inline
00346 RCP<const ParameterEntryValidator>
00347 ParameterEntry::validator() const
00348 { return validator_; }
00349 
00350 } // namespace Teuchos
00351 
00352 #endif

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7