473,624 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<uns igned charm_SendBuffe r; // 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 SendDataToNetwo rk(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 1487
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<uns igned charm_SendBuffe r; // 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 SendDataToNetwo rk(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::Enco de(signed short data )
{
return linear2ulaw(dat a);
}

i can't do this can i?
template<typena me 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::Enco de(signed short data )
{
return linear2ulaw(dat a);
}

i can't do this can i?
template<typena me 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_i f, 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
1519
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 am using is simply changing the file extension from lbi to asp and adding an "inc_" prefix. So a file named "rm_investors.lbi" becomes "inc_rm_investors.asp" The results are partially succesfull but I'm noticing that some of the transparent...
1
3348
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" xmlns="http://www.w3.org/TR/REC-html40">H<Sub>3</Sub><Font>PO</Font><Sub>4 </Sub></ss:Data></Cell> <Cell ss:StyleID="s24"><Data ss:Type="String">GC</Data></Cell> <Cell ss:StyleID="s24"><ss:Data ss:Type="String"
2
3397
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 happening when I put my XML files and the .xsl files on my ISP's system for my home page. If I try to open the XML files in Netscape 7.0 on my own machine (ie, not on the ISP's system), the pages convert file and the result is displayed in HTML.
3
22450
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? If you know it you can send me a message to ckjonny5@hotmail.com Thanks at all. Jonny
3
9908
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 ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
5
1885
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 Unbound;\ }
2
2360
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 XSLT, but it doesn't work correctly :-( Example:
6
3995
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 in c) { uint n = Convert.ToUInt32(a.acquisitionID);
1
1697
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 I want to replace all headings, I could do for each heading level (i.e. repeat the following code for h2, h3, h4, h5, etc.): <xsl:template match="h1"> <h1><xsl:apply-templates/></h1> </xsl:template>
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.