mapnik::enumeration< ENUM, THE_MAX > Class Template Reference

#include <enumeration.hpp>

List of all members.

Public Types

enum  Max { MAX = THE_MAX }
typedef ENUM native_type

Public Member Functions

 enumeration ()
 enumeration (ENUM v)
 enumeration (const enumeration &other)
void operator= (ENUM v)
void operator= (const enumeration &other)
 operator ENUM () const
ENUM max () const
void from_string (const std::string &str)
std::istream & parse (std::istream &is)
std::string as_string () const
std::ostream & print (std::ostream &os=std::cerr) const

Static Public Member Functions

static const char * get_string (unsigned i)
static bool verify (const char *filename, unsigned line_no)
static const std::string & get_full_qualified_name ()
static std::string get_name ()

Related Functions

(Note that these are not member functions.)

template<class ENUM , int THE_MAX>
std::ostream & operator<< (std::ostream &os, const mapnik::enumeration< ENUM, THE_MAX > &e)
template<class ENUM , int THE_MAX>
std::istream & operator>> (std::istream &is, mapnik::enumeration< ENUM, THE_MAX > &e)
 DEFINE_ENUM(name, e)
 IMPLEMENT_ENUM(name, strings)


Detailed Description

template<class ENUM, int THE_MAX>
class mapnik::enumeration< ENUM, THE_MAX >

Slim wrapper for enumerations. It creates a new type from a native enum and a char pointer array. It almost exactly behaves like a native enumeration type. It supports string conversion through stream operators. This is usefull for debugging, serialization/deserialization and also helps with implementing language bindings. The two convinient macros DEFINE_ENUM() and IMPLEMENT_ENUM() are provided to help with instanciation.

Limitations:
  • The enum must start at zero.
  • The enum must be consecutive.
  • The enum must be terminated with a special token consisting of the enum's name plus "_MAX".
  • The corresponding char pointer array must be terminated with an empty string.
  • The names must only consist of characters and digits (a-z, A-Z, 0-9), underscores (_) and dashes (-).
Warning:
At the moment the verify() method is called during static initialization. It quits the application with exit code 1 if any error is detected. The other solution i thought of is to do the checks at compile time (using boost::mpl).
Example:
The following code goes into the header file:
 enum fruit_enum {
      APPLE,
      CHERRY,
      BANANA,
      PASSION_FRUIT,
      fruit_enum_MAX
 };

 static const char * fruit_strings[] = {
      "apple",
      "cherry",
      "banana",
      "passion_fruit",
      ""
 };

 DEFINE_ENUM( fruit, fruit_enum);
In the corresponding cpp file do:
 IMPLEMENT_ENUM( fruit, fruit_strings );
And here is how to use the resulting type Fruit
 int
 main(int argc, char * argv[]) {
      fruit f(APPLE);
      switch ( f ) {
          case BANANA:
          case APPLE:
              cerr << "No thanks. I hate " << f << "s" << endl;
              break;
          default:
              cerr << "Hmmm ... yummy " << f << endl;
              break;
      }
      
      f = CHERRY;

      fruit_enum native_enum = f;
      
      f.from_string("passion_fruit");

      for (unsigned i = 0; i < fruit::MAX; ++i) {
          cerr << i << " = " << fruit::get_string(i) << endl;
      }

      f.from_string("elephant"); // throws illegal_enum_value

      return 0;
 }

Member Typedef Documentation

template<class ENUM, int THE_MAX>
typedef ENUM mapnik::enumeration< ENUM, THE_MAX >::native_type


Member Enumeration Documentation

template<class ENUM, int THE_MAX>
enum mapnik::enumeration::Max

Enumerator:
MAX 


Constructor & Destructor Documentation

template<class ENUM, int THE_MAX>
mapnik::enumeration< ENUM, THE_MAX >::enumeration (  )  [inline]

template<class ENUM, int THE_MAX>
mapnik::enumeration< ENUM, THE_MAX >::enumeration ( ENUM  v  )  [inline]

template<class ENUM, int THE_MAX>
mapnik::enumeration< ENUM, THE_MAX >::enumeration ( const enumeration< ENUM, THE_MAX > &  other  )  [inline]


Member Function Documentation

template<class ENUM, int THE_MAX>
std::string mapnik::enumeration< ENUM, THE_MAX >::as_string (  )  const [inline]

Returns the current value as a string identifier.

template<class ENUM, int THE_MAX>
void mapnik::enumeration< ENUM, THE_MAX >::from_string ( const std::string &  str  )  [inline]

Converts str to an enum.

Exceptions:
illegal_enum_value str is not a legal identifier.

template<class ENUM, int THE_MAX>
static const std::string& mapnik::enumeration< ENUM, THE_MAX >::get_full_qualified_name (  )  [inline, static]

template<class ENUM, int THE_MAX>
static std::string mapnik::enumeration< ENUM, THE_MAX >::get_name (  )  [inline, static]

template<class ENUM, int THE_MAX>
static const char* mapnik::enumeration< ENUM, THE_MAX >::get_string ( unsigned  i  )  [inline, static]

Static helper function to iterate over valid identifiers.

template<class ENUM, int THE_MAX>
ENUM mapnik::enumeration< ENUM, THE_MAX >::max (  )  const [inline]

template<class ENUM, int THE_MAX>
mapnik::enumeration< ENUM, THE_MAX >::operator ENUM (  )  const [inline]

Conversion operator for native enum values.

template<class ENUM, int THE_MAX>
void mapnik::enumeration< ENUM, THE_MAX >::operator= ( const enumeration< ENUM, THE_MAX > &  other  )  [inline]

Assignment operator.

References mapnik::enumeration< ENUM, THE_MAX >::value_.

template<class ENUM, int THE_MAX>
void mapnik::enumeration< ENUM, THE_MAX >::operator= ( ENUM  v  )  [inline]

Assignment operator for native enum values.

template<class ENUM, int THE_MAX>
std::istream& mapnik::enumeration< ENUM, THE_MAX >::parse ( std::istream &  is  )  [inline]

Parses the input stream is for a word consisting of characters and digits (a-z, A-Z, 0-9) and underscores (_). The failbit of the stream is set if the word is not a valid identifier.

Referenced by mapnik::enumeration< ENUM, THE_MAX >::operator>>().

template<class ENUM, int THE_MAX>
std::ostream& mapnik::enumeration< ENUM, THE_MAX >::print ( std::ostream &  os = std::cerr  )  const [inline]

Prints the string identifier to the output stream os.

template<class ENUM, int THE_MAX>
static bool mapnik::enumeration< ENUM, THE_MAX >::verify ( const char *  filename,
unsigned  line_no 
) [inline, static]

Performs some simple checks and quits the application if any error is detected. Tries to print helpful error messages.


Friends And Related Function Documentation

template<class ENUM, int THE_MAX>
DEFINE_ENUM ( name,
 )  [related]

Helper macro. Creates a typedef.

template<class ENUM, int THE_MAX>
IMPLEMENT_ENUM ( name,
strings   )  [related]

Helper macro. Runs the verify() method during static initialization.

template<class ENUM , int THE_MAX>
std::ostream & operator<< ( std::ostream &  os,
const mapnik::enumeration< ENUM, THE_MAX > &  e 
) [related]

ostream operator for enumeration

template<class ENUM , int THE_MAX>
std::istream & operator>> ( std::istream &  is,
mapnik::enumeration< ENUM, THE_MAX > &  e 
) [related]


The documentation for this class was generated from the following file:

Generated for Mapnik by doxygen 1.5.8