473,669 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialisation of STL-container - fails with maps...

Hi,

a collegue of mine is trying to write a serialisable container (reads at
construction, writes at destruction).
The writing part is pretty easy: simply iterate through the container and
write into the file.
Reading from the file is (should) be easy too:
while you're getting data from the file (stream) insert it into your
container.
Unfortunatly this fails with the container map, because the
map::value_type 's key is const :-/

This is the stripped code:

#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>

template< class containerT >
class MyContainer : public containerT
{
public:
void read()
{
typename containerT::val ue_type val;

/* normally:
read from file and make the assigment,
this is simply for demonstration
*/
val = val;

/* insert into container */
insert(end(), val);
}
};
template< class containerT >
void foo()
{
MyContainer< containerT > cont;
cont.read();
}

int main(int, char**)
{
/* compiles */
foo< std::vector< int > >();
foo< std::list< char > >();
foo< std::set< char > >();
foo< std::deque< long > >();

/* fails */
foo< std::map< char, int > >();
return 0;
}
Any ideas, how to modify the code for being able to assign to the value and
add to the container??

The read function would look something like this:

void read()
{
std::ifstream stream(m_path.c _str());
while (!stream.eof())
{
typename containerT::val ue_type val;
stream >> val;
insert(end(), val);
}
}

Thanks in advance :)
Jorge
Jul 19 '05 #1
7 2632
Jorge Schramm wrote:
Hi,

a collegue of mine is trying to write a serialisable container (reads
at construction, writes at destruction).
The writing part is pretty easy: simply iterate through the container
and write into the file.
Reading from the file is (should) be easy too:
while you're getting data from the file (stream) insert it into your
container.
Unfortunatly this fails with the container map, because the
map::value_type 's key is const :-/ [SNIP] Any ideas, how to modify the code for being able to assign to the
value and add to the container??

[SNIP]

Try to find it. If found, erase it. Then insert. This is the way to do it
with map and set.

--
Attila aka WW
Jul 19 '05 #2
Hi Attila,

thanks for your answer.
Try to find it. If found, erase it. Then insert. This is the way to do
it with map and set.


Unfortunately this is not the problem :-/ The line with the assignment fails
compiling for std::map.

| typename containerT::val ue_type val;
| val = val; // fails
This is because the value_type of map is a pair, having its key *const*.
I'd like to know an alternative :)

Jorge
Jul 19 '05 #3
Jorge Schramm wrote:
Hi Attila,

thanks for your answer.
Try to find it. If found, erase it. Then insert. This is the way
to do it with map and set.


Unfortunately this is not the problem :-/ The line with the
assignment fails compiling for std::map.
typename containerT::val ue_type val;
val = val; // fails


This is because the value_type of map is a pair, having its key
*const*. I'd like to know an alternative :)


The right eay to do it (or alternative as you call it) is to search for the
key part. If you find it, delete it. Then insert the new.

PSEUDO CODE!

void read()
{
typedef typename containerT::val ue_type value_type;
typedef typename containerT::ite rator iterator;
// read key and value
iterator it = this->find(key);
if (key != this->end()) {
this->erase(key);
}
this->insert(end() , make_pair(key,v alue));
}

BTW do *not* inherit from the conatiner! Have it as a member. By
inheriting from it you wonder to two-phase name lookup land and *all* the
names coming from the container has to be prefixed with typename or this->
to make sure it will work.

--
Attila aka WW
Jul 19 '05 #4
Attila Feher wrote:
BTW do *not* inherit from the conatiner! Have it as a member. By
inheriting from it you wonder to two-phase name lookup land and *all*
the names coming from the container has to be prefixed with typename
or this-> to make sure it will work.


Update. You will still need typename before the types if it is a member.
But since it is a member you will not run into trouble by leaving out the
this-> before the member function calls. If you do leave out (and the
compiler "starts" to support two phase name lookup) you can end up calling
the std::find instead of the members etc. In addition your container class
is not a map (if I understand it right), it is implemented in terms of a map
(or whatever container you use).

BTW I suggest you read Scott Meyers Effective STL and Item #2, Beware of the
illusion of container independent code.

--
Attila aka WW
Jul 19 '05 #5
On Tue, 07 Oct 2003 13:42:25 +0200, Jorge Schramm
<jo***********@ web.de> wrote:
Hi,

a collegue of mine is trying to write a serialisable container (reads at
construction , writes at destruction).
The writing part is pretty easy: simply iterate through the container and
write into the file.
Reading from the file is (should) be easy too:
while you're getting data from the file (stream) insert it into your
container.
Unfortunatly this fails with the container map, because the
map::value_typ e's key is const :-/


Here's a version that works on MSVC7.1. It won't work on earlier
versions, since they don't support partial specialization, which is
required to solve this problem (there may be an MSVC6 solution - I'll
have a fiddle).

