mardi 8 janvier 2019

Refactoring where unique functions have different arguments

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();
}

Aucun commentaire:

Enregistrer un commentaire