libtenum
|
00001 00008 #ifndef TENUM_BIT_FIELD_HPP_ 00009 #define TENUM_BIT_FIELD_HPP_ 00010 00011 #include <tenum/static_enum.hpp> 00012 00013 #define TENUM_SERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m) \ 00014 template< > \ 00015 template< > \ 00016 inline ::std::string \ 00017 enum_helper< TENUM_TYPE(type_m) >::serialize_impl< false, true >(TENUM_TYPE(type_m) const value_in, \ 00018 ::boost::false_type const&, \ 00019 ::boost::true_type const&) 00020 00021 #define TENUM_SERIALIZE_BIT_FIELD_DECLARATION(type_m,tuples_m) \ 00022 TENUM_SERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m); 00023 00024 #define TENUM_SERIALIZE_BIT_FIELD_DEFINITION_EACH(_,type_m,tuple_m) \ 00025 if (TENUM_CAST_UINT(value_in & TENUM_VALUE(type_m,TENUM_TUPLE_GET_VALUE(tuple_m))) != 0) { \ 00026 stream << TENUM_DEFAULT_SEPARATOR_BIT_FIELD; \ 00027 stream << serialize_impl< false, false >(TENUM_VALUE(type_m,TENUM_TUPLE_GET_VALUE(tuple_m)), \ 00028 ::boost::false_type(), \ 00029 ::boost::false_type()); \ 00030 } 00031 00032 #define TENUM_SERIALIZE_BIT_FIELD_DEFINITION(type_m,tuples_m) \ 00033 TENUM_SERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m) { \ 00034 ::std::ostringstream stream; \ 00035 BOOST_PP_SEQ_FOR_EACH(TENUM_SERIALIZE_BIT_FIELD_DEFINITION_EACH,type_m,tuples_m) \ 00036 \ 00037 ::std::string out = stream.str(); \ 00038 return out.substr(::std::min(out.length(), \ 00039 sizeof(TENUM_DEFAULT_SEPARATOR_BIT_FIELD) / sizeof(::std::string::value_type))); \ 00040 } 00041 00042 #define TENUM_DESERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m) \ 00043 template< > \ 00044 template< > \ 00045 inline TENUM_TYPE(type_m) \ 00046 enum_helper< TENUM_TYPE(type_m) >::deserialize_impl< false, true >(::std::string const& value_in, \ 00047 ::boost::false_type const&, \ 00048 ::boost::true_type const&) 00049 00050 #define TENUM_DESERIALIZE_BIT_FIELD_DECLARATION(type_m,tuples_m) \ 00051 TENUM_DESERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m); 00052 00053 #define TENUM_DESERIALIZE_BIT_FIELD_DEFINITION(type_m,tuples_m) \ 00054 TENUM_DESERIALIZE_BIT_FIELD_SIGNATURE(type_m,tuples_m) { \ 00055 ::std::size_t separator_pos = value_in.find(TENUM_DEFAULT_SEPARATOR_BIT_FIELD); \ 00056 TENUM_TYPE(type_m) flag = deserialize_impl< false, false >(value_in.substr(0, separator_pos), \ 00057 ::boost::false_type(), \ 00058 ::boost::false_type()); \ 00059 if (separator_pos != ::std::string::npos) { \ 00060 TENUM_TYPE(type_m) trailing_flags = deserialize(value_in.substr(separator_pos + 1)); \ 00061 return TENUM_CAST_ENUM(type_m, TENUM_CAST_UINT(flag) | TENUM_CAST_UINT(trailing_flags)); \ 00062 } else { \ 00063 return flag; \ 00064 } \ 00065 } 00066 00067 #define TENUM_BIT_FIELD_SERIALIZATION_DECLARATION(type_m,tuples_m) \ 00068 namespace tenum { \ 00069 template< > struct is_bit_field< TENUM_TYPE(type_m) > : ::boost::true_type {}; \ 00070 TENUM_GET_BASE_OF_DECLARATION(type_m,tuples_m) \ 00071 TENUM_SERIALIZE_DECLARATION(type_m,tuples_m) \ 00072 TENUM_DESERIALIZE_DECLARATION(type_m,tuples_m) \ 00073 TENUM_SERIALIZE_BIT_FIELD_DECLARATION(type_m,tuples_m) \ 00074 TENUM_DESERIALIZE_BIT_FIELD_DECLARATION(type_m,tuples_m) \ 00075 } \ 00076 TENUM_STREAM_OPERATORS_DECLARATION(type_m) 00077 00078 #define TENUM_BIT_FIELD_SERIALIZATION_DEFINITION(type_m,tuples_m) \ 00079 namespace tenum { \ 00080 TENUM_GET_BASE_OF_DEFINITION(type_m,tuples_m) \ 00081 TENUM_SERIALIZE_DEFINITION(type_m,tuples_m) \ 00082 TENUM_DESERIALIZE_DEFINITION(type_m,tuples_m) \ 00083 TENUM_SERIALIZE_BIT_FIELD_DEFINITION(type_m,tuples_m) \ 00084 TENUM_DESERIALIZE_BIT_FIELD_DEFINITION(type_m,tuples_m) \ 00085 } \ 00086 TENUM_STREAM_OPERATORS_DEFINITION(type_m) 00087 00088 #define TENUM_BIT_FIELD_DECLARATION(type_m,tuples_m,underlying_m) \ 00089 TENUM_ENUM_DEFINITION(type_m,BOOST_PP_SEQ_PUSH_FRONT(tuples_m,TENUM_TUPLE_VALUED(lte_unknown,0)),underlying_m) \ 00090 TENUM_ENUM_OPERATORS_DECLARATION(type_m) \ 00091 TENUM_BIT_FIELD_OPERATORS_DECLARATION(type_m) \ 00092 TENUM_BIT_FIELD_SERIALIZATION_DECLARATION(type_m,tuples_m) 00093 00094 #define TENUM_BIT_FIELD_DEFINITION(type_m,tuples_m) \ 00095 TENUM_ENUM_OPERATORS_DEFINITION(type_m) \ 00096 TENUM_BIT_FIELD_OPERATORS_DEFINITION(type_m) \ 00097 TENUM_BIT_FIELD_SERIALIZATION_DEFINITION(type_m,tuples_m) 00098 00099 #define TENUM_BIT_FIELD_I(type_m,tuples_m,underlying_m) \ 00100 TENUM_BIT_FIELD_DECLARATION(type_m,tuples_m,underlying_m) \ 00101 TENUM_BIT_FIELD_DEFINITION(type_m,tuples_m) 00102 00103 #define TENUM_BIT_FIELD(type_m,tuples_m,underlying_m) \ 00104 TENUM_BIT_FIELD_I(type_m,tuples_m,:underlying_m) 00105 #define TENUM_SIMPLE_BIT_FIELD(type_m,values_m) \ 00106 TENUM_BIT_FIELD_I(type_m,TENUM_ENUM_VALUES_COMPLETE(values_m),BOOST_PP_EMPTY()) 00107 00108 #endif /* TENUM_BIT_FIELD_HPP_ */