tly
tly

Reputation: 1262

Creating .cpp files from headers with inline methods and classes

I have made a bunch of headers that directly implement methods and classes. I know that this is not the best pattern, I did it mainly because I was lazy and used Java before. The project that im working on is getting bigger, so the compile times explode because every recompile in the main.cpp has to recompile all the classes. This is because there arent any .o files for the classes and functions so the #include preprocessor creates a huge file with all the stuff in it.

Is there a way to automatically split all these headers into .cpp and .h files and move the implementations of all functions and classes into the .cpp ?

My solution:

I created a program in java to "parse" the code with regex to find function declarations and saved the function bodies for use in the .cpp file. then I replaced the bodies with an ; and copied the function declarations in a StringBuilder and appended the body. To find the class names for ClassName:: declarations I used regex too.

My regex:

Function declarations: (?m)(?!.*[\"}])(^.*\\S+ ([a-zA-Z0-9_+\\-=\\/*^\\[\\]]+)\\s*\\(.*\\)\\s*(const)?\\s*)\\{

human-readable: "Lines that dont start with } or ", but with a random type name \\S+ followed by a space, then the function name [a-zA-Z0-9_+\\-=\\/*^\\[\\]]+, an undefined parameter list (.*), a optional const and finally a {.

Luckily this is the way i formatted my code :D

Upvotes: 1

Views: 139

Answers (2)

Hugh Perkins
Hugh Perkins

Reputation: 8582

I also hate tedious pointless manual work. What I do is use cogapp, which is a language-agnostic code generator. It rocks. I created a header file declaration generator for it, cog_addheaders.py. Then, I simply write the .cpp file, an initial, basically empty, .h file, and cogapp, plus my cogapp generator handles the rest :-)

For static and virtual, I add macros for STATIC and VIRTUAL, which expand to nothing in the .cpp, and static and virtual in the .h file. I've been using it for a while, and it's super easy, I'm very comfortable with it :-)

Here's some examples:

Upvotes: 1

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

"Is there a way to split all these headers into .cpp and .h files and move the implementations of all functions and classes into the .cpp ?"

You can of course move these inline function definions to separate .cpp files from the header files.

Suppose you have something like

class A {
public:
    int foo();

private:
    int x;
};

in the header, and

inline int A::foo() {
    return x;
}

in the .cpp, the compiler will anyway decide to optimize such code being inlined or not (think of the inline keyword merely as being a hint).

Unless you have class template declarations/definitions you should almost always move your definition code to separate compilation units, instead of having it in the headers (and so needing to recompile any dependent translation units using that header file upon changes in your definition).

Upvotes: 0

Related Questions