2012-12-13 34 views
1

我在寫一些讀取XML字符串的C++,並使用XML文件中與屬性鍵匹配的值填充某些結構成員。目前,此處的第一次傳遞使得關鍵值對的stl :: unordered_map成爲可能。下一步是解釋值字符串並將它們作爲目標類型返回以存儲在結構中。有些類型有點複雜,但我有可以轉換它們的方法。使用mpl :: vector作爲函數表

我想要做的是使用mpl :: vector將鍵映射到使用get_value_ *方法轉換值的結構成員。我認爲它看起來有點像:

typedef boost::mpl mpl; 
using namespace std; 

template<string K, typename T, T& dest, boost::function<void(MapTypePtr, string, T& dest)> > 
struct MapperRow {}; 

struct Mapper : mpl::vector< 
    MapperRow < "death_star_radius", Length, death_star.radius, get_value_units >, 
    MapperRow < "death_star_energy", Energy, death_star.energy, get_value_units >, 
    MapperRow < "operational",  bool, death_star.operational, get_value_simple >, 
    MapperRow < "num_tie_fighters", int, death_star.tie_fighers, get_value_simple > 
> {}; 

長度和能量類型是boost :: units :: quantity的typedefs。

這可以通過boost元編程實現嗎?如果我在正確的軌道上,我該如何讓它運行?我是否需要迭代mpl :: vector?

+0

我不明白的問題,但你可以在模板聲明中不會像使用字符串那樣使用類。這隻適用於像'bool'或'unsigned'這樣的基本類型。 – Peter

+0

可能有mpl包裝類型,它以某種方式嗎?我可能會問不可能的事情,但我已經看到類似於升壓狀態機的東西,所以我希望能夠接近。 – MattSmith

+0

是的,有辦法。不是很難(使用一個有許多字符作爲參數的模板)。爲此,我發現使用代碼生成器可能更簡單,因爲您已經擁有XML,只需要一個xsl或其他類型的轉換器來生成代碼。 – Peter

回答

1

這是絕對有可能的(見下文),但我不確定它是否值得麻煩...難道你不能使用更簡單的方法,也許是繼承和多態嗎?

儘管如此,這裏是用Boost.MPL一個有效的解決方案:

// MapperRow holds the necessary parsing informations: type of the member, 
// type of the object, pointer to the appropriate member, parsing function 
template<typename Type, typename Clazz, Type Clazz::*Member, 
     void (*Parser)(const std::string &, Type &)> 
struct MapperRow 
{ 
    typedef Type type; 
    typedef Clazz clazz; 
    typedef Type Clazz::*memberType; 
    static const memberType member; 
    typedef void (*parserType)(const std::string &, Type &); 
    static const parserType parser; 
}; 

template <typename Type, typename Clazz, Type Clazz::*Member, 
      void (*Parser)(const std::string &, Type &)> 
const typename MapperRow<Type, Clazz, Member, Parser>::memberType 
    MapperRow<Type, Clazz, Member, Parser>::member = Member; 

template <typename Type, typename Clazz, Type Clazz::*Member, 
      void (*Parser)(const std::string &, Type &)> 
const typename MapperRow<Type, Clazz, Member, Parser>::parserType 
    MapperRow<Type, Clazz, Member, Parser>::parser = Parser; 


// fill iterates over a map key->MapperRow, trying to find the given key. 
// if found, it calls the parsing function, otherwise it asserts false (should 
// probably throw an exception instead) 
template <typename Clazz, typename First, typename Last> 
struct fill_impl 
{ 
    static void apply(Clazz &obj, const std::string &key, 
         const std::string &value) 
    { 
     typedef typename mpl::deref<First>::type entry; 
     static const char *curKey = 
      mpl::c_str< typename 
       mpl::first<entry>::type 
      >::value; 

     if (key == curKey) 
     { 
      typedef typename mpl::second<entry>::type Row; 

      Row::parser(value, obj.*Row::member); 
     } 
     else 
     { 
      fill_impl< 
       Clazz, typename 
       mpl::next<First>::type, 
       Last 
      >::apply(obj, key, value); 
     } 
    } 
}; 

template <typename Clazz, typename Last> 
struct fill_impl<Clazz, Last, Last> 
{ 
    static void apply(Clazz &obj, const std::string &key, 
         const std::string &value) 
    { 
     assert(false && "key not found"); 
    } 
}; 

template <typename Map, typename Clazz> 
void fill(Clazz &obj, const std::string &key, const std::string &value) 
{ 
    fill_impl< 
     Clazz, typename 
     mpl::begin<Map>::type, typename 
     mpl::end<Map>::type 
    >::apply(obj, key, value); 
} 

使用示例:

template <typename T> 
void get_value_units(const std::string &str, T &value) 
{ 
    value = T::from_value(boost::lexical_cast<typename T::value_type>(str)); 
} 

template <typename T> 
void get_value_simple(const std::string &str, T &value) 
{ 
    value = boost::lexical_cast<T>(str); 
} 

typedef boost::units::quantity<boost::units::si::energy> Energy; 
typedef boost::units::quantity<boost::units::si::length> Length; 

struct DeathStar 
{ 
    Length radius; 
    Energy energy; 
    bool operational; 
    int tie_fighters; 
}; 

// Could be clearer with MPLLIBS_STRING* 
typedef mpl::map< 
    mpl::pair< 
     mpl::string<'deat','h_st','ar_r','adiu','s'>, 
     MapperRow< Length , DeathStar, &DeathStar::radius, 
        &get_value_units<Length> > >, 
    mpl::pair< 
     mpl::string<'deat','h_st','ar_e','nerg','y'>, 
     MapperRow< Energy, DeathStar, &DeathStar::energy, 
        &get_value_units<Energy> > >, 
    mpl::pair< 
     mpl::string<'oper','atio','nal'>, 
     MapperRow< bool, DeathStar, &DeathStar::operational, 
        &get_value_simple<bool> > >, 
    mpl::pair< 
     mpl::string<'num_','tie_','figh','ters'>, 
     MapperRow< int, DeathStar, &DeathStar::tie_fighters, 
        &get_value_simple<int> > > 
> death_star_map; 

int main() 
{ 
    DeathStar ds; 
    fill<death_star_map>(ds, "death_star_radius", "12"); 
    fill<death_star_map>(ds, "death_star_energy", "34"); 
    fill<death_star_map>(ds, "operational", "1"); 
    fill<death_star_map>(ds, "num_tie_fighters", "56"); 

    std::cout << "Radius: " << ds.radius << '\n'; 
    std::cout << "Energy: " << ds.energy << '\n'; 
    std::cout << "Operational: " << std::boolalpha << ds.operational << '\n'; 
    std::cout << "Tie fighters: " << ds.tie_fighters << '\n'; 
} 

* MPLLIBS_STRING

+0

感謝您的回答。不,這不值得麻煩,但對我來說,這似乎應該是可能的,並會幫助我理解mpl。我會調解你的答案,看看我是否能夠啓迪! – MattSmith

相關問題