Member Avatar for gadgets1010

Hi. I'm trying to implement uniform interfaces for two breakout boards (adafruit's fona and sparkfun's Si4703 breakout) and I'm not sure how to go about writing wrapper classes. Can someone guide me through the basics of accomplishing this task?

Recommended Answers

All 2 Replies

Well when you make a wrapper class you are basically redoing the public facing side of the class. Lets say the class has a bunch of everloads that you never want to be seen. What your wrapper class would provide is the one function you want and it would forward it on to the actual class.

clas Complexe
{
public:
    int foo(int);
    int foo(short);
    int foo(long);
    int foo(long long);
    //...
};

class Wrapper
{
private:
    Complexe hiddenClass
public:
    // this is the only one I want to see
    int foo(int someInt)
    {
        return hiddenClass.foo(someInt);
    }
    //...
};

So you can see from aboce we will still use the complexe class but wrapper will provided a more trimmed down interface. You can also do this for blocks of function to give them a more object look and feel.

Wrapper classes don't have to encapsulate another class, they can for instance encapsulte a legacy C interface or in your case a propriatory interface to some board.

If you are trying to implement the same wrapper interface for 2 different boards it is worth using an interface class so that the majority of you code just deals with the common interface.

class Interface
{
public:
  virtual ~Interface(){} // Virtual destructor for class designed as a base

  virtual int initialiseBoard() = 0;

  virtual int setRegister(int register, unsigned value) = 0;
}

class Fona : public Interface
{
public:
  Fona();

  virtual ~Fona();

  virtual int initialiseBoard();

  virtual int setRegister(int register, unsigned value);
}

int Fona::initialiseBoard()
{
  int result = 0;
  // Code to send initialisation parameters and commands to the board

  return result;
}

All you code can deal with the Interface class via references and pointers you instantiate the classes you need (possibly using a factory class or function).

However having seen the 2 boards you want to interface to, a GSM module and an FM radio it seems to me that their interfaces may have little in common but I haven't actually checked the data sheets.

commented: If they have little in common, does that make this task very difficult or does it make it impossible? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.