Simbody  3.6
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-15 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 #include <utility>
36 
37 namespace SimTK {
38  // Forward declaration.
39  template<typename T> class Value;
40 
41 //==============================================================================
42 // ABSTRACT VALUE
43 //==============================================================================
50 public:
52  virtual AbstractValue* clone() const = 0;
53 
56  virtual String getTypeName() const = 0;
57 
60  virtual String getValueAsString() const = 0;
61 
65  virtual bool isCompatible(const AbstractValue& other) const = 0;
66 
69  virtual void compatibleAssign(const AbstractValue& source) = 0;
70 
74  { compatibleAssign(v); return *this; }
75 
78  template <typename T>
79  const T& getValue() const {
80  return Value<T>::downcast(*this).get();
81  }
82 
85  template <typename T>
86  T& updValue() {
87  return Value<T>::updDowncast(*this).upd();
88  }
89 
90  virtual ~AbstractValue() {}
91 };
92 
96 inline std::ostream& operator<<(std::ostream& o, const AbstractValue& v)
97 { o << v.getValueAsString(); return o; }
98 
99 
100 
101 //==============================================================================
102 // VALUE <T>
103 //==============================================================================
107 template <class T> class Value : public AbstractValue {
108 public:
111  Value() {}
112 
115  explicit Value(const T& value) : m_thing(value) {}
116 
120  explicit Value(const T&& value) : m_thing(std::move(value)) {}
121 
122  ~Value() = default;
123 
126  Value(const Value& value) : m_thing(value.m_thing) {}
127 
130  Value(const Value&& value) : m_thing(std::move(value.m_thing)) {}
131 
134  Value& operator=(const Value& value)
135  { m_thing = value.m_thing; return *this; }
136 
141  Value& operator=(const Value&& value)
142  { m_thing = std::move(value.m_thing); return *this; }
143 
146  Value& operator=(const T& value)
147  { m_thing = value; return *this; }
148 
151  Value& operator=(const T&& value)
152  { m_thing = std::move(value); return *this; }
153 
157  const T& get() const {return m_thing;}
158 
162  T& upd() {return m_thing;}
163 
166  void set(const T& value) {m_thing = value;}
167 
170  void set(const T&& value) {m_thing = std::move(value);}
171 
175  operator const T&() const {return get();}
176 
180  operator T&() {return upd();}
181 
185  Value* clone() const override {return new Value(*this);}
186 
190  bool isCompatible(const AbstractValue& value) const override
191  { return isA(value); }
192 
195  void compatibleAssign(const AbstractValue& value) override {
196  if (!isA(value))
198  getTypeName());
199  *this = downcast(value);
200  }
201 
204  String getTypeName() const override {return NiceTypeName<T>::namestr();}
205 
208  String getValueAsString() const override
209  { return "Value<" + getTypeName() + ">"; }
210 
213  static bool isA(const AbstractValue& value)
214  { return dynamic_cast<const Value*>(&value) != nullptr; }
215 
219  static const Value& downcast(const AbstractValue& value)
220  { return SimTK_DYNAMIC_CAST_DEBUG<const Value&>(value); }
221 
226  { return SimTK_DYNAMIC_CAST_DEBUG<Value&>(value); }
227 
229  static Value& downcast(AbstractValue& value) {return updDowncast(value);}
230 
231 private:
232  T m_thing;
233 };
234 
235 
236 
237 } // namespace SimTK
238 
239 #endif // SimTK_SimTKCOMMON_VALUE_H_
Value * clone() const override
Covariant implementation of the AbstractValue::clone() method.
Definition: Value.h:185
static const Value & downcast(const AbstractValue &value)
Downcast a const reference to an AbstractValue to a const reference to this type Value<T>.
Definition: Value.h:219
static Value & updDowncast(AbstractValue &value)
Downcast a writable reference to an AbstractValue to a writable reference to this type Value<T>...
Definition: Value.h:225
Value()
Creates a Value<T> whose contained object of type T has been default constructed. ...
Definition: Value.h:111
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
Value(const T &&value)
Creates a Value<T> whose contained object is move constructed from the given value.
Definition: Value.h:120
std::ostream & operator<<(std::ostream &o, const AbstractValue &v)
Write a human-readable representation of an AbstractValue to an output stream, using the getValueAsSt...
Definition: Value.h:96
T & updValue()
Retrieve the original (type-erased)thing as read-write.
Definition: Value.h:86
bool isCompatible(const AbstractValue &value) const override
Test whether a given AbstractValue is assignment-compatible with this Value object.
Definition: Value.h:190
virtual String getValueAsString() const =0
Return a human-readable representation of the value stored in this AbstractValue object.
STL namespace.
static const std::string & namestr()
The default implementation of namestr() attempts to return a nicely demangled and canonicalized type ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:867
Value(const T &value)
Creates a Value<T> whose contained object is copy constructed from the given value.
Definition: Value.h:115
T & upd()
Return a writable reference to the object of type T that is contained in this Value object...
Definition: Value.h:162
AbstractValue & operator=(const AbstractValue &v)
Invokes the compatibleAssign() method which will perform the assignment if the source object is compa...
Definition: Value.h:73
Value(const Value &&value)
Move constructor invokes the type T move constructor, if available, to move the given value into this...
Definition: Value.h:130
Value & operator=(const T &&value)
Assign a new value to the contained object, using the type T move assignment operator, if available.
Definition: Value.h:151
Value & operator=(const Value &&value)
The move assignment here invokes type T move assignment on the contained object, it does not invoke A...
Definition: Value.h:141
Value & operator=(const T &value)
Assign a new value to the contained object, using the type T copy assignment operator.
Definition: Value.h:146
Value & operator=(const Value &value)
The copy assignment here invokes type T copy assignment on the contained object, it does not invoke A...
Definition: Value.h:134
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:318
virtual String getTypeName() const =0
Return a human-readable form of the object type that is stored in the concrete derived class underlyi...
const T & getValue() const
Retrieve the original (type-erased)thing as read-only.
Definition: Value.h:79
void compatibleAssign(const AbstractValue &value) override
If the given AbstractValue is assignment-compatible, perform the assignment.
Definition: Value.h:195
String getTypeName() const override
Use NiceTypeName to produce a human-friendly representation of the type T.
Definition: Value.h:204
String getValueAsString() const override
(Not implemented yet) Produce a human-friendly representation of the contained value of type T...
Definition: Value.h:208
static bool isA(const AbstractValue &value)
Return true if the given AbstractValue is an object of this type Value<T>.
Definition: Value.h:213
Definition: Exception.h:261
const T & get() const
Return a const reference to the object of type T that is contained in this Value<T> object...
Definition: Value.h:157
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
static Value & downcast(AbstractValue &value)
Deprecated – use updDowncast() instead.
Definition: Value.h:229
Abstract base class representing an arbitrary value of unknown type.
Definition: Value.h:49
Mandatory first inclusion for any Simbody source or header file.
virtual AbstractValue * clone() const =0
Create a deep copy of this object.
Value(const Value &value)
Copy constructor invokes the type T copy constructor to copy the contained value. ...
Definition: Value.h:126
virtual bool isCompatible(const AbstractValue &other) const =0
Check whether the other object contains a value that is assignment- compatible with this one...
virtual void compatibleAssign(const AbstractValue &source)=0
If the source contains a compatible value, assign a copy of that value into this object.
virtual ~AbstractValue()
Definition: Value.h:90
Concrete templatized class derived from AbstractValue, adding generic value type-specific functionali...
Definition: Value.h:39