Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74036 - in sandbox/enums/libs/enums/test: . enum_class/conversion
From: vicente.botet_at_[hidden]
Date: 2011-08-23 19:51:40


Author: viboes
Date: 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
New Revision: 74036
URL: http://svn.boost.org/trac/boost/changeset/74036

Log:
Enums: make it work without Boost.Conversion
Text files modified:
   sandbox/enums/libs/enums/test/EnumClass.cpp | 7
   sandbox/enums/libs/enums/test/EnumClass.hpp | 277 +++++++++++++++++++++++++--------------
   sandbox/enums/libs/enums/test/ExType.hpp | 2
   sandbox/enums/libs/enums/test/Jamfile.v2 | 3
   sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_invalid_int_fail.cpp | 3
   sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_valid_int_pass.cpp | 3
   6 files changed, 192 insertions(+), 103 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-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -18,7 +18,9 @@
 #include "./f.hpp"
 #include <boost/detail/lightweight_test.hpp>
 #include <boost/enums/enum_subrange_traiter.hpp>
+#ifndef BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
 #include <boost/conversion/is_extrinsically_explicitly_convertible.hpp>
+#endif
 #include <boost/static_assert.hpp>
 
 #define RUN_TIME
@@ -31,9 +33,9 @@
   using namespace boost::enums;
 
   std::cout << __LINE__ << std::endl;
+#ifndef BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
   BOOST_STATIC_ASSERT(( boost::conversion::is_extrinsically_explicitly_convertible< const char*, EnumClass >::value));
   BOOST_STATIC_ASSERT(( boost::conversion::is_extrinsically_explicitly_convertible< std::string, EnumClass >::value));
-
   { // The wrapper can be constructed from a valid const char* representation
     std::string str="Enum2";
     EnumClass e = boost::conversion::explicit_convert_to<EnumClass>(str);
@@ -41,6 +43,7 @@
     EnumClass e2 = boost::conversion::explicit_convert_to<EnumClass>("Enum2");
     BOOST_TEST(e2==EnumClass::Enum2);
   }
+#endif
 #ifdef COMPILE_TIME2
   { // Construction of the wrapper from const char * compile fails
     const char* ptr = 0;
@@ -53,12 +56,14 @@
     // ... fail
   }
 #endif
+#ifndef BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
   { // The wrapper can be constructed from a valid std::string representation
     std::string str = "Enum2";
     EnumClass e = explicit_convert_to<EnumClass>(str);
     BOOST_TEST(e==EnumClass::Enum2);
     BOOST_TEST(strcmp(c_str(e),"Enum2")==0);
   }
