Include counter in boost::fusion::for_each - c++

For my current project it is necessary to iterate over a boost::fusion container. For this I found boost::fusion::for_each quite useful. Now it is necessary for me to increase a counter for each time the operator() of Functor got called.
Firstly, I tried to add a member int count to Functor, but this is not possible, as it is necessary that operator() must be a const Memberfunction of Functor.
The following short code snippet is an illustration of what I wanted to achieve.
#include <boost/fusion/container/vector.hpp>
#include <string>
#include <boost/fusion/include/for_each.hpp>
#include <iostream>
struct Functor {
template<typename T>
void operator()(T& t) const{
std::cout << typeid(t).name() << " -> " << t << std::endl;
// Desired Output:
// int -> 5 : 0
// std::string -> test : 1
// double -> 5.3 : 2
// double -> 10. : 3
}
};
int main(int argc, char**args) {
typedef boost::fusion::vector<int, std::string, double, double> TData;
TData v(5, std::string("test"), 5.3, 10.);
boost::fusion::for_each(v, Functor());
}
Is there a workaround for this kind of problem?

Related

Store maps in template class by reference

So lately I learnt how to copy a map's reference and the next step is that I want to store map references in a template class.
So in my main method my goal is to create map_storage where I store my maps and do different calculations on them.
Template class:
#include <map>
#include <compare>
#include <utility>
#ifndef MAPALIGN_H
#define MAPALIGN_H
template<class Key, class T, class Compare = std::less<Key> >
class map_storage
{
public:
map_storage() : size_(0), data_(new std::map<Key, T, Compare>[100]){}
void add(std::map<Key, T, Compare> &temp_map)
{
data_[size_] = temp_map;
size_ += 1;
}
void action(){
std::cout << std::endl;
std::cout << "Size on action " << size_ << std::endl;
for(int i=0; i<size_; i++){
print_map(data_[i]);
}
}
void print_map(std::map<Key, T, Compare> &temp_map){
std::cout << "Size of given map is : " << temp_map.size() << std::endl;
}
void modifyMaps(..){
....
}
private:
int size_;
std::map<Key, T, Compare>* data_;
};
#endif
When I modify the maps in data_ (add new key,values etc.) I want to see the changes in main too and the same thing for the other way around.
My question is that How can I store the references of the maps in a template class?
Example of the main method:
#include <iostream>
#include "mapalign.h"
#include <string>
#include <map>
int main()
{
std::map<std::string, int> map1;
maps_storage<std::stringm int> maps_storage;
maps_storage.add(map1);
map1["asd"] = 1;
map1["dsa"] = 2;
// This should print 1 and 2 but prints 1 and 0 because the map is not in the map_storage by reference.
map_storage.action();
return 0;
}
Output:
1
0
Expected Output:
1
2

std::function & std::forward with variadic templates

Recently I was reading about variadic templates and based on an example I've seen online I was trying to implement a basic event-system. So far it seems to work fine but I was trying to go a step further and allow N number of arguments to be passed to an event handler function / callback, unfortunately the build error I'm getting is the following and I'm not sure what I'm doing wrong. I looked into similar source codes but still cant figure out what's the issue.
D:\Development\lab\c-cpp\EventEmitter3\src\main.cpp:30:68: error: parameter packs not expanded with '...':
return std::any_cast<std::function<R(Args)>>(eventCallback)(std::forward<Args>(args)...);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D:\Development\lab\c-cpp\EventEmitter3\src\main.cpp:30:68: note: 'Args'
Build finished with error(s).
Here is what I have so far, if you remove the ... the event system works fine for the 2 registered events in main.
#include <any>
#include <string>
#include <iostream>
#include <functional>
#include <unordered_map>
class EventEmitter
{
private:
std::unordered_map<std::string, std::any> events;
public:
EventEmitter() {}
void on(const std::string &eventName, const std::any &eventCallback)
{
events[eventName] = eventCallback;
}
template <typename R>
R emit(const std::string &eventName)
{
const std::any &eventCallback = events[eventName];
return std::any_cast<std::function<R(void)>>(eventCallback)();
}
template <typename R, typename... Args>
R emit(const std::string &eventName, Args &&...args)
{
const std::any &eventCallback = events[eventName];
return std::any_cast<std::function<R(Args)>>(eventCallback)(std::forward<Args>(args)...);
}
virtual ~EventEmitter() {}
};
int fun1()
{
std::cout << "fun1" << std::endl;
return 1;
}
double fun2(int i)
{
std::cout << "fun2" << std::endl;
return double(i);
}
double fun3(int x, int y)
{
std::cout << "fun3" << std::endl;
return double(x + y);
}
int main(int argc, char *argv[])
{
EventEmitter e;
e.on("fun1", std::function<int(void)>(fun1));
e.on("fun2", std::function<double(int)>(fun2));
e.emit<int>("fun1");
e.emit<double, int>("fun2", 1);
// Variadic would have been handy right here I guess?
// e.on("fun3", std::function<double(int, int)>(fun3));
// e.emit<double, int>("fun3", 1, 2);
return 0;
}
How can I fix this?
Well, you need to expand it.
return std::any_cast<std::function<R(Args...)>>(eventCallback)(std::forward<Args>(args)...);
^^^^^^^

