DSPatch  v.5.01
SignalBus.h
1 /************************************************************************
2 DSPatch - The C++ Flow-Based Programming Framework
3 Copyright (c) 2012-2018 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 LICENSE included in the packaging of this file.
11 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 #pragma once
26 
27 #include <dspatch/Signal.h>
28 
29 #include <vector>
30 
31 namespace DSPatch
32 {
33 
34 namespace internal
35 {
36 class SignalBus;
37 }
38 
40 
48 class DLLEXPORT SignalBus final
49 {
50 public:
51  NONCOPYABLE( SignalBus );
52  DEFINE_PTRS( SignalBus );
53 
54  SignalBus();
55  SignalBus( SignalBus&& );
56  ~SignalBus();
57 
58  void SetSignalCount( int signalCount );
59  int GetSignalCount() const;
60 
61  Signal::SPtr const& GetSignal( int signalIndex ) const;
62 
63  bool HasValue( int signalIndex ) const;
64 
65  template <class ValueType>
66  ValueType* GetValue( int signalIndex ) const;
67 
68  template <class ValueType>
69  bool SetValue( int signalIndex, ValueType const& newValue );
70 
71  bool CopySignal( int toSignalIndex, Signal::SPtr const& fromSignal );
72  bool MoveSignal( int toSignalIndex, Signal::SPtr const& fromSignal );
73 
74  void ClearAllValues();
75 
76  std::type_info const& GetType( int signalIndex ) const;
77 
78 private:
79  std::vector<Signal::SPtr> _signals;
80 
81  std::unique_ptr<internal::SignalBus> p;
82 };
83 
84 template <class ValueType>
85 ValueType* SignalBus::GetValue( int signalIndex ) const
86 {
87  if ( (size_t)signalIndex < _signals.size() )
88  {
89  return _signals[signalIndex]->GetValue<ValueType>();
90  }
91  else
92  {
93  return nullptr;
94  }
95 }
96 
97 template <class ValueType>
98 bool SignalBus::SetValue( int signalIndex, ValueType const& newValue )
99 {
100  if ( (size_t)signalIndex < _signals.size() )
101  {
102  _signals[signalIndex]->SetValue( newValue );
103  return true;
104  }
105  else
106  {
107  return false;
108  }
109 }
110 
111 } // namespace DSPatch
Signal container.
Definition: SignalBus.h:48