Click here to Skip to main content
Click here to Skip to main content

STL Serialization Library (STL-SL)

By , 9 Oct 2006
 

A Quick User Guide

Using the STL Serialization Library (STL-SL) is a three-step process. In the first step, you declare the STL type you want to serialize using STL-SL.

value_trait < int > int_filer;
value_trait < std::list< bool > > bool_list_filer;
value_trait < std::map<std::string, std::multimap< float, bool > > > complex_stl_filer;

In the code snippet, three different types are declared. Each type has a varying degree of complexity. int_filer is the simplest of all, where an int is parameterized in the value_trait template class. int_filer can therefore be used to serialize and load an int from a serialization file.

Similarly, the declaration of bool_list_filer prepares it for serializing a list of bool types. And complex_stl_filer prepares a map of string and multimap data types where the multimap holds float, bool pairs as shown above.

In the next step, a file_interface is created that requires a serialization file name where the serialization data will be written-to or read-from.

stl_trait_writer file_interface (serialization_filename);

Note that file_interface does not require the type during declaration, meaning STL objects of different types may be serialized in the same serialization file.

Also, since the serialization file-interface declaration is not related to the data-file declaration, the above two steps may take each other's place.

In the third and the last stage, data is serialized into the file or loaded from it.

filer.serialize ( stl_object, file_interface );

filer.load ( stl_object, file_interface );

The following snippet illustrates the use of all the three steps described above:

//declare serialization-file interfaces
stl_trait_writer file_interface ( ".\\serialization-file.txt");

//declare the STL object 
std::vector < int > stl_object;


//declare the data filer 
value_trait < std::vector < int > > data_filer;


//...populate data in stl_object...// 
//serialize data in stl_object in the file pointed by file_interface
data_filer.serialize ( stl_object, file_interface );

//-- or --//

//load data in the file pointed by file_interface in stl_object STL object
data_filer.load ( stl_object, file_interface );
//...use data in stl_object...//

STL Serialization Library

This article presents a set of template classes capable of serializing STL objects in the user defined file format (by default, a text file).

The STL Serialization Library is composed of two major parts: Serialization Filer and Serialization Template Classes.

Serialization Filer

This consists of the following three classes:

//Serialization-file writer class.
//This guy writes the data to the file specified by 'file_path'.
//NOTE: This class does not recognize the data objects containing spaces, tabs, 
// new-line characters in them. This may be fixed by overloading '<<' operator and 
// adding escape-sequencing logic in it.
class stl_trait_writer: public std::ofstream
{
public:
      stl_trait_writer(const std::string& file_path):std::ofstream(file_path.c_str())
      {}
};

//Serialization-file reader class.
//This guy reads the data from the file specified by 'file_path'.
//NOTE: This class does not recognize the data objects containing spaces, tabs, 
// new-line characters in them. This may be fixed by overloading '>>' operator and 
// adding escape-sequencing logic in it.
class file_trait_reader: public std::ifstream
{
public:
      file_trait_reader(const std::string& file_path):std::ifstream(file_path.c_str())
      {}
};

//Serialization filer class.
//This guy presents the set of reader, writer objects responsible for reading and 
// writing to the serialization file.
template <class writer_trait , class reader_trait>
class filer_trait
{
public:
      typedef typename writer_trait writer_type;
      typedef typename reader_trait reader_type;
};

The stl_trait_writer and file_trait_reader classes provide the basic file I/O mechanism. The filer_trait class is a filer that only pairs the above two class types. An alternate approach may be where the filer_trait class implements the file I/O mechanism for simplicity at the cost of scalability.

The file writer class (stl_trait_writer) may be replaced with a user-class to change the serialization file format to, say for example, XML. A file reader class provided for loading the serialization data from a file (file_trait_writer) will have to be altered to a class capable of understanding the user file format.

Serialization Template Classes

The set of classes in this module comprises of the language mechanism for breaking down complex STL types into basic types and serializing/de-serializing the basic data types.

//Basic datatype serializer class.
//Triggers the read or write to the serialization file for the basic datatypes.
//NOTE: This class has been tweaked to work with the 'stl_trait_writer' class.
template <class val_trait, class val_filer_trait = 
              filer_trait<stl_trait_writer, file_trait_reader> >
