Re-work Port construction slightly so that _flags is always initialised before reset...
[ardour.git] / libs / ardour / internal_port.cc
1 /*
2     Copyright (C) 2007 Paul Davis 
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 <pbd/error.h>
21 #include <ardour/internal_port.h>
22 #include <ardour/audioengine.h>
23
24 #include "i18n.h"
25
26 using namespace ARDOUR;
27 using namespace std;
28
29 AudioEngine* InternalPort::engine = 0;
30
31 void
32 InternalPort::set_engine (AudioEngine* e) 
33 {
34         engine = e;
35 }
36
37 InternalPort::InternalPort (const string& str, DataType type, Flags flags)
38         : Port (flags)
39 {
40         set_name (str);
41         _type = type;
42 }
43
44 InternalPort::~InternalPort ()
45 {
46         disconnect ();
47 }
48
49 void
50 InternalPort::set_latency (nframes_t val)
51 {
52         _latency = val;
53 }
54
55 bool
56 InternalPort::connected_to (const string& portname) const
57 {
58         /* caller must hold process lock */
59
60         for (list<InternalPort*>::const_iterator p = _connections.begin(); p != _connections.end(); ++p) {
61                 if ((*p)->name() == portname) {
62                         return true;
63                 }
64         }
65
66         return false;
67 }
68
69 const char** 
70 InternalPort::get_connections () const
71 {
72         /* caller must hold process lock */
73
74         int i;
75         list<InternalPort*>::const_iterator p;
76
77         if (_connections.empty()) {
78                 return 0;
79         }
80
81         char **names = (char**) malloc (sizeof (char*) * ( _connections.size() + 1));
82         
83
84         for (i = 0, p = _connections.begin(); p != _connections.end(); ++p, ++i) {
85                 names[i] = (char*) (*p)->name().c_str();
86         }
87
88         names[i] = 0;
89
90         return (const char**) names;
91 }
92
93 int
94 InternalPort::connected() const 
95 {
96         /* caller must hold process lock */
97         return !_connections.empty();
98 }
99
100 int
101 InternalPort::set_name (string str)
102 {
103         _name = "internal:";
104         _name += str;
105
106         return 0;
107 }
108
109 string
110 InternalPort::short_name () 
111 {
112         return _name.substr (9);
113 }
114
115 void
116 InternalPort::connect (InternalPort& src, InternalPort& dst)
117 {
118         /* caller must hold process lock */
119
120         src._connections.push_back (&dst);
121         dst._connections.push_back (&src);
122 }
123
124 void
125 InternalPort::disconnect (InternalPort& a, InternalPort& b)
126 {
127         /* caller must hold process lock */
128         a._connections.remove (&b);
129         b._connections.remove (&a);
130 }
131
132 int
133 InternalPort::disconnect ()
134 {
135         /* caller must hold process lock */
136
137         for (list<InternalPort*>::const_iterator p = _connections.begin(); p != _connections.end(); ) {
138                 list<InternalPort*>::const_iterator tmp;
139
140                 tmp = p;
141                 ++tmp;
142                        
143                 disconnect (*this, **p);
144                 
145                 p = tmp;
146         }
147
148         _connections.clear ();
149         
150         return 0;
151 }
152
153 int
154 InternalPort::reestablish ()
155 {
156         return 0;
157 }
158
159 void
160 InternalPort::recompute_total_latency () const
161 {
162         return;
163 }
164