rice/Enum.hpp in rice-3.0.0 vs rice/Enum.hpp in rice-4.0.0

- old
+ new

@@ -1,117 +1,67 @@ #ifndef Rice__Enum__hpp_ #define Rice__Enum__hpp_ -#include "to_from_ruby_defn.hpp" -#include "Address_Registration_Guard.hpp" -#include "Array.hpp" -#include "Hash.hpp" -#include "String.hpp" -#include "Module.hpp" #include "Data_Type.hpp" +#include <map> namespace Rice { + /*! + * \example enum/sample_enum.cpp + */ -//! Default traits for the Enum class template. -template<typename Enum_T> -struct Default_Enum_Traits -{ - //! Converts the enum value to a long. - static long as_long(Enum_T value); -}; + //! A wrapper for enumerated types. + /*! Provides a simple type-safe wrapper for enumerated types. At the + * ruby level, the class will have convenience methods for iterating + * over all the defined enum values, converting the values to strings, + * and more. + * + * \param Enum_T the enumerated type + * + * Example: + * \code + * enum Color { Red, Green, Blue }; + * Enum<Color> rb_cColor = define_enum<Color>("Color") + * .define_value("Red", Red) + * .define_value("Green", Green) + * .define_value("Blue", Blue); + * \endcode + */ + template<typename Enum_T> + class Enum : public Data_Type<Enum_T> + { + using Underlying_T = std::underlying_type_t<Enum_T>; -/*! - * \example enum/sample_enum.cpp - */ + public: -//! A wrapper for enumerated types. -/*! Provides a simple type-safe wrapper for enumerated types. At the - * ruby level, the class will have convenience methods for iterating - * over all the defined enum values, converting the values to strings, - * and more. - * - * \param Enum_T the enumerated type - * \param Enum_Traits specifies the traits of the enumerated type. - * - * Example: - * \code - * enum Color { Red, Green, Blue }; - * Enum<Color> rb_cColor = define_enum<Color>("Color") - * .define_value("Red", Red) - * .define_value("Green", Green) - * .define_value("Blue", Blue); - * \endcode - */ -template<typename Enum_T, typename Enum_Traits = Default_Enum_Traits<Enum_T> > -class Enum - : public Module_impl<Data_Type<Enum_T>, Enum<Enum_T, Enum_Traits> > -{ -public: - //! Default constructor. - Enum(); + Enum() = default; - //! Construct and initialize. - Enum( - char const * name, - Module module = rb_cObject); + //! Construct and initialize. + Enum(char const* name, Module module = rb_cObject); - //! Copy constructor. - Enum(Enum const & other); + //! Define a new enum value. + /*! \param name the name of the enum value. + * \param value the value to associate with name. + * \return *this + */ + Enum<Enum_T>& define_value(std::string name, Enum_T value); - //! Assignment operator. - Enum & operator=(Enum const & other); + //! Maps an enum value to the correct Ruby object + /*! \param klass The bound Ruby class + * \param enumValue The enum value + * \return Object - The Ruby wrapper */ + static Object from_enum(Class klass, Enum_T enumValue); - //! Destructor. - virtual ~Enum(); + private: + void define_methods(Data_Type<Enum_T> klass); - //! Define a new enum value. - /*! \param name the name of the enum value. - * \param value the value to associate with name. - * \return *this - */ - Enum<Enum_T, Enum_Traits> & define_value( - char const * name, - Enum_T value); + static inline std::map<Enum_T, std::string> valuesToNames_; + }; - void swap(Enum & other); - -private: - //! Initialize the enum type. - /*! Must be called only once. - * \param name the name of the class to define - * \param module the module in which to place the enum class. - * \return *this - */ - Enum<Enum_T, Enum_Traits> & initialize( - char const * name, - Module module = rb_cObject); - -private: - static Object each(Object self); - static Object to_s(Object self); - static Object to_i(Object self); - static Object inspect(Object self); - static Object compare(Object lhs, Object rhs); - static Object eql(Object lhs, Object rhs); - static Object hash(Object self); - static Object from_int(Class klass, Object i); - -private: - Array enums_; - Address_Registration_Guard enums_guard_; - - Hash names_; - Address_Registration_Guard names_guard_; -}; - -template<typename T> -Enum<T> define_enum( - char const * name, - Module module = rb_cObject); - + template<typename T> + Enum<T> define_enum(char const* name, Module module = rb_cObject); } // namespace Rice #include "Enum.ipp" -#endif // Rice__Enum__hpp_ - +#endif // Rice__Enum__hpp_ \ No newline at end of file