Expand variadic arguments in initializer list fails

I try to create a simple program in which I want to create vector of future arguments.
I created a wrapper function which is used to submit lambda functions and stores internally in a vector the future objects
I use an intermediate step in which I create an initiliazer_list using variadic arguments . But fails to compile. I try to use to call a function in order to push the elements in the vector and fails to compile as well
Below is the code
#include <iostream>
#include <thread>
#include <future>
#include <functional>
#include <cstdlib>
#include <chrono>
#include <initializer_list>
using namespace std;
using FunctPtr = function<int(int, int) >;
using FutureInt = future<int>;
using AsyncVector = vector<FutureInt>;
AsyncVector asyncVec;
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
auto initList {pFunc... };
for (auto & element : initList)
{
asyncVec.emplace_back(async(launch::async, element,4,5));
}
}
int main()
{
int a;
int b;
auto addPtr = [](int x, int y)->int
{
std::cout << "add :" << x + y << std::endl;
return x + y;
};
auto multPtr = [](int x, int y)->int
{
std::cout << "mult :" << x * y << std::endl;
return x * y;
};
// submit(add,4,5);
submit(addPtr, multPtr);
for (auto & v : asyncVec)
{
std::cout << "Wait for " << v.get() << std::endl;
}
}
Yes, they are of different types so cannot be in the same init-list easily.
Your best options should probably be:
Either push them all into asyncVec in the same fold-expression.
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
(asyncVec.emplace_back(async(launch::async, std::move(pFunc), 4, 5)), ...);
}
Or, if they all are of the same signature, type-erase them, like keeping them in an array of std::function.
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
for (auto &&element: {std::function<int(int, int)>(std::move(pFunc))...})
{
asyncVec.emplace_back(async(launch::async, std::move(element), 4, 5));
}
}
(I have specified function signature explicitly though compiler should be able to deduce it.)
Or, if all closures are captureless and of the same signature, simply cast them to the same type when calling submit:
using SameType = int (*)(int, int);
submit(static_cast<SameType>(addPtr), static_cast<SameType>(mulPtr));
This way your original submit should work as is.

Using boost::mpl, how can I get how many template classes are not "Empty", and call some macro with this number?

I want to call a macro with some arguments depending on the result of boost::mpl::eval_if (or a similar function) that could give how many template arguments are not empty. Say we have some pseudocode like the following:
struct EmptyType { };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
eval_if<is_not_same<arg1, EmptyType>, FILL_MY_CLASS_DEFINE(1)> else
eval_if<is_not_same<arg2, EmptyType>, FILL_MY_CLASS_DEFINE(2)> else
eval_if<is_not_same<arg3, EmptyType>, FILL_MY_CLASS_DEFINE(3)>;
};
I am trying to fill my class with some content depending on how many arguments are EmptyType. I wonder how such thing can be done in C++03 via Boost.MPL/Preprocessor or some other Boost library?
You don't need preprocessor or mpl. Partial specialization is you need:
Edit This works in C++03, see it live: https://ideone.com/6MaHJ
#include <iostream>
#include <string>
struct EmptyType { };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
// FILL_MY_CLASS_DEFINE(3)
};
template<class arg1, class arg2>
class my_class<arg1,arg2,EmptyType>
{
// FILL_MY_CLASS_DEFINE(2)
};
template<class arg1>
class my_class<arg1,EmptyType,EmptyType>
{
// FILL_MY_CLASS_DEFINE(1)
};
template<>
class my_class<EmptyType,EmptyType,EmptyType>
{
// FILL_MY_CLASS_DEFINE(0)
};
int main(int argc, const char *argv[])
{
my_class<std::string, double, int> a;
my_class<std::string, int> b;
my_class<void> c;
return 0;
}
Are you looking for variadic templates?
#include <tuple>
#include <iostream>
#include <string>
template <typename... Arg>
struct my_class
{
// getting the size of the argument list:
enum { size = sizeof...(Arg) }; // in absense of static fields with initializers...
// demo filling the struct with data:
std::tuple<Arg...> arg_data;
my_class(Arg&&... a) : arg_data(std::forward<Arg>(a)...) { }
};
int main(int argc, const char *argv[])
{
my_class<std::string, int> a("hello world", 42);
std::cout << "size: " << a.size << std::endl;
std::cout << "last: " << std::get<a.size-1>(a.arg_data) << std::endl;
return 0;
}
Output:
size: 2
last: 42
When you have many template arguments, a partial specialization can be impractical and error-prone.
The code below will do what you want, but as it was already mentioned in other answers, it's not always the best way to proceed.
#include <boost/mpl/count_if.hpp>
#include <boost/mpl/not.hpp>
#include <boost/type_traits/is_same.hpp>
using boost::is_same;
using boost::mpl::_;
using boost::mpl::not_;
using boost::mpl::count_if;
#define FILL_MY_CLASS_DEFINE(x) static const int __x__ = x // an example, watch out: no semicolon at the end
struct EmptyType { };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
// count the types which are not equal to EmptyType
static const long NonEmptyCount = count_if<type, not_<is_same<_, EmptyType> > >::value;
// invoke a macro with an argument
FILL_MY_CLASS_DEFINE(NonEmptyCount);
};

