|
Boost Users : |
From: Jon Belinfante (Jon.Belinfante_at_[hidden])
Date: 2007-01-03 07:50:05
Hi
I am at present introducing myself to Boost Serialization which from my
first impressions of is very impressive to use and I like what I see.
I have an ongoing set of classes to serialiise and find most of the problems
I have found can be resolved from the Boost documentation.
However one problem I have is that a huge bulk of my classes used
boost::smart_ptrs each of which seem to add an extra level of complexity and
at present I can not see my own resolution to.
I have one simple case of a smart string class - as shown below which causes
me a problem. This source code is to create a smart string class which uses
a wrapped shared_ptr to a std::string - the idea behind this Smart String
class is to reduce memory sizes for huge quantity of repeated strings in an
application. -Please see source code. This class works fine apart the
serialization which either does not compile or crashes with assertion
errors.
Please see below source code.
As this code stands with line in the serialization block
ar & m_stringPtr;
I get a long compilation error -
c:\Boost\boost_1_33_1\boost\serialization\shared_ptr.hpp(235) : error C2027:
use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>' - with it
complatining with the line
oa << a; which is in the main code block.
However if I change to code
ar & * m_stringPtr;
The code compiles correctly and it serializes the SmartString - however it
run-time throws in trying to de-serialise at
ia >> b;
Any ideas here where I am going wrong - I would be most greatful.
Thanks
/********************************************/
#include <set>
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
BOOST_SERIALIZATION_SHARED_PTR(std::string)
/**
Smart String Class
Basically holds a String only by a ptr and the pimpl method.
Idea is to only ever store a shared_ptr and let boost do a reference count
on its usage.
Idea to of course save time/speed of string storage.
*/
class SmartString
{
public:
/** overriden stream operator */
friend std::ostream& operator<<
(std::ostream &os, const SmartString &cSmartString);
SmartString()
{
m_stringPtr.reset();
}
SmartString( const std::string& s)
{
boost::shared_ptr< std::string > tempPtr( new std::string(
s) );
m_stringPtr = tempPtr;
}
SmartString( const char* s)
{
boost::shared_ptr< std::string > tempPtr( new std::string(
s) );
m_stringPtr = tempPtr;
}
SmartString( const SmartString& other)
{
m_stringPtr = other.m_stringPtr;
}
SmartString& operator=(const SmartString& other)
{
m_stringPtr = other.m_stringPtr;
return *this;
}
inline SmartString& operator=( const std::string& otherStr)
{
boost::shared_ptr< std::string > tempPtr( new std::string(
otherStr) );
m_stringPtr = tempPtr;
return *this;
}
bool operator() (
const SmartString& t1,
const SmartString& t2) const;
inline bool operator<( const SmartString& otherStr)
{
const std::string other= *otherStr.m_stringPtr;
return *m_stringPtr < other;
}
inline bool operator<=( const SmartString& otherStr)
{
const std::string other= *otherStr.m_stringPtr;
return *m_stringPtr <= other;
}
inline bool operator>( const SmartString& otherStr)
{
const std::string other= *otherStr.m_stringPtr;
return *m_stringPtr > other;
}
inline bool operator>=( const SmartString& otherStr)
{
const std::string other= *otherStr.m_stringPtr;
return *m_stringPtr >= other;
}
inline bool operator==( const SmartString& otherStr )
{
return *m_stringPtr == *otherStr.m_stringPtr;
}
inline bool operator==( const SmartString& otherStr ) const
{
return *m_stringPtr == *otherStr.m_stringPtr;
}
inline bool operator==( const std::string& otherStr )
{
return *m_stringPtr == otherStr;
}
inline bool operator==( const char* c )
{
int x = ::strcmp( (*m_stringPtr).c_str(), c);
return x? false:true;
}
inline const std::string& getString( ) const
{
return *m_stringPtr;
}
inline bool operator!=( const SmartString& otherStr)
{
const std::string other= *otherStr.m_stringPtr;
return *m_stringPtr != other;
}
inline bool operator!= ( const std::string& otherStr)
{
return *m_stringPtr != otherStr;
}
inline const char& operator[] (const int index) const
{
if ( (index >=0) && ( index <=
static_cast<int>((*m_stringPtr).size() )))
return (*m_stringPtr)[index];
else
{
std::string message( "Out of range index on " +
(*m_stringPtr) );
throw std::out_of_range(message);
}
}
/** overriden + operation to retunn addition of two strings */
friend SmartString operator+(const SmartString& s1,const SmartString
&s2);
virtual ~SmartString(){}
friend class boost::serialization::access;
protected:
template <class Archive>
void serialize (
Archive& ar, const unsigned long file_version)
{
ar & m_stringPtr;
}
boost::shared_ptr<std::string> m_stringPtr;
};
std::ostream& operator<<
(std::ostream &os, const SmartString& cSmartString)
{
const std::string str( cSmartString.getString() );
os << str.c_str();
return os;
}
SmartString operator+(const SmartString& s1,const SmartString &s2)
{
std::string str = s1.getString();
str += s2.getString();
return SmartString ( str.c_str() );
}
int main( int, const char*[] )
{
const SmartString a("ABC");
const char* fileName = "smartStringFile";
{
std::ofstream ofs(fileName);
boost::archive::text_oarchive oa( ofs );
oa << a;
}
SmartString b;
{
std::ifstream ifs( fileName, std::ios::binary );
boost::archive::text_iarchive ia( ifs );
ia >> b;
}
}
/********************************************/
**********************************************************
Mitsubishi UFJ Securities International plc ("MUSI") is registered in England, company number 1698498 and registered office at 6 Broadgate, London EC2M 2AA. MUSI is part of the Mitsubishi UFJ Financial Group and is authorised and regulated by The Financial Services Authority. This message is intended solely for the individual addressee named above. The information contained in this e-mail is confidential and may be legally privileged. If you are not the intended recipient please delete in its entirety. Messages sent via this medium may be subject to delays, non-delivery and unauthorised alteration. The information contained herein or attached hereto has been obtained from sources we believe to be reliable but we do not represent that it is accurate or complete. Any reference to past performance should not be taken as an indication of future performance.
The information contained herein or attached hereto is not to be construed as an offer or solicitation to buy or sell any security, instrument or investment. MUSI or any affiliated company, may have an interest, position, or effect transactions, in any investment mentioned herein. Any opinions or recommendations expressed herein are solely those of the author or analyst and are subject to change without notice.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net