class value_trait
{
public:
      typedef typename val_filer_trait::writer_type writer_trait;
      typedef typename val_filer_trait::reader_type reader_trait;

      void serialize(const val_trait& val, writer_trait &pen)
      {
            pen << val << "\n"; //a tweak for 'stl_trait_writer' class defined above.
            //pen << val; //correct code, this should replace above line of code should
                          //you choose to implement your own 'stl_trait_writer' class.

            pen.flush();
      }

      void load(val_trait& val, reader_trait &pen)
      {
            pen >> val;
      }
};

The value_trait class is responsible for serializing and loading basic data types. The code shown above is tweaked to be used with the Serialization Filer Classes described in the previous section.

The following excerpt illustrates the code that breaks down complex STL types into their primitive components.

//Sequence-list datatype serializer class.
//Triggers the read or write to the serialization file for the Sequence-list datatypes.
//This class takes care of STL types -- list, vector, stack, queue, deque
// and priority_queue
//NOTE: 'basic_string' type is not treated as sequence-list, but as basic type.
template <class sequence_list_type, class val_filer_trait >
class sequence_list_value_trait
{
public:
      typedef typename val_filer_trait::writer_type writer_trait;
      typedef typename val_filer_trait::reader_type reader_trait;

      typedef typename sequence_list_type::size_type size_type;   
      typedef typename sequence_list_type::value_type value_type;

      void serialize (sequence_list_type& val, writer_trait &pen )
      {
            value_trait<size_type, val_filer_trait> size_filer;
            size_filer.serialize (val.size(), pen);

            for(sequence_list_type::iterator i=val.begin(); i != val.end(); i++)
            {
                  value_trait<value_type, val_filer_trait> val_trait_key_filer;

                  val_trait_key_filer.serialize(*i,pen);
            }
      }

      void load (sequence_list_type& val, reader_trait &pen )
      {
            value_trait<size_type, val_filer_trait> size_reader;
            size_type val_size=0;
            size_reader.load(val_size, pen);

            for(; val_size > 0; val_size--)
            {
                  value_type element;

                  value_trait<value_type, val_filer_trait> val_trait_key_reader;

                  val_trait_key_reader.load(element, pen);

                  val.push_back(element);
            }
      }
};

The sequence_list_value_trait class breaks down the list, vector, stack, queue, deque, and priority_queue STL types to their finer component types. Thus, a vector of int type will be broken down into a list of int types in the sequence iterated by the default vector::iterator type. And the int type values are serialized by the value_trait class.

//Triggers the read or write to the serialization file for the Associative-list
// datatypes.
//This class takes care of STL types -- map, multimap, set, multiset
template <class associative_list_type, class val_filer_trait >
class associative_list_value_trait
{
public:

      typedef typename val_filer_trait::writer_type writer_trait;
      typedef typename val_filer_trait::reader_type reader_trait;

      typedef typename associative_list_type::size_type size_type;
      typedef typename associative_list_type::key_type key_type;
      typedef typename associative_list_type::mapped_type data_type;

      void serialize (associative_list_type& val, writer_trait &pen )
      {
            value_trait<size_type, val_filer_trait> size_filer;
            size_filer.serialize (val.size(), pen);

            for(associative_list_type::iterator i=val.begin(); i != val.end(); i++)
            {
                  value_trait<key_type, val_filer_trait> val_trait_key_filer;
                  value_trait<data_type, val_filer_trait> val_trait_data_filer;


                  val_trait_key_filer.serialize(i->first,pen);
                  val_trait_data_filer.serialize(i->second,pen);
            }
      }

      void load (associative_list_type& val, reader_trait &pen )
      {
            value_trait<size_type, val_filer_trait> size_reader;
            size_type val_size=0;
            size_reader.load(val_size, pen);

            for(; val_size > 0; val_size--)
            {
                  key_type key_element;
                  value_trait<key_type, val_filer_trait> val_trait_key_reader;
                  val_trait_key_reader.load(key_element, pen);

                  data_type data_element;
                  value_trait<data_type, val_filer_trait> val_trait_data_reader;
                  val_trait_data_reader.load(data_element, pen);

                  val.insert (std::pair<key_type, data_type> (key_element, data_element));
            }
      }
};

