Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pimpl idiom without using dynamic memory allocation

we want to use pimpl idiom for certain parts of our project. These parts of the project also happen to be parts where dynamic memory allocation is forbidden and this decision is not in our control.

So what i am asking is, is there a clean and nice way of implementing pimpl idiom without dynamic memory allocation?

Edit
Here are some other limitations: Embedded platform, Standard C++98, no external libraries, no templates.

like image 279
erelender Avatar asked Feb 07 '11 13:02

erelender


People also ask

Is Pimpl a bridge pattern?

The bridge pattern can also be thought of as two layers of abstraction. When there is only one fixed implementation, this pattern is known as the Pimpl idiom in the C++ world.

Is dynamic memory on the stack?

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.


2 Answers

Warning: the code here only showcases the storage aspect, it is a skeleton, no dynamic aspect (construction, copy, move, destruction) has been taken into account.

I would suggest an approach using the C++0x new class aligned_storage, which is precisely meant for having raw storage.

// header class Foo { public: private:   struct Impl;    Impl& impl() { return reinterpret_cast<Impl&>(_storage); }   Impl const& impl() const { return reinterpret_cast<Impl const&>(_storage); }    static const size_t StorageSize = XXX;   static const size_t StorageAlign = YYY;    std::aligned_storage<StorageSize, StorageAlign>::type _storage; }; 

In the source, you then implement a check:

struct Foo::Impl { ... };  Foo::Foo() {   // 10% tolerance margin   static_assert(sizeof(Impl) <= StorageSize && StorageSize <= sizeof(Impl) * 1.1,                 "Foo::StorageSize need be changed");   static_assert(StorageAlign == alignof(Impl),                 "Foo::StorageAlign need be changed");   /// anything } 

This way, while you'll have to change the alignment immediately (if necessary) the size will only change if the object changes too much.

And obviously, since the check is at compilation time, you just cannot miss it :)

If you do not have access to C++0x features, there are equivalents in the TR1 namespace for aligned_storage and alignof and there are macros implementations of static_assert.

like image 62
Matthieu M. Avatar answered Oct 06 '22 12:10

Matthieu M.


pimpl bases on pointers and you can set them to any place where your objects are allocated. This can also be a static table of objects declared in the cpp file. The main point of pimpl is to keep the interfaces stable and hide the implementation (and its used types).

like image 35
jdehaan Avatar answered Oct 06 '22 12:10

jdehaan