Guest User

Untitled

a guest
Jan 22nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #ifndef CXXDB_HPP_1350390166
  2. #define CXXDB_HPP_1350390166
  3.  
  4. #include <boost/typeof/typeof.hpp>
  5. #include <map>
  6. #include <boost/mpl/find.hpp>
  7. #include <boost/shared_ptr.hpp>
  8. #include <boost/fusion/algorithm.hpp>
  9. #include <boost/fusion/tuple.hpp>
  10.  
  11. namespace cxxdb
  12. {
  13.   namespace cxxdb_details
  14.   {
  15.     template <typename Column, typename Tuple_ptr>
  16.     struct db_column
  17.     {
  18.       typedef std::multimap<typename Column::type, Tuple_ptr> map_type;
  19.       map_type map;
  20.  
  21.       typedef typename map_type::const_iterator const_iterator;
  22.  
  23.       map_type& get_map()
  24.       {
  25.         return map;
  26.       }
  27.     };
  28.  
  29.     template <size_t Num, typename Output, typename Input>
  30.     struct convert_tuple_helper
  31.     {
  32.       static void copy(Output& out, Input const& in)
  33.       {
  34.         boost::fusion::at_c<Num - 1>(out).value = boost::fusion::at_c<Num - 1>(in);
  35.         convert_tuple_helper<Num - 1, Output, Input>::copy(out, in);
  36.       }
  37.     };
  38.  
  39.     template <typename Output, typename Input>
  40.     struct convert_tuple_helper<0, Output, Input>
  41.     {
  42.       static void copy(Output& out, Input const& in)
  43.       {
  44.       }
  45.     };
  46.  
  47.     template <typename Output, typename Input>
  48.     Output convert_tuple(Input const& in)
  49.     {
  50.       Output ret;
  51.       convert_tuple_helper<boost::fusion::tuple_size<Output>::value, Output, Input>::copy(ret, in);
  52.       return ret;
  53.     }
  54.  
  55.  
  56.     template <typename Tuple, size_t num = boost::fusion::tuple_size<Tuple>::value>
  57.     struct inherit_tuple_elements
  58.       : boost::fusion::tuple_element<num, Tuple>::type
  59.       , inherit_tuple_elements<Tuple, num - 1>
  60.     {
  61.     };
  62.  
  63.     template <typename Tuple>
  64.     struct inherit_tuple_elements<Tuple, 0>
  65.       : boost::fusion::tuple_element<0, Tuple>::type
  66.     {
  67.     };
  68.  
  69.     template <typename Tuple, typename InternalTuple, typename T>
  70.     struct get_db_column
  71.     {
  72.       typedef db_column<T, boost::shared_ptr<InternalTuple> > type;
  73.     };
  74.  
  75.     template <typename Tuple>
  76.     struct db
  77.     {
  78.       template <typename T>
  79.       struct get_internal_type
  80.       {
  81.         typedef typename T::type type;
  82.       };
  83.       typedef typename boost::mpl::transform<Tuple, get_internal_type<boost::mpl::_> >::type data_t;
  84.  
  85.       struct columns_type
  86.         : inherit_tuple_elements<
  87.         typename boost::mpl::transform<Tuple, get_db_column<Tuple, data_t, boost::mpl::_> >::type
  88.         >
  89.       {
  90.       } columns;
  91.  
  92.  
  93.     typedef boost::shared_ptr<data_t> tuple_ptr;
  94.  
  95.     struct inserter
  96.     {
  97.       inserter(db& d, tuple_ptr p): m_db(d), m_ptr(p) {}
  98.  
  99.       template<typename T>
  100.       void operator()(T& t) const
  101.       {
  102.         typedef db_column<T, tuple_ptr> db_column_t;
  103.  
  104.         m_db.columns.db_column_t::get_map().insert(std::make_pair(t.value, m_ptr));
  105.       }
  106.  
  107.       db& m_db;
  108.       tuple_ptr& m_ptr;
  109.     };
  110.  
  111.     void insert(data_t line)
  112.     {
  113.       tuple_ptr ptr(new data_t(line));
  114.  
  115.       Tuple t = convert_tuple<Tuple>(line);
  116.  
  117.       boost::fusion::for_each(t, inserter(*this, ptr));
  118.     }
  119.  
  120.     template <typename T>
  121.     std::pair<
  122.       typename db_column<T, tuple_ptr>::const_iterator,
  123.       typename db_column<T, tuple_ptr>::const_iterator
  124.       > select(typename T::type const& value)
  125.       {
  126.         return columns.db_column<T, tuple_ptr>::get_map().equal_range(value);
  127.       }
  128.  
  129.       template <typename Column, typename Iter>
  130.       static typename Column::type get_field(Iter& iter)
  131.       {
  132.         BOOST_AUTO(line, *(iter->second));
  133.         Tuple t = convert_tuple<Tuple>(line);
  134.         return (*boost::fusion::find<Column>(t)).value;
  135.       }
  136.  
  137.       template <typename Iter>
  138.       static data_t const& get(Iter& iter)
  139.       {
  140.         return *(iter->second);
  141.       }
  142.     };
  143.  
  144.     template <typename T>
  145.     struct field
  146.     {
  147.       typedef T type;
  148.       type value;
  149.     };
  150.  
  151.   }
  152.  
  153.   using cxxdb_details::field;
  154.   using cxxdb_details::db;
  155.  
  156.   using boost::fusion::tuple;
  157. }
  158.  
  159. #endif // CXXDB_HPP_1350390166
Add Comment
Please, Sign In to add comment