The associative_list_value_trait class is a complicated version of the sequence_list_value_trait class where in the map, multimap, set and multiset STL types are broken down into their key and data-element types and broken down further as required, or serialized.

The following extract depicts the mechanism by which a complex STL type initiates the breakdown process using the associative_list_value_trait and sequence_list_value_trait classes covered above.

//STL vector datatype serializer class.
//Triggers the read or write to the serialization file for the STL vector datatype.
template <class val_trait_key, class val_trait_data, class val_filer_trait  > 
class value_trait< std::vector<val_trait_key, val_trait_data> , val_filer_trait  >
{
public:

      typedef typename val_filer_trait::writer_type writer_trait;
      typedef typename val_filer_trait::reader_type reader_trait;

      typedef std::vector<val_trait_key, val_trait_data> vector_value_trait;

      void serialize (vector_value_trait& val, writer_trait &pen )
      {
            sequence_list_value_trait<vector_value_trait, 
                     val_filer_trait> sequence_list_value_filer;
            
            sequence_list_value_filer.serialize (val, pen);
      }

      void load (vector_value_trait& val, reader_trait &pen )
      {

            sequence_list_value_trait<vector_value_trait, 
                     val_filer_trait> sequence_list_value_reader;

            sequence_list_value_reader.load (val, pen);
      }
};

The above section of code initiates the dissolution of the vector STL type into granular components for further serialization. Similar logic is implemented for the list, stack, queue, deque, and priority_queue STL types.

//STL multimap datatype serializer class.
//Triggers the read or write to the serialization file for the STL multimap datatype .
template <class val_trait_key, class val_trait_data, class val_filer_trait  > 
class value_trait< std::multimap<val_trait_key, val_trait_data> , val_filer_trait  > 
{
public:

      typedef typename val_filer_trait::writer_type writer_trait;
      typedef typename val_filer_trait::reader_type reader_trait;

      typedef std::multimap<val_trait_key, val_trait_data> multimap_value_trait;

      void serialize (multimap_value_trait& val, writer_trait &pen )
      {

            associative_list_value_trait<multimap_value_trait, 
                        val_filer_trait> associative_list_value_filer;

            associative_list_value_filer.serialize (val, pen);
      }

      void load (multimap_value_trait& val, reader_trait &pen )
      {

            associative_list_value_trait<multimap_value_trait, 
                        val_filer_trait> associative_list_value_reader;

            associative_list_value_reader.load (val, pen);
      }
};

Like the above excerpt that dealt with fragmenting the vector type to finer components, this piece of code dissolves a multimap into its key type and data-type component for further serialization. Similar logic may be implemented for other associative containers such as map, set, and multiset.

Comments

It should be noted that the above library creates a class for every sub-type in the STL filer declaration. Although this dose not account to any extra runtime over-head, it doubles the number of classes that are serialized in a namespace. If the class count exceeds that supported by your compiler, you might want to re-factor the class hierarchy.

The above fact may be seen as a disadvantage, but it should be noted that the class count does not exert any overhead at run-time. The advantage is that STL-SL code is type safe.

STL-SL as presented here is the first revision; you may want to include the type information about the STL type that is being serialized in the serialization file, thus aiding run-time type checking in STL-SL.

Other libraries such as BOOST are available that are much more powerful but arguably not necessarily intuitive. These libraries address a multitude of other problems such as class versioning, pointer restoration, and data portability. The purpose of this article is not to replace them; this article should be seen as a light-weight, easy to use alternative. Moreover, code presented here may be viewed as an STL serialization engine for an even-fuller implementation of a serialization library.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Aniruddha Jahagirdar
Web Developer
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI want to add number of structure in a list. But library shows error?memberMaruthuj19 Oct '08 - 19:21 
I have structure(Appinfo)with common data type and another structure datatype. Number Appinfo structure to be added in a list and have to serilize. let me know is it possible to serilize with this library.
GeneralCompiling error when the data is a class typememberBing1688886 Mar '08 - 5:38 
For data set: std::set<CMyType>, I get an compiling error
 
Error 1 error C2664: 'value_trait::load' : cannot convert parameter 1 from '__w64 unsigned int' to 'CMyType &' d:\workspace\stl_sl.h 190
 
