Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same outer structure only one difference between functions

Tags:

c++

c++11

I have many functions that do roughly the same apart from the what variable the modify

struct example
{
    std::string name;
    std::string category;
};

using ObjName = std::string;
using Value = std::string;

bool updateName(const ObjName &name, const Value& value) ...
bool updateCategory(const ObjName &name,const Value& value)
{
    //  boost optional pointing to struct reference
    auto obj = findOjb(name);
    if (obj)
    {
        obj.get().category = value; // variable name changes 
        return true;
    }
    return false;
}

What I am wondering is what I can do to combine the code ? I suspect it will involve templates maybe traites/functors but I am unsure of how to approach it any ideas ?

like image 201
gda2004 Avatar asked Jul 16 '26 08:07

gda2004


2 Answers

Reworking Daerst's code to remove that awful offsetof in favor of pointers-to-members...

struct example
{
    std::string name;
    std::string category;
};

bool updateVariable(const ObjName &name, std::string example::*member, std::string const &value)
{
    // your code ...

    // Access
    rule.get().*member = value

    // rest of your code
}

bool updateName(const ObjName &oldname, const ObjName& newName)
{
    return updateVariable(name, &example::name, newName));
}

bool updateCategory(const ObjName &name, Category &cat)
{
    return updateVariable(name, &example::category, cat));
}
like image 164
Quentin Avatar answered Jul 17 '26 22:07

Quentin


You could use lambdas:

template <typename Accessor>
bool updateVariable(const ObjName& name, const Value& value, Accessor access) {
    auto obj = findObj(name);
    if (obj)
    {
        access(obj.get()) = value;
        return true;
    }
    return false;
}
bool updateCategory(const ObjName& name, const Value& value) {
    return updateVariable(name, value,
        [](Example& e) -> Value& { return e.category; });
}

This is a bit more flexible than the pointer-to-member solution. You can make it even more flexible by having the lambda do the setting instead of returning a reference.

like image 45
Sebastian Redl Avatar answered Jul 17 '26 23:07

Sebastian Redl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!