Ardour  9.0-pre0-582-g084a23a80d
enumwriter.h
Go to the documentation of this file.
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 #pragma once
22 
23 #include <map>
24 #include <string>
25 #include <vector>
26 #include <exception>
27 #include <sstream>
28 
29 #include "pbd/libpbd_visibility.h"
30 
31 namespace PBD {
32 
33 class LIBPBD_API unknown_enumeration : public std::exception {
34  public:
35  unknown_enumeration (std::string const & e) throw() {
36  std::stringstream s;
37  s << "unknown enumerator " << e << " in PBD::EnumWriter";
38  _message = s.str ();
39  }
40 
41  ~unknown_enumeration () throw() {}
42 
43  virtual const char *what() const throw() {
44  return _message.c_str();
45  }
46 
47 private:
48  std::string _message;
49 };
50 
52  public:
53  static EnumWriter& instance();
54  static void destroy();
55 
56  void register_distinct (std::string type, std::vector<int>, std::vector<std::string>);
57  void register_bits (std::string type, std::vector<int>, std::vector<std::string>);
58 
59  std::string write (std::string type, int value);
60  int read (std::string type, std::string value);
61 
62  void add_to_hack_table (std::string str, std::string hacked_str);
63 
64  private:
67 
69  std::vector<int> values;
70  std::vector<std::string> names;
71  bool bitwise;
72 
74  EnumRegistration (std::vector<int>& v, std::vector<std::string>& s, bool b)
75  : values (v), names (s), bitwise (b) {}
76  };
77 
78  typedef std::map<std::string, EnumRegistration> Registry;
80 
81  std::string write_bits (EnumRegistration&, int value);
82  std::string write_distinct (EnumRegistration&, int value);
83 
84  int read_bits (EnumRegistration&, std::string value);
85  int read_distinct (EnumRegistration&, std::string value);
86 
88  static std::map<std::string,std::string> hack_table;
89 
90  int validate (EnumRegistration& er, int value) const;
91  int validate_bitwise (EnumRegistration& er, int value) const;
92 };
93 
94 }
95 
96 #define enum_2_string(e) (PBD::EnumWriter::instance().write (typeid(e).name(), e))
97 #define string_2_enum(str,e) (PBD::EnumWriter::instance().read (typeid(e).name(), (str)))
98 
void add_to_hack_table(std::string str, std::string hacked_str)
static EnumWriter & instance()
int validate_bitwise(EnumRegistration &er, int value) const
void register_bits(std::string type, std::vector< int >, std::vector< std::string >)
int read_distinct(EnumRegistration &, std::string value)
void register_distinct(std::string type, std::vector< int >, std::vector< std::string >)
static void destroy()
static std::map< std::string, std::string > hack_table
Definition: enumwriter.h:88
int read_bits(EnumRegistration &, std::string value)
std::string write(std::string type, int value)
int read(std::string type, std::string value)
std::string write_distinct(EnumRegistration &, int value)
static EnumWriter * _instance
Definition: enumwriter.h:87
std::string write_bits(EnumRegistration &, int value)
int validate(EnumRegistration &er, int value) const
std::map< std::string, EnumRegistration > Registry
Definition: enumwriter.h:78
Registry registry
Definition: enumwriter.h:79
virtual const char * what() const
Definition: enumwriter.h:43
unknown_enumeration(std::string const &e)
Definition: enumwriter.h:35
#define LIBPBD_API
Definition: axis_view.h:42
std::vector< std::string > names
Definition: enumwriter.h:70
EnumRegistration(std::vector< int > &v, std::vector< std::string > &s, bool b)
Definition: enumwriter.h:74