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

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;
}

Related

C++ Template instancing with mutual reference

Is there a way to allow two or more templates instanciations to mutually refer to each other ?
Example :
/* invalid C++ */
/* we suppose MyTemplate1 and MyTemplate2 are declared */
typedef MyTemplate1<MyInstance2> MyInstance1;
typedef MyTemplate2<MyInstance1> MyInstance2;
I suppose there is none, still asking just in case I missed something.
Adding more precision, I want to achieve such a construction :
/* invalid C++ */
#include <iostream>
template <typename typeT> struct MyStruct1 {
static void print(unsigned i) {
std::cout << "MyStruct1 : " << i << std::endl;
if (i > 0) {
typeT::print(i - 1);
}
}
};
template <typename typeT> struct MyStruct2 {
static void print(unsigned i) {
std::cout << "MyStruct2 : " << i << std::endl;
if (i > 0) {
typeT::print(i - 1);
}
}
};
/* of course this is invalid, since you can't reference MyInstance2
before it is declared */
typedef MyStruct1<MyInstance2> MyInstance1;
typedef MyStruct2<MyInstance1> MyInstance2;
int main() {
MyInstance1::print(5);
return 0;
}
output should be :
MyStruct1 : 5
MyStruct2 : 4
MyStruct1 : 3
MyStruct2 : 2
MyStruct1 : 1
MyStruct2 : 0
Please note I'm not trying to achieve a similar output, but a similar construct, where two (or more) templates instances refer to each other
with as few as possible additional code : it shall be easy to do mutual reference instantiation. However, for the implementation code of the two templates, I don't care if they are complicated.
Here's a solution that at least gives the correct output. If it's also a viable solution for your use case is not very clear though but maybe it can at least help you clarify your question a bit more.
#include <iostream>
template <template <typename> typename TemplateT> struct TemplateType {
template <typename typeT>
static void print(unsigned i) {
TemplateT<typeT>::print(i);
}
};
template <typename typeT> struct MyStruct1 {
static void print(unsigned i) {
std::cout << "MyStruct1 : " << i << std::endl;
if (i > 0) {
typeT::template print<TemplateType<MyStruct1>>(i - 1);
}
}
};
template <typename typeT> struct MyStruct2 {
static void print(unsigned i) {
std::cout << "MyStruct2 : " << i << std::endl;
if (i > 0) {
typeT::template print<TemplateType<MyStruct2>>(i - 1);
}
}
};
typedef MyStruct1<TemplateType<MyStruct2>> MyInstance1;
int main() {
MyInstance1::print(5);
return 0;
}
One way is to use class forward declaration:
template<typename T> class M
{
static int foo(int i) { return i ? T::foo(i - 1) : 0; }
};
struct A;
struct B;
struct A : M<B>{};
struct B : M<A>{};
Not same code exactly but you have recursion.
I finally found a satisfying construct, which involves using a tierce struct acting as a context to declare subs elements. It isn't forcibly the best solution for anyone, and I will probably have to adapt it a bit more to fit my very need, but here is the code :
#include <iostream>
#include <type_traits>
template <typename K, typename T> struct TypePair {
typedef K key;
typedef T type;
};
template <typename Context, typename P0, typename... PN> struct TypeMap {
template <typename K> struct get {
typedef typename std::conditional<
std::is_same<typename P0::key, K>::value,
typename P0::type::template actual<Context>,
typename TypeMap<Context, PN...>::template get<K>::type>::type type;
};
};
struct TypeNotFound {};
template <typename Context, typename P> struct TypeMap<Context, P> {
template <typename K> struct get {
typedef
typename std::conditional<std::is_same<typename P::key, K>::value,
typename P::type::template actual<Context>,
TypeNotFound>::type type;
};
};
/* defining a context to link all classes together */
template <typename... TN> struct Context {
template <typename K> struct Access {
typedef typename TypeMap<Context<TN...>, TN...>::template get<K>::type type;
};
};
/* templates we want to cross ref, note that context is passed as a parameter*/
template <typename ContextT, typename Id2> struct MyStruct1Actual {
static void print(unsigned i) {
std::cout << "MyStruct1 : " << i << std::endl;
if (i > 0) {
ContextT::template Access<Id2>::type::print(i - 1);
}
}
};
template <typename ContextT, typename Id1> struct MyStruct2Actual {
static void print(unsigned i) {
std::cout << "MyStruct2 : " << i << std::endl;
if (i > 0) {
ContextT::template Access<Id1>::type::print(i - 1);
}
}
};
/* wrappers to not have to always pass context when instancing templates */
template <typename type> struct MyStruct1 {
template <typename ContextT> using actual = MyStruct1Actual<ContextT, type>;
};
template <typename type> struct MyStruct2 {
template <typename ContextT> using actual = MyStruct2Actual<ContextT, type>;
};
/* Enum and dummy id, could simply use Enum actually, but using classes a Id
can prove to be more elegant with complex structures, expecially as it could be
used to automatically create pairs instead of having to precise Id */
enum Ids : int { Struct1, Struct2 };
template <Ids id> struct Id {};
// instancing all stuff withing context
// clang-format off
typedef Context<
TypePair< Id<Struct1>, MyStruct1< Id<Struct2> > >,
TypePair< Id<Struct2>, MyStruct2< Id<Struct1> > >
> Ctx;
// clang-format on
typedef Ctx::Access<Id<Struct1>>::type S1;
int main() {
S1::print(5);
return 0;
}
Shortening names an giving more meaning than Context or TypePair will be mandatory, but the idea is here.