+#endif
   std::cout << __LINE__ << std::endl;
 #ifdef COMPILE_TIME2
   { // Construction of the wrapper from const char * compile fails

Modified: sandbox/enums/libs/enums/test/EnumClass.hpp
==============================================================================
--- sandbox/enums/libs/enums/test/EnumClass.hpp (original)
+++ sandbox/enums/libs/enums/test/EnumClass.hpp 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -29,115 +29,198 @@
 #include <cassert>
 #include <cstring>
 #include <string>
-#include <boost/enums/scoped/enum_class_cons.hpp>
-#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/scoped/enum_class_cons.hpp>
+//#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,
- Enum1,
- Enum2
-} BOOST_ENUM_CLASS_CONS_END(EnumClass, unsigned char)
-#else
+ enum class EnumClass : unsigned char
+ {
+ Default
+ = 3
+ , Enum1
+ , Enum2
 
-struct EnumClassNS {
- enum type {
- Default = 3,
- Enum1,
- Enum2
+ }
+ ;
+ inline bool operator ==(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ ==
+ (unsigned char)(rhs);
+ }
+ inline bool operator !=(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ !=
+ (unsigned char)(rhs);
+ }
+ inline bool operator <(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ <
+ (unsigned char)(rhs);
+ }
+ inline bool operator <=(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ <=
+ (unsigned char)(rhs);
+ }
+ inline bool operator >(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ >
+ (unsigned char)(rhs);
+ }
+ inline bool operator >=(EnumClass lhs, EnumClass rhs) {
+ return (unsigned char)(lhs)
+ >=
+ (unsigned char)(rhs);
+ }
+
+ namespace boost {
+ namespace enums {
+ namespace meta {
+ template <>
+ struct underlying_type<EnumClass>
+ {
+ typedef unsigned char type;
+ };
+ }
+ }
+ }
+ namespace boost {
+ namespace conversion {
+ template <>
+ struct explicit_converter_cp<EnumClass,unsigned char> : true_type {
+ EnumClass operator()(unsigned char const &v) {
+ return EnumClass(v);
+ }
+ };
+ template <>
+ struct explicit_converter_cp<unsigned char,EnumClass> : true_type {
+ unsigned char operator()(EnumClass const &v) {
+ return boost::enums::underlying_value(v);
+ }
+ };
+ template <>
+ struct explicit_converter_cp<EnumClass,boost::enums::native_type<EnumClass>::type>
+ : true_type {
+ EnumClass operator()(boost::enums::native_type<EnumClass>::type const &v) {
+ return v;
+ }
+ };
+ }
+ }
+ namespace boost {
+ namespace enums {
+ namespace meta {
+ template <>
+ struct size<EnumClass>
+ {
+ enum {value=3};
   };
-};
-typedef boost::enums::enum_class_cons<EnumClassNS, unsigned char> EnumClass;
-
-#endif
-BOOST_ENUMS_SPECIALIZATIONS(EnumClass, unsigned char)
-
-//! conversion from c-string.
-inline EnumClass explicit_convert_to(const char* str
- , boost::conversion::dummy::type_tag<EnumClass> const&
-)
-{
- if (strcmp(str, "Default") ==0) { return boost::explicit_convert_to<EnumClass>(EnumClass::Default); }
- if (strcmp(str, "Enum1") ==0) { return boost::explicit_convert_to<EnumClass>(EnumClass::Enum1); }
- if (strcmp(str, "Enum2") ==0) { return boost::explicit_convert_to<EnumClass>(EnumClass::Enum2); }
- throw "invalid string for EnumClass";
-}
-
-//! conversion from std::string.
-inline EnumClass explicit_convert_to(const std::string& str
- , boost::conversion::dummy::type_tag<EnumClass> const&
-)
-{
- return boost::explicit_convert_to<EnumClass>(str.c_str());
-}
+ template <>
+ struct pos<EnumClass, EnumClass :: Default>
+ {
+ static const std::size_t value = 0;
+ };
+ template <>
+ struct val<EnumClass, 0>
+ {
+ static const
+ boost::enums::native_type<EnumClass>::type value =
+ EnumClass::Default;
+ }; template <>
+ struct pos<EnumClass, EnumClass :: Enum1>
+ {
+ static const std::size_t value = 1;
+ };
+ BOOST_STATIC_ASSERT((::boost::enums::meta::pos<EnumClass, EnumClass::Enum1>::value == 1));
 
-//!explicit conversion to c-string.
-inline const char* c_str(EnumClass e)
-{
+ template <>
+ struct val<EnumClass, 1>
+ {
+ static const
+ boost::enums::native_type<EnumClass>::type value =
+ EnumClass::Enum1;
+ }; template <>
+ struct pos<EnumClass, EnumClass :: Enum2>
+ {
+ static const std::size_t value = 2;
+ };
+ template <>
+ struct val<EnumClass, 2>
+ {
+ static const
+ boost::enums::native_type<EnumClass>::type value =
+ EnumClass::Enum2;
+ };
+ template <>
+ struct enum_traits<EnumClass>
+ : linear_enum_traiter<EnumClass>
+ {};
+ }
+ }
+ }
+ namespace boost {
+ namespace conversion {
+ template <>
+ struct explicit_converter_cp<EnumClass,const char*> : true_type {
+ EnumClass operator()(const char* str) {
+ if (strcmp(
+ str,
+ "Default"
+ ) == 0)
+ {
+ return boost::conversion::explicit_convert_to<EnumClass>(
+ EnumClass::Default
+ );
+ } if (strcmp(
+ str,
+ "Enum1"
+ ) == 0)
+ {
+ return boost::conversion::explicit_convert_to<EnumClass>(
+ EnumClass::Enum1
+ );
+ } if (strcmp(
+ str,
+ "Enum2"
+ ) == 0)
+ {
+ return boost::conversion::explicit_convert_to<EnumClass>(
+ EnumClass::Enum2
+ );
+ }
+ throw "invalid string for "
+ "EnumClass";
+ }
+ };
+ template <>
+ struct explicit_converter_cp<EnumClass,std::string> : true_type {
+ EnumClass operator()(std::string const& str) {
+ return boost::conversion::explicit_convert_to<EnumClass>(
+ str.c_str()
+ );
+ }
+ };
+ }
+ }
+ inline
+ const char* c_str(EnumClass e)
+ {
     switch (boost::enums::native_value(e))
     {
- case EnumClass::Default : return("EnumClass::Default");
- case EnumClass::Enum1: return("EnumClass::Enum1");
- case EnumClass::Enum2 : return("EnumClass::Enum2");
- default:
- return("EnumClass::???");
+ case EnumClass::Default :
+ return("Default"); case EnumClass::Enum1 :
+ return("Enum1"); case EnumClass::Enum2 :
+ return("Enum2");
+ default:
+ throw "invalid value for "
+ "EnumClass";
     }
-}
-
-namespace boost {
- namespace enums {
- namespace meta {
-
- template <>
- struct size<EnumClass>
- {
- BOOST_STATIC_CONSTEXPR std::size_t value = 3;
- };
- template <>
- struct pos<EnumClass, EnumClass::Default>
- {
- BOOST_STATIC_CONSTEXPR std::size_t value = 0;
- };
- template <>
- struct pos<EnumClass, EnumClass::Enum1>
- {
- BOOST_STATIC_CONSTEXPR std::size_t value = 1;
- };
- template <>
- struct pos<EnumClass, EnumClass::Enum2>
- {
- BOOST_STATIC_CONSTEXPR std::size_t value = 2;
- };
-
- template <>
- struct val<EnumClass, 0>
- {
- BOOST_STATIC_CONSTEXPR boost::enums::native_type<EnumClass>::type value = EnumClass::Default;
- };
- template <>
- struct val<EnumClass, 1>
- {
- BOOST_STATIC_CONSTEXPR boost::enums::native_type<EnumClass>::type value = EnumClass::Enum1;
- };
- template <>
- struct val<EnumClass, 2>
- {
- BOOST_STATIC_CONSTEXPR boost::enums::native_type<EnumClass>::type value = EnumClass::Enum2;
- };
- template <>
- struct enum_traits<EnumClass>
- : linear_enum_traiter<EnumClass>
- {
- };
- } // namespace meta
   }
-}
 #else
 
 

Modified: sandbox/enums/libs/enums/test/ExType.hpp
==============================================================================
--- sandbox/enums/libs/enums/test/ExType.hpp (original)
+++ sandbox/enums/libs/enums/test/ExType.hpp 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -13,7 +13,7 @@
 #ifndef EXTYPE_HPP
 #define EXTYPE_HPP
 
-#include <boost/enums/scoped/enum_type_cons.hpp>
+//#include <boost/enums/scoped/enum_type_cons.hpp>
 
 #include <boost/enums/scoped.hpp>
 #include <boost/enums/ordinal.hpp>

Modified: sandbox/enums/libs/enums/test/Jamfile.v2
==============================================================================
--- sandbox/enums/libs/enums/test/Jamfile.v2 (original)
+++ sandbox/enums/libs/enums/test/Jamfile.v2 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -37,8 +37,7 @@
         <toolset>gcc-mingw-4.5.0:<cxxflags>-Wno-missing-field-initializers
         <toolset>gcc-mingw-4.5.0:<cxxflags>-fdiagnostics-show-option
         <toolset>msvc:<cxxflags>/wd4127
- #<define>BOOST_OPAQUE_NOT_DEPENDS_ON_CONVERSION
- <define>BOOST_CONVERSION_DOUBLE_CP
+ <define>BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
     ;
 
    test-suite "enum_class"

Modified: sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_invalid_int_fail.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_invalid_int_fail.cpp (original)
+++ sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_invalid_int_fail.cpp 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -15,11 +15,12 @@
 
 int main() {
   using namespace boost::enums;
+#ifndef BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
 
   { // Explicit conversion from invalid int results in run-time error (undefined behavior)
     EnumClass e(boost::conversion::explicit_convert_to<EnumClass>((unsigned char)(6)));
     BOOST_TEST((unsigned char)(native_value(e))==(unsigned char)(6));
   }
-
+#endif
   return boost::report_errors();
 }

Modified: sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_valid_int_pass.cpp
==============================================================================
--- sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_valid_int_pass.cpp (original)
+++ sandbox/enums/libs/enums/test/enum_class/conversion/explicit_conversion_from_valid_int_pass.cpp 2011-08-23 19:51:39 EDT (Tue, 23 Aug 2011)
@@ -15,11 +15,12 @@
 
 int main() {
   using namespace boost::enums;
+#ifndef BOOST_ENUMS_NOT_DEPENDS_ON_CONVERSION
 
   { // Explicit conversion from valid int works
     EnumClass e(boost::conversion::explicit_convert_to<EnumClass>((unsigned char)(4)));
     BOOST_TEST(e==EnumClass::Enum1);
   }
-
+#endif
   return boost::report_errors();
 }


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