English 中文(简体)
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:

  • 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)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签