1 /* 2 * Copyright (C) 2006-2015 Paul Davis <paul@linuxaudiosystems.com> 3 * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net> 4 * Copyright (C) 2015 Robin Gareus <robin@gareus.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along 17 * with this program; if not, write to the Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 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 #include <sstream> 29 30 #include "pbd/libpbd_visibility.h" 31 32 namespace PBD { 33 34 class LIBPBD_API unknown_enumeration : public std::exception { 35 public: unknown_enumeration(std::string const & e)36 unknown_enumeration (std::string const & e) throw() { 37 std::stringstream s; 38 s << "unknown enumerator " << e << " in PBD::EnumWriter"; 39 _message = s.str (); 40 } 41 throw()42 ~unknown_enumeration () throw() {} 43 what()44 virtual const char *what() const throw() { 45 return _message.c_str(); 46 } 47 48 private: 49 std::string _message; 50 }; 51 52 class LIBPBD_API EnumWriter { 53 public: 54 static EnumWriter& instance(); 55 static void destroy(); 56 57 void register_distinct (std::string type, std::vector<int>, std::vector<std::string>); 58 void register_bits (std::string type, std::vector<int>, std::vector<std::string>); 59 60 std::string write (std::string type, int value); 61 int read (std::string type, std::string value); 62 63 void add_to_hack_table (std::string str, std::string hacked_str); 64 65 private: 66 EnumWriter (); 67 ~EnumWriter (); 68 69 struct EnumRegistration { 70 std::vector<int> values; 71 std::vector<std::string> names; 72 bool bitwise; 73 EnumRegistrationEnumRegistration74 EnumRegistration() {} EnumRegistrationEnumRegistration75 EnumRegistration (std::vector<int>& v, std::vector<std::string>& s, bool b) 76 : values (v), names (s), bitwise (b) {} 77 }; 78 79 typedef std::map<std::string, EnumRegistration> Registry; 80 Registry registry; 81 82 std::string write_bits (EnumRegistration&, int value); 83 std::string write_distinct (EnumRegistration&, int value); 84 85 int read_bits (EnumRegistration&, std::string value); 86 int read_distinct (EnumRegistration&, std::string value); 87 88 static EnumWriter* _instance; 89 static std::map<std::string,std::string> hack_table; 90 91 int validate (EnumRegistration& er, int value) const; 92 int validate_bitwise (EnumRegistration& er, int value) const; 93 }; 94 95 } 96 97 #define enum_2_string(e) (PBD::EnumWriter::instance().write (typeid(e).name(), e)) 98 #define string_2_enum(str,e) (PBD::EnumWriter::instance().read (typeid(e).name(), (str))) 99 100 #endif /* __pbd_enumwriter_h__ */ 101