DSPatch v.9.2.0
Loading...
Searching...
No Matches
SignalBus.h
1/******************************************************************************
2DSPatch - The Refreshingly Simple C++ Dataflow Framework
3Copyright (c) 2023, Marcus Tomlinson
4
5BSD 2-Clause License
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are met:
9
101. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12
132. 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF 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 <deque>
34
35namespace DSPatch
36{
37
38namespace internal
39{
40class SignalBus;
41}
42
44
52class DLLEXPORT SignalBus final
53{
54public:
55 NONCOPYABLE( SignalBus );
56
57 SignalBus();
59 ~SignalBus();
60
61 void SetSignalCount( int signalCount );
62 int GetSignalCount() const;
63
64 Signal& GetSignal( int signalIndex );
65
66 bool HasValue( int signalIndex ) const;
67
68 template <class ValueType>
69 ValueType* GetValue( int signalIndex );
70
71 template <class ValueType>
72 bool SetValue( int signalIndex, ValueType const& newValue );
73
74 template <class ValueType>
75 bool MoveValue( int signalIndex, ValueType&& newValue );
76
77 bool SetSignal( int toSignalIndex, Signal const& fromSignal );
78 bool MoveSignal( int toSignalIndex, Signal& fromSignal );
79
80 void ClearAllValues();
81
82 std::type_info const& GetType( int signalIndex ) const;
83
84private:
85 std::deque<Signal> _signals;
86
87 std::unique_ptr<internal::SignalBus> p;
88};
89
90template <class ValueType>
91ValueType* SignalBus::GetValue( int signalIndex )
92{
93 if ( (size_t)signalIndex < _signals.size() )
94 {
95 return _signals[signalIndex].GetValue<ValueType>();
96 }
97 else
98 {
99 return nullptr;
100 }
101}
102
103template <class ValueType>
104bool SignalBus::SetValue( int signalIndex, ValueType const& newValue )
105{
106 if ( (size_t)signalIndex < _signals.size() )
107 {
108 _signals[signalIndex].SetValue( newValue );
109 return true;
110 }
111 else
112 {
113 return false;
114 }
115}
116
117template <class ValueType>
118bool SignalBus::MoveValue( int signalIndex, ValueType&& newValue )
119{
120 if ( (size_t)signalIndex < _signals.size() )
121 {
122 _signals[signalIndex].MoveValue( std::move( newValue ) );
123 return true;
124 }
125 else
126 {
127 return false;
128 }
129}
130
131} // namespace DSPatch
Signal container.
Definition: SignalBus.h:53
Value container used to carry data between components.
Definition: Signal.h:49