Value.h

Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_VALUE_H_
00002 #define SimTK_SimTKCOMMON_VALUE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTKcommon                               *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00035 #include "SimTKcommon/internal/common.h"
00036 
00037 #include "SimTKcommon/internal/String.h"
00038 #include "SimTKcommon/internal/Exception.h"
00039 
00040 #include <limits>
00041 #include <typeinfo>
00042 #include <sstream>
00043 
00044 namespace SimTK {
00045 
00049 class AbstractValue {
00050 public:
00051     AbstractValue() { }
00052     virtual ~AbstractValue() { }
00053 
00054     virtual String      getTypeName() const = 0;    
00055     virtual String      getValueAsString() const = 0;
00056     virtual bool        isCompatible(const AbstractValue&) const = 0;
00057     virtual void compatibleAssign(const AbstractValue&) = 0;
00058         
00059     AbstractValue& operator=(const AbstractValue& v) { compatibleAssign(v); return *this; }
00060     
00061     virtual AbstractValue* clone() const = 0;
00062 };
00063 
00064 inline std::ostream& 
00065 operator<<(std::ostream& o, const AbstractValue& v) { o << v.getValueAsString(); return o; }
00066 
00075 template <class T> class Value : public AbstractValue {
00076 public:
00077     Value() { } // contained value is default-constructed
00078     explicit Value(const T& t) : thing(t) { }
00079     // default copy, destructor
00080 
00081     // Define assignment explicitly here to avoid implicitly calling AbstractValue's
00082     // assignment operator.    
00083     Value& operator=(const Value& v) { thing = v.thing; return *this; }
00084  
00085     Value& operator=(const T& t) { thing=t; return *this; }
00086     operator const T&() const    { return thing; } // automatic conversion to T
00087     operator T&()                { return thing; } // automatic conversion to T
00088     
00089     const T& get()      const { return thing; }
00090   
00091     // two ways to assign to a new value
00092     void     set(const T& t)  { thing = t; }    
00093     T&       upd()            { return thing; }
00094 
00095     bool isCompatible(const AbstractValue& v) const { return isA(v); }        
00096     void compatibleAssign(const AbstractValue& v) {
00097         if (!isA(v)) SimTK_THROW2(Exception::IncompatibleValues,v.getTypeName(),getTypeName());
00098         *this = downcast(v);
00099     }
00100     String getTypeName() const { return NiceTypeName<T>::name(); }
00101     String getValueAsString() const 
00102     { std::ostringstream s; s << thing; return s.str(); }
00103     
00104     AbstractValue* clone() const { return new Value(*this); }
00105     SimTK_DOWNCAST(Value,AbstractValue);
00106 protected:
00107     T thing;
00108 };
00109 
00110 
00111 
00112 } // namespace SimTK
00113 
00114 #endif // SimTK_SimTKCOMMON_VALUE_H_

Generated on Thu Aug 12 16:37:33 2010 for SimTKcore by  doxygen 1.6.1