Converting to using templates

Discussion in 'C++' started by tech, Apr 16, 2008.

  1. tech

    tech Guest

    Hi, i have the following design issue. In our app we use different
    codecs to encode/decode packets of data
    I have defined a base codec class as follows

    class CCodec
    {
    public:
    CCodec() {};
    virtual ~CCodec(){};

    virtual unsigned char Encode(short ibuf ) = 0;

    virtual short Decode(unsigned char ibuf) = 0;
    };

    std::vector<unsigned char> m_SendBuffer; // defined in Voip class

    bool Voip::SendData(signed short* buf, size_t size)
    {
    for(int i = 0; i != ENC_BUF_LEN; ++i)
    {
    SendBuffer = m_codec->Encode(buf); // m_Codec points to
    concrete implementation
    }

    // then SendDataToNetwork(SendBuffer)

    }

    ENC_BUF_LEN = size of the SendBuffer vector

    What i need to do is send a packet of data to the network in the
    SendData function
    which is given some data, i've got type short above but it could be
    any type ie char, long etc

    the Encode function takes a short and returns a char (as thats what
    compression does) however
    as we will support different codecs the signature of this function
    could vary, it would probably always
    return a char but its input parameter could vary like with Send data.

    The above is hardcoded to use shorts but i need something generic ,
    can this be done with templates
    some how?
    tech, Apr 16, 2008
    #1
    1. Advertising

  2. tech

    Noah Roberts Guest

    tech wrote:
    > Hi, i have the following design issue. In our app we use different
    > codecs to encode/decode packets of data
    > I have defined a base codec class as follows
    >
    > class CCodec
    > {
    > public:
    > CCodec() {};
    > virtual ~CCodec(){};
    >
    > virtual unsigned char Encode(short ibuf ) = 0;
    >
    > virtual short Decode(unsigned char ibuf) = 0;
    > };
    >
    > std::vector<unsigned char> m_SendBuffer; // defined in Voip class
    >
    > bool Voip::SendData(signed short* buf, size_t size)
    > {
    > for(int i = 0; i != ENC_BUF_LEN; ++i)
    > {
    > SendBuffer = m_codec->Encode(buf); // m_Codec points to
    > concrete implementation
    > }
    >
    > // then SendDataToNetwork(SendBuffer)
    >
    > }
    >
    > ENC_BUF_LEN = size of the SendBuffer vector
    >
    > What i need to do is send a packet of data to the network in the
    > SendData function
    > which is given some data, i've got type short above but it could be
    > any type ie char, long etc
    >
    > the Encode function takes a short and returns a char (as thats what
    > compression does) however
    > as we will support different codecs the signature of this function
    > could vary, it would probably always
    > return a char but its input parameter could vary like with Send data.
    >
    > The above is hardcoded to use shorts but i need something generic ,
    > can this be done with templates
    > some how?


    What you seem to be saying is that SendData could have data other than
    shorts and that the codec thing could be the same.

    template< typename T >
    bool SendData(T * buf, size_t s)
    {
    ....buffer = encode->Encode(buf);
    }

    Same with CCodec except your encoder is probably going to have to work
    with sizeof(T) instead of whatever hard coded value you have in there.

    There are actually numerous options for doing the CCodec class,
    including having it, itself be a template, its function Encode be a
    template, or Encode being overridden per type.

    This seem pretty basic, so maybe I don't understand the question.
    Noah Roberts, Apr 16, 2008
    #2
    1. Advertising

  3. tech

    tech Guest

    Thanks, but how i can have a virtual template Encode function

    for example i have the following G711 concrete implmentation

    unsigned char CG711ULAW::Encode(signed short data )
    {
    return linear2ulaw(data);
    }

    i can't do this can i?
    template<typename T>
    virtual unsigned char Encode(T ibuf ) = 0;
    tech, Apr 16, 2008
    #3
  4. tech

    Noah Roberts Guest

    tech wrote:
    > Thanks, but how i can have a virtual template Encode function
    >
    > for example i have the following G711 concrete implmentation
    >
    > unsigned char CG711ULAW::Encode(signed short data )
    > {
    > return linear2ulaw(data);
    > }
    >
    > i can't do this can i?
    > template<typename T>
    > virtual unsigned char Encode(T ibuf ) = 0;
    >
    >


    Right, you can't do that.

    You either have to make your class hierarchy templated, or you'll need
    to make Encode functions for all types that might go through the
    SendData function. You'll want to enforce that with some kind of
    metafunction and boost::enable_if, if possible (you're not constrained,
    as I once was, from using boost).

    You could look up "Non-Virtual Interface" too...I don't know what help
    it will offer.

    A templated class structure would look like so:

    template < typename T >
    class Top
    {
    public:
    unsigned char(T elem) const = 0;
    // this function is probably not changing the class? const
    }:

    template < typename T >
    class Bottom : public Top<T>
    {
    public:
    unsigned char(T elem) const { ...implement...}
    };

    There are other, more complex structures I can think of that could solve
    problems like this, but in the end it's something you need to do
    specific to your needs. So look into the above, NVI, and possibly the
    curiously reoccurring template pattern as tools that you could build a
    solution from.
    Noah Roberts, Apr 16, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    581
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    375
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    453
  4. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    357
    tom_usenet
    Aug 4, 2004
  5. recover
    Replies:
    2
    Views:
    788
    recover
    Jul 25, 2006
Loading...

Share This Page