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/List.h" 00038 #include "SimTKcommon/internal/String.h" 00039 #include "SimTKcommon/internal/Exception.h" 00040 00041 #include <limits> 00042 #include <typeinfo> 00043 #include <sstream> 00044 00045 namespace SimTK { 00046 00050 class AbstractValue { 00051 public: 00052 AbstractValue() { } 00053 virtual ~AbstractValue() { } 00054 00055 virtual String getTypeName() const = 0; 00056 virtual String getValueAsString() const = 0; 00057 virtual bool isCompatible(const AbstractValue&) const = 0; 00058 virtual void compatibleAssign(const AbstractValue&) = 0; 00059 00060 AbstractValue& operator=(const AbstractValue& v) { compatibleAssign(v); return *this; } 00061 00062 virtual AbstractValue* clone() const = 0; 00063 }; 00064 00065 inline std::ostream& 00066 operator<<(std::ostream& o, const AbstractValue& v) { o << v.getValueAsString(); return o; } 00067 00072 template <class T> class ValueHelper : public AbstractValue { 00073 public: 00074 ValueHelper() { } // contained value is default-constructed 00075 explicit ValueHelper(const T& t) : thing(t) { } 00076 // default copy, destructor 00077 00078 // Define assignment explicitly here to avoid implicitly calling AbstractValue's 00079 // assignment operator. 00080 ValueHelper& operator=(const ValueHelper& v) { thing = v.thing; return *this; } 00081 00082 const T& get() const { return thing; } 00083 00084 // two ways to assign to a new value 00085 void set(const T& t) { thing = t; } 00086 T& upd() { return thing; } 00087 00088 bool isCompatible(const AbstractValue& v) const { return isA(v); } 00089 void compatibleAssign(const AbstractValue& v) { 00090 if (!isA(v)) SimTK_THROW2(Exception::IncompatibleValues,v.getTypeName(),getTypeName()); 00091 *this = downcast(v); 00092 } 00093 String getTypeName() const { return TypeInfo<T>::name(); } 00094 String getValueAsString() const 00095 { std::ostringstream s; s << thing; return s.str(); } 00096 00097 AbstractValue* clone() const { return new ValueHelper(*this); } 00098 SimTK_DOWNCAST(ValueHelper,AbstractValue); 00099 protected: 00100 T thing; 00101 }; 00102 00107 template <class T> class Value : public ValueHelper<T> { 00108 public: 00109 Value() { } 00110 explicit Value(const T& t) : ValueHelper<T>(t) { } 00111 // default copy, assignment, destructor 00112 00113 // "this->" kludge below appears to be required by gcc 3.4.4. 00114 Value& operator=(const T& t) { this->thing=t; return *this; } 00115 operator const T&() const { return this->thing; } // automatic conversion to T 00116 operator T&() { return this->thing; } // automatic conversion to T 00117 00118 SimTK_DOWNCAST2(Value,ValueHelper<T>,AbstractValue); 00119 private: 00120 // NO DATA MEMBERS ALLOWED 00121 }; 00122 00123 00124 } // namespace SimTK 00125 00126 #endif // SimTK_SimTKCOMMON_VALUE_H_