1

Is there a nicer way of doing this

auto commodityOneLeg = boost::bind(&VegaFactory::load_commodity_one_leg,this,conn,_1);
std::map<std::string,decltype(commodityOneLeg)> methods;
methods.insert(std::make_pair("COMMODITYONELEG",commodityOneLeg));
methods.insert(std::make_pair("FXOPTION",boost::bind(&VegaFactory::load_fx_index,this,conn,_1)));
methods.insert(std::make_pair("FXBARROPT",boost::bind(&VegaFactory::load_fx_bar_opt,this,conn,_1)));

methods.insert(std::make_pair("COMMODITYINDEX",boost::bind(&VegaFactory::load_fx_index,this,conn,_1)));
auto f = methods.find(trade_table);

if(f != methods.end()) {
    fx_opt = (f->second)(t_id);
}

Is there a way of declaring the type of std:map<> without having to declare a mapping first on the previous line? I guess I mean aesthetically - Code should look neat right?

Is there a cleaner/simpler way to do this c++ string switch statement overall when the input is a 'trade type' string.

Edit

To clarify further. I can manually write out the type of the boost:bind type but that seems excessive. And this is probably a really good example of where auto and decltype can be used to simplify the code. However having to declare one entry in the map one way and the others in a different way just looks wrong; so that's what I want to address

1
  • Maybe you could use boost signals? Commented Jul 3, 2013 at 17:54

1 Answer 1

1

IMHO using Boost.Signals2 is a more clear way. There is also the Boost.Signals library but it is deprecated starting from Boost 1.54. The following code demonstrates it. I think something similar is possible to implement using the Boost.Function library too.

#include <boost/signals2.hpp>
#include <map>
#include <string>

typedef boost::signals2::signal<bool (int)> CSignal;
typedef CSignal::slot_type CSignalSlotType;
typedef std::map<std::string, CSignalSlotType> CMethodMap;

bool Func1(int a, int b) {
    return a == b;
}

bool Func2(int a, int b) {
    return a < b;
}

int main(int, char *[]) {
    CMethodMap methods;
    methods.insert(std::make_pair("Func1", boost::bind(&Func1, 1, _1)));
    methods.insert(std::make_pair("Func2", boost::bind(&Func2, 2, _1)));

    auto it = methods.find("Func1");
    if(it != methods.end()) {
        CSignal signal;
        signal.connect(it->second);
        auto rt = signal(2);
        if (rt) {
            const bool result = *rt;
        }
    }
    return 0;
}

Here is a sample code using the Boost.Function. It looks even simpler but I used to use the Signals2 library.

#include <map>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>

typedef boost::function<bool (int)> CFunction;
typedef std::map<std::string, CFunction> CMethodMap;

bool Func1(int a, int b) {
    return a == b;
}

bool Func2(int a, int b) {
    return a < b;
}

int main(int, char *[]) {
    CMethodMap methods;
    methods.insert(std::make_pair("Func1", boost::bind(&Func1, 1, _1)));
    methods.insert(std::make_pair("Func2", boost::bind(&Func2, 2, _1)));

    auto it = methods.find("Func1");
    if(it != methods.end()) {
        auto &f = it->second;
        const bool result = f(2);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now thats clean. I'll have a browse through Signals2 as I didn't know about that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.