mx::mxEternal< Type > Class Template Reference

List of all members.

Public Types

typedef std::map< std::string,
Type >::iterator 
iterator

Public Member Functions

 mxEternal (std::string name="etern.mxdb", mxEternalRW< Type > *typeobj=new mxEternalRW< Type >)
void setStreamRW (mxEternalRW< Type > *typeobjx)
int read ()
int save ()
const std::string getName () const
Type & operator[] (std::string n)
map< std::string, Type > * operator-> ()


Detailed Description

template<typename Type>
class mx::mxEternal< Type >

persistence of objects

Definition at line 84 of file mxeternal.h.


Member Typedef Documentation

template<typename Type>
typedef std::map<std::string, Type>::iterator mx::mxEternal< Type >::iterator

typedef

Definition at line 126 of file mxeternal.h.


Constructor & Destructor Documentation

template<typename Type >
mx::mxEternal< Type >::mxEternal ( std::string  name = "etern.mxdb",
mxEternalRW< Type > *  typeobj = new mxEternalRW<Type> 
) [inline]

mxEternal default constructor

Parameters:
name name of file to write object stream to
typeobj read/write object containing object read/write functions

Definition at line 174 of file mxeternal.h.

References mx::mxEternal< Type >::read().

00175         {
00176                 typeobj = typeobject;
00177                 name = vname;
00178                 read();
00179         }


Member Function Documentation

template<typename Type>
const std::string mx::mxEternal< Type >::getName (  )  const [inline]

get the name of the database file

Returns:
string of the name of database

Definition at line 109 of file mxeternal.h.

00109 { return name; } // get this DB filename

template<typename Type>
map<std::string, Type>* mx::mxEternal< Type >::operator-> (  )  [inline]

overloaded -> operator

Returns:
pointer to map of variables of type Type

Definition at line 123 of file mxeternal.h.

00123 { return &this_map; } // use operator -> to access the maps member functions

template<typename Type>
Type& mx::mxEternal< Type >::operator[] ( std::string  n  )  [inline]

overloaded [] operator for access object in array by n

Parameters:
n string for array access
Returns:
array variable index of type specified in template parameter

Definition at line 117 of file mxeternal.h.

00117 { return this_map[n]; }

template<typename Type >
int mx::mxEternal< Type >::read (  )  [inline]

read the database from the file specified in constructor

Returns:
integer for success of operation

Definition at line 195 of file mxeternal.h.

Referenced by mx::mxEternal< Type >::mxEternal().

00196         {
00197 
00198                 if(!this_map.empty())  this_map.erase(this_map.begin(), this_map.end());
00199 
00200                 fobj.open(name.c_str(), std::ios::in | std::ios::binary);
00201                 if(!fobj.is_open()) return 1; // no db or removed start over
00202 
00203                 unsigned int number_of_types = 0;
00204 
00205                 fobj.read((char*)&number_of_types, sizeof(number_of_types));
00206 
00207                 for(unsigned int i = 0; (i < number_of_types) && (!fobj.eof()); i++)
00208                 {
00209                         unsigned int name_index = 0;
00210                         fobj.read((char*)&name_index, sizeof(name_index));
00211 
00212                         if(static_cast<int>(name_index) < 0) return -1;
00213 
00214                         char *buf = 0;
00215 
00216                         try
00217                         {
00218 
00219                                 buf = new char [ name_index+1 ];
00220                                 fobj.read((char*)buf, name_index);
00221                                 buf[name_index] =  0;
00222                                 readType();
00223                                 this_map[buf] = typedata;
00224                                 delete [] buf;
00225                                 buf = 0;
00226 
00227                         }
00228                         catch (...)
00229                         {
00230 
00231                                 if(buf != 0) delete [] buf;
00232 
00233                         }
00234 
00235                 }
00236                 fobj.close();
00237                 return 0;
00238         }

template<typename Type >
int mx::mxEternal< Type >::save (  )  [inline]

write teh database to file specified in constructor

Returns:
integer for success of operation

Definition at line 239 of file mxeternal.h.

00240         {
00241 
00242                 fobj_write.open(name.c_str(), std::ios::out | std::ios::binary);
00243                 if(!fobj_write.is_open()) return -1;
00244 
00245                 unsigned int length = this_map.size();
00246 
00247                 fobj_write.write((char*)&length, sizeof(length));
00248 
00249                 typename mxEternal<Type>::iterator i;
00250 
00251                 for(i= this_map.begin(); i!=this_map.end(); i++)
00252                 {
00253 
00254                         unsigned int len =i->first.length();
00255                         const char *str = i->first.c_str();
00256                         fobj_write.write((char*)&len, sizeof(len));
00257                         fobj_write.write((char*)str,len);
00258                         typedata = i->second;
00259                         writeType();
00260                 }
00261 
00262                 fobj_write.close();
00263                 return 0;
00264 
00265         }

template<typename Type >
void mx::mxEternal< Type >::setStreamRW ( mxEternalRW< Type > *  typeobjx  )  [inline]

setStreamRW set the current read/write stream

Parameters:
typeobjx current stream object pointer

Definition at line 181 of file mxeternal.h.

00182         {
00183                 if(typeobj != 0) delete typeobj;
00184                 typeobj = 0;
00185                 typeobj = typeobjx;
00186         }


The documentation for this class was generated from the following file:

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8