Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r70237 - sandbox/enums/boost/enums/pp
From: vicente.botet_at_[hidden]
Date: 2011-03-20 19:27:59


Author: viboes
Date: 2011-03-20 19:27:59 EDT (Sun, 20 Mar 2011)
New Revision: 70237
URL: http://svn.boost.org/trac/boost/changeset/70237

Log:
Enums: Added String conversions
Text files modified:
   sandbox/enums/boost/enums/pp/enum_declaration.hpp | 89 +++++++++++++++++++++++++++++++++++----
   sandbox/enums/boost/enums/pp/enumerator_definition.hpp | 4
   2 files changed, 82 insertions(+), 11 deletions(-)

Modified: sandbox/enums/boost/enums/pp/enum_declaration.hpp
==============================================================================
--- sandbox/enums/boost/enums/pp/enum_declaration.hpp (original)
+++ sandbox/enums/boost/enums/pp/enum_declaration.hpp 2011-03-20 19:27:59 EDT (Sun, 20 Mar 2011)
@@ -28,6 +28,65 @@
  
  */
 
+#define BOOST_ENUMS_ENUM_DCL_STR_TO_ID(P, ENUM, ED) \
+ if (strcmp( \
+ str, \
+ BOOST_ENUMS_ENUMERATOR_DEFINITION_STR(ED) \
+ ) == 0) \
+ { \
+ return boost::convert_to<ENUM>( \
+ ENUM::BOOST_ENUMS_ENUMERATOR_DEFINITION_ID(ED) \
+ ); \
+ }
+
+#define BOOST_ENUMS_ENUM_DCL_ID_TO_STR(P, ENUM, ED) \
+ case ENUM::ENUM::BOOST_ENUMS_ENUMERATOR_DEFINITION_ID(ED) : \
+ return(BOOST_ENUMS_ENUMERATOR_DEFINITION_STR(ED));
+
+
+#define BOOST_ENUMS_DCL_STRING_CONVERSIONS(ENUM, EL) \
+ inline friend \
+ ENUM convert_to( \
+ const char* str, \
+ boost::dummy::type_tag<ENUM> const& \
+ ) \
+ { \
+ BOOST_PP_SEQ_FOR_EACH( \
+ BOOST_ENUMS_ENUM_DCL_STR_TO_ID, \
+ ENUM, \
+ EL \
+ ) \
+ throw "invalid string for " \
+ BOOST_PP_STRINGIZE(ENUM); \
+ } \
+ \
+ inline friend \
+ ENUM convert_to( \
+ const std::string& str, \
+ boost::dummy::type_tag<ENUM> const& \
+ ) \
+ { \
+ return boost::convert_to<ENUM>( \
+ str.c_str() \
+ ); \
+ } \
+ \
+ inline friend \
+ const char* c_str(ENUM e) \
+ { \
+ switch (boost::enums::native_value(e)) \
+ { \
+ BOOST_PP_SEQ_FOR_EACH( \
+ BOOST_ENUMS_ENUM_DCL_ID_TO_STR, \
+ ENUM, \
+ EL \
+ ) \
+ default: \
+ throw "invalid value for " \
+ BOOST_PP_STRINGIZE(ENUM); \
+ } \
+ } \
+
 /**
  
  @brief Generates a @c boost::enums::meta::size specialization.
@@ -53,8 +112,11 @@
   template <> \
   struct size<BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC)> \
   { \
- static const std::size_t value; \
+ static const std::size_t value=BOOST_PP_SEQ_SIZE(EL); \
   }; \
+
+
+#define BOOST_ENUMS_ENUM_DCL_SIZE_SPE2(NS_EC, EL) \
   const std::size_t \
   size<BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC)>::value = \
     BOOST_PP_SEQ_SIZE(EL);
@@ -66,8 +128,9 @@
  <b>Warning</b>: This macro is presented here for exposition only reasons and is not part of the interface.
  
  <b>Parameters</b>:
- - P: the position in the ENUMERATOR_LIST sequence
+ - P: NOT USED
  - QNAME, the @p NAMESPACES_CLASS qualified name
+ - P: the position in the ENUMERATOR_LIST sequence
  - ED, the ENUMERATOR_DEFINITION sequence
  
  <b>Result</b>:
@@ -89,7 +152,7 @@
  
  */
 
