I have the following data types:
typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;
typedef struct {
int id ;
char *label
Value value ;
size ;
}StructA ;
Does anyone know how I can serialize StructA into a raw memory block and
calculate the number of bytes of the memory block?
Someone out there should surely know how to do this - if even they don't
know how to do it, but can point me to a reference (my Google searches
have not come up with anything remotely useful) - I'd be most grateful
mtia 9 10779
Hi,
I would do it as follows:
At a serialize data function (in your case taking a class with void* and
writeptr to keep track of the written bytes or char* I usually use an
ostream istream for it).
Serialize( const CMyStream& MyStream )
{
if( MyStream.Write )
{
memcpy( MyStream.BasePt r + MyStream.Writte n, &l, sizeof l );
MyStream.Writte n += sizeof( l );
memcpy( MyStream.BasePt r + MyStream.Writte n, &l, sizeof f );
MyStream.Writte n += sizeof( f );
}
else
{
// Copy the other way around
}
}
// TODO: You probably don't want another class accessing the data members of
MyStream directly i.e. hide stuff
// Optional: Take care of little endian big endian conversion
// Optional: Take care of different sizes of longs for different
architectures
// Optional overload << operators for every type
I myself usually use ostream and istream which allows me directly to write
to files (ofstream derived) or memory (stringstream derived) or just to the
console ostream itself
--
Regards, Ron AF Greve http://moonlit.xs4all.nl
"Alfonso Morra" <sw***********@ the-ring.com> wrote in message
news:dh******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com... I have the following data types:
typedef union { long l ; double f; char* s ; void* p ; } Value ;
typedef struct { int id ; char *label Value value ; size ; }StructA ;
Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
Someone out there should surely know how to do this - if even they don't know how to do it, but can point me to a reference (my Google searches have not come up with anything remotely useful) - I'd be most grateful
mtia
Alfonso Morra wrote: I have the following data types:
typedef union { long l ; double f; char* s ; void* p ; } Value ;
typedef struct { int id ; char *label Value value ; size ; }StructA ;
Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
Someone out there should surely know how to do this - if even they don't know how to do it, but can point me to a reference (my Google searches have not come up with anything remotely useful) - I'd be most grateful
You cannot serialize pointers. You can only serialize what they are
pointing to. You haven't told us what your pointers are pointing to so
it's hard to answer the question. I can guess what the char* pointers
point to but the void* pointer is worrying me.
john
> Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
This part of your question is easy though. Use an ostringstream.
ostringstream buf;
buf.write( ... );
buf.write( ... );
buf.write( ... );
buf.write( ... );
....
string block = buf.str();
block.data(); // the raw data
block.size(); // the size of the raw data
> You cannot serialize pointers. You can only serialize what they are pointing to. You haven't told us what your pointers are pointing to so it's hard to answer the question. I can guess what the char* pointers point to but the void* pointer is worrying me.
john
If the char* pointer is not worrying me, it certainly is bothering me,
because you can tell if that is a char array or a sole pointer to a single
char.
Of course void* is heaps worse.
Ben
Err, remove const of course :-)
--
Regards, Ron AF Greve http://moonlit.xs4all.nl
"Moonlit" <news moonlit xs4all nl> wrote in message
news:43******** *************** @news.xs4all.nl ... Hi,
I would do it as follows:
At a serialize data function (in your case taking a class with void* and writeptr to keep track of the written bytes or char* I usually use an ostream istream for it).
Serialize( const CMyStream& MyStream ) { if( MyStream.Write ) { memcpy( MyStream.BasePt r + MyStream.Writte n, &l, sizeof l ); MyStream.Writte n += sizeof( l ); memcpy( MyStream.BasePt r + MyStream.Writte n, &l, sizeof f ); MyStream.Writte n += sizeof( f );
} else { // Copy the other way around }
}
// TODO: You probably don't want another class accessing the data members of MyStream directly i.e. hide stuff // Optional: Take care of little endian big endian conversion // Optional: Take care of different sizes of longs for different architectures // Optional overload << operators for every type
I myself usually use ostream and istream which allows me directly to write to files (ofstream derived) or memory (stringstream derived) or just to the console ostream itself --
Regards, Ron AF Greve
http://moonlit.xs4all.nl
"Alfonso Morra" <sw***********@ the-ring.com> wrote in message news:dh******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...I have the following data types:
typedef union { long l ; double f; char* s ; void* p ; } Value ;
typedef struct { int id ; char *label Value value ; size ; }StructA ;
Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
Someone out there should surely know how to do this - if even they don't know how to do it, but can point me to a reference (my Google searches have not come up with anything remotely useful) - I'd be most grateful
mtia
John Harrison wrote: Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
This part of your question is easy though. Use an ostringstream.
ostringstream buf; buf.write( ... ); buf.write( ... ); buf.write( ... ); buf.write( ... ); ... string block = buf.str(); block.data(); // the raw data block.size(); // the size of the raw data
John Harrison wrote: Does anyone know how I can serialize StructA into a raw memory block and calculate the number of bytes of the memory block?
This part of your question is easy though. Use an ostringstream.
ostringstream buf; buf.write( ... ); buf.write( ... ); buf.write( ... ); buf.write( ... ); ... string block = buf.str(); block.data(); // the raw data block.size(); // the size of the raw data
Hi John,
This is the most useful response I have had so far (and that includes
responses from the c.lang ng).
The char* are ptrs to C (null terminated) strings. You can ignore the
void* for now. I just want to get an overview of how to go about
streaming a structure with nested structures/pointers. An example using
a struct that contains a char* alone will suffice. For example, how
would I stream this struct:
struct named_pair_ {
int id ;
char* key ;
}
typedef struct {
int id ;
char *label1 ;
char *label2;
struct named_pair_ *np ;
} structA ;
Could you please elaborate with a little bit more code?. I'm not
familiar with ostringstream and references I have looked up for the
write() method, do not map easily to the "code" you provided above.
mtia
John Harrison wrote: You cannot serialize pointers. You can only serialize what they are
A little bit off-topic: You can, with some effort:
http://upp.sourceforge .net/src$PtePtr$en-us.html
http://upp.sourceforge .net/reference$Ptr.h tml
Mirek
Here's something I came up with that mirrors what John Harrison is
referring to with respect to ostringstream. The getBuffer function has
since been re-written to use a vector but this might provide some
clues. Of course the experts will comment if I'm way off so ... ;)
#ifndef DATA_STREAM_H
#define DATA_STREAM_H
# include <iostream>
# include <string>
# include <map>
# include <typeinfo>
# include <sstream>
///////////////////////////////////////////////////
// data_stream
///////////////////////////////////////////////////
struct data_stream
{
std::string classId;
std::string buffer;
size_t curPos;
data_stream(std ::string clsId)
: classId(clsId), curPos(0)
{}
data_stream(cha r* buf, size_t size)
: curPos(0)
{
std::string temp(buf, size);
size_t pos = temp.find(':');
if (pos == std::string::np os || 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 data_stream& operator<<(data _stream& ds, const T& t)
{
std::ostringstr eam out;
out<<t;
ds<<out.str();
return ds;
}
template <class T>
inline data_stream& operator>>(data _stream& ds, T& t)
{
if (ds.isValid()) {
std::string s;
ds>>s;
std::istringstr eam in(s);
in>>t;
}
return ds;
}
inline data_stream& operator<<(data _stream& ds, const std::string& s)
{
if (ds)
{
ds.buffer += s;
ds.buffer += '\0';
ds.curPos = ds.buffer.size( );
}
return ds;
}
inline data_stream& operator>>(data _stream& ds, std::string& s)
{
if (ds)
{
s = &ds.buffer[ds.curPos];
ds.curPos += s.size();
if (ds.curPos < ds.buffer.lengt h() &&
ds.buffer[ds.curPos] == '\0')
ds.curPos++; // add terminating zero
}
return ds;
}
#endif
Depending on wethere I'm transmitting or receiving data..... Lets
assume I'll transmit data.
std::string buffer("served_ data:"); // test data
buffer+="2";
buffer+='\0';
buffer+="5.0";
buffer+='\0';
buffer+="Terlim bom bom";
buffer+='\0';
data_stream st (&buffer[0], buffer.length() );
if (st)
{
//
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure containing pointers
into a "flattened" bit stream.
Here is my code (it dosen't work - compiles fine, pack appears to work,
but unpack retrieves...
|
by: Alfonso Morra |
last post by:
I have the following data types:
typedef union {
long l ;
double f;
char* s ;
void* p ;
} Value ;
typedef struct {
|
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure containing pointers
into a "flattened" bit stream.
Here is my code (it dosen't work).
I would be grateful for any feedback that helps fix this. My...
|
by: Alfonso Morra |
last post by:
Hi,
I am at the end of my tether now - after spending several days trying to
figure how to do this. I have finally written a simple "proof of
concept" program to test serializing a structure containing pointers
into a "flattened" bit stream.
Here is my code (it dosen't work - compiles fine, pack appears to work,
but unpack retrieves...
|
by: Just D |
last post by:
Hi,
I need to write a serialization (to XML string) and restoring (from XML
string) of a very complicated object. The object uses a few classes, one
class has two ArrayLists, etc. The general structure supposes to have 2-3
levels.
What's easier, to write a serialization method for each class used by the
main class and a short method to...
| |
by: Uma - Chellasoft |
last post by:
Hai,
I am new to VB.Net programming, directly doing socket programming. In C, I
will be able to map the message arrived in a socket directly to a structure.
Is this possible in VB.Net. Can anyone please help me with some sample
codings and guidance?
Can you also suggest some other news group available for socket programming
in VB.Net?
|
by: Steve Richter |
last post by:
KeyValuePair<string,stringhas a ToString method that returns the
KeyValue pair seperated by a comma and enclosed in :
Is this method used as a building block for serialization?
The reason I ask is because I dont see a "FromString" or "Parse"
static method that takes a string as an argument and returns a
KeyValuePair<string,stringobject.
|
by: =?Utf-8?B?UGlnZ3k=?= |
last post by:
Hi to all,
I am getting this System.OutOfMemoryException calling the
Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method.
The type of <streamis IO.MemoryStream
=====Exception:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException'
was
thrown.
|
by: Andrew Smallshaw |
last post by:
I'm working on a data structure that began life as a skip list
derivative, but has evolved to the point that it now only has a
passing resemblance to them. Each node of this structure has a
few constant size variables (ints and the like), an array holding
the actual data (chars in this case) and a random number of pointers
to other nodes in...
|
by: dantz |
last post by:
hi, I would just like to know if there is a way to manually calculate the size of my structure to anticipate the file size of the object after serialization?
Is it reliable to depend on the file size of the usual result after serializing a certain structure? I mean, if I serialize an object then get the size of the result, will the result on...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |