DSPatch  v.7.0.1
SignalBus.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/Signal.h>
32 
33 #include <vector>
34 
35 namespace DSPatch
36 {
37 
38 namespace internal
39 {
40 class SignalBus;
41 }
42 
44 
52 class DLLEXPORT SignalBus final
53 {
54 public:
55  NONCOPYABLE( SignalBus );
56  DEFINE_PTRS( SignalBus );
57 
58  SignalBus();
59  SignalBus( SignalBus&& );
60  ~SignalBus();
61 
62  void SetSignalCount( int signalCount );
63  int GetSignalCount() const;
64 
65  Signal::SPtr const& GetSignal( int signalIndex ) const;
66 
67  bool HasValue( int signalIndex ) const;
68 
69  template <class ValueType>
70  ValueType* GetValue( int signalIndex ) const;
71 
72  template <class ValueType>
73  bool SetValue( int signalIndex, ValueType const& newValue );
74 
75  bool CopySignal( int toSignalIndex, Signal::SPtr const& fromSignal );
76  bool MoveSignal( int toSignalIndex, Signal::SPtr const& fromSignal );
77 
78  void ClearAllValues();
79 
80  std::type_info const& GetType( int signalIndex ) const;
81 
82 private:
83  std::vector<Signal::SPtr> _signals;
84 
85  std::unique_ptr<internal::SignalBus> p;
86 };
87 
88 template <class ValueType>
89 ValueType* SignalBus::GetValue( int signalIndex ) const
90 {
91  if ( (size_t)signalIndex < _signals.size() )
92  {
93  return _signals[signalIndex]->GetValue<ValueType>();
94  }
95  else
96  {
97  return nullptr;
98  }
99 }
100 
101 template <class ValueType>
102 bool SignalBus::SetValue( int signalIndex, ValueType const& newValue )
103 {
104  if ( (size_t)signalIndex < _signals.size() )
105  {
106  _signals[signalIndex]->SetValue( newValue );
107  return true;
108  }
109  else
110  {
111  return false;
112  }
113 }
114 
115 } // namespace DSPatch