473,563 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

type t determination - operator << and >>


Consider

#include <iostream>
#include <string>
#include <map>

using namespace std;

struct dstream // data_stream class
{
string classId;
string buffer;
size_t curPos;

dstream(string clsId)
: classId(clsId), curPos(0){}

dstream(char* buf, int size)
: curPos(0)
{
string temp(buf, size);
//int pos = temp.find(':');
size_t pos = temp.find(':');

if (pos == string::npos || pos == 0 || pos == size-1)
return;
classId = temp.substr(0, pos);
buffer = temp.substr(pos +1);
}

bool isValid() { return curPos <= buffer.length() ; }
operator bool() { return isValid(); }

size_t getBufferSize() {
return isValid()? classId.size() + 1 +
buffer.size() : 0;
}

size_t getBuffer(char* & buf, size_t siz)
{
size_t sizNeeded = getBufferSize() ;
size_t sizClass = classId.size();
if (buf == NULL || siz < sizNeeded)
{
delete buf;
buf = new char[sizNeeded];
}
memcpy(buf, classId.c_str() , sizClass);
buf[sizClass] = ':';
memcpy(&buf[sizClass+1], buffer.c_str(), buffer.size());
return sizNeeded;
}
};

template <class T>
inline dstream& operator<<(dstr eam& ds, const T& t) { //// (1)
if (ds.isValid())
{
size_t curPos = ds.curPos;
ds.curPos += sizeof(T);
ds.buffer.resiz e(ds.curPos);
memcpy(&ds.buff er[curPos], &t, sizeof(t));
}
return ds;
}

// non const version of above the same .

template <class T>
inline dstream& operator>>(dstr eam& ds, T& t) { //// (2)
if (ds.isValid())
{
size_t curPos = ds.curPos;
ds.curPos += sizeof(t);
if (ds.isValid())
memcpy(&t, &ds.buffer[curPos], sizeof(t));
}
return ds;
}

// non const version of above the same .
// since bool is 4 bytes on GCC and 1 on .NET,
// maybe we shouldn't use them
// then again we' could also turn them into characters and back
//
inline dstream& operator>>(dstr eam& ds, bool& b) {
if (ds.isValid()) // perhaps i should just do 'if (ds)'
{
unsigned char uc;
ds >> uc;
//b == (uc) ? true : false;
b == (uc != '\0') ? true : false;
}
return ds;
}

// and so on
inline dstream& operator>>(dstr eam& ds, const bool& b) {
}
inline dstream& operator>>(dstr eam& ds, bool& b) {
}
inline dstream& operator>>(dstr eam& ds, const bool& b) {
}
inline dstream& operator>>(dstr eam& ds, string& s) {
}

// now comes the factory ...
class factory;
typedef factory* (*CreateInstanc eFunc)( void );

class factory
{
// factory map is mapping class names to create functions
static map<string, CreateInstanceF unc> factoryMap;
public:
virtual string getClassId() = 0;
virtual bool loadFromStream( dstream& ds) = 0;
virtual bool storeToStream(d stream& ds) = 0;

static factory* createInstance( const dstream& ds)
{ }

static bool registerClass(c onst string& className, CreateInstanceF unc
createFunc)
{
}
dstream* store() {}
};

// now we're ready
class outgoing_msg : public factory
{
// test all the types
int i;
double d;
string s;
char c;
bool b;
public:
outgoing_msg() : i(1), d(2.), s("abc"), b(false), c('X')
{
// call register class on the incoming_msg side.
}

string getClassId() { return "outgoing_m sg"; }

bool loadFromStream( dstream& ds)
{
ds >> i >> d >> s >> b >> c;
return ds.isValid();
}
bool storeToStream(d stream& ds)
{
ds << i << d << s << b << c;
return ds.isValid();
}
};

int main()
{
// create an instance of outgoing_msg.
// fill objects (i, d, s, etc with 'stuff' );;
// callt he store member function.
// Now we have a bucket of bits for transmittal across the pipe
}

For simplicity I trimmed most of the code and I do hope I didn't
provide too much. That said at issue is the lines marked ' (1) and
(2). The operators >> and << are called for types int, long, double
and character.

I need a way to determine if the type (hence t) is int, long or double?
How would I achieve that. The reason I'm asking centers around calling
the byteSwap routine (below) on those types.
#include <algorithm> //required for std::swap

#define ByteSwap5(x) ByteSwap((unsig ned char *) &x,sizeof(x) )

void ByteSwap(unsign ed char * b, int n)
{
register int i = 0;
register int j = n-1;
while (i<j)
{
std::swap(b[i], b[j]);
i++, j--;
}
}

so now - pseudo code
if (typeid = int)
byteSwap( (unsigned char*)t , sizeof(int));
////////////
One other thing:
if (ds.isValid()) //(1)

if (ds) //(2)

In theory options 1 and 2 are the same. So far so good? That said, I'm
reminded of the day when I first saw.

if ( cin >> x)

It puzzled me cause I wondered why operator>> was returning a pointer.
I soon learned that operator<< returns a reference to iostream and that
one could test a class object on true or false by simply providing an
operator bool() member fucntion.

I still have trouble sometimes when i look at these operators,
nonetheless, what's the use of (1). I could easily get rid of option
1?
Thanks in advance for the help.

Jul 23 '05 #1
0 1396

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

Similar topics

2
10548
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
4
2213
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the amount my operator has for bother cin , cout operator<< and >> thanks Dan
3
2031
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
1
2035
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
1
3162
by: Mike Strieder | last post by:
How can i get the text of the System.Type e.g. "base64Binary" from the .Net type "byte" I can not found any Function to give back this Schematype as string. thx for your help
3
2752
by: | last post by:
I have been researching articles on google on how to create a simple RSS feed that sucks <title><blurb><link><date> out of a sql server 2000 database via an aspx page. I know it has to be pushed into a <xml> document but not sure which direction to take. Is there perhaps a starter document which uses sql server as the data source I can...
2
2239
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include...
6
1781
by: iLL | last post by:
Okay, I’m just a little confused on exactly what the system is doing when I say: #include <iostream> class test { private: int i; public:
3
3357
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above...
0
7885
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. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
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...
0
5213
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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...

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.