Simbody  3.5
Value.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_VALUE_H_
2 #define SimTK_SimTKCOMMON_VALUE_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
28 
31 
32 #include <limits>
33 #include <typeinfo>
34 #include <sstream>
35 
36 namespace SimTK {
37 
42 public:
44  virtual ~AbstractValue() { }
45 
46  virtual String getTypeName() const = 0;
47  virtual String getValueAsString() const = 0;
48  virtual bool isCompatible(const AbstractValue&) const = 0;
49  virtual void compatibleAssign(const AbstractValue&) = 0;
50 
51  AbstractValue& operator=(const AbstractValue& v) { compatibleAssign(v); return *this; }
52 
53  virtual AbstractValue* clone() const = 0;
54 };
55 
56 inline std::ostream&
57 operator<<(std::ostream& o, const AbstractValue& v) { o << v.getValueAsString(); return o; }
58 
67 template <class T> class Value : public AbstractValue {
68 public:
69  Value() { } // contained value is default-constructed
70  explicit Value(const T& t) : thing(t) { }
71  // default copy, destructor
72 
73  // Define assignment explicitly here to avoid implicitly calling AbstractValue's
74  // assignment operator.
75  Value& operator=(const Value& v) { thing = v.thing; return *this; }
76 
77  Value& operator=(const T& t) { thing=t; return *this; }
78  operator const T&() const { return thing; } // automatic conversion to T
79  operator T&() { return thing; } // automatic conversion to T
80 
81  const T& get() const { return thing; }
82 
83  // two ways to assign to a new value
84  void set(const T& t) { thing = t; }
85  T& upd() { return thing; }
86 
87  bool isCompatible(const AbstractValue& v) const { return isA(v); }
90  *this = downcast(v);
91  }
93  // TODO: should have some general way to serialize these.
95  { return "Value<" + getTypeName() + ">"; }
96 
97  AbstractValue* clone() const { return new Value(*this); }
99 protected:
100  T thing;
101 };
102 
103 
104 
105 } // namespace SimTK
106 
107 #endif // SimTK_SimTKCOMMON_VALUE_H_
Value & operator=(const T &t)
Definition: Value.h:77
#define SimTK_DOWNCAST(Derived, Parent)
Add public static method declaration in class derived from an abstract parent to assist in downcastin...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:554
virtual void compatibleAssign(const AbstractValue &)=0
Value()
Definition: Value.h:69
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
static std::string namestr()
The default implementation of namestr() attempts to return a nicely demangled type name on all platfo...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:769
T & upd()
Definition: Value.h:85
void compatibleAssign(const AbstractValue &v)
Definition: Value.h:88
AbstractValue & operator=(const AbstractValue &v)
Definition: Value.h:51
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:313
virtual AbstractValue * clone() const =0
virtual String getTypeName() const =0
Definition: Exception.h:260
String getTypeName() const
Definition: Value.h:92
virtual String getValueAsString() const =0
std::ostream & operator<<(std::ostream &o, const ContactForce &f)
Definition: CompliantContactSubsystem.h:387
Value & operator=(const Value &v)
Definition: Value.h:75
String getValueAsString() const
Definition: Value.h:94
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
Abstract base class representing an arbitrary value of self-describing type.
Definition: Value.h:41
Mandatory first inclusion for any Simbody source or header file.
AbstractValue * clone() const
Definition: Value.h:97
T thing
Definition: Value.h:100
AbstractValue()
Definition: Value.h:43
bool isCompatible(const AbstractValue &v) const
Definition: Value.h:87
virtual bool isCompatible(const AbstractValue &) const =0
Value(const T &t)
Definition: Value.h:70
virtual ~AbstractValue()
Definition: Value.h:44
Templatized version of the abstract class, providing generic type-specific functionality that does no...
Definition: Value.h:67