+
+/** This must not be called in code executed as a response to a backend event,
+ * as it uses the backend port_get_all_connections().
+ * @return true if any of this bundle's channels are connected to anything.
+ */
+bool
+Bundle::connected_to_anything (AudioEngine& engine)
+{
+ PortManager& pm (engine);
+
+ for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
+ Bundle::PortList const & ports = channel_ports (i);
+
+ for (uint32_t j = 0; j < ports.size(); ++j) {
+
+ /* ports[j] may not be an Ardour port, so use the port manager directly
+ rather than doing it with Port.
+ */
+
+ if (pm.connected (ports[j])) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+void
+Bundle::set_ports_are_inputs ()
+{
+ _ports_are_inputs = true;
+ emit_changed (DirectionChanged);
+}
+
+void
+Bundle::set_ports_are_outputs ()
+{
+ _ports_are_inputs = false;
+ emit_changed (DirectionChanged);
+}
+
+/** Set the name.
+ * @param n New name.
+ */
+void
+Bundle::set_name (string const & n)
+{
+ _name = n;
+ emit_changed (NameChanged);
+}
+
+/** @param b Other bundle.
+ * @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
+ */
+bool
+Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
+{
+ uint32_t const N = nchannels().n_total();
+
+ if (b->nchannels().n_total() != N) {
+ return false;
+ }
+
+ /* XXX: probably should sort channel port lists before comparing them */
+
+ for (uint32_t i = 0; i < N; ++i) {
+ if (channel_ports (i) != b->channel_ports (i)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+DataType
+Bundle::channel_type (uint32_t c) const
+{
+ assert (c < nchannels().n_total());
+
+ Glib::Threads::Mutex::Lock lm (_channel_mutex);
+ return _channel[c].type;
+}
+
+ostream &
+operator<< (ostream& os, Bundle const & b)
+{
+ os << "BUNDLE " << b.nchannels() << " channels: ";
+ for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) {
+ os << "( ";
+ Bundle::PortList const & pl = b.channel_ports (i);
+ for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
+ os << *j << " ";
+ }
+ os << ") ";
+ }
+
+ return os;
+}
+
+bool
+Bundle::operator== (Bundle const & other)
+{
+ return _channel == other._channel;
+}
+
+/** Given an index of a channel as the nth channel of a particular type,
+ * return an index of that channel when considering channels of all types.
+ *
+ * e.g. given a bundle with channels:
+ * fred [audio]
+ * jim [audio]
+ * sheila [midi]
+ *
+ * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
+ * 0th MIDI channel.
+ *
+ * If t == NIL, we just return c.
+ */
+
+uint32_t
+Bundle::type_channel_to_overall (DataType t, uint32_t c) const
+{
+ if (t == DataType::NIL) {
+ return c;
+ }
+
+ Glib::Threads::Mutex::Lock lm (_channel_mutex);
+
+ vector<Channel>::const_iterator i = _channel.begin ();
+
+ uint32_t o = 0;
+
+ while (1) {
+
+ assert (i != _channel.end ());
+
+ if (i->type != t) {
+ ++i;
+ } else {
+ if (c == 0) {
+ return o;
+ }
+ --c;
+ }
+
+ ++o;
+ }
+
+ abort(); /* NOTREACHED */
+ return -1;
+}
+
+/** Perform the reverse of type_channel_to_overall */
+uint32_t
+Bundle::overall_channel_to_type (DataType t, uint32_t c) const
+{
+ if (t == DataType::NIL) {
+ return c;
+ }
+
+ Glib::Threads::Mutex::Lock lm (_channel_mutex);
+
+ uint32_t s = 0;
+
+ vector<Channel>::const_iterator i = _channel.begin ();
+ for (uint32_t j = 0; j < c; ++j) {
+ if (i->type == t) {
+ ++s;
+ }
+ ++i;
+ }
+
+ return s;
+}