which points to
 
template <class sequence_list_type, class val_filer_trait >
class sequence_list_value_trait
{
......
 
void load_set (sequence_list_type& val, reader_trait &pen )
{
value_trait<value_type, val_filer_trait> size_reader;
size_type val_size=0;
----> size_reader.load(val_size, pen);
 
...............
}
}
 

This is on VS2005, and I have implemented operators << and >> in CMyType
 
Please help

GeneralDoes not compile with sets under VS2005membersharkwatch29 Aug '07 - 19:04 
Hi,
 
I tried using the library for the following type:
std::map< std::string, std::set< int > >
 
generating the following error:
c:\documents and settings\walter\my documents\visual studio 2005\projects\peering engine\object sharing engine\stlsl.h(98) : error C2039: 'mapped_type' : is not a member of 'std::set<_Kty>'
 
When I modified the example code from:
std::map > >
to:
std::map > >
 
I got the same error.
 
Any thoughts?
GeneralRe: Does not compile with sets under VS2005memberAniruddha Jahagirdar2 Sep '07 - 17:52 
Greetings,
 
Thanks for digging this error out!
You ran into this error because there is no mapped_type member defined in the associative list kind of stl familiy of objects.
 
std::set happens to be a sequence list kind. I am sorry its my mistake.
Following changes & quick workarounds shall get your job done.
 
But I suggest some re-work in the STL-SL design. I have come up with a better, simpler model but I am willingly waiting to find some time to put copy optimizations..
 
The fix comes as one ammendment (a fix) and one addition (a workaround) in the presented code.
 
CHANGE #1

//STL set datatype serializer class.
//Triggers the read or write to the serialization file for the STL set datatype .
template
class value_trait< std::set , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::set set_value_trait;
 
void serialize (set_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (set_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load_set (val, pen);
}
};

 
CHANGE #2
Add the following method in template class

template
class sequence_list_value_trait

{...}

void load_set (sequence_list_type& val, reader_trait &pen )
{
value_trait size_reader;
size_type val_size=0;
size_reader.load(val_size, pen);
 
for(; val_size > 0; val_size--)
{
value_type element;
value_trait val_trait_key_reader;
val_trait_key_reader.load(element, pen);
val.insert(element);
}
}

 
Works for me, should also work for you..
 
Good Luck..!
 
AJ
GeneralRe: Does not compile with sets under VS2005memberKikoa20 Nov '07 - 0:34 
Same problem for me (VS 2003). stlsl.h modified as you suggest :
 

// stlsl.h : Declares and Defines the STL Serialization Libray (STL-SL) template classes.
// Author: Aniruddha Jayant Jahagirdar (ajDOTsubscribeAT-THE-RATEgmailFULL-STOPcom)
// Date: October 2, 2006
// Revision: 0.1
 
//required for STL-SL classes
#include
#include <map>
#include
#include
#include
#include
#include
#include
 
//required for 'stl_trait_writer' and 'file_trait_reader'
#include
 

//Serialization-file writer class.
//This guy writes the data to the file specified by 'file_path'.
//NOTE: This class does not recoginse the data objects containing spaces, tabs, newline
// charactes in them. This may be fixed by overloading '<<' operator and adding
// escape-sequencing logic in it.
class stl_trait_writer: public std::ofstream
{
public:
 
stl_trait_writer(const std::string& file_path):std::ofstream(file_path.c_str())
{
 
}
};
 
//Serialization-file reader class.
//This guy reads the data from the file specified by 'file_path'.
//NOTE: This class does not recoginse the data objects containing spaces, tabs, newline
// charactes in them. This may be fixed by overloading '>>' operator and adding
// escape-sequencing logic in it.
class file_trait_reader: public std::ifstream
{
public:
 
file_trait_reader(const std::string& file_path):std::ifstream(file_path.c_str())
{
 
}
};
 
//Serialization filer class.
//This guy presents the set of reader, writer objects responsible for reading and writing
// to the serialization file.
template
class filer_trait
{
public:
 
typedef typename writer_trait writer_type;
typedef typename reader_trait reader_type;
};
 
