Strange C ++ pattern to reduce compilation time

I found a template in the OpenSource code of the Tizen Project that can reduce the compilation time of the project. It is used in many places in the project.

As an example, I chose class names ClientSubmoduleSupport. It is short. Here are their sources: client_submode_support.h, client_submode_support.cpp.

As you can see client_submode_support.h, the class that does the work for is defined ClientSubmoduleSupportand client_submode_support.cppexists .ClientSubmoduleSupportImplementationClientSubmoduleSupport

Do you know this model? I am curious about the pros and cons of this approach.

+4
source share
4 answers

" ", Pimpl".

: " , "

Souce: " "

+6

, sergej, Pimpl , Bridge.

C . , ++, , C ( pro - - C).

C

C struct, :

// Foo.h:
#ifndef FOO_H
#define FOO_H

struct Foo* foo_create(void);
void foo_destroy(struct Foo* foo);
void foo_do_something(struct Foo* foo);

#endif

// Foo.c:
#include "Foo.h"

struct Foo 
{
    // ...
};

struct Foo* foo_create(void)
{
    return malloc(sizeof(struct Foo));
}

void foo_destroy(struct Foo* foo)
{
    free(foo);
}

void foo_do_something(struct Foo* foo)
{
    // Do something with foo state.
}

Pimpl, pro C. C private structs, struct . , , .

++ private, , , - Pimpl, C UDT class .

, , , , , , , :

[Opaque Pointer]-------------------------->[Internal Data Fields]

... , , , , - - .

, . , .

, , , ( C, , ++, , ). , , , , ( ) /.

, Java, ( ).

:

( ) , , O (1), . , Foos, , , Foo () ( ).

:

, , , , Foo ( Foo) Foo . , , , , Foo .

(, , ) Pimpl, . , , . , , , . , , - , ( , ) .

" , ! ! choppa!" - , - .

, class struct . .

Foo , , . , , Foo , Bar.h. , Foo.h Bar.h ( Foo.cpp ). Bar.h , , , , Pimpl.

, Pimpls , . , . , .

, Foo, . Foo.cpp, , Foo, -, . , (, ) , PITA.

, . >

, , , ..

API ABI

pro ( API) - API ( , ), . , class struct , , , , ABI. :

[Plugin Developer]----------------->[Internal Data Fields]

, - , ABI , . : , , , , , ABI.

(Pimpl) , ABI.

[Plugin Developer]----->[Opaque Pointer]----->[Internal Data Fields]

... , , .

, , :

:

  • .
  • . (, , ), .
  • , / ( , , , , ).
  • ABI, , ABI.

:

  • ( ).
  • : / ( - ).
  • , , , .

TL; DR

, , , C.

+3

, , , API, -. , .

( , API )

0

Using this template to reduce compilation time is widely discussed in J. Lakos. "Large-scale software design in C ++" (Addison-Wesley, 1996).

Herb Sutter is also discussed on the merits of this approach here .

0
source

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


All Articles