DSPatch  v.2.42
C++ Cross-Platform, Object-Oriented, Flow-Based Programming Library
 All Classes Pages
DspWireBus.cpp
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 #include "DspWireBus.h"
26 #include "DspComponent.h"
27 #include "DspWire.h"
28 
29 //=================================================================================================
30 
31 DspWireBus::DspWireBus( bool isLinkedComponentReceivingSignals )
32 : _isLinkedComponentReceivingSignals( isLinkedComponentReceivingSignals ) {}
33 
34 //-------------------------------------------------------------------------------------------------
35 
36 DspWireBus::~DspWireBus()
37 {
38  RemoveAllWires();
39 }
40 
41 //=================================================================================================
42 
43 bool DspWireBus::AddWire( DspComponent* linkedComponent, unsigned short fromSignalIndex, unsigned short toSignalIndex )
44 {
45  for( unsigned short i = 0; i < _wires.size(); i++ )
46  {
47  if( _wires[i].linkedComponent == linkedComponent &&
48  _wires[i].fromSignalIndex == fromSignalIndex &&
49  _wires[i].toSignalIndex == toSignalIndex )
50  {
51  return false; // wire already exists
52  }
53  }
54 
55  for( unsigned short i = 0; i < _wires.size(); i++ )
56  {
57  if( _isLinkedComponentReceivingSignals &&
58  _wires[i].linkedComponent == linkedComponent &&
59  _wires[i].toSignalIndex == toSignalIndex ) // if there's a wire to the receiving component's input already
60  {
61  RemoveWire( i ); // remove the wire (only one wire can connect to an input at a time)
62  break;
63  }
64  else if( !_isLinkedComponentReceivingSignals &&
65  _wires[i].toSignalIndex == toSignalIndex )
66  {
67  RemoveWire( i ); // remove the wire (only one wire can connect to an input at a time)
68  break;
69  }
70  }
71 
72  _wires.push_back( DspWire( linkedComponent, fromSignalIndex, toSignalIndex ) );
73 
74  return true;
75 }
76 
77 //-------------------------------------------------------------------------------------------------
78 
79 bool DspWireBus::RemoveWire( DspComponent* linkedComponent, unsigned short fromSignalIndex, unsigned short toSignalIndex )
80 {
81  for( unsigned short i = 0; i < _wires.size(); i++ )
82  {
83  if( _wires[i].linkedComponent == linkedComponent &&
84  _wires[i].fromSignalIndex == fromSignalIndex &&
85  _wires[i].toSignalIndex == toSignalIndex )
86  {
87  RemoveWire( i );
88  return true;
89  }
90  }
91 
92  return false;
93 }
94 
95 //-------------------------------------------------------------------------------------------------
96 
97 bool DspWireBus::RemoveWire( unsigned short wireIndex )
98 {
99  if( wireIndex > _wires.size() )
100  {
101  return false;
102  }
103 
104  for( unsigned short j = wireIndex; j < ( _wires.size() - 1 ); j++ )
105  {
106  _wires[j] = _wires[j + 1]; // shift all other elements up
107  }
108  _wires.pop_back(); // remove end item
109 
110  return true;
111 }
112 
113 //-------------------------------------------------------------------------------------------------
114 
115 void DspWireBus::RemoveAllWires()
116 {
117  for( unsigned short i = 0; i < _wires.size(); i++ )
118  {
119  RemoveWire( i );
120  }
121 }
122 
123 //-------------------------------------------------------------------------------------------------
124 
125 DspWire* DspWireBus::GetWire( unsigned short wireIndex )
126 {
127  if( wireIndex < _wires.size() )
128  {
129  return &_wires[wireIndex];
130  }
131  else
132  {
133  return NULL;
134  }
135 }
136 
137 //-------------------------------------------------------------------------------------------------
138 
139 unsigned short DspWireBus::GetWireCount() const
140 {
141  return _wires.size();
142 }
143 
144 //=================================================================================================