//Basic datatype serializer class.
//Triggers the read or write to the serialization file for the basic datatypes.
//NOTE: This class has been tweaked to work with the 'stl_trait_writer' class.
template >
class value_trait
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::set set_value_trait;
 
void serialize (set_value_trait& val, writer_trait &pen )
{
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (set_value_trait& val, reader_trait &pen )
{
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load_set (val, pen);
}
 
/* void serialize(const val_trait& val, writer_trait &pen)
{
pen << val << "\n"; //a tweak for 'stl_trait_writer' class defined above.
//pen << val; //correct code, this should replace above line of code, should you impliment your own 'stl_trait_writer' class
pen.flush();
}
 
void load(val_trait& val, reader_trait &pen)
{
pen >> val;
}
*/
 
};
 
//Associative-list datatype serializer class.
//Triggers the read or write to the serialization file for the Associative-list datatypes.
//This class takes care of STL types -- map, multimap, set, multiset
template
class associative_list_value_trait
{
public:
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef typename associative_list_type::size_type size_type;
typedef typename associative_list_type::key_type key_type;
typedef typename associative_list_type::mapped_type data_type;
 
void serialize (associative_list_type& val, writer_trait &pen )
{
value_trait size_filer;
size_filer.serialize (val.size(), pen);
 
for(associative_list_type::iterator i=val.begin(); i != val.end(); i++)
{
value_trait val_trait_key_filer;
value_trait val_trait_data_filer;
 
val_trait_key_filer.serialize(i->first,pen);
val_trait_data_filer.serialize(i->second,pen);
}
}
 
void load (associative_list_type& val, reader_trait &pen )
{
value_trait size_reader;
size_type val_size=0;
size_reader.load(val_size, pen);
 
for(; val_size > 0; val_size--)
{
key_type key_element;
value_trait val_trait_key_reader;
val_trait_key_reader.load(key_element, pen);
 
data_type data_element;
value_trait val_trait_data_reader;
val_trait_data_reader.load(data_element, pen);
 
val.insert (std::pair (key_element, data_element));
}
 
}
};
 
//Sequence-list datatype serializer class.
//Triggers the read or write to the serialization file for the Sequence-list datatypes.
//This class takes care of STL types -- list, vector, stack, queue, deque, priority_queue
//NOTE: 'basic_string' type is not treated as sequence-list in this implimentation.
template
class sequence_list_value_trait
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef typename sequence_list_type::size_type size_type;
typedef typename sequence_list_type::value_type value_type;
 
void serialize (sequence_list_type& val, writer_trait &pen )
{
value_trait size_filer;
size_filer.serialize (val.size(), pen);
 
for(sequence_list_type::iterator i=val.begin(); i != val.end(); i++)
{
value_trait val_trait_key_filer;
val_trait_key_filer.serialize(*i,pen);
 
//investigate: what goes in here in STL implimentation of sequence lists --
// from what I feel, it is the "next node pointer" of the interal linked-list
//value_trait val_trait_data_filer;
}
}
 
void load (sequence_list_type& val, reader_trait &pen )
{
value_trait size_reader;
size_type val_size=0;
size_reader.load(val_size, pen);
 
for(; val_size > 0; val_size--)
{
value_type element;
value_trait val_trait_key_reader;
val_trait_key_reader.load(element, pen);
val.push_back(element);
}
}
 
void load_set (sequence_list_type& val, reader_trait &pen )
{
value_trait size_reader;
size_type val_size=0;
size_reader.load(val_size, pen);
 
for(; val_size > 0; val_size--)
{
value_type element;
value_trait val_trait_key_reader;
val_trait_key_reader.load(element, pen);
val.insert(element);
}
}
};
 
//STL map datatype serializer class.
//Triggers the read or write to the serialization file for the STL map datatype .
template
class value_trait< std::map , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::map map_value_trait;
 
void serialize (map_value_trait& val, writer_trait &pen )
{
 
associative_list_value_trait<map_value_trait, val_filer_trait> associative_list_value_filer;
 
associative_list_value_filer.serialize (val, pen);
}
 
void load (map_value_trait& val, reader_trait &pen )
{
 
associative_list_value_trait<map_value_trait, val_filer_trait> associative_list_value_reader;
 
associative_list_value_reader.load (val, pen);
}
};
 

