开发者

Refactoring function calls while reducing code duplication of resulting class definitions

I have a header file with about 400 function declarations and its corresponding source file with the definitions.

In order to replace the implementation with a mock at runtime, I want to replace the implementation with calls to an object that will contain the implementation instead (pointer to implementation - pImpl).

This means I will have the following files:

    开发者_StackOverflow中文版
  • mainFile.h - contains the method declarations as before (must remain as I cannot replace the interface with the client code)
  • IImpl.h - Abstract base (interface) for the implementation object
  • mainFile.cpp - Will contain method definitions where all it does it call the corresponding method on IImpl*
  • SpecificImpl.cpp - Contains the declaration and definition of the specific implementation
  • MockImpl.cpp - Contains the declaration and definition of the implementation used during testing

The main problem with this is the duplication of the 400 method declarations which appear in the main header file and again in each class definition.

Is there any way I can avoid this duplication? I was trying with macros, but then the order of the includes became too specific...


What about something like this:

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

Update...

If you want to make it even more macro-cryptic, you may change the macro to:

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

and this will allow you to declare the regular function list that is not part of any object too:

For abstract class:

METHOD_LIST(virtual, =0)

For derived class:

METHOD_LIST(virtual, ;)

For regular function list:

METHOD_LIST(;, ;)

If you need to debug this mess, then I recommend using 'g++ -M' to see the result of the preprocessor .


Use a pure virtual class as an Interface and a good IDE with refactoring support.

Pure virtual classes are like an API contract (in other languages sometimes called Interface). You declare a method once and then inherit from it in the 3-4 other classes. You only change a method in the Interface and it gets changed in the dependent classes...or at least those won't compile any more and you know that you have to make the change there as well.

Read more about this e.g. in wikipedia.


One way to do it is using refactoring tools; most modern IDEs have that. For example, the Eclipse CDT has a refactor option. Gerd also suggested one.

Another way is to use a code instrumentation tool like pin. These tools are used mainly for profiling, but might suit your purpose.

UPDATE:

If your problem isn't much about writing code, but more at maintainence, I think pin is a decent option (run-time change). Another way is to use aspect oriented programming. For example, Aspect C++.


Write a little code generator that takes the method declarations from the main header file and copies them to the SpecificImpl and MockImpl header files.


If this is a common problem, then learn to use templates, and look into generic/generative/meta- programming techniques. (teh rabbit-hole is deep)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