From fc8de511c6fec6b33cf580fcefe78d3bd52802fe Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Sun, 29 Jan 2012 18:27:26 +0000 Subject: [PATCH] Thread: Added detail/scoped_enumm.hpp file and adaptat enum classes to the new interface [SVN r76777] --- include/boost/thread/cv_status.hpp | 8 +- include/boost/thread/detail/config.hpp | 32 ++---- include/boost/thread/detail/scoped_enum.hpp | 113 ++++++++++++++++++++ include/boost/thread/future.hpp | 23 ++-- 4 files changed, 136 insertions(+), 40 deletions(-) create mode 100644 include/boost/thread/detail/scoped_enum.hpp diff --git a/include/boost/thread/cv_status.hpp b/include/boost/thread/cv_status.hpp index e6dcd0c3..eb86ef00 100644 --- a/include/boost/thread/cv_status.hpp +++ b/include/boost/thread/cv_status.hpp @@ -9,18 +9,18 @@ #ifndef BOOST_THREAD_CV_STATUS_HPP #define BOOST_THREAD_CV_STATUS_HPP -#include +#include namespace boost { // enum class cv_status; - BOOST_DECLARE_STRONG_ENUM_BEGIN(cv_status) + BOOST_SCOPED_ENUM_DECLARE_BEGIN(cv_status) { no_timeout, timeout - }; - BOOST_DECLARE_STRONG_ENUM_END(cv_status) + } + BOOST_SCOPED_ENUM_DECLARE_END(cv_status) } #endif // header diff --git a/include/boost/thread/detail/config.hpp b/include/boost/thread/detail/config.hpp index 274a23d0..9a4ca4cb 100644 --- a/include/boost/thread/detail/config.hpp +++ b/include/boost/thread/detail/config.hpp @@ -10,7 +10,6 @@ #include #include - #if !defined BOOST_THREAD_VERSION #define BOOST_THREAD_VERSION 1 #else @@ -19,34 +18,17 @@ #endif #endif -#if ! defined BOOST_THREAD_DONT_USE_CHRONO +#if ! defined BOOST_THREAD_DONT_USE_SYSTEM +#define BOOST_THREAD_USES_SYSTEM +#endif + +#if ! defined BOOST_THREAD_DONT_USE_CHRONO && ! defined BOOST_THREAD_DONT_USE_SYSTEM #define BOOST_THREAD_USES_CHRONO #endif +#if ! defined BOOST_THREAD_DONT_USE_MOVE #define BOOST_THREAD_USES_MOVE - -#ifdef BOOST_NO_SCOPED_ENUMS -#define BOOST_DECLARE_STRONG_ENUM_BEGIN(x) \ - struct x { \ - enum enum_type - -#define BOOST_DECLARE_STRONG_ENUM_END(x) \ - enum_type v_; \ - inline x() {} \ - inline x(enum_type v) : v_(v) {} \ - inline operator int() const {return v_;} \ - friend inline bool operator ==(x lhs, int rhs) {return lhs.v_==rhs;} \ - friend inline bool operator ==(int lhs, x rhs) {return lhs==rhs.v_;} \ - friend inline bool operator !=(x lhs, int rhs) {return lhs.v_!=rhs;} \ - friend inline bool operator !=(int lhs, x rhs) {return lhs!=rhs.v_;} \ - }; - -#define BOOST_STRONG_ENUM_NATIVE(x) x::enum_type -#else // BOOST_NO_SCOPED_ENUMS -#define BOOST_DECLARE_STRONG_ENUM_BEGIN(x) enum class x -#define BOOST_DECLARE_STRONG_ENUM_END(x) -#define BOOST_STRONG_ENUM_NATIVE(x) x -#endif // BOOST_NO_SCOPED_ENUMS +#endif #if BOOST_WORKAROUND(__BORLANDC__, < 0x600) # pragma warn -8008 // Condition always true/false diff --git a/include/boost/thread/detail/scoped_enum.hpp b/include/boost/thread/detail/scoped_enum.hpp new file mode 100644 index 00000000..5358263c --- /dev/null +++ b/include/boost/thread/detail/scoped_enum.hpp @@ -0,0 +1,113 @@ +// Copyright (C) 2012 +// Vicente J. Botet Escriba +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_THREAD_DETAIL_SCOPED_ENUM_HPP +#define BOOST_THREAD_DETAIL_SCOPED_ENUM_HPP + +#include +#include + +namespace boost +{ + +#ifdef BOOST_NO_SCOPED_ENUMS + template + struct underlying_type + { + typedef typename NT::underlying_type type; + }; + + template + UT underlying_cast(NT v) + { + return v.underlying(); + } + + template + inline + typename EC::enum_type native_value(EC e) + { + return e.native(); + } + +#else // BOOST_NO_SCOPED_ENUMS + + template + struct underlying_type + { + //typedef typename std::underlying_type::type type; + }; + + template + UT underlying_cast(NT v) + { + return static_cast(v); + } + + template + inline + EC native_value(EC e) + { + return e; + } + +#endif +} + + +#ifdef BOOST_NO_SCOPED_ENUMS + +#ifndef BOOST_NO_EXPLICIT_CONVERSION_OPERATORS + +#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \ + explicit operator underlying_type() const { return underlying(); } + +#else + +#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR + +#endif + +#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(NT, UT) \ + struct NT { \ + typedef UT underlying_type; \ + enum enum_type + +#define BOOST_SCOPED_ENUM_DECLARE_END(NT) \ + ; \ + NT() {} \ + NT(enum_type v) : v_(v) {} \ + explicit NT(underlying_type v) : v_(v) {} \ + underlying_type underlying() const { return v_; } \ + enum_type native() const { return enum_type(v_); } \ + BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \ + friend bool operator ==(NT lhs, enum_type rhs) { return enum_type(lhs.v_)==rhs; } \ + friend bool operator ==(enum_type lhs, NT rhs) { return lhs==enum_type(rhs.v_); } \ + friend bool operator !=(NT lhs, enum_type rhs) { return enum_type(lhs.v_)!=rhs; } \ + friend bool operator !=(enum_type lhs, NT rhs) { return lhs!=enum_type(rhs.v_); } \ + private: \ + underlying_type v_; \ + }; + +#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(NT) \ + BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(NT,int) + +#define BOOST_SCOPED_ENUM_NATIVE(NT) NT::enum_type +#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(NT) struct NT + +#else // BOOST_NO_SCOPED_ENUMS + +#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(NT,UT) enum class NT:UT +#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(NT) enum class NT +#define BOOST_SCOPED_ENUM_DECLARE_END(NT) ; + +#define BOOST_SCOPED_ENUM_NATIVE(NT) NT +#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(NT) enum class NT + +#endif // BOOST_NO_SCOPED_ENUMS + + +#endif // BOOST_THREAD_DETAIL_SCOPED_ENUM_HPP diff --git a/include/boost/thread/future.hpp b/include/boost/thread/future.hpp index bbc9b77e..255d13a1 100644 --- a/include/boost/thread/future.hpp +++ b/include/boost/thread/future.hpp @@ -8,6 +8,7 @@ #define BOOST_THREAD_FUTURE_HPP #include +#include #include #include #include @@ -45,14 +46,14 @@ namespace boost { //enum class future_errc - BOOST_DECLARE_STRONG_ENUM_BEGIN(future_errc) + BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc) { broken_promise, future_already_retrieved, promise_already_satisfied, no_state - }; - BOOST_DECLARE_STRONG_ENUM_END(future_errc) + } + BOOST_SCOPED_ENUM_DECLARE_END(future_errc) namespace system { @@ -66,22 +67,22 @@ namespace boost } //enum class launch - BOOST_DECLARE_STRONG_ENUM_BEGIN(launch) + BOOST_SCOPED_ENUM_DECLARE_BEGIN(launch) { async = 1, deferred = 2, any = async | deferred - }; - BOOST_DECLARE_STRONG_ENUM_END(launch) + } + BOOST_SCOPED_ENUM_DECLARE_END(launch) //enum class future_status - BOOST_DECLARE_STRONG_ENUM_BEGIN(future_status) + BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_status) { ready, timeout, deferred - }; - BOOST_DECLARE_STRONG_ENUM_END(future_status) + } + BOOST_SCOPED_ENUM_DECLARE_END(future_status) BOOST_THREAD_DECL const system::error_category& future_category(); @@ -92,14 +93,14 @@ namespace boost error_code make_error_code(future_errc e) { - return error_code(static_cast(e), boost::future_category()); + return error_code(underlying_cast(e), boost::future_category()); } inline BOOST_THREAD_DECL error_condition make_error_condition(future_errc e) { - return error_condition(static_cast(e), future_category()); + return error_condition(underlying_cast(e), future_category()); } }