//STL multimap datatype serializer class.
//Triggers the read or write to the serialization file for the STL multimap datatype .
template
class value_trait< std::multimap , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::multimap multimap_value_trait;
 
void serialize (multimap_value_trait& val, writer_trait &pen )
{
 
associative_list_value_trait associative_list_value_filer;
 
associative_list_value_filer.serialize (val, pen);
}
 
void load (multimap_value_trait& val, reader_trait &pen )
{
 
associative_list_value_trait associative_list_value_reader;
 
associative_list_value_reader.load (val, pen);
}
};
 
//STL set datatype serializer class.
//Triggers the read or write to the serialization file for the STL set datatype .
template
class value_trait< std::set , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::set set_value_trait;
 
void serialize (set_value_trait& val, writer_trait &pen )
{
 
associative_list_value_trait associative_list_value_filer;
 
associative_list_value_filer.serialize (val, pen);
}
 
void load (set_value_trait& val, reader_trait &pen )
{
 
associative_list_value_trait associative_list_value_reader;
 
associative_list_value_reader.load (val, pen);
}
};
 
//STL multiset datatype serializer class.
//Triggers the read or write to the serialization file for the STL multiset datatype .
template
class value_trait< std::multiset , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::multiset multiset_value_trait;
 
void serialize (multiset_value_trait& val, writer_trait &pen )
{
 
associative_list_value_trait associative_list_value_filer;
 
associative_list_value_filer.serialize (val, pen);
}
 
void load (multiset_value_trait& val, reader_trait &pen )
{
 
associative_list_value_trait associative_list_value_reader;
 
associative_list_value_reader.load (val, pen);
}
};
 

//STL list datatype serializer class.
//Triggers the read or write to the serialization file for the STL list datatype .
template
class value_trait< std::list , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::list list_value_trait;
 
