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

Home Posts Topics Members FAQ

storing "almost-POD" classes to binary file


Hello!

Suppose I have a class that contains only public members
of builtin types and a default constructor. Because of the
default constructor it is no longer an aggregate and therefore
no longer POD, according to my understanding of

http://www.fnal.gov/docs/working-gro...x/doc/POD.html

I need to be able to serialize this class to/from a binary file
and read() and write() look like the easiest way. But if I
understand correctly, this is *not* guaranteed to work because
the class is non-POD, right?

Arrays of this class (let's call it 'data') need to be
stored/restored to/from a binary file in a templated class
datafile<data>. The class 'datafile' is a part of a library,
while 'data' is supplied by the library user who wants to store
arrays of 'data' into the file represented by 'datafile<data> '.

How would you go around this? I could remove the default
ctor and add a public init() method, which would make it POD,
but what happens if the user tries her 'data' class that
contains non-POD members, therefore making it non-POD.

Perhaps I should require that the data class supplies
some methods like serialize_to_by tes() and restore_from_by tes()?
That would be easy for the library, but annoying for users
who would then instead of simple

class data {
public:
int myint;
double mydouble;
long int mylong;
};

have to use

class data {
public:
int myint;
double mydouble;
long int mylong;

void serialize_to_by tes(...) {
// ... quite a few lines of code
}

void restore_from_by tes(...) {
// ... quite a few lines of code
}
};

Or perhaps storing/reading classes like that is safe even
though they contain members that are not strictly POD because
they have constructors? I am assuming no members contain
static data, there are also no pointers or pointers-to-members
involved.

TIA,
- J.
Dec 18 '05 #1
2 1975
On Sun, 18 Dec 2005 18:49:17 +0100, Jacek Dziedzic
<jacek@no_spam. tygrys.no_spam. net> wrote:

Hello!

Suppose I have a class that contains only public members
of builtin types and a default constructor. Because of the
default constructor it is no longer an aggregate and therefore
no longer POD, according to my understanding of

http://www.fnal.gov/docs/working-gro...x/doc/POD.html

I need to be able to serialize this class to/from a binary file
and read() and write() look like the easiest way. But if I
understand correctly, this is *not* guaranteed to work because
the class is non-POD, right?

Arrays of this class (let's call it 'data') need to be
stored/restored to/from a binary file in a templated class
datafile<data> . The class 'datafile' is a part of a library,
while 'data' is supplied by the library user who wants to store
arrays of 'data' into the file represented by 'datafile<data> '.

How would you go around this? I could remove the default
ctor and add a public init() method, which would make it POD,
but what happens if the user tries her 'data' class that
contains non-POD members, therefore making it non-POD.

Perhaps I should require that the data class supplies
some methods like serialize_to_by tes() and restore_from_by tes()?
That would be easy for the library, but annoying for users
who would then instead of simple

class data {
public:
int myint;
double mydouble;
long int mylong;
};

have to use

class data {
public:
int myint;
double mydouble;
long int mylong;

void serialize_to_by tes(...) {
// ... quite a few lines of code
}

void restore_from_by tes(...) {
// ... quite a few lines of code
}
};

Or perhaps storing/reading classes like that is safe even
though they contain members that are not strictly POD because
they have constructors? I am assuming no members contain
static data, there are also no pointers or pointers-to-members
involved.

TIA,
- J.


I think the only workable idea is the one requiring users to implement
or overload the serialization functions themselves. Even for POD
structs, there might be byte padding and/or alignment issues, and the
default case where there is no padding is trivial enough to implement
that it isn't really worth putting into a library.

--
Bob Hairgrove
No**********@Ho me.com
Dec 18 '05 #2

Jacek Dziedzic wrote:
Hello!

Suppose I have a class that contains only public members
of builtin types and a default constructor. Because of the
default constructor it is no longer an aggregate and therefore
no longer POD.


Yep. OTOH, if you put all the data members in a base class, that
base class will be a POD. You can serialize that class. Deserializing
is a bit trickier. You can deserialize a temporary and then copy it C++
style over the base object (or write another ctor that takes a base
const&)

HTH,
Michiel Salters

Dec 19 '05 #3

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

Similar topics

18
2542
by: Lorem Ipsum | last post by:
interesting! I just found a page in which Explorer's View Source does nothing! How did they do that?
9
4053
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked with binary files before so I'm confused with how to store the binary file into memory. What sort of...
14
3764
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL character is encountered. But when my program runs it gets a SIGSEGV (Segmentation vioalation) signal. Whats wrong? And is there a better way than mine to solve this task (most likely)
17
2397
by: Peter Oliphant | last post by:
In the 'old days', we could create a pointer to an instance of a variable like so: int i = 58 ; int* i_ptr = &i ; int j = *i_ptr ; // j = 58 Now, in /clr how do we do the same? That is, if I replace '*' with '^' what do I replace '&' with to generate a 'pointer' (is 'x^' called a 'reference' per chance?) to the instance? That is:
2
3431
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage() fileitem = form if fileitem.file: # It's an uploaded file; count lines
6
3192
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to me like there were performance issues at the time. How about the different types? The MS docs I would expect Access to differentiate and handle appropriately (i.e. .DOC and .XLS).. but how about ..PDF? and can I stash a .TXT document in the...
3
7072
by: MIUSS | last post by:
Hello everyone! I got a problem with creating new line... I tried this: lstrcpy(NewLineIdr, TEXT("\r\n")); I already tried this: NewLineIdr = 0x000D; NewLineIdr = 0x000A; NewLineIdr = 0x0000; I need it to use it in my saving procedure because I need to create tabullary text file. But my effort usually results in one or two small
2
4713
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes which Inherits MarshalByRefObject (ok, at this stage it only contains one class... but its gonna grow) - Class Library containing common classes and interfaces that will be shared between all projects. This include interfaces for the server...
3
7542
by: rustyhowell | last post by:
I'm trying to store binary data in a sqlite database and call into the db using pysqlite 3. What I've got so far is this: import sqlite con = sqlite.connect(DB_PATH) cur = con.cursor() query = """create table t1( ID INTEGER PRIMARY KEY, data BLOB );"""
17
2775
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
0
8466
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
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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
8813
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7412
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...
1
6212
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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 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.