The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Classes | Namespaces | Macros | Functions
make_enum.hpp File Reference

Defines the MAKE_ENUM macro. More...

#include <cassert>
#include <exception>
#include <string>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor.hpp>
#include <istream>
#include <ostream>
Include dependency graph for make_enum.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  bad_enum_cast
 
class  enum_tag
 

Namespaces

 make_enum_detail
 

Macros

#define ADD_PAREN_1(A, B)   ((A, B)) ADD_PAREN_2
 
#define ADD_PAREN_2(A, B)   ((A, B)) ADD_PAREN_1
 
#define ADD_PAREN_1_END
 
#define ADD_PAREN_2_END
 
#define MAKEPAIRS(INPUT)   BOOST_PP_CAT(ADD_PAREN_1 INPUT,_END)
 
#define PP_SEQ_FOR_EACH_I_PAIR(macro, data, pairs)   BOOST_PP_SEQ_FOR_EACH_I(macro, data, MAKEPAIRS(pairs))
 
#define CAT2(A, B)   A ## B
 
#define CAT3(A, B, C)   A ## B ## C
 
#define EXPAND_ENUMVALUE_NORMAL(r, data, i, record)   BOOST_PP_TUPLE_ELEM(2, 0, record) = i,
 
#define EXPAND_ENUMFUNC_NORMAL(r, data, i, record)   if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) return BOOST_PP_TUPLE_ELEM(2, 0, record);
 
#define EXPAND_ENUMPARSE_NORMAL(r, data, i, record)   if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) { *this = BOOST_PP_TUPLE_ELEM(2, 0, record); return true; }
 
#define EXPAND_ENUMFUNCREV_NORMAL(r, data, i, record)   if(data == BOOST_PP_TUPLE_ELEM(2, 0, record)) return BOOST_PP_TUPLE_ELEM(2, 1, record);
 
#define EXPAND_ENUMFUNCTIONCOUNT(r, data, i, record)   + 1
 
#define MAKE_ENUM(NAME, CONTENT)
 

Functions

void make_enum_detail::debug_conversion_error (const std::string &temp, const bad_enum_cast &e)
 

Detailed Description

Defines the MAKE_ENUM macro.

Currently this comes in 1-argument and 2-argument versions.

Example usage:

MAKE_ENUM(enumname,
(val1, "name1")
(val2, "name2")
(val3, "name3")
)

What it does:

Generates a struct enumname which holds an enum and provides functions to convert to and from string, as well as a constant enumname::count, which is set to the number of possible enum values defined.

// Throws bad_enum_cast if the string value is not recognized.
enumname enumname::string_to_enum(const std::string&);
// Returns the specified default instead of throwing if the string value is
// not recognized. Never throws.
enumname enumname::string_to_enum(const std::string&, enumname);
// Never throws.
std::string enumname::enum_to_string(enumname);
// Count of defined enum values.
const size_t enumname::count;

It also defines the following stream operations:

// Never throws. Fails an assertion check if the value passed is not defined
// by the enumeration.
std::ostream& operator<<(std::ostream&, enumname);
// Never throws. Sets the stream's fail bit if the input is not recognized.
std::istream& operator>>(std::istream&, enumname&);

In case of a bad string -> enum conversion from istream input, the istream will have its fail bit set. This means that lexical_casts will throw a bad_lexical_cast in the similar scenario; however, note that bad_lexical_cast does not provide any details about the error.

It is recommended to use MAKE_ENUM types with the built-in versions of lexical_cast or lexical_cast_default provided by Wesnoth (see util.hpp). However, if you do not want twml_exception to be thrown under any circumstances, use the string_to_enumname functions instead.

See src/tests/test_make_enum.cpp for example code.

Definition in file make_enum.hpp.

Macro Definition Documentation

#define ADD_PAREN_1 (   A,
 
)    ((A, B)) ADD_PAREN_2

Definition at line 128 of file make_enum.hpp.

#define ADD_PAREN_1_END

Definition at line 130 of file make_enum.hpp.

#define ADD_PAREN_2 (   A,
 
)    ((A, B)) ADD_PAREN_1

Definition at line 129 of file make_enum.hpp.

#define ADD_PAREN_2_END

Definition at line 131 of file make_enum.hpp.

#define CAT2 (   A,
 
)    A ## B

Definition at line 136 of file make_enum.hpp.

#define CAT3 (   A,
  B,
 
)    A ## B ## C

Definition at line 137 of file make_enum.hpp.

#define EXPAND_ENUMFUNC_NORMAL (   r,
  data,
  i,
  record 
)    if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) return BOOST_PP_TUPLE_ELEM(2, 0, record);

Definition at line 144 of file make_enum.hpp.

#define EXPAND_ENUMFUNCREV_NORMAL (   r,
  data,
  i,
  record 
)    if(data == BOOST_PP_TUPLE_ELEM(2, 0, record)) return BOOST_PP_TUPLE_ELEM(2, 1, record);

Definition at line 148 of file make_enum.hpp.

#define EXPAND_ENUMFUNCTIONCOUNT (   r,
  data,
  i,
  record 
)    + 1

Definition at line 151 of file make_enum.hpp.

#define EXPAND_ENUMPARSE_NORMAL (   r,
  data,
  i,
  record 
)    if(data == BOOST_PP_TUPLE_ELEM(2, 1, record)) { *this = BOOST_PP_TUPLE_ELEM(2, 0, record); return true; }

Definition at line 146 of file make_enum.hpp.

#define EXPAND_ENUMVALUE_NORMAL (   r,
  data,
  i,
  record 
)    BOOST_PP_TUPLE_ELEM(2, 0, record) = i,

Definition at line 140 of file make_enum.hpp.

#define MAKE_ENUM (   NAME,
  CONTENT 
)

Definition at line 157 of file make_enum.hpp.

#define MAKEPAIRS (   INPUT)    BOOST_PP_CAT(ADD_PAREN_1 INPUT,_END)

Definition at line 132 of file make_enum.hpp.

#define PP_SEQ_FOR_EACH_I_PAIR (   macro,
  data,
  pairs 
)    BOOST_PP_SEQ_FOR_EACH_I(macro, data, MAKEPAIRS(pairs))

Definition at line 133 of file make_enum.hpp.