c++ - Better way of using an opaque pointer for Pimpl -


I write a C ++ wrapper library around several hardware libraries for embedded systems (firmware level) using several libraries I am from the different vendors (C or C ++) should be an apiartist, which is the API vendor fronted by the header file ... All Vendor Header Libraries are not included in any of my header files.

I have a general pattern that is making the seller member data opaque, using only an "Indicator" seller / struct / class / typedef / pod type for an indicator.

  // myclass.h class MyClass {... private: vendoring * vendor data; };  

and implementation (note: each implementation vendor is specific; all are same * .h file)

  // myclass_for_vendor_x.cpp #include " Seller ".h" ... {Vendor Data & gt; doSomething (); or Vendor (Seller Data, ...); or whatever  

I have the problem that < Code> vending can be many different things. It can be a class, structure, type or blur, I do not know, and I do not want to care about the header file. If you choose Incorrect, it does not compile that my header file along with the vendor title file is also included, for example, if it is in vending in "vendor.h" The actual declaration of :

  is done, typedef struct {int a; int b;} vending;  

Then you can only Vendorating can not be forwarded as class VendorThing; . I do not care what type of vending is exactly, I want it to be one The Public interface that assign it void * as the location for the thinking (ie pointer and he thinks about it), and implement it using the pointer type.

The two solutions found in QT are the "D-Pointer" method, where you VendorThing a new structure vendeting

 < Code> // myclass.h struct VendorThingWrapper; Class MyClass {... Personal: Seller Reader * Seller Dealer; };  

and in your CPP file

  // myclass.cpp #include "vendor.h" struct using vendor {seller text * vendor data; }; ... {Seller Vendor: Data-wrapper-> Seller Data- & gt; DoSomething (); }  

But it adds a second point difference, which is not a big deal, but as it is targeting embedded systems, I do not want to add that overhead, because The language is only ''

The second thing is simply declaring it as zero

  // myclass.h class MyClass {... Private: Zero * Seller Data Accredited; };  

and in implememtation

  // myclass.cpp #include "vendor.h" #define vendor data (vendor * vendor data request). . {Vendetta- & gt; DoSomething (); }  

But #define always leave a bad taste in my mouth something better should be.

You can avoid extra pointers by using:

  #include "vendor.h" struct using vendor: public vendor {};  

Of course, at that time, it makes more sense to use the name MyClassData instead of VendorThingWrapper .

MyClass.h:

struct MyClassData; Class myclass {public: myclass (); ~ MyClass (); Private: MyClassData * myClassData; };

MyClass.cpp:

Structure MyClassData: Public Vending {}; MyClass :: MyClass (): myClassData (New MyClassData ()) {} MyClass :: ~ MyClass () {myClassData; }

Update

I was able to compile and create the following program. Unknown struct is not a problem.

  struct MyClassData; Class myclass {public: myclass (); ~ MyClass (); Private: MyClassData * myClassData; }; Typedef Structure {int a; Int b; } Vendorating; Structure MyClassData: Public Vending {}; MyClass :: MyClass (): myClassData (New MyClassData ()) {myClassData- & gt; A = 10; MyClassData-> B = 20; } MyClass :: ~ MyClass () {myClassData; } Int main () {}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -