Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name resolution errors #22

Merged
merged 3 commits into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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