class Function
{
public:
std::string Name;
void call(std::string x);
Function(std::string Nam)
{
Name = Nam;
}
};
std::vector<Function> funcs;
void Load_FuncLib()
{
Function print("print");
Function add("add");
print.call(std::string x)
{
std::cout<< x <<"\n";
}
add.call(std::string x)
{
std::cout<< std::stoi(x) + std::stoi(x) << "\n";
}
funcs.push_back(print);
funcs.push_back(add);
funcs.at(0).call("Hello world");
}
I want it to run the function print.call("Hello world"); but it will not work as I don't know how to set a function, which is already declared, nor do I know how to call it using a vector.
You most likely want to achieve something like this?
#include <unordered_map>
#include <iostream>
#include <string>
#include <functional>
int main() {
std::unordered_map<std::string, std::function<void (const std::string&)>> funcs;
funcs["print"] = [](const std::string& str) {
std::cout << str << '\n';
};
funcs["add"] = [](const std::string& str) {
int i = std::stoi(str);
std::cout << i + i << '\n';
};
funcs["print"]("Hello, World!");
funcs["add"]("12");
}
https://ideone.com/Ke4aEK
At any time you can reset a certain value of the hash map with another function.
Also, depending on your needs, you may use std::function or just plain function pointers.
Related
why gcc 9.4 not check parameters when bind a class member funtion to a std::function viriable, but check when bind a global function? here is example code, CreateRpcFun has a parameter, but Test member function print doesn't have any other parameters except this, bind print to CreateRpcFun works well, but global funtion print2 cannot, can anybody explain why?
#include <functional>
#include <iostream>
#include <string>
using namespace std;
using CreateRpcFun = std::function<void(const string &)>;
class Test {
public:
Test() : str_("nihao!") {}
// bind print to CreateRpcFun passed compile
void print() { cout << str_ << endl; }
private:
string str_;
};
class Holder {
public:
CreateRpcFun CreateRpc;
};
class Other {
public:
Other(Holder h, string str) : h_(h), str_(str) {}
void run() { h_.CreateRpc("world!"); }
private:
Holder h_;
string str_;
};
void print1(const string &str) { cout << str << endl; }
void print2() { cout << "magic" << endl; }
int main() {
Test t;
Holder h;
h.CreateRpc = std::bind(&Test::print, &t);
Other o(h, "hhhh");
o.run();
h.CreateRpc = &print1;
h.CreateRpc("test");
// h.CreateRpc = &print2; // compile error
// h.CreateRpc("test");
}
I am coming from c# so please excuse anything I may get wrong in trying to ask this question.
I have created a map that contains a string and a method to handle invoking a method by a string:
//MyClass.h
void SerializeCustomData();
std::unordered_map<std::string, void(MyClass::*)()> functionMap;
MyMethod() {
functionMap["SerializeCustomData"] = &MyClass::SerializeCustomData;
};
My question is; how can I have my map take in a parameter for the method? Either a generic type or a string in c++?
Example:
SerializeCustomData(std::string);
#include <functional>
#include <iostream>
#include <unordered_map>
class MyClass
{
std::unordered_map<std::string, std::function<void(MyClass&, std::string)>> functionMap;
public:
void MyMethod()
{
functionMap.emplace("SerializeCustomData", &MyClass::SerializeCustomData);
}
void CallSerialize()
{
functionMap.at("SerializeCustomData")(*this, "argument");
}
void SerializeCustomData(std::string s)
{
std::cout << "hello: " << s << "\n";
}
};
int main()
{
MyClass c;
c.MyMethod();
c.CallSerialize();
}
Here the function from the map is called with a MyClass& argument and a string. Note that the MyClass isn't bound to the callback, and so we're passing it explicitly.
#include <boost/ref.hpp>
//#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/mem_fn.hpp>
using namespace std;
using namespace boost::lambda;
class Base {
public:
Base () {}
bool toBeRemoved() const {
return true;
}
};
class status : public Base {
std::string name_;
bool ok_;
public:
status(const std::string& name):name_(name),ok_(true) {}
void break_it() {
ok_=false;
}
bool is_broken() const {
return ok_;
}
void report() const {
std::cout << name_ << " is " <<
(ok_ ? "working nominally":"terribly broken") << '\n';
}
std::string getStatus() const {
return ok_ ? "1" : "0";
}
};
class some_class {
public:
int test() {
std::vector<boost::shared_ptr<status> > s_statuses = getStatus(); //some func
std::set<string> s;
std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::lambda::bind(boost::mem_fn(&status::getStatus), boost::ref(*_1)));
// approach #2
// std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::lambda::bind(boost::mem_fn(&status::getStatus), boost::ref(*_1), _1));
// approach #3
// std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), boost::bind(&status::getStatus), _1));
std::copy(s.begin(), s.end(), ostream_iterator<string>(std::cout, "-"));
std::cout << endl;
return 0;
}
}
For all the approaches above, I am getting the error "can call member function without object" on the line containing the bind call. I have tried using boost::lambda::bind and boost::bind as well. Though this way of using bind works if objects are defined, for example in main function. I assume I am making some silly mistake here, but I am not able to figure out why these all approaches working, or it could be the case that this is not the right way of doing at all.
Could someone please help me resolve this on how to properly use boost bind for non-static member of class which are stored in stl containers ?
Thanks,
You should just need to use boost::mem_fn. (Note, you could also use std::mem_fn if available.)
std::transform(s_statuses.begin(), s_statuses.end(), std::inserter(s, s.begin()), std::mem_fn(&status::getStatus));
#include <boost/bind.hpp>
#include <iostream>
using namespace std;
using boost::bind;
class A {
public:
void print(string &s) {
cout << s.c_str() << endl;
}
};
typedef void (*callback)();
class B {
public:
void set_callback(callback cb) {
m_cb = cb;
}
void do_callback() {
m_cb();
}
private:
callback m_cb;
};
void main() {
A a;
B b;
string s("message");
b.set_callback(bind(A::print, &a, s));
b.do_callback();
}
So what I'm trying to do is to have the print method of A stream "message" to cout when b's callback is activated. I'm getting an unexpected number of arguments error from msvc10. I'm sure this is super noob basic and I'm sorry in advance.
replace typedef void (*callback)(); with typedef boost::function<void()> callback;
A bound function doesn't produce an ordinary function, so you cannot just store it in a regular function pointer. However, boost::function is able to handle anything as long as it is callable with the correct signature, so that's what you want. It will work with a function pointer, or a functor created with bind.
After a few corrections to your code, I came up with this:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
// i prefer explicit namespaces, but that's a matter of preference
class A {
public:
// prefer const refs to regular refs unless you need to modify the argument!
void print(const std::string &s) {
// no need for .c_str() here, cout knows how to output a std::string just fine :-)
std::cout << s << std::endl;
}
};
// holds any arity 0 callable "thing" which returns void
typedef boost::function<void()> callback;
class B {
public:
void set_callback(callback cb) {
m_cb = cb;
}
void do_callback() {
m_cb();
}
private:
callback m_cb;
};
void regular_function() {
std::cout << "regular!" << std::endl;
}
// the return type for main is int, never anything else
// however, in c++, you may omit the "return 0;" from main (and only main)
// which will have the same effect as if you had a "return 0;" as the last line
// of main
int main() {
A a;
B b;
std::string s("message");
// you forget the "&" here before A::print!
b.set_callback(boost::bind(&A::print, &a, s));
b.do_callback();
// this will work for regular function pointers too, yay!
b.set_callback(regular_function);
b.do_callback();
}
I'm fairly green when it comes to c++0x, lambda, and such so I hope you guys can help me out with this little problem.
I want to store a bunch of callbacks in a vector and then use for_each to call them when the time is right. I want the callback functions to be able to accept arguments. Here's my code right now. The trouble is in void B::do_another_callbacks(std::string &)
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <vector>
#include <iostream>
#include <algorithm>
class A {
public:
void print(std::string &s) {
std::cout << s.c_str() << std::endl;
}
};
typedef boost::function<void(std::string&)> another_callback;
typedef boost::function<void()> callback;
typedef std::vector<callback> callback_vector;
typedef std::vector<another_callback> another_callback_vector;
class B {
public:
void add_callback(callback cb) {
m_cb.push_back(cb);
}
void add_another_callback(another_callback acb) {
m_acb.push_back(acb);
}
void do_callbacks() {
for_each(m_cb.begin(), m_cb.end(), this);
}
void do_another_callbacks(std::string &s) {
std::tr1::function<void(another_callback , std::string &)> my_func = [] (another_callback acb, std::string &s) { acb(s); }
for_each(m_acb.begin(), m_acb.end(), my_func(_1, s));
}
void operator() (callback cb) { cb(); }
private:
callback_vector m_cb;
another_callback_vector m_acb;
};
void main() {
A a;
B b;
std::string s("message");
std::string q("question");
b.add_callback(boost::bind(&A::print, &a, s));
b.add_callback(boost::bind(&A::print, &a, q));
b.add_another_callback(boost::bind(&A::print, &a, _1));
b.do_callbacks();
b.do_another_callbacks(s);
b.do_another_callbacks(q);
}
I thought I might be able to do something like this...
void do_another_callbacks(std::string &s) {
for_each(m_acb.begin(), m_acb.end(), [&s](another_callback acb) {
acb(s);
});
}
But that doesn't compile in MSVC2010
The problem with the long example is that my_func(_1,s) is evaluated right there and then. You need to use std::bind (or boost::bind) to invoke the function on each element in the range.
The alternative code that you posted does indeed work, but the whole example fails to compile because of the code in do_callbacks:
void do_callbacks() {
for_each(m_cb.begin(), m_cb.end(), this);
}
this is of type B*, which is not callable. If you define a result_type typedef to match the return type of operator() then you could use std::ref(*this) instead. The following code compiles and runs under MSVC10:
#include <functional>
#include <vector>
#include <iostream>
#include <algorithm>
class A {
public:
void print(std::string &s) {
std::cout << s.c_str() << std::endl;
}
};
typedef std::function<void(std::string&)> another_callback;
typedef std::function<void()> callback;
typedef std::vector<callback> callback_vector;
typedef std::vector<another_callback> another_callback_vector;
class B {
public:
void add_callback(callback cb) {
m_cb.push_back(cb);
}
void add_another_callback(another_callback acb) {
m_acb.push_back(acb);
}
void do_callbacks() {
std::for_each(m_cb.begin(), m_cb.end(), std::ref(*this));
}
void do_another_callbacks(std::string &s) {
std::for_each(m_acb.begin(), m_acb.end(), [&s](another_callback acb) {
acb(s);
});
}
typedef void result_type;
void operator() (callback cb) { cb(); }
private:
callback_vector m_cb;
another_callback_vector m_acb;
};
int main() {
A a;
B b;
std::string s("message");
std::string q("question");
b.add_callback(std::bind(&A::print, &a, s));
b.add_callback(std::bind(&A::print, &a, q));
b.add_another_callback(std::bind(&A::print, &a, std::placeholders::_1));
b.do_callbacks();
b.do_another_callbacks(s);
b.do_another_callbacks(q);
}