Tables of C++ member functions - c++

I need a table that maps codes to C++ member functions. Suppose we have this class:
class foo
{
bool one() const;
bool two() const;
bool call(char*) const;
};
What I want is a table like this:
{
{ “somestring”, one },
{ ”otherstring”, two }
};
So that if I have a foo object f, f.call(”somestring”) would look up “somestring” in the table, call the one() member function, and return the result.
All of the called functions have identical prototypes, i.e., they are const, take no parameters, and return bool.
Is this possible? How?

Yes, it's possible, using pointer to member syntax.
Using the prototypes you supplied, the map would be.
std::map< std::string, bool( foo::*)() const>
It would be called with this syntax
this->*my_map["somestring"]();
That odd-looking ->* operator is for pointer to member functions, which can have some odd considerations, due to inheritance. (It's not just a raw address, as -> would expect)

Since you only need to store members of the same class, with the same arguments and return types, you can use pointer-to-member-functions:
bool foo::call(char const * name) const {
static std::map<std::string, bool (foo::*)() const> table
{
{"one", &foo::one},
{"two", &foo::two}
};
auto entry = table.find(name);
if (entry != table.end()) {
return (this->*(entry->second))();
} else {
return false;
}
}
That uses the new initialisation syntax of C++11. If your compiler doesn't support it, there are various other options. You could initialise the map with a static function:
typedef std::map<std::string, bool (foo::*)() const> table_type;
static table_type table = make_table();
static table_type make_table() {
table_type table;
table["one"] = &foo::one;
table["two"] = &foo::two;
return table;
}
or you could use Boost.Assignment:
static std::map<std::string, bool (foo::*)() const> table =
boost::assign::map_list_of
("one", &foo::one)
("two", &foo::two);
or you could use an array, and find the entry with std::find_if (or a simple for loop if your library doesn't have that yet), or std::binary_search if you make sure the array is sorted.

Yes.
struct foo_method
{
std::string name;
bool (foo::*pfun)() const;
};
foo_method methodTable[] =
{
{ “somestring”, &foo::one },
{ ”otherstring”, &foo::one }
};
void foo::call(const char* name) const
{
size_t size = sizeof(methodTable)/sizeof(*methodTable);
for(size_t i = 0 ; i < size ; ++i)
{
if ( methodTable[i].name == name )
{
bool (foo::*pfun)() const = methodTable[i].pfun;
(this->*pfun)(); //invoke
}
}
}

I would go with boost::function with std::map. Concretely, something like this :
typedef boost::function<bool()> MyFunc;
typedef std::map<std::string, MyFunc> MyFuncMap;
Then, given an instance of MyFuncMap, you could just do map["something"](). Then you could wrap that in a class that overloads operator(). You could use function pointers/references, but I prefer using boost::function because it allows me to bind pointers to member functions (using boost::bind) or use other function objects. You can also test boost::function in conditionals as you would with regular function pointers.
Here is the relevant documentation :
Boost.Function
Boost.Bind
Good luck!
Edit: Regarding your question about the const member and boost::function, here's an example :
#include <boost/function.hpp>
#include <boost/bind.hpp>
typedef boost::function<bool ()> FuncPtr;
struct Test
{
bool test() const
{
std::cout << "yay" << std::endl;
}
};
int main(int argc, char **argv)
{
Test t;
FuncPtr ptr = boost::bind(&Test::test, &t);
ptr();
}

I'd just like to add that a pointer to a member function is meaningless without having an instance of a class on which to call it. The situation you've described accounts for this (and I think you know this), however in other situations, it may be necessary to encapsulate the function pointer with a pointer or reference to the instance to which it corresponds in some sort of functor construct.

Related

boost::function vs function pointers

I'm implementing a generic setting reader.
The idea is that I have an application which settings can be boolean, integers and strings.
Then I have a Config class where the getters for such settings are implemented, the config class takes a customer in the constructor, so that it knows it will read settings for that very customer.
I'm having troubles in having that working, I think I am misusing boost::function confusing it with plain function pointer.
In the maps I'd like to have references while the boost::function shall be binded only at config read time since there I have allocated a Config instance for the given customer.
The problem is that I cannot use function pointers without typedefs and this complicates the template work, any wiser solution ?
#include "Config.h"
class ConfigReader
{
ConfigReader();
template<class R>
R readConfig(std::string customer, std::string settingName);
private:
typedef bool (Config::* BoolConfigFunctor) () const;
std::map<std::string, BoolConfigFunctor> boolConfigMap;
typedef int(Config::* IntConfigFunctor) () const;
std::map<std::string, IntConfigFunctor> integerConfigMap;
typedef std::string (Config::* StrConfigFunctor) () const;
std::map<std::string, StrConfigFunctor> stringConfigMap;
template<class R>
std::map<std::string, R (Config::* ) () > getConfigMap();
}
ConfigReader()
{
// INIT all settings you want to be readable in the functor maps
boolConfigMap["USE_NEW_VERSION"] = &Config::useNewVersion;
boolConfigMap["MAINTENANCE_MODE"] = &Config::isMaintenance;
integerConfigMap["TIMEOUT"] = &Config::getTimeout;
stringConfigMap["USERNAME"] = &Config::getUserName;
...
}
template<class R>
R readConfig(std::string customer, std::string settingName)
{
R returnValue;
typedef typename std::map<AMD_STD::string, R (Config::* ) () > ConfigMap_t;
typedef typename ConfigMap_t::const_iterator ConfigMapIter_t;
ConfigMap_t configMap = getConfigMap<R>();
ConfigMapIter_t configIter = configMap.find(settingName);
if (configIter != configMap.end())
{
Config config(customer); // Real instance of Config against which we want to call the function
boost::function<R ()> configFunction;
configFunction =
boost::bind(
configIter->second,
config);
returnValue= configFunction();
}
return returnValue;
}
template<>
std::map<std::string, bool (Config::* ) ()> ConfigReader::getConfigMap()
{
return boolConfigMap;
}
template<>
std::map<std::string, int (Config::* ) ()> ConfigReader::getConfigMap()
{
return integerConfigMap;
}
template<>
std::map<std::string, string (Config::* ) ()> ConfigReader::getConfigMap()
{
return stringConfigMap;
}
UPDATE
it did work by using function references in maps rather than boost::function
You can't use member function pointers as normal function pointers, unless the member functions are static. You should instead use Boost bind with a specific object instance:
boolConfigMap["USE_NEW_VERSION"] = boost::bind(&Config::useNewVersion, someInstanceOfConfig);
The reason that (non-static) member function pointers are not the same as normal function pointers (or static member function pointers) is that member functions have a hidden "zeroeth" argument, that is the this pointer inside the member function.
Also, your declaration of the boost::function objects should be only e.g.
boost::function<bool()>
That will handle all type of functions returning a bool and taking no arguments.
If your compiler is new enough, you might also want to change to be using std::function and std::bind.
After your edit to show with member function pointers: You have to call the function pointers correctly as well, like
(config.*configIter->second)();

Using different interface with stl datastructure

I want to create a class with these two constructors, but unordered_map<void*,void*> * and those two created in constructor is not compatible. How can I change to make the following code works while preserving the prototype of the constructors.
struct eq_fun
{
bool operator()(void* s1, const void* s2) const
{
return ( _cmp_fn((void*)s1,(void*)s2) == 0 );
}
int (*_cmp_fn)(void*, void*);
eq_fun(int (*fn)(void*, void*)):_cmp_fn(fn){}
};
struct hash_fun
{
size_t operator()(const void *p) const
{
return _hash_fn(p);
}
int (*_hash_fn)(const void*);
hash_fun(int (*fn)(const void*)):_hash_fn(fn){}
};
class MyClass {
private:
unordered_map<void*,void*> *h_map;
public:
template<class EQ,class HF>MyClass()
{ h_map = new unordered_map<void*,void*,HF,EQ>(); }
MyClass(int (*h)(const void*),int (*cmp)(void*,void*))
{ h_map = new unordered_map<void*,void*,hash_fun,eq_fun>(0,hash_fun(h),eq_fun(cmp)); }
};
It seems you're trying to create a different templated member depending on the template arguments of the constructor. This is impossible in C++ in two ways.
This:
unordered_map<void*,void*> *h_map;
h_map = new hash_map<void*,void*,HF,EQ>();
is invalid because hash_map is not a derived class of unordered_map.
You cannot mix uncovertible template parameters like you're trying to do with the EQ and HF parameters. hash_map<..,..,HF1> and hash_map<..,..,HF2> are not compatible types.
The only way out that I can see is to choose either hash_map or unordered_map and make MyClass a template MyClass<HF, EQ>.
Note that when the TR1 class hash_map was accepted into C++11 it was renamed unordered_map. So they're the same thing, and it makes no sense to mix these two types in the same code. Reference: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1456.html

Universal function pointer

There is some class which have methods like:
int getSomething1();
std::string getSomething2();
someClass getSomething3();
There is structure which describes fields of this class like:
{"name of field", pointer to getter, std::type_info}
Then I would like to use it as follows:
if(type == int){
field_int = (int)getter();
}
else if(type == std::string){
field_string = (std::string)getter();
}
etc.
How to transform getters like
int getSomething1();
std::string getSomething2();
etc.
to some universal function pointer and then to get the correct value of field?
This answer of mine to another question addresses your problem pretty well. With some minor modifications, you get this:
template<class C, class T>
T get_attribute(const C& instance, T (C::*func)() const) {
return (instance.*func)();
}
Assuming the following:
struct Foo {
int getSomething1() const;
std::string getSomething2() const;
someClass getSomething3() const;
};
You can use it like this:
Foo foo;
int value = get_attribute<Foo, int>(foo, &Foo::getSomething1);
std::string value = get_attribute<Foo, std::string>(foo, &Foo::getSomething2);
someClass value = get_attribute<Foo, someClass>(foo, &Foo::getSomething3);
You can of course transform get_attribute to a functor to bind some or all of the arguments.
There is no formal universal function pointer, the equivalent of void*
for data. The usual solution is to use void (*)(); you are guaranteed
that you can convert any (non-member) function pointer to this (or any
other function pointer type) and back without loss of information.
If there is a certain similarity in the function signatures (e.g. all
are getters, with no arguments) and how they are used, it may be
possible to handle this with an abstract base class and a set of derived
classes (possibly templated); putting pointers to instances of these
classes in a map would definitely be more elegant than an enormous
switch.
What you are trying to achieve can be better achieved with already existing containers such as a boost fusion sequence. I'd advice that you try this first.
Templates to the rescue!
// Create mapping of type to specific function
template <typename T> T getSomething(); // No default implementation
template <> int getSomething<int>() { return getSomething1(); }
template <> std::string getSomething<std::string>() { return getSomething2(); }
template <> someClass getSomething<someClass>() { return getSomething3(); }
// Convenience wrapper
template <typename T> void getSomething(T& t) { t = getSomething<T>(); }
// Use
int i = getSomething<int>();
std::string s;
getSomething(s);
As I understand, your difficulty is in storing the function pointers, since they are of different types. You can solve this using Boost.Any and Boost.Function.
#include <boost/any.hpp>
#include <boost/function.hpp>
int getInt() {
return 0;
}
std::string getString() {
return "hello";
}
int main()
{
boost::function<boost::any ()> intFunc(getInt);
boost::function<boost::any ()> strFunc(getString);
int i = boost::any_cast<int>(intFunc());
std::string str = boost::any_cast<std::string>(strFunc());
}

null pointer when getting function pointer using boost::function::target

After reading this answer I thought I had a solution. At least the answer there is what I would like to do but I'm having a problem with the implementation.
here is an outline of what I am trying to do
typedef map<string, double*> myMap;
typedef int (*ftwpt)(const char*, const struct stat*, int);
typedef boost::function<int(const char*, const struct stat*, int)> MyFTWFunction;
int myFunction(const char*, const struct stat*, int, myMap*);
int main()
{
myMap m_map;
char tmpdir[] = "/tmp/mytmp";
MyFTWFunction f = boost::bind(myFunction,_1,_2,_3, &m_map);
ftwpt* fpt = f.target<ftwpt>();
if (fpt)
status = ftw(tmpdir, *fpt, 50);
else
{
cout << "Boost could not perform runtime conversion on function pointer" << endl;
return (EXIT_FAILURE);
}
}
the program compiles with no errors or warnings but I am getting a null pointer (fpt) returned from f.target(); at runtime. From references linked on the above stackoverflow question it seems a null pointer is returned if boost is unable to perform the runtime conversion. But I have no idea why Boost might not be able to perform the runtime conversion. Any ideas?
For that to work, you would need to know the exact type of the bind expression that you store into the boost::function object. The object boost::bind(....) returns is some weird expression template, not a function pointer.
To understand why this is needed, consider how boost::function is implemented in principle
struct base { virtual ~base() { } };
template<typename T>
struct derived : base {
derived(T t):t(t) { }
T t;
};
struct function {
template<typename T>
function(T t) {
base *b = new derived<T>(t);
}
template<typename T>
T *target() {
if(typeid(*b) == typeid(derived<T>))
return &static_cast< derived<T>* >(b)->t;
return 0;
}
base *b;
};
That's the most fundamental structure, without the operator() bloat - much like boost::any. The mechanism is called type-erasure: The constructor accepts objects of arbitrary types, and then stores an object encapsulated into an object that you may reach through virtual function calls (boost::function is optimized like hell, using its own vtable and stack-allocation to avoid new for small types and so on).
For function pointers, this works great, because you know the type of the function that you assign to the boost::function object. But for complex callable objects, it doesn't quite work anymore.
To be able to see it working and to see that it's not just working with function pointers, but also with bind expressions, consider the following code
template<typename T>
struct id { typedef T type; };
template<typename T>
id<T> make_id(T) { return id<T>(); }
struct any_type {
template<typename T>
operator id<T>() const { return id<T>(); }
};
template<typename T, typename Fn>
T *get_target(boost::function<Fn> &f, id<T>)
{ return f.template target<T>(); }
void f(int a, int b) { std::cout << a << " " << b << std::endl; }
int main() {
boost::function<void(int)> g = boost::bind(&f, _1, 10);
(*get_target(g, true ? any_type() : make_id(boost::bind(&f, _1, 10))))(2);
}
Within get_target you know the type of what boost::bind returns. You can use that to call the target call and return the object that's wrapped inside the boost::function. Within main we then call the bind expression. Please read Eric Niebler's article Conditional Love to see how this code snippet works.
The other answer points out why your code doesn't work. Here is a really ugly solution that sort of does, kind of, for certain limited situations.
typedef int (*ftwpt)(const char*, const struct stat*, int);
typedef boost::function<int(const char*, const struct stat*, int)> MyFTWFunction;
template <MyFTWFunction *callback>
class callback_binder {
public:
static int callbackThunk(const char *s, const struct stat *st, int i) {
return (*callback)(s, i);
}
};
extern void register_callback(callback_t f);
int random_func(const char *s, const struct stat *st, int i)
{
if (s && *s) {
return i;
} else {
return -1;
}
}
MyFTWFunction myfunc;
int main(int argc, const char *argv[])
{
myfunc = random_func;
register_callback(&callback_binder<&myfunc>::callbackThunk);
return 0;
}
The rules for using pointers as template arguments require that the pointer passed in as an argument be a pointer to a global variable. That global variable can, of course, be declared in an anonymous namespace.
It's ugly, and if you wanted to have several possible instances of myMap possible called back with at the same time you'd need as many global MyFTWFunction variables as possible simultaneous instances of myMap. Mostly this automates the creation of a thunk function that uses the content of a global variable to fill in the missing parameter.
Here is a version that is a LOT less flexible that does approximately the same thing for this narrow case that may make it more obvious what's going on here:
#include <map>
#include <string>
using ::std::map;
using ::std::string;
typedef map<string, double*> myMap;
typedef int (*callback_t)(const char *, struct stat *st, int);
int myFunction(const char*, struct stat *st, int, myMap*);
template <myMap **map_ptr>
class myMap_binder {
public:
static int call_my_function(const char *s, struct stat *st, int i) {
return myFunction(s, st, i, *map_ptr);
}
};
extern void register_callback(callback_t f);
myMap *mainmap;
myMap *othermap;
int main(int argc, const char *argv[])
{
myMap m_map;
myMap m_map2;
mainmap = &m_map;
othermap = &m_map2;
register_callback(&myMap_binder<&mainmap>::call_my_function);
register_callback(&myMap_binder<&othermap>::call_my_function);
return 0;
}
As you can see myMap_binder is a template that auto-generates thunk functions that stuff in the contents of a global variable into a call to your callback function.
This is a few years late, but maybe it'll help you in the future. My problem was slightly different, but you can still get the answer you want from the solution; read here:
> Messaging system: Callbacks can be anything

Map of boost function of different types?

i was wondering if there was a way to do this in C++?
void func1(const std::string& s)
{
std::cout << s << std::endl;
}
void func2(int me)
{
std::cout << me << std::endl;
}
int main()
{
std::map<std::string, boost::function< ??? > > a_map;
a_map["func1"] = &func1;
a_map["func1"]("HELLO");
}
Is there any way to do what i have above using boost function and a map?
There are ways to store the functions, the problem is, in order to be able to call the function with the desired argument you'd have to know the calling signature of the function anyways, and if you have that information, you might as well use separate maps, or use a more complicated object than boost::function.
If you're willing to do a bit of work and have a finite number of signatures, you could just do something like this:
class MultiFunc
{
protected:
MultiFunc() {}
public:
typedef void (*stringFunc)(const std::string&);
typedef void (*intFunc)(int);
static MultiFunc *Create(stringFunc function);
static MultiFunc *Create(intFunc function);
virtual void operator()(const string &) { throw exception(); }
virtual void operator()(int) { throw exception(); }
virtual ~MultiFunc();
};
class MultiFuncString : public MultiFunc
{
private:
stringFunc Function;
public:
MultiFuncString(stringFunc function) : Function(function) {}
virtual void operator()(const string &arg) { Function(arg); }
};
class MultiFuncInt : public MultiFunc
{
private:
intFunc Function;
public:
MultiFuncInt(intFunc function) : Function(function) {}
virtual void operator()(int arg) { Function(arg); }
};
MultiFunc *MultiFunc::Create(MultiFunc::stringFunc function)
{
return new MultiFuncString(function);
}
MultiFunc *MultiFunc::Create(MultiFunc::intFunc function)
{
return new MultiFuncInt(function);
}
void func1(const std::string& s)
{
std::cout << s << std::endl;
}
void func2(int me)
{
std::cout << me << std::endl;
}
int main()
{
map<string, MultiFunc *> a_map;
a_map["func1"] = MultiFunc::Create(&func1);
(*a_map["func1"])("Hello");
a_map["func2"] = MultiFunc::Create(&func2);
(*a_map["func2"])(3);
// Remember to delete the MultiFunc object, or use smart pointers.
}
This outputs:
Hello
3
Unfortunately, you can't make templated virtual functions or you easily generalize this all.
You probably can't use the std::map since it is a homogenous container. Try, something like boost::variant (they support the visitor pattern) or boost::tuple
What you are trying to do sounds a little weird. Normally, you would have a container be a collection of abstract types or objects or functions with the same signature. Otherwise, how would you know how to call the function when you are iterating the container? I like to make the container a collection of function objects with a known signature, then use Boost.Bind to store closures that call the function with additional arguments.
For example:
typedef boost::function<void, void> Function;
typedef std::map<std::string, Function> Functions;
Functions functions:
void foo()
{
...
}
functions["foo"] = foo;
void bar(std::string &s)
{
...
}
// binds the value "hello" to the s parameter
functions["bar"] = boost::bind(bar, "hello");
read this link below. It talks about using boost::bind to store the function pointers in std::map
http://www.gamedev.net/community/forums/topic.asp?topic_id=526381&whichpage=1&#3411515
store interfaces:
struct IStringData
{
virtual std::string get() const = 0;
virtual ~IStringData() {}
};
and make implementaions, one will just hold string value, other implementation will store functor, maybe you will have other implementations in future.
No. You can't. Since boost::function isn't polymorphic, it breaks down there. (It takes a fixed set of argument types.)
There was talk about work in that direction on the boost mail-list, though, so search the archives and see if there is some code you could youse.
A workaround would be to use boost::function but then you need to add to the map not your real functions (i.e. func1/func2) but dispatch functions that extracts the type from the any-container and calls the real function. (And bails if it's wrong, just as in any dynamic langugage.)