generic ``to_integral`` helper function (convert strongly typed enumeration values to underlying type)

This commit is contained in:
artemp 2013-12-11 13:56:11 -05:00
parent 6a0e593d87
commit 7cf452da77
2 changed files with 12 additions and 7 deletions

View file

@ -370,11 +370,16 @@ boost::optional<T> get_optional(symbolizer_base const& sym, keys key)
return boost::optional<T>();
}
template<typename Enum>
constexpr auto to_integral(Enum e) -> typename std::underlying_type<Enum>::type
{
return static_cast<typename std::underlying_type<Enum>::type>(e);
}
typedef std::tuple<const char*, mapnik::symbolizer_base::value_type, std::function<std::string(enumeration_wrapper)> > property_meta_type;
property_meta_type const& get_meta(mapnik::keys key);
mapnik::keys get_key(std::string const& name);
// concrete symbolizer types
struct MAPNIK_DECL point_symbolizer : public symbolizer_base {};
struct MAPNIK_DECL line_symbolizer : public symbolizer_base {};

View file

@ -101,13 +101,13 @@ mapnik::keys get_key(std::string const& name)
{
std::string name_copy(name);
boost::algorithm::replace_all(name_copy,"_","-");
for (unsigned i=0;i<static_cast<unsigned>(keys::MAX_SYMBOLIZER_KEY);++i)
for (unsigned i=0; i< to_integral(keys::MAX_SYMBOLIZER_KEY) ; ++i)
{
property_meta_type const& item = key_meta[i];
if (name_copy == std::get<0>(item))
{
return static_cast<mapnik::keys>(i);
}
property_meta_type const& item = key_meta[i];
if (name_copy == std::get<0>(item))
{
return static_cast<mapnik::keys>(i);
}
}
throw std::runtime_error("no key found for '" + name + "'");
return static_cast<mapnik::keys>(0);