DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspRunType.h
1 /************************************************************************
2 DSPatch - Cross-Platform, Object-Oriented, Flow-Based Programming Library
3 Copyright (c) 2012-2013 Marcus Tomlinson
4 
5 This file is part of DSPatch.
6 
7 GNU Lesser General Public License Usage
8 This file may be used under the terms of the GNU Lesser General Public
9 License version 3.0 as published by the Free Software Foundation and
10 appearing in the file LGPLv3.txt included in the packaging of this
11 file. Please review the following information to ensure the GNU Lesser
12 General Public License version 3.0 requirements will be met:
13 http://www.gnu.org/copyleft/lgpl.html.
14 
15 Other Usage
16 Alternatively, this file may be used in accordance with the terms and
17 conditions contained in a signed written agreement between you and
18 Marcus Tomlinson.
19 
20 DSPatch is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 ************************************************************************/
24 
25 #ifndef DSPRUNTYPE_H
26 #define DSPRUNTYPE_H
27 
28 //-------------------------------------------------------------------------------------------------
29 
30 #include <utility>
31 #include <typeinfo>
32 
33 //=================================================================================================
35 
46 {
47 public:
48  DspRunType()
49  : _valueHolder( NULL ) {}
50 
51  template< typename ValueType >
52  DspRunType( const ValueType& value )
53  {
54  _valueHolder = new _DspRtValue< ValueType >( value );
55  }
56 
57  DspRunType( const DspRunType& other )
58  {
59  if( other._valueHolder != NULL )
60  {
61  _valueHolder = other._valueHolder->GetCopy();
62  }
63  else
64  {
65  _valueHolder = NULL;
66  }
67  }
68 
69  virtual ~DspRunType()
70  {
71  delete _valueHolder;
72  }
73 
74 public:
75  DspRunType& MoveTo( DspRunType& rhs )
76  {
77  std::swap( _valueHolder, rhs._valueHolder );
78  return *this;
79  }
80 
81  void CopyFrom( const DspRunType& rhs )
82  {
83  if( _valueHolder != NULL && rhs._valueHolder != NULL &&
84  _valueHolder->GetType() == rhs._valueHolder->GetType() )
85  {
86  _valueHolder->SetValue( rhs._valueHolder );
87  }
88  else
89  {
90  *this = rhs;
91  }
92  }
93 
94  template< typename ValueType >
95  DspRunType& operator=( const ValueType& rhs )
96  {
97  if( typeid( rhs ) == GetType() )
98  {
99  ( ( _DspRtValue< ValueType >* ) _valueHolder )->value = rhs;
100  }
101  else
102  {
103  DspRunType( rhs ).MoveTo( *this );
104  }
105  return *this;
106  }
107 
108  DspRunType& operator=( DspRunType rhs )
109  {
110  rhs.MoveTo( *this );
111  return *this;
112  }
113 
114 public:
115  bool IsEmpty() const
116  {
117  return !_valueHolder;
118  }
119 
120  const std::type_info& GetType() const
121  {
122  if( _valueHolder != NULL )
123  {
124  return _valueHolder->GetType();
125  }
126  else
127  {
128  return typeid( void );
129  }
130  }
131 
132  template< typename ValueType >
133  static ValueType* RunTypeCast( DspRunType* operand )
134  {
135  if( operand != NULL && operand->GetType() == typeid( ValueType ) )
136  {
137  return &static_cast< DspRunType::_DspRtValue< ValueType >* >( operand->_valueHolder )->value;
138  }
139  else
140  {
141  return NULL;
142  }
143  }
144 
145  template< typename ValueType >
146  static inline const ValueType* RunTypeCast( const DspRunType* operand )
147  {
148  return RunTypeCast< ValueType >( const_cast< DspRunType* >( operand ) );
149  }
150 
151 private:
152  class _DspRtValueHolder
153  {
154  public:
155  virtual ~_DspRtValueHolder() {}
156 
157  public:
158  virtual const std::type_info& GetType() const = 0;
159  virtual _DspRtValueHolder* GetCopy() const = 0;
160  virtual void SetValue( _DspRtValueHolder* valueHolder ) = 0;
161  };
162 
163  template< typename ValueType >
164  class _DspRtValue : public _DspRtValueHolder
165  {
166  public:
167  _DspRtValue( const ValueType& value )
168  : value( value ) {}
169 
170  public:
171  virtual const std::type_info& GetType() const
172  {
173  return typeid( ValueType );
174  }
175 
176  virtual _DspRtValueHolder* GetCopy() const
177  {
178  return new _DspRtValue( value );
179  }
180 
181  void SetValue( _DspRtValueHolder* valueHolder )
182  {
183  value = ( ( _DspRtValue< ValueType >* ) valueHolder )->value;
184  }
185 
186  public:
187  ValueType value;
188 
189  private:
190  _DspRtValue& operator=( const _DspRtValue& ); // disable copy-assignment
191  };
192 
193 private:
194  _DspRtValueHolder* _valueHolder;
195 };
196 
197 //=================================================================================================
198 
199 #endif // DSPRUNTYPE_H