DSPatch  v.7.0.1
Signal.h
1 /******************************************************************************
2 DSPatch - The Refreshingly Simple C++ Dataflow Framework
3 Copyright (c) 2020, Marcus Tomlinson
4 
5 BSD 2-Clause License
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 ******************************************************************************/
28 
29 #pragma once
30 
31 #include <dspatch/Common.h>
32 
33 namespace DSPatch
34 {
35 
37 
48 class DLLEXPORT Signal final
49 {
50 public:
51  NONCOPYABLE( Signal );
52  DEFINE_PTRS( Signal );
53 
54  Signal();
55  ~Signal();
56 
57  bool HasValue() const;
58 
59  template <class ValueType>
60  ValueType* GetValue();
61 
62  template <class ValueType>
63  void SetValue( ValueType const& newValue );
64 
65  bool CopySignal( Signal::SPtr const& fromSignal );
66  bool MoveSignal( Signal::SPtr const& fromSignal );
67 
68  void ClearValue();
69 
70  std::type_info const& GetType() const;
71 
72 private:
73  struct _ValueHolder
74  {
75  NONCOPYABLE( _ValueHolder );
76 
77  _ValueHolder() = default;
78  virtual ~_ValueHolder() = default;
79 
80  virtual std::type_info const& GetType() const = 0;
81  virtual _ValueHolder* GetCopy() const = 0;
82  virtual void SetValue( _ValueHolder* valueHolder ) = 0;
83  };
84 
85  template <class ValueType>
86  struct _Value final : _ValueHolder
87  {
88  NONCOPYABLE( _Value );
89 
90  _Value( ValueType const& value )
91  : value( value )
92  , type( typeid( ValueType ) )
93  {
94  }
95 
96  virtual std::type_info const& GetType() const override
97  {
98  return type;
99  }
100 
101  virtual _ValueHolder* GetCopy() const override
102  {
103  return new _Value( value );
104  }
105 
106  virtual void SetValue( _ValueHolder* valueHolder ) override
107  {
108  value = ( (_Value<ValueType>*)valueHolder )->value;
109  }
110 
111  ValueType value;
112  std::type_info const& type;
113  };
114 
115  _ValueHolder* _valueHolder = nullptr;
116  bool _hasValue = false;
117 };
118 
119 template <class ValueType>
120 ValueType* Signal::GetValue()
121 {
122  // You might be thinking: Why the raw pointer return here?
123 
124  // This is mainly for performance, and partly for readability. Performance, because returning a
125  // shared_ptr here means having to store the value as a shared_ptr in Signal::_Value too. This
126  // adds yet another level of indirection to the value, as well as some reference counting
127  // overhead. These Get() and Set() methods are VERY frequently called, so doing as little as
128  // possible with the data here is best, which actually aids in the readably of the code too.
129 
130  if ( _hasValue && GetType() == typeid( ValueType ) )
131  {
132  return &( (_Value<ValueType>*)_valueHolder )->value;
133  }
134  else
135  {
136  return nullptr;
137  }
138 }
139 
140 template <class ValueType>
141 void Signal::SetValue( ValueType const& newValue )
142 {
143  if ( GetType() == typeid( ValueType ) )
144  {
145  ( (_Value<ValueType>*)_valueHolder )->value = newValue;
146  }
147  else
148  {
149  delete _valueHolder;
150  _valueHolder = new _Value<ValueType>( newValue );
151  }
152  _hasValue = true;
153 }
154 
155 } // namespace DSPatch