Boost :: function vs function pointers

I use a universal reader. The idea is that I have an application whose parameters can be logical, integer and string. Then I have a Config class in which getters for such parameters are implemented, the configuration class accepts the client in the constructor, so that it knows that it will read the settings for this client itself.

I'm having problems with what I'm working on, I think I am abusing the boost :: function, which confuses it with a simple function pointer.

On the maps, I would like to have links, while I boost::functionshould be attached only at the time of reading the configuration, since there I allocated an instance Configfor this client.

The problem is that I cannot use function pointers without typedefs, and this complicates the work of the template, 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 worked using function references on maps, not boost :: function

+4
source share
1 answer

You cannot use member function pointers as regular function pointers, unless member functions are static. Instead, use a boost bind with a specific instance of the object:

boolConfigMap["USE_NEW_VERSION"] = boost::bind(&Config::useNewVersion, someInstanceOfConfig);

, () - ( -), , - "zeroeth", this .

, boost::function , ,

boost::function<bool()>

, bool .


, std::function std::bind.


, -: ,

(config.*configIter->second)();
+3

All Articles