first vaguely working version using PresentationInfo
[ardour.git] / libs / ardour / presentation_info.cc
1 /*
2     Copyright (C) 2016 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 #include <sstream>
20 #include <typeinfo>
21
22 #include <cassert>
23
24 #include "pbd/enumwriter.h"
25 #include "pbd/error.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28
29 #include "ardour/presentation_info.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using std::string;
36
37 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
38 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
39 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
40 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
41
42 PresentationInfo::PresentationInfo (std::string const & str)
43 {
44         if (parse (str)) {
45                 throw failed_constructor ();
46         }
47 }
48
49 int
50 PresentationInfo::parse (string const& str)
51 {
52         std::stringstream s (str);
53
54         /* new school, segmented string "NNN:TYPE" */
55         string f;
56         char c;
57         s >> _order;
58         /* skip colon */
59         s >> c;
60         /* grab flags */
61         s >> f;
62         _flags = Flag (string_2_enum (f, _flags)|GroupOrderSet);
63         std::cerr << "Parsed [" << str << "] as " << _order << " + " << enum_2_string (_flags) << std::endl;
64         return 0;
65 }
66
67 int
68 PresentationInfo::parse (uint32_t n, Flag f)
69 {
70         if (n < UINT16_MAX) {
71                 assert (f != Flag (0));
72                 _order = n;
73                 _flags = Flag (f|GroupOrderSet);
74         } else {
75                 _order = (n & 0xffff);
76                 _flags = Flag ((n >> 16)|GroupOrderSet);
77         }
78
79         return 0;
80 }
81
82 std::string
83 PresentationInfo::to_string() const
84 {
85         std::stringstream ss;
86
87         /* Do not save or selected hidden status, or group-order set bit */
88
89         Flag f = Flag (_flags & ~(Hidden|Selected|GroupOrderSet));
90
91         ss << _order << ':' << enum_2_string (f);
92
93         return ss.str();
94 }
95
96 PresentationInfo::Flag
97 PresentationInfo::get_flags (XMLNode const& node)
98 {
99         const XMLProperty *prop;
100         XMLNodeList nlist = node.children ();
101         XMLNodeConstIterator niter;
102         XMLNode *child;
103
104         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
105                 child = *niter;
106
107                 if (child->name() == X_("PresentationInfo")) {
108                         if ((prop = child->property (X_("value"))) != 0) {
109                                 PresentationInfo pi (prop->value());
110                                 return pi.flags ();
111                         }
112                 }
113         }
114         return Flag (0);
115 }
116
117 std::ostream&
118 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& rid)
119 {
120         return o << rid.to_string ();
121 }