Refactoring function calls while reducing duplication of resulting class definitions

I have a header file with approximately 400 function declarations and the corresponding source definition file.

To replace the implementation with a layout at runtime, I want to replace the implementation with calls to an object that will contain the implementation instead (the pointer to the implementation is pImpl).

This means that I will have the following files:

  • mainFile.h - contains method declarations, as before (should remain, since I can not replace the interface with client code)
  • IImpl.h - Abstract base (interface) for the implementation object
  • mainFile.cpp - Will contain method definitions, where all this it calls the corresponding method on IImpl *
  • SpecificImpl.cpp - contains a declaration and definition of a specific implementation
  • MockImpl.cpp - contains a declaration and definition of the implementation used during testing

The main problem is duplicating the 400-tag declarations that appear in the main header file and again in each class definition.

Is there any way to avoid this duplication? I tried with macros, but then the order of inclusions became too specific ...

+3
source share
5 answers

Something like that:

//=======================================
// Macro definition of method list
//=======================================
#define METHOD_LIST(ABSTRACT) \
    virtual void Foo1() ABSTRACT; \
    virtual void Foo2() ABSTRACT; \
    virtual void Foo3() ABSTRACT

//=======================================
// Declaration of Abstract base class
//=======================================
class IBase
{
public:
    METHOD_LIST( = 0);
};

//=======================================
// Declaration of Specific
//=======================================
class CSpecific : public IBase
{
public:
    METHOD_LIST();
};

//=======================================
// Declaration of Mock class
//=======================================
class CMock : public IBase
{
public:
    METHOD_LIST();
};

Updating ...

If you want to make it even more macrocritical, you can change the macro to:

#define METHOD_LIST(VIRTUAL, ABSTRACT) \
    VIRTUAL void Foo1() ABSTRACT; \
    VIRTUAL void Foo2() ABSTRACT;

, - :

:

METHOD_LIST(virtual, =0)

:

METHOD_LIST(virtual, ;)

:

METHOD_LIST(;, ;)

, "g++ -M", .

+3

IDE .

API ( , ). , 3-4 . ... , , , , .

, . wikipedia.

+1

- ; IDE . , Eclipse CDT . .

- , pin. , .

UPDATE:

, , , pin - ( ). - - . , Aspect ++.

0

, SpecificImpl MockImpl.

0

, // . ( )

0

Source: https://habr.com/ru/post/1722271/


All Articles