aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Enum.h
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>2001-03-01 00:22:23 +0000
committerFritz Elfert <felfert@to.com>2001-03-01 00:22:23 +0000
commit75007da068aac10fed90fc5234ab2d50deed3e61 (patch)
treebe0a8ebba9bad4ccab4600c60d9e7fcc21dc7837 /lib/Enum.h
parentd8ae774198cf180fcaa4a2512dd94b4dbfdfd72d (diff)
downloadplptools-75007da068aac10fed90fc5234ab2d50deed3e61.tar.gz
plptools-75007da068aac10fed90fc5234ab2d50deed3e61.tar.bz2
plptools-75007da068aac10fed90fc5234ab2d50deed3e61.zip
Modified icons.
Started NLS support. Added kpsion KDE2 application. Unified file-headers.
Diffstat (limited to 'lib/Enum.h')
-rw-r--r--lib/Enum.h380
1 files changed, 206 insertions, 174 deletions
diff --git a/lib/Enum.h b/lib/Enum.h
index 8dcaa1b..a106f5a 100644
--- a/lib/Enum.h
+++ b/lib/Enum.h
@@ -1,10 +1,36 @@
-/*--*-c++-*-------------------------------------------------------------
- * $Id$
- *---------------------------------------------------------------------*/
-
+/*-*-c++-*-
+ * $Id$
+ *
+ * This file is part of plptools.
+ *
+ * Copyright (C) 2000 Henner Zeller <hzeller@to.com>
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
#ifndef _ENUM_H_
#define _ENUM_H_
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_LIBINTL_H
+#include <intl.h>
+#endif
+
#include <map>
#include <string>
@@ -18,69 +44,69 @@
*/
class EnumBase {
protected:
+ /**
+ * maps integers (typically: enumeration values) to
+ * Strings. Takes care of the fact, that an Integer may map
+ * to multiple strings (sometimes multiple enumeration values
+ * represent the same integer).
+ *
+ * Provides a means to get the string representation of an
+ * integer and vice versa.
+ *
+ * @author Henner Zeller
+ */
+ class i2sMapper {
+ private:
/**
- * maps integers (typically: enumeration values) to
- * Strings. Takes care of the fact, that an Integer may map
- * to multiple strings (sometimes multiple enumeration values
- * represent the same integer).
- *
- * Provides a means to get the string representation of an
- * integer and vice versa.
- *
- * @author Henner Zeller
- */
- class i2sMapper {
- private:
- /**
- * there can be one value, mapping to multiple
- * strings. Therefore, we need a multimap.
- */
- typedef multimap<long, const char*> i2s_map_t;
-
- /**
- * just for the record. Mapping back a string to the
- * Integer value in question. Since Symbols must be unique,
- * there is only a 1:1 relation as opposed to i2s_map_t. So
- * we can use a normal map here.
- *
- * Since in the usual application, mapping a string back
- * to its value is not important performance wise (typically
- * in a frontend), so it is implemented as exhaustive search,
- * not as extra map. Saves some bits of memrory ..
- */
- //typedef map<const char*, long> s2i_map_t;
+ * there can be one value, mapping to multiple
+ * strings. Therefore, we need a multimap.
+ */
+ typedef multimap<long, const char*> i2s_map_t;
- i2s_map_t stringMap;
- public:
- /**
- * adds a new int -> string mapping
- * Does NOT take over responsibility for the
- * pointer (i.e. it is not freed), so it is save
- * to add constant strings provided in the program code.
- */
- void add(long, const char*);
+ /**
+ * just for the record. Mapping back a string to the
+ * Integer value in question. Since Symbols must be unique,
+ * there is only a 1:1 relation as opposed to i2s_map_t. So
+ * we can use a normal map here.
+ *
+ * Since in the usual application, mapping a string back
+ * to its value is not important performance wise (typically
+ * in a frontend), so it is implemented as exhaustive search,
+ * not as extra map. Saves some bits of memrory ..
+ */
+ //typedef map<const char*, long> s2i_map_t;
- /**
- * returns the string representation for this integer.
- * If there are multiple strings for this integer,
- * return a comma delimited list.
- */
- string lookup(long) const;
+ i2s_map_t stringMap;
+ public:
+ /**
+ * adds a new int -> string mapping
+ * Does NOT take over responsibility for the
+ * pointer (i.e. it is not freed), so it is save
+ * to add constant strings provided in the program code.
+ */
+ void add(long, const char*);
+
+ /**
+ * returns the string representation for this integer.
+ * If there are multiple strings for this integer,
+ * return a comma delimited list.
+ */
+ string lookup(long) const;
- /**
- * returns the integer associated with the
- * given string or -1 if the value
- * is not found (XXX: this should throw
- * an exception).
- */
- long lookup (const char *) const;
+ /**
+ * returns the integer associated with the
+ * given string or -1 if the value
+ * is not found (XXX: this should throw
+ * an exception).
+ */
+ long lookup (const char *) const;
- /**
- * returns true, if we have an representation for
- * the given integer.
- */
- bool inRange(long) const;
- };
+ /**
+ * returns true, if we have an representation for
+ * the given integer.
+ */
+ bool inRange(long) const;
+ };
};
/**
@@ -89,7 +115,7 @@ protected:
*
* The string representation capability is needed to provide a
* generic input frontend for any Enumeration because text labels
- * are needed in GUIs, and, of course, aids debugging, because you
+ * are needed in GUIs, and, of course, aids debugging, because you
* can provide a readable presentation of an entry if something
* goes wrong.
*
@@ -115,118 +141,124 @@ protected:
template<typename E>
class Enum : private EnumBase {
private:
- struct sdata {
- /**
- * The constructor of the static data part.
- * You've to provide a constructor for each Enumeration
- * you want to wrap with this class. Initializes
- * the string Representation map, the readable name
- * of this Enumeration and a default value.
- *
- * The constructor is called automatically on definition,
- * so this makes sure, that the static part is initialized
- * properly before the program starts.
- */
- sdata();
- i2sMapper stringRep;
- string name;
- E defaultValue;
- };
- static sdata staticData;
-
+ struct sdata {
/**
- * The actual value hold by this instance
+ * The constructor of the static data part.
+ * You've to provide a constructor for each Enumeration
+ * you want to wrap with this class. Initializes
+ * the string Representation map, the readable name
+ * of this Enumeration and a default value.
+ *
+ * The constructor is called automatically on definition,
+ * so this makes sure, that the static part is initialized
+ * properly before the program starts.
*/
- E value;
+ sdata();
+ i2sMapper stringRep;
+ string name;
+ E defaultValue;
+ };
+ static sdata staticData;
+
+ /**
+ * The actual value hold by this instance
+ */
+ E value;
public:
- /**
- * default constructor.
- * Initialize with default value.
- */
- Enum() : value(staticData.defaultValue) {}
-
- /**
- * initialize with Enumeration given.
- */
- Enum(E init) : value(init){
- // if this hits you and you're sure, that the
- // value is right .. is this Enum proper
- // initialized in the Enum<E>::sdata::sdata() ?
- assert(inRange(init));
- }
-
- /**
- * initialize with the string representation
- * XXX: throw Exception if not found ?
- */
- Enum(const string& s) : value(getValueFor(s)) {
- assert(inRange(value));
- }
+ /**
+ * default constructor.
+ * Initialize with default value.
+ */
+ Enum() : value(staticData.defaultValue) {}
- /**
- * assign an Enumeration of this type. In debug
- * version, assert, that it is really in the Range of
- * this Enumeration.
- */
- inline Enum& operator = (E setval) {
- value = setval;
- assert(inRange(setval));
- return *this;
- }
+ /**
+ * initialize with Enumeration given.
+ */
+ Enum(E init) : value(init){
+ // if this hits you and you're sure, that the
+ // value is right .. is this Enum proper
+ // initialized in the Enum<E>::sdata::sdata() ?
+ assert(inRange(init));
+ }
- /**
- * returns the enumeration value hold with this
- * enum.
- */
- inline operator E () const { return value; }
+ /**
+ * initialize with the string representation
+ * XXX: throw Exception if not found ?
+ */
+ Enum(const string& s) : value(getValueFor(s)) {
+ assert(inRange(value));
+ }
- /**
- * returns the String representation for the value
- * represented by this instance.
- */
- string toString() const { return getStringFor(value); }
+ /**
+ * assign an Enumeration of this type. In debug
+ * version, assert, that it is really in the Range of
+ * this Enumeration.
+ */
+ inline Enum& operator = (E setval) {
+ value = setval;
+ assert(inRange(setval));
+ return *this;
+ }
- /**
- * This static member returns true, if the integer value
- * given fits int the range of this Enumeration. Use this
- * to verify input/output.
- * Fitting in the range of Enumeration here means, that
- * there actually exists a String representation for it,
- * so this Enumeration is needed to be initialized properly
- * in its Enum<E>::sdata::sdata() constructor, you've to
- * provide. For convenience, use the ENUM_DEFINITION() macro
- * for this.
- */
- static bool inRange(long i) {
- return (staticData.stringRep.inRange(i));
- }
+ /**
+ * returns the enumeration value hold with this
+ * enum.
+ */
+ inline operator E () const { return value; }
- /**
- * returns the Name for this enumeration. Useful for
- * error reporting.
- */
- static string getEnumName() { return staticData.name; }
-
- /**
- * gives the String represenatation of a specific
- * value of this Enumeration.
- */
- static string getStringFor(E e) {
- return staticData.stringRep.lookup((long) e);
- }
-
- /**
- * returns the Value for a specific String.
- * XXX: throw OutOfRangeException ?
- */
- static E getValueFor(const string &s) {
- return (E) staticData.stringRep.lookup(s.getCStr());
- }
+ /**
+ * returns the String representation for the value
+ * represented by this instance.
+ */
+ string toString() const { return getStringFor(value); }
+
+ /**
+ * returns the C string representation for the value
+ * represented by this instance.
+ */
+ operator const char *() const { return toString().c_str(); }
+
+ /**
+ * This static member returns true, if the integer value
+ * given fits int the range of this Enumeration. Use this
+ * to verify input/output.
+ * Fitting in the range of Enumeration here means, that
+ * there actually exists a String representation for it,
+ * so this Enumeration is needed to be initialized properly
+ * in its Enum<E>::sdata::sdata() constructor, you've to
+ * provide. For convenience, use the ENUM_DEFINITION() macro
+ * for this.
+ */
+ static bool inRange(long i) {
+ return (staticData.stringRep.inRange(i));
+ }
+
+ /**
+ * returns the Name for this enumeration. Useful for
+ * error reporting.
+ */
+ static string getEnumName() { return staticData.name; }
+
+ /**
+ * gives the String represenatation of a specific
+ * value of this Enumeration.
+ */
+ static string getStringFor(E e) {
+ return staticData.stringRep.lookup((long) e);
+ }
+
+ /**
+ * returns the Value for a specific String.
+ * XXX: throw OutOfRangeException ?
+ */
+ static E getValueFor(const string &s) {
+ return (E) staticData.stringRep.lookup(s.getCStr());
+ }
};
/**
- * Helper macro to construct an enumeration wrapper Enum<E> for
+ * Helper macro to construct an enumeration wrapper Enum<E> for
* a specific enum type.
*
* It defines the static variable holding the static
@@ -237,7 +269,7 @@ public:
*
* usage example:
* <pre>
- * // declaration of enumeration; somewhere
+ * // declaration of enumeration; somewhere
* class rfsv {
* [...]
* enum PSI_ERROR_CODES { E_PSI_GEN_NONE, E_PSI_GEN_FAIL, E_PSI_GEN_ARG };
@@ -260,30 +292,30 @@ public:
* @author Henner Zeller
*/
#define ENUM_DEFINITION(EnumName, initWith) \
- /** \
+/** \
* The definition of the static variable holding the static \
* data for this Enumeration wrapper. \
*/ \
- Enum<EnumName>::sdata Enum<EnumName>::staticData; \
- /** \
+Enum<EnumName>::sdata Enum<EnumName>::staticData; \
+/** \
* actual definition of the constructor for the static data. \
* This is called implicitly by the definition above. \
*/ \
- Enum<EnumName>::sdata::sdata() : \
- name(#EnumName),defaultValue(initWith)
+Enum<EnumName>::sdata::sdata() : \
+name(#EnumName),defaultValue(initWith)
/**
* Writes enumeration's string representation.
*/
template <typename E>
inline ostream& operator << (ostream& out, const Enum<E> &e) {
- return out << e.toString();
+ return out << gettext(e.toString().c_str());
}
-/*
+#endif /* _ENUM_H_ */
+
+/*
* Local variables:
- * c-basic-offset: 8
+ * c-basic-offset: 4
* End:
*/
-
-#endif /* _ENUM_H_ */