convert boost::fusion::set to boost::fusion::map by using boost::fusion::fold

I have a fusion set and would like to convert it into a fusion map.
#include <cstdlib>
#include <iostream>
#include <boost/fusion/include/fold.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/include/make_map.hpp>
#include <boost/fusion/include/push_back.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/fusion/container/set.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_key.hpp>
struct node_base
{
int get() {return 123;}
};
struct node_a : public node_base
{
node_a(){std::cout << "node_a::Ctor"<< std::endl;}
};
struct node_b : public node_base
{
node_b(){std::cout << "node_b::Ctor"<< std::endl;}
};
struct node_c : public node_base
{
node_c(){std::cout << "node_c::Ctor"<< std::endl;}
};
typedef ::boost::fusion::set<node_a,node_b,node_c> my_fusion_set;
my_fusion_set my_set;
struct push_back_map
{
typedef ::boost::fusion::map<> result_type;
template<class NODE>
::boost::fusion::map<> operator() (const ::boost::fusion::map<> & m, const NODE & n)
{
return ::boost::fusion::push_back(
m
, ::boost::fusion::make_pair<NODE>(n)
);
}
};
::boost::fusion::map<> my_map =
::boost::fusion::fold(
my_set
, ::boost::fusion::map<>()
, push_back_map()
);
struct print_map
{
template<class P>
void operator()(P & p) const
{
std::cout << p.second.get() << std::endl;
}
};
int main()
{
::boost::fusion::for_each(my_map, print_map());
std::cout << ::boost::fusion::at_key<node_a>(my_set).get() << std::endl;
//std::cout << ::boost::fusion::at_key<node_a>(my_map).second.get() << std::endl;
system("pause");
return 0;
}
This code compiles. But it couldn't print out the results of my_map. my_set is constructed successfully. my_map is simply converted from a my_set with each element's original type as the key and its instance object from default constructor as the value. I am wondering if my_map is created successfully from fusion::fold. my_set is able to query by calling fusion::at_key<> on it, but my_map doesn't work for at_key<>.
Thanks!
I couldn't think of a concise way to create fusion::map from
fusion::set with fold.
So this answer won't resolve your question directly.
I post this in case this would give you some hint.
How about
preparing a type list(as my_types in the following), and
creating fusion::set and fusion::map from that list
instead of creating fusion::map from fusion::set directly?
For example:
namespace bf = boost::fusion;
namespace bm = boost::mpl;
typedef bm::vector<node_a,node_b,node_c> my_types;
typedef bm::fold<
my_types
, bm::vector<>
, bm::push_back< bm::_1, bf::pair< bm::_2, bm::_2 > >
>::type my_map_types;
int main() {
bf::result_of::as_set<my_types>::type my_set;
bf::result_of::as_map<my_map_types>::type my_map;
std::cout << bf::at_key<node_a>(my_set).get() << std::endl;
std::cout << bf::at_key<node_a>(my_map).get() << std::endl;
}
Here is a test on ideone.
Incidentally, if key-type and value-type in your fusion::map are always
identical, fusion::set will meet that purpose.