summaryrefslogtreecommitdiff
path: root/libs/pbd/enumwriter.cc
diff options
context:
space:
mode:
authorPaul Davis <paul@linuxaudiosystems.com>2006-12-22 16:09:43 +0000
committerPaul Davis <paul@linuxaudiosystems.com>2006-12-22 16:09:43 +0000
commit6d4e6dc580c18ee940a30345656c70c054a6fa2d (patch)
treea2812be9279504cca1db6f3c9d0b70bf84d52639 /libs/pbd/enumwriter.cc
parent585e49184209f6f1679ddb6ee628efd02f7cbb0d (diff)
start using global, 100% generic enum to/from string object
git-svn-id: svn://localhost/ardour2/trunk@1246 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd/enumwriter.cc')
-rw-r--r--libs/pbd/enumwriter.cc194
1 files changed, 194 insertions, 0 deletions
diff --git a/libs/pbd/enumwriter.cc b/libs/pbd/enumwriter.cc
new file mode 100644
index 0000000000..c42cc3a5c2
--- /dev/null
+++ b/libs/pbd/enumwriter.cc
@@ -0,0 +1,194 @@
+/*
+ Copyright (C) 2006 Paul Davis
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+ $Id$
+*/
+
+#include <pbd/enumwriter.h>
+#include <pbd/error.h>
+#include <pbd/compose.h>
+
+using namespace std;
+using namespace PBD;
+
+#include "i18n.h"
+
+EnumWriter* EnumWriter::_instance = 0;
+
+EnumWriter::EnumWriter ()
+{
+ if (_instance == 0) {
+ _instance = this;
+ }
+}
+
+EnumWriter::~EnumWriter ()
+{
+}
+
+void
+EnumWriter::register_distinct (string type, vector<int> v, vector<string> s)
+{
+ pair<string,EnumRegistration> newpair;
+ pair<Registry::iterator,bool> result;
+
+ newpair.first = type;
+ newpair.second = EnumRegistration (v, s, false);
+
+ result = registry.insert (newpair);
+
+ if (!result.second) {
+ warning << string_compose (_("enum type \"%1\" already registered with the enum writer"), type) << endmsg;
+ }
+}
+
+void
+EnumWriter::register_bits (string type, vector<int> v, vector<string> s)
+{
+ pair<string,EnumRegistration> newpair;
+ pair<Registry::iterator,bool> result;
+
+ newpair.first = type;
+ newpair.second = EnumRegistration (v, s, true);
+
+ result = registry.insert (newpair);
+
+ if (!result.second) {
+ warning << _("enum type \"%1\" already registered with the enum writer") << endmsg;
+ }
+}
+
+string
+EnumWriter::write (string type, int value)
+{
+ Registry::iterator x = registry.find (type);
+
+ if (x == registry.end()) {
+ error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
+ throw unknown_enumeration();
+ }
+
+ if (x->second.bitwise) {
+ return write_bits (x->second, value);
+ } else {
+ return write_distinct (x->second, value);
+ }
+}
+
+int
+EnumWriter::read (string type, string value)
+{
+ Registry::iterator x = registry.find (type);
+
+ if (x == registry.end()) {
+ error << string_compose (_("EnumWriter: unknown enumeration type \"%1\""), type) << endmsg;
+ throw unknown_enumeration();
+ }
+
+ if (x->second.bitwise) {
+ return read_bits (x->second, value);
+ } else {
+ return read_distinct (x->second, value);
+ }
+}
+
+string
+EnumWriter::write_bits (EnumRegistration& er, int value)
+{
+ vector<int>::iterator i;
+ vector<string>::iterator s;
+ string result;
+
+ for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
+ if (value & (*i)) {
+ if (!result.empty()) {
+ result += ',';
+ }
+ result += (*s);
+ }
+ }
+
+ return result;
+}
+
+string
+EnumWriter::write_distinct (EnumRegistration& er, int value)
+{
+ vector<int>::iterator i;
+ vector<string>::iterator s;
+
+ for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
+ if (value == (*i)) {
+ return (*s);
+ }
+ }
+
+ return string();
+}
+
+int
+EnumWriter::read_bits (EnumRegistration& er, string str)
+{
+ vector<int>::iterator i;
+ vector<string>::iterator s;
+ int result = 0;
+ bool found = false;
+ string::size_type comma;
+
+ do {
+
+ comma = str.find_first_of (',');
+ string segment = str.substr (0, comma);
+
+ for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
+ if (segment == (*s)) {
+ result |= (*i);
+ found = true;
+ }
+ }
+
+ if (comma == string::npos) {
+ break;
+ }
+
+ str = str.substr (comma+1);
+
+ } while (true);
+
+ if (!found) {
+ throw unknown_enumeration();
+ }
+
+ return result;
+}
+
+int
+EnumWriter::read_distinct (EnumRegistration& er, string str)
+{
+ vector<int>::iterator i;
+ vector<string>::iterator s;
+
+ for (i = er.values.begin(), s = er.names.begin(); i != er.values.end(); ++i, ++s) {
+ if (str == (*s)) {
+ return (*i);
+ }
+ }
+
+ throw unknown_enumeration();
+}
+
+