473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

cross platform: any alternatives to pimpl?

I want to design a class which will encapsulate some system specific
representation, in this case time structs:
#if unix_implementation

#include <time.h>
class Cfoo
{
private:
timespec someTime_;
public:
// various public methods
};

#endif

#if w32_implementation

#include <windows.h>
class Cfoo
{
private:
SYSTEMTIME someTime_;
public:
// various public methods
};

#endif

I'd like to have a single header with no #if/#ifdef and with no system
specific headers being included (eg windows.h).

The obvious way of doing this is with a pimpl, keeping all the system
specific things in the implementation class. However, in some
implementations, someTime_ will be an unsigned long. I don't want the
overhead of a pimpl (dynamic allocations and forwarding functions) for
the sake of managing 4 bytes of data. Nor do I want to get involved
with fixed-sized allocators.

Is there a better way of doing this? It occurs to me that there ought
to be a way of doing this with templates, since template code is only
compiled as needed.

--
Simon Elliott http://www.ctsn.co.uk
Oct 20 '05 #1
4 3485

Simon Elliott wrote:
I'd like to have a single header with no #if/#ifdef and with no system
specific headers being included (eg windows.h).
... The obvious way of doing this is with a pimpl, keeping all the system
specific things in the implementation class. However, in some
implementations, someTime_ will be an unsigned long.
The keyword here is "some". Since it may be different in "others" and
even different in the future, the pimpl idiom is great for keeping the
implementation details out of the header and being ready for the future
(or other platforms).
I don't want the overhead of a pimpl (dynamic allocations and
forwarding functions) for the sake of managing 4 bytes of data.
The "overhead" is rather minimal - especially when you deal with 4
bytes of data as you say. Also, are you sure that this part of your
program will be "time critical"? Sounds a lot like premature
optimization to me, which you should shy away from.

The benefit of a clean header file and implementation freedom far
outweight any overhead IMHO.
Is there a better way of doing this?


For API clarity, platform independence and future proofing - no! ;)

Cheers,
Andre

Oct 20 '05 #2

Simon Elliott wrote:
I want to design a class which will encapsulate some system specific
representation, in this case time structs:
#if unix_implementation

#include <time.h>
class Cfoo
{
private:
timespec someTime_;
public:
// various public methods
};

#endif

#if w32_implementation

#include <windows.h>
class Cfoo
{
private:
SYSTEMTIME someTime_;
public:
// various public methods
};

#endif

I'd like to have a single header with no #if/#ifdef and with no system
specific headers being included (eg windows.h).

The obvious way of doing this is with a pimpl, keeping all the system
specific things in the implementation class.


class Cfoo
{
private:
union {
unsigned long ul;
CfooImpl *ptr;
} impl;
public:
// Some public methods ...
};

The Windows implementation of Cfoo will use a class derived from
CfooImpl to wrap a SYSTEMTIME struct. The CfooImpl pointer is stored in
the union member Cfoo::impl.ptr.
The unix implementation of Cfoo can use the union member Cfoo::impl.ul
to store the epoch time.

Since it's a union, you save space without #ifdef.

Off topic remark: do you think you should use analogous time
representations? The Win32 analogy of the 32 bit unix time is the 64
bit hundreds of nanoseconds thing (FILETIME) since some date in 16xx.
SYSTEMTIME gives you a ``broken down'' time, similar to the standard C
``struct time'' which you can use in Windows or POSIX environments.

It would make sense to either use a ``broken down'' time representation
on both platforms, where you can extract the year, month, day, etc, or
use a numeric representation on both platforms which lends itself to
displacement arithmetic. The similarity in semantics will probably be
easier to maintain the classes in parallel that way, since similar
things will be done in similar ways.

Oct 20 '05 #3
Simon Elliott wrote:
The obvious way of doing this is with a pimpl, keeping all the system
specific things in the implementation class. However, in some
implementations, someTime_ will be an unsigned long. I don't want the
overhead of a pimpl (dynamic allocations and forwarding functions) for
the sake of managing 4 bytes of data. Nor do I want to get involved
with fixed-sized allocators.

Is there a better way of doing this? It occurs to me that there ought
to be a way of doing this with templates, since template code is only
compiled as needed.


class Cfoo
{
private:
union {
unsigned long ul;
CfooImpl *ptr;
} impl;
public:
// Some public methods ...
};

The Windows implementation of Cfoo will use a class derived from
CfooImpl to wrap a SYSTEMTIME struct. The CfooImpl pointer is stored in

the union member Cfoo::impl.ptr.

The implementation of Cfoo which packs time into an unsigned long can
use the union member Cfoo::impl.ul.

Since it's a union, you save space without #ifdef.

Oct 20 '05 #4
On 20/10/2005, Kaz Kylheku wrote:
class Cfoo
{
private:
union {
unsigned long ul;
CfooImpl *ptr;
} impl;
public:
// Some public methods ...
};

The Windows implementation of Cfoo will use a class derived from
CfooImpl to wrap a SYSTEMTIME struct. The CfooImpl pointer is stored
in the union member Cfoo::impl.ptr.
The unix implementation of Cfoo can use the union member Cfoo::impl.ul
to store the epoch time.
Interesting idea. I hadn't considered using a union.
Since it's a union, you save space without #ifdef.

Off topic remark: do you think you should use analogous time
representations? The Win32 analogy of the 32 bit unix time is the 64
bit hundreds of nanoseconds thing (FILETIME) since some date in 16xx.
SYSTEMTIME gives you a ``broken down'' time, similar to the standard C
``struct time'' which you can use in Windows or POSIX environments.

It would make sense to either use a ``broken down'' time
representation on both platforms, where you can extract the year,
month, day, etc, or use a numeric representation on both platforms
which lends itself to displacement arithmetic. The similarity in
semantics will probably be easier to maintain the classes in parallel
that way, since similar things will be done in similar ways.


Absolutely. I used SYSTEMTIME as a (bad) example of what I was trying
to achieve. For W32 I may well just use GetTickCount() as the elapsed
time will never exceed its limits.
--
Simon Elliott http://www.ctsn.co.uk
Oct 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
0
by: demibee | last post by:
Thought this might be of interest to some out there... For those who've never encountered it, it's a free, open-source, cross-platform GUI library (Windows/Mac/Linux). Forte's Agent 2.0 will be...
6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
7
by: Charles | last post by:
I'd like to develop a simple cross-platform application in C++. I'd like it to run in Windows, OS X, PC-BSD and Linux. From my research, it seems I should use Qt or Gtk as a graphical library. Do...
6
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
2
by: Graham Reitz | last post by:
What are good strategies for selecting, either at run-time or compile time, various pimpl'ed implementations? While retaining the ability to switch implementations without recompiling. Boost...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.