tags:

views:

174

answers:

3

I have a class that my client uses to Get() a packet. The packet contains a std::vector whose type is not known until the packet is generated internally in my Interface class (in this example, it depends on the Packet::type variable).

I was wondering if template could be used for this since the Packet class is just a generic class whose type can be pretty much anything.

The problem with it, as far as I can think of, is that the client has no clue what type of packet it is until he gets the packet and look at the Packet::type member. So, this wouldnt work because he wouldnt be able to declare a variable that Get() would return (?)

Could template be used elegantly in this case?

One alternative I could think of is to define a base class, and create a child class for each type. Then, the Get() method could return a pointer to the base class. Then client can simply look at the Packet::type (packet->type) and cast it to be the appropriate child class. But that's a bit messy? Is there a more elegant solution?

The code below demonstrates the scenario roughly:

enum
{
  T_FLOAT,
  T_COMPLEX
} TYPE_ENUM;

// T can either be of type float or std::complex
template<typename T>
class Packet
{
public:
  TYPE_ENUM       type;
  std::vector<T>  data;
};

class Interface
{

public:
  // Method that client calls to obtain the packet
  Packet<>  Get()
  {
    return queue.pop(); // return current packet in queue
  }

private:
  Queue<Packet>   queue;
};
+3  A: 

You would want to create a base class, and keep the template Packet class that you have. Call this base class PacketBase. Your Packet class would derive from the new PacketBase class. That is to say each Packet<> type that is generated at compile time, will derive from PacketBase.

class PacketBase
{ 
};

// T can either be of type float or std::complex or ...
template<typename T>
class Packet : public PacketBase
{
public:
  std::vector<T>  data;

  //Put other members you need here
  //Note you don't need the type member that you had before. 
};

Interface::Get would return a PacketBase*. Here PacketBase is just used as a generic name that holds any Packet<> type. The queue would store a collection of PacketBase*.

class Interface
{

public:
  // Method that client calls to obtain the packet
  PacketBase*  Get()
  {
    return queue.pop(); // return current packet in queue
  }

private:
  Queue<PacketBase*>   queue;
};

To figure out which type of Packet you have you can use RTTI with dynamic_cast.

InterfaceObject o;
//fill the queue
PacketBase *pPacket = o.Get();
if(dynamic_cast<Packet<float> * >(pPacket) != NULL)
   ;//You have a Packet<float>
else if(dynamic_cast<Packet<std::complex> * >(pPacket) != NULL)
   ;//You have a Packet<std::complex>
//... for each Packet<> type you have

You can also beef up your PacketBase with some virtual methods. Then you can call those directly instead of the RTTI dynamic_cast stuff.

Brian R. Bondy
+8  A: 

Templates are all about compile time type resolution ... if you can't determine the type until runtime then you don't have a good fit for applying templates.

You will need to have the runtime switch on the final packet type as you've described.

Rob Walker
+5  A: 

Use an abstract base class and virtual methods for the operations on the packet type. You shouldn't need to cast the packet class. A collection of templated classes is problematic - you will have trouble adding and removing items from it.

1800 INFORMATION