(How to) Create own polygon type in boost geometry and use multi_polygon type with it?

I'm currently trying to extend boost::geometry polygons with some
additional information. However the compiler starts
#include <boost/geometry.hpp>
namespace bg = boost::geometry;
using point_t = bg::model::d2::point_xy<double>;
using polygon_t = bg::model::polygon<point_t>;
using mpolygon_t = bg::model::multi_polygon<polygon_t>;
using taggedPolygon_t = std::tuple<polygon_t, void*>;
using multiTaggedPolygon_t = bg::model::multi_polygon<taggedPolygon_t>;
void foo()
{
mpolygon_t poly; // OK
taggedPolygon_t taggedPoly; // OK
mpolygon_t mpoly; // OK
multiTaggedPolygon_t poly; // Compile error
}
Does anybody have a hint how to get that stuff right?
My intent is to store some additional information and attach it to the polygon for later usage.
I also tried to use inheritance instead of std::tuple:
struct taggedPolygon_t : bg::model::polygon<point_t>
{
void* tag;
};
namespace boost { namespace geometry { namespace traits
{
template<> struct tag<taggedPolygon_t> { typedef polygon_tag type; };
template<> struct ring_const_type<taggedPolygon_t> { typedef const taggedPolygon_t& type; };
template<> struct ring_mutable_type<taggedPolygon_t> { typedef taggedPolygon_t& type; };
template<> struct interior_const_type<taggedPolygon_t> { typedef const taggedPolygon_t type; };
template<> struct interior_mutable_type<taggedPolygon_t> { typedef taggedPolygon_t type; };
template<> struct exterior_ring<taggedPolygon_t> { typedef const taggedPolygon_t type; };
template<> struct interior_rings<taggedPolygon_t> { typedef const taggedPolygon_t type; };
} } } // namespace boost::geometry::traits
But the problem remains.
taggedPolygon_t taggedPoly; // OK
Obviously ok. It just declares a tuple object. Tuples pose no restrictions on the template arguments.
multiTaggedPolygon_t poly; // Compile error
That's not OK, because it defines a multi_polugon<> instance. That type does pose concept requirements on the template argument type: it must model the Polygon concept.
A tuple does not satisfy those requirements.
Definition
The Polygon Concept is defined as following:
there must be a specialization of traits::tag defining polygon_tag as type
there must be a specialization of traits::ring_type defining the type of its exterior ring and interior rings as type
this type defined by ring_type must fulfill the Ring Concept
there must be a specialization of traits::interior_type defining the type of the collection of its interior rings as type; this collection itself must fulfill a Boost.Range Random Access Range Concept
there must be a specialization of traits::exterior_ring with two functions named get, returning the exterior ring, one being const, the other being non const
there must be a specialization of traits::interior_rings with two functions named get, returning the interior rings, one being const, the other being non const
So let's be quick and dirty here:
Note, the docs seem to be slightly out of sync w.r.t. the mutable/const distinction.
namespace boost::geometry::traits {
template <typename Underlying, typename Tag>
struct ring_mutable_type<taggedGeometry<Underlying, Tag> > : ring_mutable_type<Underlying> {};
template <typename Underlying, typename Tag>
struct ring_const_type<taggedGeometry<Underlying, Tag> > : ring_const_type<Underlying> {};
template <typename Underlying, typename Tag>
struct interior_mutable_type<taggedGeometry<Underlying, Tag> > : interior_mutable_type<Underlying> {};
template <typename Underlying, typename Tag>
struct interior_const_type<taggedGeometry<Underlying, Tag> > : interior_const_type<Underlying> {};
template <typename Underlying, typename Tag>
struct tag<taggedGeometry<Underlying, Tag> > : tag<Underlying> {};
template <typename Underlying, typename Tag>
struct exterior_ring<taggedGeometry<Underlying, Tag> > : exterior_ring<Underlying> {};
template <typename Underlying, typename Tag>
struct interior_rings<taggedGeometry<Underlying, Tag> > : interior_rings<Underlying> {};
}
Now you can compile your declaration.
Live On Coliru
mpolygon_t mpoly; // OK
multiTaggedPolygon_t poly; // OK
static_assert(std::is_same_v<bg::ring_type<mpolygon_t>::type, bg::ring_type<multiTaggedPolygon_t>::type>, "");
Note I said "quick and dirty". Because this isn't enough.
More...
Note I silently changed from std::tuple<> to a custom struct for
convenience. If not, you'd have to delegate the using the tuple getter:
template <typename Underlying, typename Tag>
struct exterior_ring<taggedGeometry<Underlying, Tag> > : exterior_ring<Underlying> {
using G = taggedGeometry<Underlying, Tag>;
using base = exterior_ring<Underlying>;
static decltype(auto) get(G& v) { return base::get(std::get<0>(v)); }
static decltype(auto) get(G const& v) { return base::get(std::get<0>(v)); }
};
template <typename Underlying, typename Tag>
struct interior_rings<taggedGeometry<Underlying, Tag> > : interior_rings<Underlying> {
using G = taggedGeometry<Underlying, Tag>;
using base = interior_rings<Underlying>;
static decltype(auto) get(G& v) { return base::get(std::get<0>(v)); }
static decltype(auto) get(G const& v) { return base::get(std::get<0>(v)); }
};
That'd also work: Live On Coliru
Demo
Now you can actually use it:
Live On Coliru
int main() {
multiTaggedPolygon_t poly;
bg::read_wkt("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), "
"((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),"
"(30 20, 20 15, 20 25, 30 20)))", poly);
std::string reason;
if (!bg::is_valid(poly, reason)) {
std::cout << "Correcting data: " << reason << "\n";
bg::correct(poly);
}
std::cout << bg::wkt(poly) << " has an area of " << bg::area(poly) << "\n";
}
Prints:
Correcting data: Geometry has wrong orientation
MULTIPOLYGON(((40 40,45 30,20 45,40 40)),((20 35,45 20,30 5,10 10,10 30,20 35),(30 20,20 25,20 15,30 20))) has an area of 712.5
CAVEATS
Note that tagged polygons are not "supported" in mutating/producing algorithms.
For example, if you intersect two polygons, the result will be polygons freshly constructed and built with the generic methods the library defines, which means you "lose" the tag info.
Listing
For posterity Live On Coliru
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/algorithms/area.hpp>
#include <iostream>
namespace bg = boost::geometry;
using point_t = bg::model::d2::point_xy<double>;
using polygon_t = bg::model::polygon<point_t>;
using mpolygon_t = bg::model::multi_polygon<polygon_t>;
template <typename Geo, typename Tag = void*>
using taggedGeometry = std::tuple<Geo, Tag>;
/*
template <typename Geo, typename Tag = void*>
struct taggedGeometry : Geo {
using Geo::Geo;
Tag _tag_data;
};
*/
namespace boost::geometry::traits {
template <typename Underlying, typename Tag>
struct ring_mutable_type<taggedGeometry<Underlying, Tag> > : ring_mutable_type<Underlying> {};
template <typename Underlying, typename Tag>
struct ring_const_type<taggedGeometry<Underlying, Tag> > : ring_const_type<Underlying> {};
template <typename Underlying, typename Tag>
struct interior_mutable_type<taggedGeometry<Underlying, Tag> > : interior_mutable_type<Underlying> {};
template <typename Underlying, typename Tag>
struct interior_const_type<taggedGeometry<Underlying, Tag> > : interior_const_type<Underlying> {};
template <typename Underlying, typename Tag>
struct tag<taggedGeometry<Underlying, Tag> > : tag<Underlying> {};
template <typename Underlying, typename Tag>
struct exterior_ring<taggedGeometry<Underlying, Tag> > : exterior_ring<Underlying> {
using G = taggedGeometry<Underlying, Tag>;
using base = exterior_ring<Underlying>;
static decltype(auto) get(G& v) { return base::get(std::get<0>(v)); }
static decltype(auto) get(G const& v) { return base::get(std::get<0>(v)); }
};
template <typename Underlying, typename Tag>
struct interior_rings<taggedGeometry<Underlying, Tag> > : interior_rings<Underlying> {
using G = taggedGeometry<Underlying, Tag>;
using base = interior_rings<Underlying>;
static decltype(auto) get(G& v) { return base::get(std::get<0>(v)); }
static decltype(auto) get(G const& v) { return base::get(std::get<0>(v)); }
};
}
using taggedPolygon_t = taggedGeometry<polygon_t>;
using multiTaggedPolygon_t = bg::model::multi_polygon<taggedPolygon_t>;
int main() {
multiTaggedPolygon_t poly;
bg::read_wkt("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), "
"((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),"
"(30 20, 20 15, 20 25, 30 20)))", poly);
std::string reason;
if (!bg::is_valid(poly, reason)) {
std::cout << "Correcting data: " << reason << "\n";
bg::correct(poly);
}
std::cout << bg::wkt(poly) << " has an area of " << bg::area(poly) << "\n";
}
I found out how to do it via inheritance (2nd code snippet):
struct taggedPolygon_t : bg::model::polygon<point_t>
{
void* tag;
};
namespace boost { namespace geometry { namespace traits
{
template<> struct tag<taggedPolygon_t> { typedef polygon_tag type; };
template<> struct ring_const_type<taggedPolygon_t> { typedef const bg::model::polygon<point_t>::ring_type& type; };
template<> struct ring_mutable_type<taggedPolygon_t> { typedef bg::model::polygon<point_t>::ring_type& type; };
template<> struct interior_const_type<taggedPolygon_t> { typedef const bg::model::polygon<point_t>::inner_container_type& type; };
template<> struct interior_mutable_type<taggedPolygon_t> { typedef bg::model::polygon<point_t>::inner_container_type& type; };
template<> struct exterior_ring<taggedPolygon_t>
{
static bg::model::polygon<point_t>::ring_type& get(bg::model::polygon<point_t>& p) {return p.outer(); }
static bg::model::polygon<point_t>::ring_type const& get(bg::model::polygon<point_t> const& p) {return p.outer(); }
};
template<> struct interior_rings<taggedPolygon_t>
{
static bg::model::polygon<point_t>::inner_container_type& get(bg::model::polygon<point_t>& p) {return p.inners(); }
static bg::model::polygon<point_t>::inner_container_type const& get(bg::model::polygon<point_t> const& p) {return p.inners(); }
};
} } } // namespace boost::geometry::traits