-#define BOOST_ENUMS_ENUM_DCL_POS_VAL_SPE(P, QNAME, ED) \
+#define BOOST_ENUMS_ENUM_DCL_POS_VAL_SPE(R, QNAME, P, ED) \
   template <> \
   struct pos<QNAME, QNAME :: BOOST_ENUMS_ENUMERATOR_DEFINITION_ID(ED)> \
   { \
@@ -143,7 +206,7 @@
     namespace enums {
       namespace meta {
         BOOST_ENUMS_ENUM_DCL_SIZE_SPE(NS_EC, EL)
- BOOST_PP_SEQ_FOR_EACH(
+ BOOST_PP_SEQ_FOR_EACH_I(
           BOOST_ENUMS_ENUM_DCL_POS_VAL_SPE,
           BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC),
           EL
@@ -161,7 +224,7 @@
     namespace enums { \
       namespace meta { \
         BOOST_ENUMS_ENUM_DCL_SIZE_SPE(NS_EC, EL) \
- BOOST_PP_SEQ_FOR_EACH( \
+ BOOST_PP_SEQ_FOR_EACH_I( \
           BOOST_ENUMS_ENUM_DCL_POS_VAL_SPE, \
           BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC), \
           EL \
@@ -197,7 +260,9 @@
   BOOST_ENUM_NS_TYPE_START(NS_EC, UT) \
   { \
     BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(EL) \
- } \
+ }; \
+ BOOST_ENUMS_DCL_STRING_CONVERSIONS(BOOST_ENUMS_NAMESPACES_CLASS_ENUM(NS_EC), EL) \
+ typedef BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC) this_type \
   BOOST_ENUM_NS_TYPE_CONS_END(NS_EC, UT) \
   BOOST_ENUMS_ENUM_DCL_SPE(NS_EC, EL, TRAITER)
 
@@ -226,7 +291,9 @@
   BOOST_ENUM_NS_TYPE_START(NS_EC, UT) \
   { \
     BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(EL) \
- } \
+ }; \
+ BOOST_ENUMS_DCL_STRING_CONVERSIONS(BOOST_ENUMS_NAMESPACES_CLASS_ENUM(NS_EC), EL) \
+ typedef BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC) this_type \
   BOOST_ENUM_NS_TYPE_NO_CONS_END(NS_EC, UT) \
   BOOST_ENUMS_ENUM_DCL_SPE(NS_EC, EL, TRAITER)
 
@@ -256,7 +323,9 @@
   BOOST_ENUM_NS_CLASS_START(NS_EC, UT) \
   { \
     BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(EL) \
- } \
+ }; \
+ BOOST_ENUMS_DCL_STRING_CONVERSIONS(BOOST_ENUMS_NAMESPACES_CLASS_ENUM(NS_EC), EL) \
+ typedef BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC) this_type \
   BOOST_ENUM_NS_CLASS_CONS_END(NS_EC, UT) \
   BOOST_ENUMS_ENUM_DCL_SPE(NS_EC, EL, TRAITER)
 
@@ -286,7 +355,9 @@
   BOOST_ENUM_NS_CLASS_START(NS_EC, UT) \
   { \
     BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(EL) \
- } \
+ }; \
+ BOOST_ENUMS_DCL_STRING_CONVERSIONS(BOOST_ENUMS_NAMESPACES_CLASS_ENUM(NS_EC), EL) \
+ typedef BOOST_ENUMS_NAMESPACES_CLASS_QNAME(NS_EC) this_type \
   BOOST_ENUM_NS_CLASS_NO_CONS_END(NS_EC, UT) \
   BOOST_ENUMS_ENUM_DCL_SPE(NS_EC, EL, TRAITER)
 

Modified: sandbox/enums/boost/enums/pp/enumerator_definition.hpp
==============================================================================
--- sandbox/enums/boost/enums/pp/enumerator_definition.hpp (original)
+++ sandbox/enums/boost/enums/pp/enumerator_definition.hpp 2011-03-20 19:27:59 EDT (Sun, 20 Mar 2011)
@@ -111,8 +111,8 @@
 #define BOOST_ENUMS_ENUMERATOR_DEFINITION_STR(ED) \
   BOOST_PP_IIF( \
     BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(ED),2) \
- , BOOST_ENUMS_DETAIL_ENUMERATOR_DEFINITION_VAL_OK \
- , BOOST_ENUMS_DETAIL_ENUMERATOR_DEFINITION_VAL_KO \
+ , BOOST_ENUMS_DETAIL_ENUMERATOR_DEFINITION_STR_OK \
+ , BOOST_ENUMS_DETAIL_ENUMERATOR_DEFINITION_STR_KO \
   )(ED)
 
 #ifndef BOOST_ENUMS_DOXYGEN_INVOKED


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk