From 20332e24e592860638a623413da53bd180ae4400 Mon Sep 17 00:00:00 2001 From: Anthony Williams Date: Thu, 10 May 2012 17:06:15 +0000 Subject: [PATCH] Combine scoped enum emulation from thread library into detail/scoped_enum_emulation.hpp [SVN r78407] --- .../boost/detail/scoped_enum_emulation.hpp | 110 +++++++++++++++++- 1 file changed, 104 insertions(+), 6 deletions(-) diff --git a/include/boost/detail/scoped_enum_emulation.hpp b/include/boost/detail/scoped_enum_emulation.hpp index e695a20..0defa97 100644 --- a/include/boost/detail/scoped_enum_emulation.hpp +++ b/include/boost/detail/scoped_enum_emulation.hpp @@ -1,6 +1,8 @@ // scoped_enum_emulation.hpp ---------------------------------------------------------// // Copyright Beman Dawes, 2009 +// Copyright (C) 2012 Vicente J. Botet Escriba +// Copyright (C) 2012 Anthony Williams // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt @@ -38,19 +40,115 @@ #define BOOST_SCOPED_ENUM_EMULATION_HPP #include +#include + +namespace boost +{ + +#ifdef BOOST_NO_SCOPED_ENUMS + template + struct underlying_type + { + typedef typename EnumType::underlying_type type; + }; + + template + UnderlyingType underlying_cast(EnumType 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 + UnderlyingType underlying_cast(EnumType v) + { + return static_cast(v); + } + + template + inline + EC native_value(EC e) + { + return e; + } + +#endif +} + #ifdef BOOST_NO_SCOPED_ENUMS -# define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_type -# define BOOST_SCOPED_ENUM_END }; -# define BOOST_SCOPED_ENUM(name) name::enum_type +#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_START(name) enum class name -# define BOOST_SCOPED_ENUM_END -# define BOOST_SCOPED_ENUM(name) name +#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR #endif +#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \ + struct EnumType { \ + typedef UnderlyingType underlying_type; \ + EnumType() {} \ + explicit EnumType(underlying_type v) : v_(v) {} \ + underlying_type underlying() const { return v_; } \ + BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \ + private: \ + underlying_type v_; \ + typedef EnumType self_type; \ + public: \ + enum enum_type + +#define BOOST_SCOPED_ENUM_DECLARE_END2() \ + enum_type native() const { return enum_type(v_); } \ + friend bool operator ==(self_type lhs, enum_type rhs) { return enum_type(lhs.v_)==rhs; } \ + friend bool operator ==(enum_type lhs, self_type rhs) { return lhs==enum_type(rhs.v_); } \ + friend bool operator !=(self_type lhs, enum_type rhs) { return enum_type(lhs.v_)!=rhs; } \ + friend bool operator !=(enum_type lhs, self_type rhs) { return lhs!=enum_type(rhs.v_); } \ + }; + +#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \ + ; \ + EnumType(enum_type v) : v_(v) {} \ + BOOST_SCOPED_ENUM_DECLARE_END2() + +#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \ + BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int) + +#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type +#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType + +#else // BOOST_NO_SCOPED_ENUMS + +#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType:UnderlyingType +#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType +#define BOOST_SCOPED_ENUM_DECLARE_END2() +#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ; + +#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType +#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType + +#endif // BOOST_NO_SCOPED_ENUMS + +#define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name) +#define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2() +#define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name) + #endif // BOOST_SCOPED_ENUM_EMULATION_HPP