|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r70238 - in sandbox/enums/libs/enums/test: . enum_class
From: vicente.botet_at_[hidden]
Date: 2011-03-20 19:28:34
Author: viboes
Date: 2011-03-20 19:28:33 EDT (Sun, 20 Mar 2011)
New Revision: 70238
URL: http://svn.boost.org/trac/boost/changeset/70238
Log:
Enums: Added String conversions
Text files modified:
sandbox/enums/libs/enums/test/EnumClass.cpp | 4
sandbox/enums/libs/enums/test/EnumClass.hpp | 106 ++++++++++++++++++++++++++++++++++++++++
sandbox/enums/libs/enums/test/enum_class/enum_inside_union_cons.pass.cpp | 24 ++++----
3 files changed, 120 insertions(+), 14 deletions(-)
Modified: sandbox/enums/libs/enums/test/EnumClass.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/EnumClass.cpp (original)
+++ sandbox/enums/libs/enums/test/EnumClass.cpp 2011-03-20 19:28:33 EDT (Sun, 20 Mar 2011)
@@ -49,7 +49,7 @@
std::string str = "Enum2";
EnumClass e = convert_to<EnumClass>(str);
BOOST_TEST(e==EnumClass::Enum2);
- BOOST_TEST(strcmp(c_str(e),"EnumClass::Enum2")==0);
+ BOOST_TEST(strcmp(c_str(e),"Enum2")==0);
}
std::cout << __LINE__ << std::endl;
#ifdef COMPILE_TIME2
@@ -64,7 +64,7 @@
EnumClass e = convert_to<EnumClass>(str);
// ... fail
BOOST_TEST(e==EnumClass::Enum2);
- BOOST_TEST(strcmp(c_str(e), "EnumClass::CHASSE")==0);
+ BOOST_TEST(strcmp(c_str(e), "CHASSE")==0);
}
#endif
{ // Construction of the wrapper with an invalid ints results in run-time error (undefined behavior)
Modified: sandbox/enums/libs/enums/test/EnumClass.hpp
==============================================================================
--- sandbox/enums/libs/enums/test/EnumClass.hpp (original)
+++ sandbox/enums/libs/enums/test/EnumClass.hpp 2011-03-20 19:28:33 EDT (Sun, 20 Mar 2011)
@@ -33,9 +33,11 @@
#include <boost/enums/scoped/enum_class_no_cons.hpp>
#include <boost/enums/scoped/enum_type_cons.hpp>
#include <boost/enums/scoped/enum_type_no_cons.hpp>
+#include <boost/enums/pp/enum_declaration.hpp>
#define CTOR
#if 0
+#if 0
BOOST_ENUM_CLASS_START(EnumClass, unsigned char) {
Default = 3,
@@ -136,8 +138,20 @@
} // namespace meta
}
}
+#else
+
+
+BOOST_ENUMS_ENUM_CLASS_DCL_CONS((EnumClass), unsigned char,
+ ( (Default)(3) )
+ ( (Enum1) )
+ ( (Enum2) )
+ , linear_enum_traiter)
+
+
+#endif
+
//! OSTRREAM overloading
template <typename OSTREAM>
@@ -147,3 +161,95 @@
}
#endif
+#if 0
+class EnumClass {
+public:
+ enum type { Default = 3 , Enum1 , Enum2 };
+ inline friend EnumClass convert_to( const char* str, boost::dummy::type_tag<EnumClass> const& )
+ {
+ if (strcmp( str, "Default" ) == 0) { return boost::convert_to<EnumClass>( EnumClass::Default ); }
+ if (strcmp( str, "Enum1" ) == 0) { return boost::convert_to<EnumClass>( EnumClass::Enum1 ); }
+ if (strcmp( str, "Enum2" ) == 0) { return boost::convert_to<EnumClass>( EnumClass::Enum2 ); }
+ throw "invalid string for " "EnumClass";
+ }
+ inline friend EnumClass convert_to( const std::string& str, boost::dummy::type_tag<EnumClass> const& )
+ {
+ return boost::convert_to<EnumClass>( str.c_str() );
+ }
+ inline friend const char* c_str(EnumClass e)
+ {
+ switch (boost::enums::native_value(e))
+ {
+ case EnumClass::EnumClass::Default : return("Default");
+ case EnumClass::EnumClass::Enum1 : return("Enum1");
+ case EnumClass::EnumClass::Enum2 : return("Enum2");
+ default: throw "invalid value for " "EnumClass";
+ }
+ }
+ typedef EnumClass this_type ;
+ typedef int underlying_type;
+private:
+ underlying_type val_;
+public:
+ EnumClass() : val_(static_cast<underlying_type>(type())) { }
+ EnumClass(type v) : val_(static_cast<underlying_type>(v)) { }
+ explicit EnumClass(underlying_type v) : val_(v) { }
+ EnumClass& operator =(type rhs) { val_=static_cast<underlying_type>(rhs); return *this; }
+ static EnumClass default_value() { EnumClass res; res.val_=static_cast<underlying_type>(EnumClass::type()); return res; }
+ static EnumClass convert_to(underlying_type v) { EnumClass res; res.val_=v; return res; }
+ static EnumClass convert_to(type v) { EnumClass res; res.val_=static_cast<underlying_type>(v); return res; }
+ type native_value() const { return type(val_); }
+ underlying_type underlying_value() const { return val_; }
+ inline friend EnumClass convert_to(int v , boost::dummy::type_tag<EnumClass> const& ) { return EnumClass::convert_to(v); }
+ inline friend EnumClass convert_to(boost::enums::native_type<EnumClass>::type v , boost::dummy::type_tag<EnumClass> const& ) { return EnumClass::convert_to(v); }
+ inline friend int convert_to(EnumClass v , boost::dummy::type_tag<int> const& ) { return boost::enums::underlying_value(v); }
+ inline friend boost::enums::native_type<EnumClass>::type convert_to(EnumClass v , boost::dummy::type_tag<boost::enums::native_type<EnumClass>::type> const& ) {
+ return boost::enums::native_value(v);
+ }
+ friend inline bool operator ==(EnumClass lhs, EnumClass rhs) { return lhs.native_value() == rhs.native_value(); }
+ friend inline bool operator ==(type lhs, EnumClass rhs) { return lhs == rhs.native_value(); }
+ friend inline bool operator ==(EnumClass lhs, type rhs) { return lhs.native_value() == rhs; }
+ friend inline bool operator !=(EnumClass lhs, EnumClass rhs) { return lhs.native_value() != rhs.native_value(); }
+ friend inline bool operator !=(type lhs, EnumClass rhs) { return lhs != rhs.native_value(); }
+ friend inline bool operator !=(EnumClass lhs, type rhs) { return lhs.native_value() != rhs; }
+ friend inline bool operator <(EnumClass lhs, EnumClass rhs) { return lhs.native_value() < rhs.native_value(); }
+ friend inline bool operator <(type lhs, EnumClass rhs) { return lhs < rhs.native_value(); }
+ friend inline bool operator <(EnumClass lhs, type rhs) { return lhs.native_value() < rhs; }
+ friend inline bool operator <=(EnumClass lhs, EnumClass rhs) { return lhs.native_value() <= rhs.native_value(); }
+ friend inline bool operator <=(type lhs, EnumClass rhs) { return lhs <= rhs.native_value(); }
+ friend inline bool operator <=(EnumClass lhs, type rhs) { return lhs.native_value() <= rhs; }
+ friend inline bool operator >(EnumClass lhs, EnumClass rhs) { return lhs.native_value() > rhs.native_value(); }
+ friend inline bool operator >(type lhs, EnumClass rhs) { return lhs > rhs.native_value(); }
+ friend inline bool operator >(EnumClass lhs, type rhs) { return lhs.native_value() > rhs; }
+ friend inline bool operator >=(EnumClass lhs, EnumClass rhs) { return lhs.native_value() >= rhs.native_value(); }
+ friend inline bool operator >=(type lhs, EnumClass rhs) { return lhs >= rhs.native_value(); }
+ friend inline bool operator >=(EnumClass lhs, type rhs) { return lhs.native_value() >= rhs; }
+};
+namespace boost {
+ namespace enums {
+ template <> struct scoping_type<native_type<EnumClass>::type> { typedef EnumClass type; };
+ }
+}
+namespace boost {
+ namespace enums {
+ template <> struct is_enum<EnumClass> : mpl::true_ { };
+ }
+}
+namespace boost {
+ namespace enums {
+ namespace meta {
+ template <> struct size< EnumClass> {
+ static const std::size_t value;
+ };
+ const std::size_t size< EnumClass>::value = 3;
+ template <> struct pos<EnumClass, EnumClass :: Default> { static const std::size_t value = 2; };
+ template <> struct val<EnumClass, 2> { static const boost::enums::native_type<EnumClass>::type value = EnumClass::Default; };
+ template <> struct pos<EnumClass, EnumClass :: Enum1> { static const std::size_t value = 3; };
+ template <> struct val<EnumClass, 3> { static const boost::enums::native_type<EnumClass>::type value = EnumClass::Enum1; };
+ template <> struct pos<EnumClass, EnumClass :: Enum2> { static const std::size_t value = 4; };
+ template <> struct val<EnumClass, 4> { static const boost::enums::native_type<EnumClass>::type value = EnumClass::Enum2; };
+ template <> struct enum_traits< EnumClass> : linear_enum_traiter< EnumClass> {};
+ }
+ }
+}
+#endif
Modified: sandbox/enums/libs/enums/test/enum_class/enum_inside_union_cons.pass.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/enum_class/enum_inside_union_cons.pass.cpp (original)
+++ sandbox/enums/libs/enums/test/enum_class/enum_inside_union_cons.pass.cpp 2011-03-20 19:28:33 EDT (Sun, 20 Mar 2011)
@@ -73,6 +73,18 @@
BOOST_ENUM_NS_CLASS_CONS_END( (Ex)(EC_Cons), int)
+
+
+#define MyNS_EC (Ex)(EC_Cons)
+#define MyENUMS ( (E0)(3) ) ( (E1) ) ( (E2) )
+
+
+BOOST_ENUM_NS_CLASS_START(MyNS_EC, int)
+{
+ BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(MyENUMS)
+}
+BOOST_ENUM_NS_CLASS_CONS_END(MyNS_EC, int)
+BOOST_ENUMS_ENUM_DCL_SPE(MyNS_EC, MyENUMS, linear_enum_traiter)
namespace boost {
namespace enums {
namespace meta {
@@ -86,18 +98,6 @@
}
}
}
-
-#define MyNS_EC (Ex)(EC_Cons)
-#define MyENUMS ( (E0)(3) ) ( (E1) ) ( (E2) )
-
-
-BOOST_ENUM_NS_CLASS_START(MyNS_EC, int)
-{
- BOOST_ENUMS_ENUMERATOR_LIST_GENERATE(MyENUMS)
-}
-BOOST_ENUM_NS_CLASS_CONS_END(MyNS_EC, int)
-BOOST_ENUMS_ENUM_DCL_SPE(MyNS_EC, MyENUMS, linear_enum_traiter)
-
#else
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