473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Converting to using templates

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 charm_SendBuffer; // defined in Voip class

bool Voip::SendData(signed short* buf, size_t size)
{
for(int i = 0; i != ENC_BUF_LEN; ++i)
{
SendBuffer[i] = m_codec->Encode(buf[i]); // 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?
Jun 27 '08 #1
3 1478
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 charm_SendBuffer; // defined in Voip class

bool Voip::SendData(signed short* buf, size_t size)
{
for(int i = 0; i != ENC_BUF_LEN; ++i)
{
SendBuffer[i] = m_codec->Encode(buf[i]); // 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[i] = encode->Encode(buf[i]);
}

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.
Jun 27 '08 #2
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;
Jun 27 '08 #3
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.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rob McLennan - ZETLAND | last post by:
Hi, I am in the process of converting Dreamweaver library items to server side includes (SSI) on a website made up of ASP files. The pages I am modifying are Dreamweaver templates. The process I...
1
by: Bill Sneddon | last post by:
I am using an XML file produced by doing a save-as in Excel. The file has content that looks like one of these three examples lines: <Cell ss:StyleID="s24"><ss:Data ss:Type="String"...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
3
by: Jonny Au | last post by:
Hi everyone, I have a problem about email message format converting. I want to write a program in VB.NET to convert the EML format email message to MSG format, does anyone knows how to do it?...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
5
by: Andreas Micheler | last post by:
Hi, I have several long complex C macros in the math module of aUCBLogo like #define _XFUNC_primitive(NAME)\ NodeP _##NAME(PairPP arg)\ { ((ObjectP)(arg))->var()->NAME( arg );\ return...
2
by: jkflens | last post by:
Hello, i convert one XML-document by using XSLT into another XML-document. First change all attributes to elements is no problem. Then i try to insert a new element into the new document by...
6
by: damian | last post by:
Problem: I am looking to reduce my code size because I have many very simliar functions e.g.: private uint GenerateAcquisitionID(AcquisitionType c) { uint max = 0; foreach (AcquisitionType a...
1
by: Izhaki | last post by:
Hi, I'm creating a system where my XML includes HTML tags (<h1></h1>) in addition to other XML elements (<book></book>). I would like to render the HTML tags back to HTML using XSL. Considering...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.