A simple example to illustrate writing an abstraction layer in C ++

I have only rudimentary knowledge in C ++. I am trying to implement a hardware abstraction layer (HAL) in C ++. Suppose I want to implement this Data class. Based on the platform, data can be sent wired or wireless.

class Data() { public Data() { //create random data } public sendData() { // send data } public platform_action1() { // do some platform specific action } 

}

 // My HAL int HAL() { Data myData; myData.platform_action1(); myData.sendData(); return 0; } 

Now, if I have two wired and wireless platforms, how can I extend this class and organize my files so that HAL() does not change.

Also, I do not need dynamic binding using the keyword "virtual". In my case, the platform is known at compile time.

//I do not want to do this:)...

 int HAL() { Data* data = new WiredData(); data.sendData(); data = new WirelessData(); data.sendData(); } 

In my case, the platform is known at compile time.

From world C, this is a simple task, like filling pointers to specific platforms.

Take, for example, the thread class in the Boost C ++ API. The class automatically spawns threads by calling either the Windows threading API or the platform-based Linux threading API. So my HAL is really platform independent.

  • Thank you, Chris
0
c ++
source share
4 answers

This is more a design issue than a real C ++ question, but the term you are looking for is polymorphism . You can take your Data class and create two classes that inherit its WiredData and `WirelessData, this will allow you to do something like this:

 Data data1 = new WiredData(); Data data2 = new WirelessData(); data1.sendData(); data2.sendData(); 
Polymorphism

comes into play when you call sendData() on the data1 and data2 objects, the compiler will call the sendData() method for each particular subtype, even if they are declared as a Data type

+1
source share

From world C, this is a simple task, like filling pointers to specific platforms.

This is almost the same in C ++. You can create the sendData () function of the virtual data class (the virtual keyword prefix for signing), and then output the Wired and Wireless implementation classes that specify the corresponding sendData() functionality. Then you have some if statement in which you decide which one to use and save the Data* variable in the Wired or Wireless object as needed ... when you call pointer->sendData() , it will call the corresponding implementation. It's all very simple - you have to do a couple of introductory C ++ tutorials online or grab a book. There are other stackoverflow related issues that list recommended training materials.

Edit: The schema as requested in your comment below.

 class Wrapper { Data* p_; public: void sendData() { if (not p_) p_ = ... ? new Wired() : new Wireless(); p_->sendData(); } } 
+1
source share

Why not put definitions for members that change to files: -

Data_Windows.cpp and Data_Unix.cpp (for example), and then use your assembly system to include only the desired file in your assembly on this platform? Or am I missing something here ...?

0
source share
 Can we do this using PIMPL(Private Implementation) approach? This is what I am thinking ... // In Data.h class PlatformDataProcess;           // forward declaration of Pimpl class Data { public:  Data (const IPC& ipc); // process IPC  ~Data();  Data( const Data &rhs );  // undefined for simplicity  Data& operator=( Data );  void  process_ipc(); private:  PlatformDataProcess *pimpl_;        // the Pimpl }; // In Wired.cpp #include "Data.h" class PlatformDataProcess { public:  void SendData()  {// send data on wired} }; // In Data.cpp Data::Data()  :  pimpl_( new PlatformDataProcess() ) { } Data::~Data() {  delete  pimpl_; } void  Data::SendData() {  pimpl_->SendData();    // do some private work } int HAL() { // receive IPC Data* data = new Data(ipc); data->SendData(); } So all the user needs to do is supply the platform specific file like wired.cpp . 
0
source share

All Articles