Find the index of an element in a fusion map - c++

I am struggling with a small piece of functionality I'm looking for.
I have a class which contains a fusion::map. I would like to use a variadic constructor to initialise the elements in that map.
I expect the easiest way to do this is to construct a fusion::vector from the constructor arguments, and then call for_each on the map, and setting each pair's value to its corresponding element in the vector.
However, in order to do this I need to calculate the index of the pair, based on its key type. (pair::first_type)
Can anyone help me?
Please see example code below:
#include <iostream>
#include <boost/fusion/container.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/mpl.hpp>
#include <boost/fusion/include/has_key.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/mpl/transform.hpp>
namespace fusion = boost::fusion;
namespace mpl = boost::mpl;
// given a field, returns a fusion pair of <field, field::type>
template<class field>
struct make_pair
{
typedef typename fusion::result_of::make_pair<field, typename field::type>::type type;
};
// given a sequence of fields, returns a fusion map which maps field -> field::type
template<class... fields>
struct make_map
{
typedef typename boost::fusion::vector<fields...> vector;
typedef typename mpl::transform<vector, make_pair<mpl::_1>>::type pair_sequence;
typedef typename fusion::result_of::as_map<pair_sequence>::type type;
};
// initialise each member of a map with the corresponding element in the vector
template<typename vector>
struct init
{
init(vector& v) : _v(v) {}
template <typename pair>
void operator()(pair const& data) const
{
// TODO: use pair::first_type to find the index of this pair in the map, and set
// data.second to at_c<index>(_v);
}
vector& _v;
};
struct field1 { typedef int type; };
struct field2 { typedef int type; };
struct my_map
{
template<typename... args>
my_map(args... a)
{
typedef typename boost::fusion::vector<args...> vector;
vector arg_vec(a...);
fusion::for_each(_map, init<vector>(arg_vec));
}
typedef typename make_map<field1, field2>::type map;
map _map;
};
struct print
{
template <typename pair>
void operator()(pair const& data) const
{
std::cout << data.second << " ";
}
};
int main()
{
my_map m(1, 2);
fusion::for_each(m._map, print()); // should print '1 2'
return 0;
}

Answering my own question:
fusion::copy is all I needed:
fusion::copy(arg_vec, _map);

Adding another answer to my own question, this time for the use-case where the number of arguments is less than or equal to the number of elements in the map.
A working solution is below:
#include <iostream>
#include <boost/fusion/container.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/mpl.hpp>
#include <boost/fusion/include/has_key.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/fusion/include/copy.hpp>
#include <boost/fusion/include/begin.hpp>
#include <boost/fusion/include/next.hpp>
#include <boost/fusion/include/key_of.hpp>
#include <boost/fusion/iterator/key_of.hpp>
namespace fusion = boost::fusion;
namespace mpl = boost::mpl;
// given a field, returns a fusion pair of <field, field::type>
template<class field>
struct make_pair
{
typedef typename fusion::result_of::make_pair<field, typename field::type>::type type;
};
// given a sequence of fields, returns a fusion map which maps field -> field::type
template<class... fields>
struct make_map
{
typedef typename fusion::result_of::as_map<typename mpl::transform<boost::fusion::vector<fields...>, make_pair<mpl::_1>>::type>::type type;
};
//-------------------------------------------------------------------------------------------
// iterate through a fusion map to find the index of a given key
template<typename iter, typename key_type, typename seek_type>
struct key_index
{
typedef typename fusion::result_of::next<iter>::type next_iter;
typedef typename fusion::result_of::key_of<next_iter>::type next_key;
enum { value = 1 + key_index<next_iter, next_key, seek_type>::value };
};
template<typename iter, typename seek_type>
struct key_index<iter, seek_type, seek_type>
{
enum { value = 0 };
};
//-------------------------------------------------------------------------------------------
// copy an element from a vector to a map, if the index in the vector exists
template<typename map, typename vector, int index, bool in_vec>
struct do_copy
{
template<typename T>
void operator()(const vector& v, const T& dest)
{
const_cast<T&>(dest) = fusion::at_c<index>(v);
}
};
template<typename map, typename vector, int index>
struct do_copy<map, vector, index, false>
{
template<typename T>
void operator()(const vector&, const T&)
{ }
};
//-------------------------------------------------------------------------------------------
// initialise a map with the corresponding elements in a vector, vector may be smaller than the map
template<typename vector, typename map>
struct init
{
init(const vector& v) : _v(v) {}
template <typename pair> void operator()(const pair& data) const
{
typedef typename fusion::result_of::begin<map>::type begin_iter;
typedef typename fusion::result_of::key_of<begin_iter>::type key_type;
enum { index = key_index<begin_iter, key_type, typename pair::first_type>::value };
enum { in_vec = fusion::result_of::size<vector>::type::value > index };
do_copy<map, vector, index, in_vec>()(_v, data.second);
}
private:
const vector& _v;
};
//-------------------------------------------------------------------------------------------
struct field1 { typedef std::string type; };
struct field2 { typedef int type; };
struct field3 { typedef double type; };
struct field4 { typedef std::string type; };
struct field5 { typedef int type; };
struct field6 { typedef double type; };
struct my_map
{
template<typename... args>
my_map(args... a)
{
typedef typename fusion::vector<args...> vector;
fusion::for_each(_map, init<vector, map>(vector(a...)));
}
typedef typename make_map<field1, field2, field3, field4, field5, field6>::type map;
map _map;
};
struct print
{
template <typename pair>
void operator()(pair const& data) const
{
std::cout << data.second << " ";
}
};
//-------------------------------------------------------------------------------------------
int main()
{
my_map m("hello world", 5, 2.4);
fusion::for_each(m._map, print());
std::cout << std::endl;
return 0;
}

Related

Hashing a template type

Here, I tried to make a map that its vertex can be user-defined class. But when I try to add template type element to the unordered_set it gives error. The code is:
#include <iostream>
#include <unordered_set>
#include <string>
#include <vector>
#include <functional>
template<class T> class Edge;
template<class T> class Vertex{ // Made it a class just for its constructor.
public:
template<class A> Vertex(A vert){
A vertex = vert;
std::unordered_set<Edge<A>> adjlist;
}
};
template<class T> class Edge{ // Made it a class just for its constructor.
public:
template<class A> Edge(Vertex<A> vert1, Vertex<A> vert2, int w){
Vertex<A> *origin = &vert1;
Vertex<A> *target = &vert2;
}
};
template<class T>
class WUG{
private:
std::unordered_set<Vertex<T>> vertices;
std::unordered_set<Edge<T>> edges;
int num_of_edges;
int num_of_vertices;
public:
WUG() {
num_of_edges = 0;
num_of_vertices = 0;
}
void addVertex(T newVert) {
Vertex<T> temp = Vertex<T>(newVert);
vertices.emplace(temp); //Problem is here
}
int main(int argc, char** argv) {
WUG<char> g1 = WUG<char>();
g1.addVertex('A');
g1.addVertex('B');
g1.addVertex('C');
return 0;
}
Error: it opens hashtable_policy.h and gives error at
template <typename _Key, typename _Hash>
struct __is_noexcept_hash : std::integral_constant<bool,
noexcept(declval<const _Hash&>()(declval<const _Key&>()))> //Here
{ };
[Error] no match for call to '(const std::hash<Vertex<char> >) (const Vertex<char>&)'
How do you emplace a template type object to unordered_set? How about pair of 2 template?
I believe you need to provide special hashing and comparison functions to make the hash set (or in my example's case, hash map) work. Here is a minimal example. Tested with C++11.
#include <unordered_map>
#include <iostream>
#include <algorithm>
template<typename T>
struct foo {
typedef T value_type;
foo(T x) : x(x) {}
T x;
};
template<typename T>
struct foo_hasher {
int operator()(const T &val) const {
return std::hash<typename T::value_type>()(val.x);
}
};
template<typename T>
struct foo_equality {
bool operator()(const T &left, const T& right) const {
return left.x == right.x;
}
};
int main() {
typedef std::unordered_map<foo<int>, int, foo_hasher<foo<int>>, foo_equality<foo<int>>> Map;
Map mp;
foo<int> x(5);
mp[x] = 10;
mp[foo<int>(10)] = 22;
std::for_each(mp.begin(), mp.end(), [](const Map::value_type &val) {
std::cout << val.first.x << ", " << val.second << "\n";
});
}
Note that both my hashing and equality function are not at all restrictive - they are wrt T rather than foo, but the principal should be the same.

Create a nested map in C++ [duplicate]

If I have definitions like:
typedef map<string, Foo> Foo_map_1
typedef map<string, Foo_map_1> Foo_map_2
typedef map<string, Foo_map_2> Foo_map_3
typedef map<string, Foo_map_3> Foo_map_4
typedef map<string, Foo_map_4> Foo_map_5
Is there anyway I can generalize that so I could do, for example,
Foo_map<10>
and have a 10-fold nested map. I don't need something like boost::recursive_wrapper because the number of levels is always constant.
This is seems easy enough even for the limited C++ metaprogramming power:
#include <map>
#include <string>
template<int N, typename K, typename V>
struct NMap { typedef std::map<K, typename NMap<N-1, K, V>::type> type; };
template<typename K, typename V>
struct NMap<1, K, V> { typedef std::map<K, V> type; };
int main(int argc, const char *argv[]) {
NMap<3, int, std::string>::type m;
m[1][2][3] = "Test";
return 0;
}
This works for me.
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Foo
{
Foo() : _in(0) {}
Foo(int in) : _in(in) {}
int _in;
};
template <int N> struct Foo_map
{
map<string, Foo_map<N-1> > foo_Map;
Foo_map<N-1>& operator[](string const& key) { return foo_Map[key]; }
};
template <> struct Foo_map<1>
{
map<string, Foo> foo_Map;
Foo& operator[](string const& key) { return foo_Map[key]; }
};
int main()
{
Foo_map<1> map1;
map1["abcd"] = Foo(10);
Foo_map<2> map2;
map2["a"]["b"] = Foo(20);
Foo_map<10> map10;
map10["a"]["b"]["c"]["d"]["e"]["f"]["g"]["h"]["i"]["j"] = Foo(100);
std::cout << map1["abcd"]._in << std::endl;
std::cout << map2["a"]["b"]._in << std::endl;
std::cout << map10["a"]["b"]["c"]["d"]["e"]["f"]["g"]["h"]["i"]["j"]._in << std::endl;
}
The output of running the program:
10
20
100

c ++ Boost Loop through dimensions of model::point

I was wondering if there was a way to loop through the dimensions of a boost point model. I am trying to create a function to do calculations on two custom points, with a definable number of dimensions. In other words the number of dimensions of each point will match, however they will not be a constant value. I want to do the same operations on each dimension, so I need to do a loop in order to achieve this.
An example of what I want I want to do would be:
for(std::size_t dim = 0; dim < D; dim++){
CoordinateType d = get<dim>();
//do stuff to d
set<dim>(d);
}
I know this would not work because d is not a compile-time constant.
Thanks!
As an alternative approach I thought you should be able to adapt the Boost Geometry point model as a Fusion sequence.
Live On Coliru
#include <iostream>
namespace bg = boost::geometry;
namespace fus = boost::fusion;
int main() {
bg::model::point<double, 7, bg::cs::cartesian> p1;
// set some nice values
p1.set<0>(7); p1.set<1>(14); p1.set<2>(21); p1.set<3>(28);
p1.set<4>(35); p1.set<5>(42); p1.set<6>(49);
fus::for_each(fus::as_vector(p1), [](double x) { std::cout << x << ' '; });
}
Prints
7 14 21 28 35 42 49
This is pretty versatile (and will give you many more algorithms than just for_each). In the sample I've not gone all the way so you can actually say for_each(p1, f) instead of for_each(as_vector(p1), f) but you know... the proverbial exercise for the reader.
There's a bit of extension "glue" code involved here. I simply followed the documentation here
See the full listing here:
Live On Coliru
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/as_vector.hpp>
namespace bg_to_fusion {
using namespace boost;
struct bg_point_tag;
struct example_struct_iterator_tag;
template<typename Point, int Pos>
struct point_iterator
: fusion::iterator_base<point_iterator<Point, Pos> >
{
BOOST_STATIC_ASSERT(Pos >=0 && Pos <geometry::traits::dimension<typename remove_cv<Point>::type>::value);
typedef Point point_type;
typedef mpl::int_<Pos> index;
//typedef fusion::random_access_traversal_tag category;
typedef fusion::forward_traversal_tag category;
point_iterator(Point& p) : point_(p) {}
Point& point_;
};
}
namespace boost { namespace fusion {
// tag dispatch
namespace traits {
template <typename T, size_t dims, typename cs>
struct tag_of<geometry::model::point<T, dims, cs> > {
typedef bg_to_fusion::bg_point_tag type;
};
template <typename Point, int Pos>
struct tag_of<bg_to_fusion::point_iterator<Point, Pos> > {
typedef bg_to_fusion::example_struct_iterator_tag type;
};
}
namespace extension {
//////////////////////////////////////////////////////
// Point extension implementations
template<>
struct is_sequence_impl<bg_to_fusion::bg_point_tag>
{
template<typename T>
struct apply : mpl::true_ {};
};
template <>
struct size_impl<bg_to_fusion::bg_point_tag> {
template <typename Point>
struct apply : mpl::integral_c<size_t, geometry::traits::dimension<typename remove_cv<Point>::type>::value> { };
};
// begin
template<>
struct begin_impl<bg_to_fusion::bg_point_tag>
{
template<typename Point>
struct apply
{
typedef typename bg_to_fusion::point_iterator<Point, 0> type;
static type
call(Point& p)
{
return type(p);
}
};
};
// end
template<>
struct end_impl<bg_to_fusion::bg_point_tag>
{
template<typename Point> struct apply {
typedef typename bg_to_fusion::point_iterator<Point, geometry::traits::dimension<Point>::value> type;
static type call(Point& p) {
return type(p);
}
};
};
////////////////////////
// Iterator extension implementations
// value_of
template <>
struct value_of_impl<bg_to_fusion::example_struct_iterator_tag> {
template<typename Iterator> struct apply;
template<typename Point, int Pos>
struct apply<bg_to_fusion::point_iterator<Point, Pos> > {
typedef typename geometry::traits::coordinate_type<typename remove_cv<Point>::type>::type type;
};
};
// deref
template<>
struct deref_impl<bg_to_fusion::example_struct_iterator_tag>
{
template<typename Iterator>
struct apply;
template<typename Point, int Pos>
struct apply<bg_to_fusion::point_iterator<Point, Pos> >
{
typedef typename geometry::traits::coordinate_type<typename remove_cv<Point>::type>::type coordinate_type;
//typedef typename mpl::if_<is_const<Point>, coordinate_type const&, coordinate_type&>::type type;
typedef coordinate_type type;
static type
call(bg_to_fusion::point_iterator<Point, Pos> const& it) {
return it.point_.template get<Pos>();
}
};
};
// next
template<>
struct next_impl<bg_to_fusion::example_struct_iterator_tag> {
template<typename Iterator> struct apply
{
typedef typename Iterator::point_type point_type;
typedef typename Iterator::index index;
typedef typename bg_to_fusion::point_iterator<point_type, index::value + 1> type;
static type
call(Iterator const& i) {
return type(i.point_);
}
};
};
}
} }
I am not very familiar with boost geometry but it seems you have to iterate through the coordinates at compile time. The way to do this is by creating a recursion. The class apply below will perform such recursion and call a functor on each coordinate of the point.
All you have to do is to write your own functor and overload its operator() in case you want to specialise behaviour for certain coordinates. The functor in the example below simply prints the coordinates and is specialised when reading the 3rd coordinate.
#include <boost/geometry.hpp>
#include <iostream>
#include <type_traits>
namespace bg = boost::geometry;
template <int I>
using int_ = std::integral_constant<int, I>;
//recursive call that iterates the point and calls F on its coordinate
template <class Point, class F, std::size_t I = 0>
struct apply {
static void call(Point& point, F& f) {
f(point, int_<I>());
apply<Point, F, I+1>::call(point, f);
}
};
//specialisation to end the recursion
template <class CT, std::size_t DIM, class S, template <class, std::size_t, class> class Point, class F>
struct apply<Point<CT, DIM, S>, F, DIM> {
static void call(Point<CT, DIM, S>& point, F& f){}
};
//interface for calling the function
template <class Point, class F>
void apply_functor(Point& point, F& f) {
apply<Point, F>::call(point, f);
}
//example functor
template <class Point>
struct functor {
template <class Index>
void operator()(Point& point, Index I) {
std::cout << "I am coordinate " << Index::value << " and my value is " << bg::get<Index::value>(point) << std::endl;
}
// used for overloading when reading the 3rd coordinate
void operator()(Point& point, int_<2>) {
std::cout << "I am coordinate " << 2 << " and I am specialised with value " << bg::get<2>(point) << std::endl;
}
};
//3-dimensional point type
using point_type = bg::model::point<double, 3, bg::cs::cartesian>;
int main(int argc, char** argv) {
point_type point(1,2,3);
functor<point_type> f;
apply_functor(point, f);
return 0;
}

Boost fusion sequence type and name identification for structs and class

I am trying to use boost fusion for one of my projects and I an figuring out how to get type names and variable names for structures and classes.
#include <typeinfo>
#include <string>
#include <iostream>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/adapt_adt.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost::fusion;
struct Foo
{
int integer_value;
bool boolean_value;
};
class Bar
{
int integer_value;
bool boolean_value;
public:
Bar(int i_val, bool b_val):integer_value(i_val),boolean_value(b_val) {}
int get_integer_value() const { return integer_value; }
void set_integer_value(int i_val) { integer_value = i_val; }
bool get_boolean_value() const { return boolean_value; }
void set_boolean_value(bool b_val) { boolean_value = b_val; }
};
BOOST_FUSION_ADAPT_STRUCT(
Foo,
(int, integer_value)
(bool, boolean_value)
)
BOOST_FUSION_ADAPT_ADT(
Bar,
(int, int, obj.get_integer_value() , obj.set_integer_value(val))
(bool, bool, obj.get_boolean_value(), obj.set_boolean_value(val))
)
struct DisplayMembers
{
template <typename T>
void operator()(T& t) const {
std::cout << typeid(t).name() << " : " << boost::lexical_cast<std::string>(t) << std::endl;
}
};
int main(int argc, char *argv[])
{
struct Foo f = { 33, false};
for_each(f, DisplayMembers());
Bar b(34,true);
for_each(b, DisplayMembers());
return 0;
}
In the above example the result is
int : 33
bool : 0
struct boost::fusion::extension::adt_attribute_proxy<class Bar,0,0> : 34
struct boost::fusion::extension::adt_attribute_proxy<class Bar,1,0> : 1
I want the result as
int : integer_value : 33
bool : boolean_value : 0
int : integer_value : 34
bool : boolean_value : 1
I distilled the answer by sehe into something much simpler, provided you are using C++14
#include <iostream>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/mpl/range_c.hpp>
struct MyStruct {
std::string foo;
double bar;
};
BOOST_FUSION_ADAPT_STRUCT(MyStruct,
foo,
bar)
namespace fuz = boost::fusion;
namespace mpl = boost::mpl;
int main(int argc, char* argv[]) {
MyStruct dummy{"yo",3.14};
fuz::for_each(mpl::range_c<
unsigned, 0, fuz::result_of::size<MyStruct>::value>(),
[&](auto index){
std::cout << "Name: "
<< fuz::extension::struct_member_name<MyStruct,index>::call()
<< " Value: "
<< fuz::at_c<index>(dummy) << std::endl;
});
}
Outputs:
Name: foo Value: yo
Name: bar Value: 3.14
See it live on coliru
There's boost::fusion::extension::struct_member_name<S, N::value> to access the names.
Here's a generic fusion object visitor that I use:
namespace visitor {
template <typename Flavour, typename T> struct VisitorApplication;
namespace detail
{
template <typename V, typename Enable = void>
struct is_vector : boost::mpl::false_ { };
template <typename T>
struct is_vector<std::vector<T>, void> : boost::mpl::true_ { };
namespace iteration
{
// Iteration over a sequence
template <typename FusionVisitorConcept, typename S, typename N>
struct members_impl
{
// Type of the current member
typedef typename boost::fusion::result_of::value_at<S, N>::type current_t;
typedef typename boost::mpl::next<N>::type next_t;
typedef boost::fusion::extension::struct_member_name<S, N::value> name_t;
static inline void handle(FusionVisitorConcept& visitor, const S& s)
{
visitor.start_member(name_t::call());
VisitorApplication<FusionVisitorConcept, current_t>::handle(visitor, boost::fusion::at<N>(s));
visitor.finish_member(name_t::call());
members_impl<FusionVisitorConcept, S, next_t>::handle(visitor, s);
}
};
// End condition of sequence iteration
template <typename FusionVisitorConcept, typename S>
struct members_impl<FusionVisitorConcept, S, typename boost::fusion::result_of::size<S>::type>
{
static inline void handle(FusionVisitorConcept const&, const S&) { /*Nothing to do*/ }
};
// Iterate over struct/sequence. Base template
template <typename FusionVisitorConcept, typename S>
struct Struct : members_impl<FusionVisitorConcept, S, boost::mpl::int_<0>> {};
} // iteration
template <typename FusionVisitorConcept, typename T>
struct array_application
{
typedef array_application<FusionVisitorConcept, T> type;
typedef typename T::value_type value_type;
static inline void handle(FusionVisitorConcept& visitor, const T& t)
{
visitor.empty_array();
for (auto& el : t)
VisitorApplication<FusionVisitorConcept, value_type>::handle(visitor, el);
}
};
template <typename FusionVisitorConcept, typename T>
struct struct_application
{
typedef struct_application<FusionVisitorConcept, T> type;
static inline void handle(FusionVisitorConcept& visitor, const T& t)
{
visitor.empty_object();
iteration::Struct<FusionVisitorConcept, T>::handle(visitor, t);
}
};
template <typename FusionVisitorConcept, typename T, typename Enable = void>
struct value_application
{
typedef value_application<FusionVisitorConcept, T> type;
static inline void handle(FusionVisitorConcept& visitor, const T& t) {
visitor.value(t);
}
};
template <typename FusionVisitorConcept, typename T>
struct value_application<FusionVisitorConcept, boost::optional<T> >
{
typedef value_application<FusionVisitorConcept, boost::optional<T> > type;
static inline void handle(FusionVisitorConcept& visitor, const boost::optional<T>& t) {
if (t)
VisitorApplication<FusionVisitorConcept, T>::handle(visitor, *t);
else
; // perhaps some default action?
}
};
template <typename FusionVisitorConcept, typename T>
struct select_application
{
typedef
//typename boost::mpl::eval_if<boost::is_array<T>, boost::mpl::identity<array_application<FusionVisitorConcept, T>>,
typename boost::mpl::eval_if<detail::is_vector<T>, boost::mpl::identity<array_application <FusionVisitorConcept, T>>,
typename boost::mpl::eval_if<boost::fusion::traits::is_sequence<T>, boost::mpl::identity<struct_application<FusionVisitorConcept, T>>,
boost::mpl::identity<value_application<FusionVisitorConcept, T>>
> >::type type;
};
} // detail
template <typename FusionVisitorConcept, typename T>
struct VisitorApplication : public detail::select_application<FusionVisitorConcept, T>::type
{
};
}
template <typename FusionVisitorConcept, typename T>
void apply_fusion_visitor(FusionVisitorConcept& visitor, T const& o)
{
visitor::VisitorApplication<FusionVisitorConcept, T>::handle(visitor, o);
}
You can use it by supplying a visitor, e.g. for xml-like output:
struct DisplayMemberVisitor {
typedef std::string result_type;
DisplayMemberVisitor() { ss << std::boolalpha; }
std::string complete() { return ss.str(); }
void start_member (const char* name) {
ss << "<" << name << ">";
}
void finish_member(const char* name) {
ss << "</" << name << ">";
}
template <typename T> void value(T const& value) {
ss << value;
}
void empty_object() { }
void empty_array() { }
private:
std::stringstream ss;
};
See it Live On Coliru where (including some debug output) it prints:
<integer_value>33</integer_value><boolean_value>false</boolean_value><integer_value>34</integer_value><boolean_value>true</boolean_value>
Note that the ADT adaptation macro doesn't include a name (because none is available). You can probably quite easily make a macro FUSION_ADAPT_KEYD_ADT that also accepts a name and generates the relevant specializations of boost::fusion::extension::struct_member_name.
BONUS MATERIAL
Adding member name traits to ADT adapted members
Here's a simplistic approach that shows what little amount of work needs to be done.
#define MY_ADT_MEMBER_NAME(CLASSNAME, IDX, MEMBERNAME) \
namespace boost { namespace fusion { namespace extension { \
template <> struct struct_member_name<CLASSNAME, IDX> { typedef char const *type; static type call() { return #MEMBERNAME; } \
}; } } }
MY_ADT_MEMBER_NAME(Bar, 0, integer_value)
MY_ADT_MEMBER_NAME(Bar, 1, boolean_value)
This defines a macro to avoid most of the repetition. If you are a BOOST_PP whizkid you could somehow weave this into an adt_ex.hpp¹ header of sorts, so you could instead say:
BOOST_FUSION_ADAPT_ADT(Bar, // NOTE THIS PSEUDO-CODE
(integer_value, int, int, obj.get_integer_value(), obj.set_integer_value(val))
(boolean_value, bool, bool, obj.get_boolean_value(), obj.set_boolean_value(val)))
For now here's the ADT adapted trick Live On Coliru
¹ in case you're interested, here's a tarball of a prepared adt_ex tree (drop in alongsize adt.hpp): adt_ex.tgz as a starting point. It's just adt* but with macros and header guards renamed to adt_ex*

Hypercube with multidimensional vectors

I'm trying to implement a hypercubeclass, that is, multidimensional vectors.
I have a problem generalizing it. I'm able to make one for a three dimensional hypercube, but as mentioned, the problem is generalizing it. Could anyone help me? You should be able to write hypercube<4> w(5) to get 4 dimensions and 5 elements in each vector that is 5*5*5*5 elements in total.
Here is the code I have for the three dimensional version:
#include <vector>
using std::vector;
using namespace std;
template <int b>
class Hypercube {
public:
Hypercube(int a) : intvec(a){
for (int i = 0; i<a;i++) {
intvec[i].resize(a);
for (int j = 0;j<a;j++) {
intvec[i][j].resize(a);
}
}
}
vector<vector<int> >& operator[](int i) {
return intvec[i];
}
vector<vector<vector<int> > > intvec;
};
For this to work, you need recursive inheritence to provide the correct vector type and the initialization function. Both work recursively, for which I created a little helper struct called hcube_info:
// hypercube.h
#include <vector>
template<unsigned N>
struct hcube_info;
template<>
struct hcube_info<1>
{ // base version
typedef std::vector<int> type;
static type init(unsigned innerdim, int value = 0){
return type(innerdim, value);
}
};
template<unsigned N>
struct hcube_info
{ // recursive definition, N dimensions
private:
typedef hcube_info<N-1> base;
typedef typename base::type btype;
public:
typedef std::vector<btype> type;
static type init(unsigned innerdim, int value = 0){
return type(innerdim, base::init(innerdim, value));
}
};
As you can see, recursion all the way to the one dimensional base case. We also need to recursively initialize the vector to pass the inner dimension all the way through.
And now the real class, a nice interface around the hcube_info:
template<unsigned N>
struct hypercube
{
private:
typedef hcube_info<N> info;
typedef typename info::type vec_type;
public:
typedef typename vec_type::value_type value_type;
typedef typename vec_type::size_type size_type;
explicit hypercube(unsigned innerdim, unsigned value = 0)
: c(info::init(innerdim, value))
{
}
value_type& operator[](unsigned i){
return c[i];
}
size_type size() const{ return c.size(); }
private:
vec_type c;
};
Test program:
#include "hypercube.h"
#include <iostream>
int main(){
hypercube<4> c(5);
unsigned s = c.size() * // dim 1
c[0].size() * // dim 2
c[0][0].size() * // dim 3
c[0][0][0].size(); // dim 4
std::cout << s << '\n'; // outputs: 625 -> 5 * 5 * 5 * 5 -> 5^4
}
I would suggest something along those lines:
template <typename T, unsigned dim> class HQ {
std::vector<HQ<T,(dim-1)> > vector;
public:
HQ(unsigned size) : vector(size,HQ<T,(dim-1)>(size)) {}
};
template <typename T> class HQ<T,1> {
std::vector<T> vector;
public:
HQ(unsigned size) : vector(size,T()) {}
};
template <typename T> class HQ<T,0> {};
You can then implement your accessors for the first both templates as you wish. You can also make things a bit more simple and robust by allowing zero-dimensional matrices:
template <typename T, unsigned dim> class HQ {
std::vector<HQ<T,(dim-1)> > vector;
public:
HQ(unsigned size) : vector(size,HQ<T,(dim-1)>(size)) {}
};
template <typename T> class HQ<T,0> {
T data;
public:
HQ(unsigned size) : data() {}
};
I imagine an access operator would look something like this:
template <typename T, unsigned dim> HQ<T,(dim-1)>& HQ<T,dim>::operator[](unsigned i) {
return vector[i];
}
template <typename T, unsigned dim> HQ<T,(dim-1)> const& HQ<T,dim>::operator[](unsigned i) const {
return vector[i];
}
such that you can write
HQ<int,4> hq(5);
hq[1][4][2][0] = 77;