1

I have a class (acting as a wrapper over a library) with member functions that all follow this pattern:

MyObject& MyObject::ObjectFunction(int arg1, std::string arg2)
{
    LibraryObject destination_object;
    libraryFunction(destination_object, arg1, arg2);
    setProp(destination_object);
    ~destination_object;
    return *this;
}

I would like to refactor this so that the repeated steps (create destination object, set property, destroy destination object, return address) can be moved into a function of their own, ideally something like this:

MyObject& MyObject::genericFunction (uniqueLibraryFunction(Args)) 
{
    LibraryObject destination_object;
    uniqueLibraryFunction(Args);
    setProp(destination_object);
    ~destination_object;
    return *this;        
}

void libraryFunction1(int arg1, std::string arg2) 
{
    genericFunction(libraryFunction1(arg1, arg2));
}

void libraryFunction2(double arg1, int arg2) 
{
    genericFunction(libraryFunction2(arg1, arg2));
}

However I am using a library which has methods that require a destination object to return values to. I have tried to use variadic arguments but I can't seem to get it working since the library functions take different argument lengths and types. I have also tried to use pointer-to-function-members but couldn't get it working for the same reason (argument lengths and types differ between library functions).

My class code is the following:

class MyObject 
{
    private:
        LibraryObject prop;
    public:
        getProp();
        setProp(int prop);
}
5
  • 1
    ~destination_object; - what is this? Commented Jan 8, 2019 at 12:17
  • 2
    why do you want to call the destructor (~destination_object;) ? The object will be destroyed twice Commented Jan 8, 2019 at 12:17
  • 1
    @user463035818: it won't be, it's definitely not an explicit call to destructor, it's more like an unary operator~ Commented Jan 8, 2019 at 12:19
  • 2
    @AndriyTylychko yes it is not calling the destructor, though from what OP is writing it seems like this line is supposed to destroy the object Commented Jan 8, 2019 at 12:20
  • Your code is illogical... You declare setProp without argument but you call it with one, and you declare libraryFunction1 and libraryFunction2 as standalone function but you access genericFunction which is a member-function of MyObject.... Please provide a working example. Commented Jan 8, 2019 at 12:56

1 Answer 1

4

If I understand the question correctly, then it should be doable with a lambda:

template <class F>
MyObject& MyObject::genericFunction(F f) 
{
    LibraryObject destination_object;
    f(destination_object);
    setProp(destination_object);
    return *this;        
}

void MyObject::libraryFunction1(int arg1, std::string arg2) 
{
    genericFunction([=](LibraryObject &o) { libraryFunction1(o, arg1, arg2); });
}

void libraryFunction2(double arg1, int arg2) 
{
    genericFunction([=](LibraryObject &o) { libraryFunction2(o, arg1, arg2); });
}

Alternatively, std::bind will do the same job:

void MyObject::libraryFunction1(int arg1, std::string arg2) 
{
  genericFunction(std::bind(libraryFunction1, std::placeholders::_1, arg1, arg2));
}

The template shouldn't be an issue, since if it's private, it could easily be implemented just in the source file where all the calls to it reside. However, if that's not an option for you for whatever reason, you can use std::function<void(LibraryObject&)> instead of F.

Sign up to request clarification or add additional context in comments.

Comments

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.