Switch statement variadic template expansion

Let me please consider the following synthetic example:
inline int fun2(int x) {
return x;
}
inline int fun2(double x) {
return 0;
}
inline int fun2(float x) {
return -1;
}
int fun(const std::tuple<int,double,float>& t, std::size_t i) {
switch(i) {
case 0: return fun2(std::get<0>(t));
case 1: return fun2(std::get<1>(t));
case 2: return fun2(std::get<2>(t));
}
}
The question is how should I expand this to the general case
template<class... Args> int fun(const std::tuple<Args...>& t, std::size_t i) {
// ?
}
Guaranteeing that
fun2 can be inlined into fun
search complexity not worse than O(log(i)) (for large i).
It is known that optimizer usually uses lookup jump table or compile-time binary search tree when large enough switch expanded. So, I would like to keep this property affecting performance for large number of items.
Update #3: I remeasured performance with uniform random index value:
1 10 20 100
#TartanLlama
gcc ~0 42.9235 44.7900 46.5233
clang 10.2046 38.7656 40.4316 41.7557
#chris-beck
gcc ~0 37.564 51.3653 81.552
clang ~0 38.0361 51.6968 83.7704
naive tail recursion
gcc 3.0798 40.6061 48.6744 118.171
clang 11.5907 40.6197 42.8172 137.066
manual switch statement
gcc 41.7236
clang 7.3768
Update #2: It seems that clang is able to inline functions in #TartanLlama solution whereas gcc always generates function call.
Ok, I rewrote my answer. This gives a different approach to what TartanLlama and also what I suggested before. This meets your complexity requirement and doesn't use function pointers so everything is inlineable.
Edit: Much thanks to Yakk for pointing out a quite significant optimization (for the compile-time template recursion depth required) in comments
Basically I make a binary tree of the types / function handlers using templates, and implement the binary search manually.
It might be possible to do this more cleanly using either mpl or boost::fusion, but this implementation is self-contained anyways.
It definitely meets your requirements, that the functions are inlineable and runtime look up is O(log n) in the number of types in the tuple.
Here's the complete listing:
#include <cassert>
#include <cstdint>
#include <tuple>
#include <iostream>
using std::size_t;
// Basic typelist object
template<typename... TL>
struct TypeList{
static const int size = sizeof...(TL);
};
// Metafunction Concat: Concatenate two typelists
template<typename L, typename R>
struct Concat;
template<typename... TL, typename... TR>
struct Concat <TypeList<TL...>, TypeList<TR...>> {
typedef TypeList<TL..., TR...> type;
};
template<typename L, typename R>
using Concat_t = typename Concat<L,R>::type;
// Metafunction First: Get first type from a typelist
template<typename T>
struct First;
template<typename T, typename... TL>
struct First <TypeList<T, TL...>> {
typedef T type;
};
template<typename T>
using First_t = typename First<T>::type;
// Metafunction Split: Split a typelist at a particular index
template<int i, typename TL>
struct Split;
template<int k, typename... TL>
struct Split<k, TypeList<TL...>> {
private:
typedef Split<k/2, TypeList<TL...>> FirstSplit;
typedef Split<k-k/2, typename FirstSplit::R> SecondSplit;
public:
typedef Concat_t<typename FirstSplit::L, typename SecondSplit::L> L;
typedef typename SecondSplit::R R;
};
template<typename T, typename... TL>
struct Split<0, TypeList<T, TL...>> {
typedef TypeList<> L;
typedef TypeList<T, TL...> R;
};
template<typename T, typename... TL>
struct Split<1, TypeList<T, TL...>> {
typedef TypeList<T> L;
typedef TypeList<TL...> R;
};
template<int k>
struct Split<k, TypeList<>> {
typedef TypeList<> L;
typedef TypeList<> R;
};
// Metafunction Subdivide: Split a typelist into two roughly equal typelists
template<typename TL>
struct Subdivide : Split<TL::size / 2, TL> {};
// Metafunction MakeTree: Make a tree from a typelist
template<typename T>
struct MakeTree;
/*
template<>
struct MakeTree<TypeList<>> {
typedef TypeList<> L;
typedef TypeList<> R;
static const int size = 0;
};*/
template<typename T>
struct MakeTree<TypeList<T>> {
typedef TypeList<> L;
typedef TypeList<T> R;
static const int size = R::size;
};
template<typename T1, typename T2, typename... TL>
struct MakeTree<TypeList<T1, T2, TL...>> {
private:
typedef TypeList<T1, T2, TL...> MyList;
typedef Subdivide<MyList> MySubdivide;
public:
typedef MakeTree<typename MySubdivide::L> L;
typedef MakeTree<typename MySubdivide::R> R;
static const int size = L::size + R::size;
};
// Typehandler: What our lists will be made of
template<typename T>
struct type_handler_helper {
typedef int result_type;
typedef T input_type;
typedef result_type (*func_ptr_type)(const input_type &);
};
template<typename T, typename type_handler_helper<T>::func_ptr_type me>
struct type_handler {
typedef type_handler_helper<T> base;
typedef typename base::func_ptr_type func_ptr_type;
typedef typename base::result_type result_type;
typedef typename base::input_type input_type;
static constexpr func_ptr_type my_func = me;
static result_type apply(const input_type & t) {
return me(t);
}
};
// Binary search implementation
template <typename T, bool b = (T::L::size != 0)>
struct apply_helper;
template <typename T>
struct apply_helper<T, false> {
template<typename V>
static int apply(const V & v, size_t index) {
assert(index == 0);
return First_t<typename T::R>::apply(v);
}
};
template <typename T>
struct apply_helper<T, true> {
template<typename V>
static int apply(const V & v, size_t index) {
if( index >= T::L::size ) {
return apply_helper<typename T::R>::apply(v, index - T::L::size);
} else {
return apply_helper<typename T::L>::apply(v, index);
}
}
};
// Original functions
inline int fun2(int x) {
return x;
}
inline int fun2(double x) {
return 0;
}
inline int fun2(float x) {
return -1;
}
// Adapted functions
typedef std::tuple<int, double, float> tup;
inline int g0(const tup & t) { return fun2(std::get<0>(t)); }
inline int g1(const tup & t) { return fun2(std::get<1>(t)); }
inline int g2(const tup & t) { return fun2(std::get<2>(t)); }
// Registry
typedef TypeList<
type_handler<tup, &g0>,
type_handler<tup, &g1>,
type_handler<tup, &g2>
> registry;
typedef MakeTree<registry> jump_table;
int apply(const tup & t, size_t index) {
return apply_helper<jump_table>::apply(t, index);
}
// Demo
int main() {
{
tup t{5, 1.5, 15.5f};
std::cout << apply(t, 0) << std::endl;
std::cout << apply(t, 1) << std::endl;
std::cout << apply(t, 2) << std::endl;
}
{
tup t{10, 1.5, 15.5f};
std::cout << apply(t, 0) << std::endl;
std::cout << apply(t, 1) << std::endl;
std::cout << apply(t, 2) << std::endl;
}
{
tup t{15, 1.5, 15.5f};
std::cout << apply(t, 0) << std::endl;
std::cout << apply(t, 1) << std::endl;
std::cout << apply(t, 2) << std::endl;
}
{
tup t{20, 1.5, 15.5f};
std::cout << apply(t, 0) << std::endl;
std::cout << apply(t, 1) << std::endl;
std::cout << apply(t, 2) << std::endl;
}
}
Live on Coliru:
http://coliru.stacked-crooked.com/a/3cfbd4d9ebd3bb3a
If you make fun2 into a class with overloaded operator():
struct fun2 {
inline int operator()(int x) {
return x;
}
inline int operator()(double x) {
return 0;
}
inline int operator()(float x) {
return -1;
}
};
then we can modify dyp's answer from here to work for us.
Note that this would look a lot neater in C++14, as we could have all the return types deduced and use std::index_sequence.
//call the function with the tuple element at the given index
template<class Ret, int N, class T, class Func>
auto apply_one(T&& p, Func func) -> Ret
{
return func( std::get<N>(std::forward<T>(p)) );
}
//call with runtime index
template<class Ret, class T, class Func, int... Is>
auto apply(T&& p, int index, Func func, seq<Is...>) -> Ret
{
using FT = Ret(T&&, Func);
//build up a constexpr array of function pointers to index
static constexpr FT* arr[] = { &apply_one<Ret, Is, T&&, Func>... };
//call the function pointer at the specified index
return arr[index](std::forward<T>(p), func);
}
//tag dispatcher
template<class Ret, class T, class Func>
auto apply(T&& p, int index, Func func) -> Ret
{
return apply<Ret>(std::forward<T>(p), index, func,
gen_seq<std::tuple_size<typename std::decay<T>::type>::value>{});
}
We then call apply and pass the return type as a template argument (you could deduce this using decltype or C++14):
auto t = std::make_tuple(1,1.0,1.0f);
std::cout << apply<int>(t, 0, fun2{}) << std::endl;
std::cout << apply<int>(t, 1, fun2{}) << std::endl;
std::cout << apply<int>(t, 2, fun2{}) << std::endl;
Live Demo
I'm not sure if this will completely fulfil your requirements due to the use of function pointers, but compilers can optimize this kind of thing pretty aggressively. The searching will be O(1) as the pointer array is just built once then indexed directly, which is pretty good. I'd try this out, measure, and see if it'll work for you.

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*

Find the index of an element in a fusion map

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;
}