Overview
Features
Download
Documentation
Community
Add-Ons & Services

*Impl stuff

A general discussion forum.

*Impl stuff

Postby cx500tc » 04 Jan 2008, 21:05

Poco is a great C++ learning too for me.

That being said I have been trying to understand the rational behind the usage of the *Impl in many Poco object:

For example Pipe has a pointer to PipeImpl, same goes for Socket which has a socket to SocketImpl and so forth. Why this design choice versus inheritance.

At first I though this was a way to speed up compilation by using a forward declaration of the Implement object but each .h file includes it's Impl.h counterpart so that's not it.

- Mario

cx500tc
 
Posts: 26
Joined: 24 Sep 2007, 17:48

Re: *Impl stuff

Postby guenter » 04 Jan 2008, 22:21

The reason why the pImpl idiom is used for sockets and pipes is that Impl objects are reference counted. This allows us to use a Socket or Pipe objects as value objects, supporting copy constructor and assignment.

Code: Select all

StreamSocket s1("localhost", 1234);
StreamSocket s2 = s1;
StreamSocket s3;
...
s3 = s1;


Basically, Socket, StreamSocket, Pipe, etc. are nothing more than fancy reference counting smart pointers for the respective *Impl objects. The main advantage is cleaner code, greater ease of use and less proneness to certain programming errors.
guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: *Impl stuff

Postby guenter » 05 Jan 2008, 10:12

The #include "*Impl.h" is mostly necessary because we are using inline forwarding member functions like

Code: Select all

inline int Pipe: :writeBytes(const void* buffer, int length)
{
   return _pImpl->writeBytes(buffer, length);
}
guenter
 
Posts: 1107
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron