Skip to content

Commit

Permalink
Merge pull request #22 from cheparukhin/master
Browse files Browse the repository at this point in the history
Fix name resolution errors
  • Loading branch information
cheparukhin committed Jul 8, 2016
2 parents 18cbeb0 + c71b891 commit f520b39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 47 deletions.
77 changes: 30 additions & 47 deletions enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ constexpr const char *_final_ ## index = \

// The enums proper.

#define BETTER_ENUMS_NS(EnumType) better_enums::_data_ ## EnumType
#define BETTER_ENUMS_NS(EnumType) better_enums_data_ ## EnumType

#ifdef BETTER_ENUMS_VC2008_WORKAROUNDS

Expand All @@ -574,12 +574,10 @@ constexpr const char *_final_ ## index = \
DeclareInitialize, DefineInitialize, CallInitialize, \
Enum, Underlying, ...) \
\
namespace better_enums { \
namespace _data_ ## Enum { \
namespace better_enums_data_ ## Enum { \
\
BETTER_ENUMS_ID(GenerateSwitchType(Underlying, __VA_ARGS__)) \
\
} \
} \
\
class Enum { \
Expand Down Expand Up @@ -662,8 +660,7 @@ class Enum { \
friend struct ::better_enums::_initialize_at_program_start<Enum>; \
}; \
\
namespace better_enums { \
namespace _data_ ## Enum { \
namespace better_enums_data_ ## Enum { \
\
static ::better_enums::_initialize_at_program_start<Enum> \
_force_initialization; \
Expand All @@ -675,7 +672,6 @@ BETTER_ENUMS_CONSTEXPR_ const Enum _value_array[] = \
\
BETTER_ENUMS_ID(GenerateStrings(Enum, __VA_ARGS__)) \
\
} \
} \
\
BETTER_ENUMS_CONSTEXPR_ inline const Enum \
Expand Down Expand Up @@ -834,7 +830,33 @@ BETTER_ENUMS_CONSTEXPR_ inline bool operator <=(const Enum &a, const Enum &b) \
BETTER_ENUMS_CONSTEXPR_ inline bool operator >(const Enum &a, const Enum &b) \
{ return a._to_integral() > b._to_integral(); } \
BETTER_ENUMS_CONSTEXPR_ inline bool operator >=(const Enum &a, const Enum &b) \
{ return a._to_integral() >= b._to_integral(); }
{ return a._to_integral() >= b._to_integral(); } \
\
\
template <typename Char, typename Traits> \
std::basic_ostream<Char, Traits>& \
operator <<(std::basic_ostream<Char, Traits>& stream, const Enum &value) \
{ \
return stream << value._to_string(); \
} \
\
template <typename Char, typename Traits> \
std::basic_istream<Char, Traits>& \
operator >>(std::basic_istream<Char, Traits>& stream, Enum &value) \
{ \
std::basic_string<Char, Traits> buffer; \
\
stream >> buffer; \
::better_enums::optional<Enum> converted = \
Enum::_from_string_nothrow(buffer.c_str()); \
\
if (converted) \
value = *converted; \
else \
stream.setstate(std::basic_istream<Char, Traits>::failbit); \
\
return stream; \
}



Expand Down Expand Up @@ -1140,45 +1162,6 @@ BETTER_ENUMS_CONSTEXPR_ map<Enum, T> make_map(T (*f)(Enum))
return map<Enum, T>(f);
}



// Stream I/O operators.

// This template is used as a sort of enable_if for SFINAE. It should be
// possible to use std::enable_if, however <type_traits> is not available in
// C++98. Non-char streams are currently not supported.
template <typename T, typename Enum>
struct only_if_enum { typedef T type; };

}

template <typename Char, typename Traits, typename Enum>
inline typename better_enums::only_if_enum<std::basic_ostream<Char, Traits>,
typename Enum::_enumerated>::type&
operator <<(std::basic_ostream<Char, Traits>& stream, const Enum &value)
{
return stream << value._to_string();
}

template <typename Char, typename Traits, class Enum>
inline typename better_enums::only_if_enum<std::basic_istream<Char, Traits>,
typename Enum::_enumerated>::type&
operator >>(std::basic_istream<Char, Traits>& stream, Enum &value)
{
std::basic_string<Char, Traits> buffer;

stream >> buffer;
better_enums::optional<Enum> converted =
Enum::_from_string_nothrow(buffer.c_str());

if (converted)
value = *converted;
else
stream.setstate(std::basic_istream<Char, Traits>::failbit);

return stream;
}



#endif // #ifndef BETTER_ENUMS_ENUM_H
12 changes: 12 additions & 0 deletions test/cxxtest/general.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iosfwd>
#include <stdexcept>
#include <cxxtest/TestSuite.h>
#include <enum.h>
Expand Down Expand Up @@ -125,6 +126,17 @@ static_assert_1(*Channel::_values().begin() == +Channel::Red);
static_assert_1(*(Channel::_values().end() - 1) == +Channel::Blue);
static_assert_1(Channel::_values()[1] == +Channel::Green);

namespace name_clash_test {

struct Foo {};
std::ostream& operator<<(std::ostream&, Foo);

BETTER_ENUM(Enum, int, ONE, TWO, THREE)

static_assert_1((std::is_same<decltype(std::declval<std::ostream&>() << +Enum::ONE), std::ostream&>()));

}

#ifdef BETTER_ENUMS_CONSTEXPR_TO_STRING

constexpr bool same_string(const char *r, const char *s, size_t index = 0)
Expand Down

0 comments on commit f520b39

Please sign in to comment.