void serialize (list_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (list_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 
//STL vector datatype serializer class.
//Triggers the read or write to the serialization file for the STL vector datatype .
template
class value_trait< std::vector , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::vector vector_value_trait;
 
void serialize (vector_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (vector_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 
//STL stack datatype serializer class.
//Triggers the read or write to the serialization file for the STL stack datatype .
template
class value_trait< std::stack , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::stack stack_value_trait;
 
void serialize (stack_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (stack_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 
//STL queue datatype serializer class.
//Triggers the read or write to the serialization file for the STL queue datatype .
template
class value_trait< std::queue , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::queue queue_value_trait;
 
void serialize (queue_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (queue_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 
//STL deque datatype serializer class.
//Triggers the read or write to the serialization file for the STL deque datatype .
template
class value_trait< std::deque , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::deque deque_value_trait;
 
void serialize (deque_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (deque_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 
//STL priority_queue datatype serializer class.
//Triggers the read or write to the serialization file for the STL priority_queue datatype .
template
class value_trait< std::priority_queue , val_filer_trait >
{
public:
 
typedef typename val_filer_trait::writer_type writer_trait;
typedef typename val_filer_trait::reader_type reader_trait;
 
typedef std::priority_queue priority_queue_value_trait;
 
void serialize (priority_queue_value_trait& val, writer_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_filer;
 
sequence_list_value_filer.serialize (val, pen);
}
 
void load (priority_queue_value_trait& val, reader_trait &pen )
{
 
sequence_list_value_trait sequence_list_value_reader;
 
sequence_list_value_reader.load (val, pen);
}
};
 

Generaldon't compilememberhenryleesd17 Nov '06 - 5:43 
i tried to compile the example code on a red hat fedora linux box, with g++, but it didn't work. here's error msg. any ideas on fixing it?
thanks!
 
[sl4@hive stl-sl]$ make
g++ -Wall -g -c -o stlsl-example.o stlsl-example.cpp
In file included from stlsl-example.cpp:11:
stlsl.h:58: error: expected nested-name-specifier before "writer_trait"
stlsl.h:58: error: `writer_trait' does not name a type
stlsl.h:59: error: expected nested-name-specifier before "reader_trait"
stlsl.h:59: error: `reader_trait' does not name a type
stlsl.h: In member function `void associative_list_value_trait::serialize(associative_list_type&, typename val_filer_trait::writer_type&)':
stlsl.h:105: error: expected `;' before "i"
stlsl.h:105: error: `i' was not declared in this scope
stlsl.h: In member function `void sequence_list_value_trait::serialize(sequence_list_type&, typename val_filer_trait::writer_type&)':
stlsl.h:157: error: expected `;' before "i"
stlsl.h:157: error: `i' was not declared in this scope
stlsl.h: At global scope:
stlsl.h: In instantiation of `value_trait >, std::less, std::allocator > > > >, std::less, std::allocator >, std::less, std::allocator > > > > > > >, filer_trait >':
stlsl-example.cpp:41: instantiated from here
stlsl.h:191: error: no type named `writer_type' in `class filer_trait'
stlsl.h:192: error: no type named `reader_type' in `class filer_trait'
stlsl.h:197: error: no type named `writer_type' in `class filer_trait'
stlsl.h:205: error: no type named `reader_type' in `class filer_trait'
stlsl-example.cpp: In function `int main()':
stlsl-example.cpp:66: error: 'class value_trait >, std::less, std::allocator > > > >, std::less, std::allocator >, std::less, std::allocator > > > > > > >, filer_trait >' has no member named 'serialize'
stlsl-example.cpp:77: error: 'class value_trait >, std::less, std::allocator > > > >, std::less, std::allocator >, std::less, std::allocator > > > > > > >, filer_trait >' has no member named 'load'
make: *** [stlsl-example.o] Error 1
 


GeneralRe: don't compilememberAniruddha Jahagirdar2 Sep '07 - 17:56 
Sorry buddy, havent found time (a linux box) to put this code together to work for GCC..
QuestionVC++ 6.0?memberGHop26 Oct '06 - 6:46 
Aniruddha,
Would you mind making a VC++ 6.0 version? Thanks.

 
-G-

AnswerRe: VC++ 6.0?memberAniruddha Jahagirdar2 Sep '07 - 18:00 
Hello, sorry for a delayed reply..
I think I might find some time soon to get this thing working on all compilers soon..
I know its almost been an year since you posted but, I guess I could blame it to my earning job Smile | :)
good luck !
GeneralTo boost or not to boost? why this question:memberemilio_grv10 Oct '06 - 3:41 
NOTE: this post is to all the BOOST fans, below.
 
The thing that makes me puzzle in the C++ world is that redoing something is almost soon a matter of flame.
 
You write something and someone wakes up saying “already done”. No matter “how” and “for what”.
Boost has many fans, I understand. But it is unbelievable the motivation that makes every programmer been literally “shut-up” when defining an approach to an algorithm with “is already in Boost”.
An is not believable the approach to say “if what you write is already been done somewhere else it’s nice to tell and explain the difference”.
Yes it’s nice, but whatever you write on heart –unless you’re talking about something completely alien- is certainly be done elsewhere from somewhere else. But no-one can check the world to see any difference about any implementation.
 
So, dear Boost fans, it will be better if YOU start first, the next time you say “done in boost” to really explain the differences. May be you will understand something.
And please stop with the “already tested” litany.
Nothing starts as “tested” and the “already tested” is not enough to ask someone to install an elephant for a mice problem. And the correctness of a 2.5KB of code snippet can be proven in minutes. The one of a 100 time larger library... however many peers you have, is never really known. And one of the reason is just... the huge number of peers and the consequent entropy. Hope you know what it is.
But I really don’t understand why you read these articles. If you think that BOOST is everything you need, enjoy with it, and don’t hassle the rest of the world. Or explain the world the technical difference.

 

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:

GeneralRe: To boost or not to boost? why this question:memberAnonymuos10 Oct '06 - 8:35 
emilio_grv wrote:
The thing that makes me puzzle in the C++ world is that redoing something is almost soon a matter of flame. You write something and someone wakes up saying “already done”. No matter “how” and “for what”. Boost has many fans, I understand. But it is unbelievable the motivation that makes every programmer been literally “shut-up” when defining an approach to an algorithm with “is already in Boost”.

 
Applause, applause!
 
emilio_grv wrote:
So, dear Boost fans, it will be better if YOU start first, the next time you say “done in boost” to really explain the differences. May be you will understand something.

 
Do you think the Boost fans understand Boost enough to explain technical differences? Smile | :) Big Grin | :-D Laugh | :laugh:
GeneralRe: To boost or not to boost? why this question:memberemilio_grv11 Oct '06 - 5:01 
May be utopistic, but I like to think boost didn't wrote itself... Cool | :cool:
So at least one probably will.
 

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:

GeneralRe: To boost or not to boost? why this question:memberStephen Hewitt15 Oct '06 - 19:55 
There are a number of points on this issue. First is that in an article which is intended to educate rather then indoctrinate it seems like a good idea to inform the readers of their choices. I've got nothing against writing something myself; it's a good way to learn to ins and outs of computer programming. That said, I’ve got enough experience in the trade to know that nine times out of ten, when you make such a home grown solution to a problem that’s been solved many times by many different people, the correct thing to do when you’ve got it working is to throw your code in the bin and use an off the self implementation. This is normally a win-win situation; you get the benefit from writing it but you can use an implementation written by a group who have invested more time, money and man power then is available locally.
 
Steve

GeneralRe: To boost or not to boost? why this question:memberemilio_grv16 Oct '06 - 23:19 
May be you’re right, but may be I’m the 1 of 10 you spoken about.
 
Stephen Hewitt wrote:
That said, I’ve got enough experience in the trade to know that nine times out of ten, when you make such a home grown solution to a problem that’s been solved many times by many different people, the correct thing to do when you’ve got it working is to throw your code in the bin and use an off the self implementation. This is normally a win-win situation;

 
You have, in general to consider also these points:
  • The time you spend to seek an existing implementation
  • The “learning curve” you have about that implementation
  • The “dis-adaptation” it has with your design: feature that are include and you don’t need and missing feature you have to workaround.
There are many case that the problem is by itself so simple, that writing and testing yourself a code is more efficient than going inside all that stuff.
Unless the problem is so general you can think to solve it once forever.
 
My experience with boost is that it always have something more and something missing, and adapt to work with it is worst that avoid it.
Sometimes I got the feeling that authors want to show their intelligence blowing up features, rather than do a correct modularization.
 
More in general, OK to inform about possible choices, but a minimum of rational and comparison, instead of an “already done” without any explanation (that sounds like “shut-up, holy sh*t”) like mostly every time boost fans do, should be a minimum.
 


 

 

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:

GeneralAlready been done - Boost Serialize lib!memberyafan9 Oct '06 - 5:15 
Nice work. Good code.
 
Cheers,
 
-y
GeneralRe: Already been done - Boost Serialize lib!memberAnonymuos9 Oct '06 - 5:59 
yafan wrote:
lready been done - Boost Serialize lib!
Nice work. Good code.

 
So what?? WTF | :WTF:

GeneralRe: Already been done - Boost Serialize lib! [modified]memberJohn M. Drescher9 Oct '06 - 7:58 
Its nice to list in the article that it has already been done at X and why your method is different / better than method at X.
Last modified: Mon Oct 9 12:53:30 2006 --

GeneralRe: Already been done - Boost Serialize lib!memberAniruddha Jahagirdar9 Oct '06 - 20:20 
Thanks, I will do it rightaway!
GeneralRe: Already been done - Boost Serialize lib!memberStephen Hewitt9 Oct '06 - 22:13 
So now people have got an extra choice dumb arse. And given a choice between a library that's documented, widely used and peer reviewed and one that's not I know which I'd choose.
 
Steve

GeneralRe: Already been done - Boost Serialize lib!memberAniruddha Jahagirdar9 Oct '06 - 22:59 
Good for you arse hole
GeneralRe: Already been done - Boost Serialize lib!memberStephen Hewitt10 Oct '06 - 1:22 
Good for everyone actually.
 
Steve

GeneralRe: Already been done - Boost Serialize lib!memberJohann Gerell10 Oct '06 - 1:36 
Aniruddha, Stephen really didn't refer to you, but to the anonymous poster, which is the post he answered, not yours.
 
--
The Blog: Bits and Pieces

GeneralRe: Already been done - Boost Serialize lib!memberAniruddha Jahagirdar10 Oct '06 - 19:48 
Sorry Stephen.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 9 Oct 2006
Article Copyright 2006 by Aniruddha Jahagirdar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid