Add basic test of playlist layering.
[ardour.git] / libs / pbd / pbd / enumwriter.h
1 /* 
2     Copyright (C) 2006 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     $Id$
19 */
20
21 #ifndef __pbd_enumwriter_h__
22 #define __pbd_enumwriter_h__
23
24 #include <map>
25 #include <string>
26 #include <vector>
27 #include <exception>
28
29
30 namespace PBD {
31
32 class unknown_enumeration : public std::exception {
33   public:
34         virtual const char *what() const throw() { return "unknown enumerator in PBD::EnumWriter"; }
35 };
36
37 class EnumWriter {
38   public:
39         static EnumWriter& instance();
40         static void destroy();
41
42         void register_distinct (std::string type, std::vector<int>, std::vector<std::string>);
43         void register_bits     (std::string type, std::vector<int>, std::vector<std::string>);
44
45         std::string write (std::string type, int value);
46         int         read  (std::string type, std::string value);
47
48         void add_to_hack_table (std::string str, std::string hacked_str);
49
50   private:
51         EnumWriter ();
52         ~EnumWriter ();
53
54         struct EnumRegistration {
55             std::vector<int> values;
56             std::vector<std::string> names;
57             bool bitwise;
58
59             EnumRegistration() {}
60             EnumRegistration (std::vector<int>& v, std::vector<std::string>& s, bool b) 
61                     : values (v), names (s), bitwise (b) {}
62         };
63
64         typedef std::map<std::string, EnumRegistration> Registry;
65         Registry registry;
66
67         std::string write_bits (EnumRegistration&, int value);
68         std::string write_distinct (EnumRegistration&, int value);
69
70         int read_bits (EnumRegistration&, std::string value);
71         int read_distinct (EnumRegistration&, std::string value);
72
73         static EnumWriter* _instance;
74         static std::map<std::string,std::string> hack_table;
75
76
77         int validate (EnumRegistration& er, int value);
78 };
79
80 }
81
82 #define enum_2_string(e) (PBD::EnumWriter::instance().write (typeid(e).name(), e))
83 #define string_2_enum(str,e) (PBD::EnumWriter::instance().read (typeid(e).name(), (str)))
84
85 #endif /*  __pbd_enumwriter_h__ */