b5485ac2b3e8f98cc144eb66777e15644b1bf76e
[ardour.git] / libs / backends / wavesaudio / waves_dataport.cc
1 /*
2     Copyright (C) 2014 Waves Audio Ltd.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include "waves_dataport.h"
22
23 using namespace ARDOUR;
24
25 WavesDataPort::WavesDataPort (const std::string& inport_name, PortFlags inflags)
26     : _name (inport_name)
27     , _flags (inflags)
28 {
29     _capture_latency_range.min = 
30     _capture_latency_range.max = 
31     _playback_latency_range.min = 
32     _playback_latency_range.max = 0;
33 }
34
35
36 WavesDataPort::~WavesDataPort ()
37 {
38     _disconnect_all ();
39 }
40
41
42 int WavesDataPort::connect (WavesDataPort *port)
43 {
44     if (!port) {
45         std::cerr << "WavesDataPort::connect (): invalid (null) port to connect to!" << std::endl;
46         return -1;
47     }
48
49     if (type () != port->type ())    {
50         std::cerr << "WavesDataPort::connect (): wrong type of the port to connect to!" << std::endl;
51         return -1;
52     }
53
54     if (is_output () && port->is_output ()) {
55         std::cerr << "WavesDataPort::connect (): attempt to connect output port to output port!" << std::endl;
56         return -1;
57     }
58
59     if (is_input () && port->is_input ()) {
60         std::cerr << "WavesDataPort::connect (): attempt to connect input port to input port!" << std::endl;
61         return -1;
62     }
63
64     if (this == port) {
65         std::cerr << "WavesDataPort::connect (): attempt to connect port to itself!" << std::endl;
66         return -1; 
67     }
68
69     if (is_connected (port)) {
70         // std::cerr << "WavesDataPort::connect (): the ports are already connected!" << std::endl;
71         return -1;
72     }
73
74     _connect (port, true);
75     return 0;
76 }
77
78
79 void WavesDataPort::_connect (WavesDataPort *port, bool api_call)
80 {
81     _connections.push_back (port);
82     if (api_call) {
83         port->_connect (this, false);
84     }
85 }
86
87
88 int WavesDataPort::disconnect (WavesDataPort *port)
89 {
90     if (port == NULL) {
91         std::cerr << "WavesDataPort::disconnect (): invalid (null) port to disconnect from!" << std::endl;
92         return -1;
93     }
94
95     if (!is_connected (port)) {
96         std::cerr << "WavesDataPort::disconnect (): the ports are not connected!" << std::endl;
97         return -1;
98     }
99
100     _disconnect (port, true);
101
102     return 0;
103 }
104
105
106 void WavesDataPort::_disconnect (WavesDataPort *port, bool api_call)
107 {
108     std::vector<WavesDataPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
109     
110     if (it != _connections.end ()) { // actually, it's supposed to be always true.
111         _connections.erase (it);
112     }
113
114     if (api_call) {
115         port->_disconnect (this, false);
116     }
117
118         if (is_input() && _connections.empty()) {
119                 _wipe_buffer();
120         }
121 }
122
123
124 void WavesDataPort::disconnect_all ()
125 {
126     _disconnect_all ();
127     
128         if (is_input()) {
129                 _wipe_buffer();
130         }
131 }
132
133 void WavesDataPort::_disconnect_all ()
134 {
135     while (!_connections.empty ()) {
136         _connections.back ()->_disconnect (this, false);
137         _connections.pop_back ();
138     }
139 }
140
141 bool WavesDataPort::is_physically_connected () const
142 {
143     for (std::vector<WavesDataPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
144         if ((*it)->is_physical ()) {
145             return true;
146         }
147     }
148
149     return false;
150 }