2 Copyright (C) 2006 Paul Davis
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.
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.
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.
21 #ifndef __pbd_enumwriter_h__
22 #define __pbd_enumwriter_h__
32 class unknown_enumeration : public std::exception {
34 unknown_enumeration (std::string const & e) throw() {
36 s << "unknown enumerator " << e << " in PBD::EnumWriter";
40 ~unknown_enumeration () throw() {}
42 virtual const char *what() const throw() {
43 return _message.c_str();
52 static EnumWriter& instance();
53 static void destroy();
55 void register_distinct (std::string type, std::vector<int>, std::vector<std::string>);
56 void register_bits (std::string type, std::vector<int>, std::vector<std::string>);
58 std::string write (std::string type, int value);
59 int read (std::string type, std::string value);
61 void add_to_hack_table (std::string str, std::string hacked_str);
67 struct EnumRegistration {
68 std::vector<int> values;
69 std::vector<std::string> names;
73 EnumRegistration (std::vector<int>& v, std::vector<std::string>& s, bool b)
74 : values (v), names (s), bitwise (b) {}
77 typedef std::map<std::string, EnumRegistration> Registry;
80 std::string write_bits (EnumRegistration&, int value);
81 std::string write_distinct (EnumRegistration&, int value);
83 int read_bits (EnumRegistration&, std::string value);
84 int read_distinct (EnumRegistration&, std::string value);
86 static EnumWriter* _instance;
87 static std::map<std::string,std::string> hack_table;
90 int validate (EnumRegistration& er, int value);
95 #define enum_2_string(e) (PBD::EnumWriter::instance().write (typeid(e).name(), e))
96 #define string_2_enum(str,e) (PBD::EnumWriter::instance().read (typeid(e).name(), (str)))
98 #endif /* __pbd_enumwriter_h__ */