STArray cannot use a vector of STObject's because bad things happen when you delete them.

This commit is contained in:
JoelKatz
2012-11-26 11:20:12 -08:00
parent f5018b6cd1
commit 6013d994e2
2 changed files with 17 additions and 8 deletions

View File

@@ -934,7 +934,7 @@ STArray* STArray::construct(SerializerIterator& sit, SField::ref field)
throw std::runtime_error("Unknown field"); throw std::runtime_error("Unknown field");
} }
value.push_back(STObject(fn)); value.push_back(new STObject(fn));
value.rbegin()->set(sit, 1); value.rbegin()->set(sit, 1);
} }
@@ -1234,6 +1234,9 @@ BOOST_AUTO_TEST_SUITE(SerializedObject)
BOOST_AUTO_TEST_CASE( FieldManipulation_test ) BOOST_AUTO_TEST_CASE( FieldManipulation_test )
{ {
if (sfGeneric.isUseful())
BOOST_FAIL("sfGeneric must not be useful");
SField sfTestVL(STI_VL, 255, "TestVL"); SField sfTestVL(STI_VL, 255, "TestVL");
SField sfTestH256(STI_HASH256, 255, "TestH256"); SField sfTestH256(STI_HASH256, 255, "TestH256");
SField sfTestU32(STI_UINT32, 255, "TestU32"); SField sfTestU32(STI_UINT32, 255, "TestU32");

View File

@@ -46,6 +46,8 @@ public:
STObject(const std::vector<SOElement::ptr>& type, SerializerIterator& sit, SField::ref name) : SerializedType(name) STObject(const std::vector<SOElement::ptr>& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); } { set(sit); setType(type); }
std::auto_ptr<STObject> oClone() const { return std::auto_ptr<STObject>(new STObject(*this)); }
static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0); static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0);
virtual ~STObject() { ; } virtual ~STObject() { ; }
@@ -167,6 +169,10 @@ public:
bool operator!=(const STObject& o) const { return ! (*this == o); } bool operator!=(const STObject& o) const { return ! (*this == o); }
}; };
// allow ptr_* collections of STObject's
inline STObject* new_clone(const STObject& s) { return s.oClone().release(); }
inline void delete_clone(const STObject* s) { boost::checked_delete(s); }
inline STObject::iterator range_begin(STObject& x) { return x.begin(); } inline STObject::iterator range_begin(STObject& x) { return x.begin(); }
inline STObject::iterator range_end(STObject &x) { return x.end(); } inline STObject::iterator range_end(STObject &x) { return x.end(); }
namespace boost namespace boost
@@ -180,12 +186,12 @@ namespace boost
class STArray : public SerializedType, private IS_INSTANCE(SerializedArray) class STArray : public SerializedType, private IS_INSTANCE(SerializedArray)
{ {
public: public:
typedef std::vector<STObject> vector; typedef boost::ptr_vector<STObject> vector;
typedef std::vector<STObject>::iterator iterator; typedef boost::ptr_vector<STObject>::iterator iterator;
typedef std::vector<STObject>::const_iterator const_iterator; typedef boost::ptr_vector<STObject>::const_iterator const_iterator;
typedef std::vector<STObject>::reverse_iterator reverse_iterator; typedef boost::ptr_vector<STObject>::reverse_iterator reverse_iterator;
typedef std::vector<STObject>::const_reverse_iterator const_reverse_iterator; typedef boost::ptr_vector<STObject>::const_reverse_iterator const_reverse_iterator;
typedef std::vector<STObject>::size_type size_type; typedef boost::ptr_vector<STObject>::size_type size_type;
protected: protected:
@@ -210,7 +216,7 @@ public:
vector& getValue() { return value; } vector& getValue() { return value; }
// vector-like functions // vector-like functions
void push_back(const STObject& object) { value.push_back(object); } void push_back(const STObject& object) { value.push_back(object.oClone()); }
STObject& operator[](int j) { return value[j]; } STObject& operator[](int j) { return value[j]; }
const STObject& operator[](int j) const { return value[j]; } const STObject& operator[](int j) const { return value[j]; }
iterator begin() { return value.begin(); } iterator begin() { return value.begin(); }