Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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 (type, flags)
39 {
40         set_name (str);
41 }
42
43 InternalPort::~InternalPort ()
44 {
45         disconnect ();
46 }
47
48 void
49 InternalPort::set_latency (nframes_t val)
50 {
51         _latency = val;
52 }
53
54 bool
55 InternalPort::connected_to (const string& portname) const
56 {
57         /* caller must hold process lock */
58
59         for (list<InternalPort*>::const_iterator p = _connections.begin(); p != _connections.end(); ++p) {
60                 if ((*p)->name() == portname) {
61                         return true;
62                 }
63         }
64
65         return false;
66 }
67
68 const char** 
69 InternalPort::get_connections () const
70 {
71         /* caller must hold process lock */
72
73         int i;
74         list<InternalPort*>::const_iterator p;
75
76         if (_connections.empty()) {
77                 return 0;
78         }
79
80         char **names = (char**) malloc (sizeof (char*) * ( _connections.size() + 1));
81         
82
83         for (i = 0, p = _connections.begin(); p != _connections.end(); ++p, ++i) {
84                 names[i] = (char*) (*p)->name().c_str();
85         }
86
87         names[i] = 0;
88
89         return (const char**) names;
90 }
91
92 int
93 InternalPort::connected() const 
94 {
95         /* caller must hold process lock */
96         return !_connections.empty();
97 }
98
99 int
100 InternalPort::set_name (string str)
101 {
102         _name = "internal:";
103         _name += str;
104
105         return 0;
106 }
107
108 string
109 InternalPort::short_name () 
110 {
111         return _name.substr (9);
112 }
113
114 void
115 InternalPort::connect (InternalPort& src, InternalPort& dst)
116 {
117         /* caller must hold process lock */
118
119         src._connections.push_back (&dst);
120         dst._connections.push_back (&src);
121 }
122
123 void
124 InternalPort::disconnect (InternalPort& a, InternalPort& b)
125 {
126         /* caller must hold process lock */
127         a._connections.remove (&b);
128         b._connections.remove (&a);
129 }
130
131 int
132 InternalPort::disconnect ()
133 {
134         /* caller must hold process lock */
135
136         for (list<InternalPort*>::const_iterator p = _connections.begin(); p != _connections.end(); ) {
137                 list<InternalPort*>::const_iterator tmp;
138
139                 tmp = p;
140                 ++tmp;
141                        
142                 disconnect (*this, **p);
143                 
144                 p = tmp;
145         }
146
147         _connections.clear ();
148         
149         return 0;
150 }
151
152 int
153 InternalPort::reestablish ()
154 {
155         return 0;
156 }
157
158 void
159 InternalPort::recompute_total_latency () const
160 {
161         return;
162 }
163