1 Mayıs 2017 Pazartesi

fusion

Giriş
Açıklaması şöyle
Fusion is a library and a framework similar to both STL and the boost MPL. The structure is modeled after MPL, which is modeled after STL. It is named "fusion" because the library is reminiscent of the "fusion" of compile time meta-programming with runtime programming. The library inherently has some interesting flavors and characteristics of both MPL and STL. It lives in the twilight zone between compile time meta-programming and run time programming. STL containers work on values. MPL containers work on types. Fusion containers work on both types and values.
map Sınıfı
Şu satırı dahil ederiz.
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/map.hpp>
Elimizde bir map olsun
namespace fields {
  struct rgb;
  struct gray;
  struct keypoints;
  struct edges;
  struct objects;
}

typedef fusion::map<
  fusion::pair<fields::rgb,       string>,
  fusion::pair<fields::gray,      int>,
  fusion::pair<fields::keypoints, int>,
  fusion::pair<fields::edges,     int>,
  fusion::pair<fields::objects,   double>
> Fields;
Constructor
Şöyle yaparız
Fields A_map;
Free Style Metodlar
at_key metodu
Şöyle yaparız.
template<typename field>
void Add(typename fusion::result_of::value_at_key<Fields, field>::type data) {
  fusion::at_key<field>(A_map) = data;
}
fold metodu
Şöyle yaparız.
#include <vector>
#include <boost/fusion/container/map.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
namespace fusion = boost::fusion;

struct A;
struct B;
struct C;
std::vector<int> va = { 4, 5, 6 };
std::vector<char> vb = { 'a', 'b', 'c' };
std::vector<double> vc = { 10., 11., 12. };    

template< typename FusionIteratorT, typename Ret>
struct initialize_map_with_fusion_vector {

  template<typename T>
  Ret operator()(FusionIteratorT& i,  T& val) {
    val = fusion::deref(i);
    return fusion::next(i);
  } 
};

void use_fold_with_fusion_vector() {
  using M = 
  fusion::map<fusion::pair<A, std::vector<int>>, 
    fusion::pair<B, std::vector<char>>, fusion::pair<C, std::vector<double>>>;
  using V = 
  fusion::vector<std::vector<int>, std::vector<char>, std::vector<double>>;
  auto fm = M();
  auto fv = V(va, vb, vc);
  using FusionIteratorT = decltype(fusion::begin(fv));
  using Ret = fusion::result_of::next<fusion::result_of::begin<decltype(fv)>>;
  fusion::fold(fm, fusion::begin(fv),
    initialize_map_with_fusion_vector<FusionIteratorT, Ret > ());
}

Hiç yorum yok:

Yorum Gönder