Container persistence systems have been done before. You might want to
check out progress of the boost serialization library (see the files
section of www.boost.org)

#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <fstream>

template <class T>
struct value_traits
{
typedef T value_type;
typedef T non_const_value _type;

static std::istream& get_from_stream (std::istream& is,
non_const_value _type& t)
{
is >> t;
return is;
}
};

template <class T, class U>
struct value_traits<st d::pair<T const, U> >
{
typedef std::pair<T const, U> value_type;
typedef std::pair<T, U> non_const_value _type;
static std::istream& get_from_stream (std::istream& is,
non_const_value _type& t)
{
is >> t.first >> t.second;
return is;
}
};

template< class containerT >
class MyContainer : public containerT
{
public:
std::string m_path;

void read()
{
std::ifstream stream(m_path.c _str());
typedef value_traits<ty pename containerT::val ue_type> traits;
typename traits::non_con st_value_type val;
while (traits::get_fr om_stream(strea m, val))
{
this->insert(this->end(), val);
}
}
};
template< class containerT >
void foo()
{
MyContainer< containerT > cont;
cont.read();
}

int main(int, char**)
{
/* compiles */
foo< std::vector<int > >();
foo< std::list<char> >();
foo< std::set<char> >();
foo< std::deque<long > >();

/* fails */
foo< std::map<char, int> >();
return 0;
}
Jul 19 '05 #6
Hi tom_usenet,

thanks! Works on Linux with gcc 3.2.2 too ;)
Nice idea using partial specialisation! !

Jorge
Jul 19 '05 #7
tom_usenet wrote:
Here's a version that works on MSVC7.1. It won't work on earlier
versions, since they don't support partial specialization, which is
required to solve this problem (there may be an MSVC6 solution - I'll
have a fiddle).


*argh* my collegue uses msvc 6.0 :/

We made a workaround with a load-policy. It's not very nice but it works.

template< class containerT, typename loadPolicyT = defaultLoadPoli cy >
class MyContainer : public containerT
{
void read()
{
std::ifstream stream(m_path.c _str());
loadPolicy::loa d(stream, *this);
}
}
Thx,
Jorge
Jul 19 '05 #8

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

Similar topics

2
2021
by: unknow | last post by:
I want to do a serialisation in pure C++ but i don't know how. Do you have some useful links ? Thanks
1
1234
by: Pierre Couderc | last post by:
I want to serialise quickly a "simple" vector : (simple is to say with basic types and no pointers) such as : class c { int i,j; double z; }
2
1755
by: msnews.microsoft.com | last post by:
Hi Here is another EASY question When you serialise an object in .NET, serialisation adds defaut attributes that I dont care EXEMPLE : <root_test xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1
1838
by: lobrys | last post by:
Hi everybody here is a question I have this class that a want to serialize : Public Class BOO <XmlAttributeAttribute()> Public THING As String <XmlElementAttribute("param")> Public p() As param End Class
2
1385
by: lobrys | last post by:
Hi everybody what are the objets for the folowing XML serialisation : (I have problem with "name" and "datatype") <vehicule type="car"> <name datatype="String">Megane</name> </inventory> I Have this :
1
1801
by: McGiv | last post by:
Hi, I'm trying to serialise some objects and I've can't get the built in serialisation to output exactly what I want. For the moment I'm implementing the IXmlSerializable interface and doing it the long way. For future reference is it possible to specify how a property should be formatted when being serialised? Example: In the following code I want the Time property to be formatted not to
1
1150
by: BrentonMCA | last post by:
I want to be able to serialise an object and then pass the serialisation text as a string to a Web service without serialising to a file first. I also want to be able to deserialise from the text without using a file. How can I do these? Brenton
2
1054
by: gary | last post by:
Hi, When I serialise a class to XML which has properties I sometimes have properties that are like this - public double SpeedMPH { get { return (double)Math.Round(windSpeed*2.23693629,2); } }
2
2593
by: ashwinij | last post by:
Hello The steps which i am doing in my program 1) I am having an xml file. 2) I am performing some updations in the file using XQueryUtil class from nux package. 3)After that i am performing Serialisation ( nu.xom.Serializer ) and storing in a file. 4)This Serialised file is being appended in a separate file. 5) Steps 3) and 4) are performed for som n no. of times The problem is whenever i serialised i get this line "<?xml...
1
2475
by: OrionLee | last post by:
I am using C# to work with a 3rd party DLL (Nevron Charts), and attempting to serialise it. The serialisation itself is handled somewhere inside the DLL, so to get it to happen you call the Nevron's serialiser and then SaveToStream() which will serialises the chart object into a stream for you, which is all well and good... Now for the problem: If I create a standalone application and use the serialiser to serialise charts it all works fine....
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
8810
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
8590
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
8659
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...
1
6211
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
5683
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
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
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.