Fix a tiny memory-leak when calling vfork
[ardour.git] / libs / ardour / user_bundle.cc
1 /*
2     Copyright (C) 2012 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 "ardour/user_bundle.h"
21 #include "ardour/types_convert.h"
22 #include "pbd/i18n.h"
23 #include "pbd/compose.h"
24 #include "pbd/error.h"
25 #include "pbd/failed_constructor.h"
26 #include "pbd/xml++.h"
27
28 ARDOUR::UserBundle::UserBundle (std::string const & n)
29         : Bundle (n)
30 {
31
32 }
33
34 ARDOUR::UserBundle::UserBundle (XMLNode const & node, bool i)
35         : Bundle (i)
36 {
37         if (set_state (node, Stateful::loading_state_version)) {
38                 throw failed_constructor ();
39         }
40 }
41
42 int
43 ARDOUR::UserBundle::set_state (XMLNode const & node, int /*version*/)
44 {
45         std::string str;
46         if (!node.get_property ("name", str)) {
47                 PBD::error << _("Node for Bundle has no \"name\" property") << endmsg;
48                 return -1;
49         }
50
51         set_name (str);
52
53         XMLNodeList const channels = node.children ();
54
55         int n = 0;
56         for (XMLNodeConstIterator i = channels.begin(); i != channels.end(); ++i) {
57
58                 if ((*i)->name() != "Channel") {
59                         PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*i)->name()) << endmsg;
60                         return -1;
61                 }
62
63                 if (!(*i)->get_property ("name", str)) {
64                         PBD::error << _("Node for Channel has no \"name\" property") << endmsg;
65                         return -1;
66                 }
67
68                 DataType type(DataType::NIL);
69                 if (!(*i)->get_property ("type", type)) {
70                         PBD::error << _("Node for Channel has no \"type\" property") << endmsg;
71                         return -1;
72                 }
73
74                 add_channel (str, type);
75
76                 XMLNodeList const ports = (*i)->children ();
77
78                 for (XMLNodeConstIterator j = ports.begin(); j != ports.end(); ++j) {
79                         if ((*j)->name() != "Port") {
80                                 PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*j)->name()) << endmsg;
81                                 return -1;
82                         }
83
84                         if (!(*j)->get_property ("name", str)) {
85                                 PBD::error << _("Node for Port has no \"name\" property") << endmsg;
86                                 return -1;
87                         }
88
89                         add_port_to_channel (n, str);
90                 }
91
92                 ++n;
93         }
94
95         return 0;
96 }
97
98 XMLNode&
99 ARDOUR::UserBundle::get_state ()
100 {
101         XMLNode *node;
102
103         if (ports_are_inputs ()) {
104                 node = new XMLNode ("InputBundle");
105         } else {
106                 node = new XMLNode ("OutputBundle");
107         }
108
109         node->set_property ("name", name ());
110
111         {
112                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
113
114                 for (std::vector<Channel>::iterator i = _channel.begin(); i != _channel.end(); ++i) {
115                         XMLNode* c = new XMLNode ("Channel");
116                         c->set_property ("name", i->name);
117                         c->set_property ("type", i->type);
118
119                         for (PortList::iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
120                                 XMLNode* p = new XMLNode ("Port");
121                                 p->set_property ("name", *j);
122                                 c->add_child_nocopy (*p);
123                         }
124
125                         node->add_child_nocopy (*c);
126                 }
127         }
128
129         return *node;
130 }