|
From: <den...@us...> - 2010-09-11 19:54:35
|
Revision: 131
http://dsim.svn.sourceforge.net/dsim/?rev=131&view=rev
Author: denis_arnaud
Date: 2010-09-11 19:54:29 +0000 (Sat, 11 Sep 2010)
Log Message:
-----------
[Test] Improved memory handling for the Boost.Intrusive example. It is now closer to the StdAir library usage.
Modified Paths:
--------------
trunk/dsim/test/boost/intrusive/BomAbstract.hpp
trunk/dsim/test/boost/intrusive/BomRoot.hpp
trunk/dsim/test/boost/intrusive/FlightDate.hpp
trunk/dsim/test/boost/intrusive/LegDate.hpp
trunk/dsim/test/boost/intrusive/SegmentDate.hpp
trunk/dsim/test/boost/intrusive/bom.cpp
Modified: trunk/dsim/test/boost/intrusive/BomAbstract.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -6,10 +6,13 @@
// //////////////////////////////////////////////////////////////////////
// STL
#include <cassert>
+#include <iosfwd>
#include <sstream>
#include <string>
+// Boost.Intrusive
+#include <boost/functional/hash.hpp>
-namespace intrusive {
+namespace stdair {
/** BomAbstract. */
class BomAbstract {
@@ -20,9 +23,90 @@
oStr << idx;
_key = oStr.str();
}
+
+ // Comparison operators
+ friend bool operator== (const BomAbstract &a, const BomAbstract &b) {
+ return a._key == b._key;
+ }
+
+ friend bool operator!= (const BomAbstract &a, const BomAbstract &b) {
+ return a._key != b._key;
+ }
+
+ // The hash function
+ friend std::size_t hash_value (const BomAbstract &i) {
+ return boost::hash<std::string>() (i._key);
+ }
+
+ public:
+ // /////////// Display support methods /////////
+ /** Dump a Business Object into an output stream.
+ @param ostream& the output stream. */
+ virtual void toStream (std::ostream& ioOut) const = 0;
+
+ /** Read a Business Object from an input stream.
+ @param istream& the input stream. */
+ virtual void fromStream (std::istream& ioIn) = 0;
+
+ /** Get the serialised version of the Business Object. */
+ virtual std::string toString() const = 0;
+
protected:
std::string _key;
};
}
+
+/**
+ Piece of code given by Nicolai M. Josuttis, Section 13.12.1 "Implementing
+ Output Operators" (p653) of his book "The C++ Standard Library: A Tutorial
+ and Reference", published by Addison-Wesley.
+ */
+template <class charT, class traits>
+inline
+std::basic_ostream<charT, traits>&
+operator<< (std::basic_ostream<charT, traits>& ioOut,
+ const stdair::BomAbstract& iBom) {
+ /**
+ string stream:
+ - with same format
+ - without special field width
+ */
+ std::basic_ostringstream<charT,traits> ostr;
+ ostr.copyfmt (ioOut);
+ ostr.width (0);
+
+ // Fill string stream
+ iBom.toStream (ostr);
+
+ // Print string stream
+ ioOut << ostr.str();
+
+ return ioOut;
+}
+
+/**
+ Piece of code given by Nicolai M. Josuttis, Section 13.12.1 "Implementing
+ Output Operators" (pp655-657) of his book "The C++ Standard Library:
+ A Tutorial and Reference", published by Addison-Wesley.
+ */
+template <class charT, class traits>
+inline
+std::basic_istream<charT, traits>&
+operator>> (std::basic_istream<charT, traits>& ioIn,
+ stdair::BomAbstract& ioBom) {
+ // Fill Bom object with input stream
+ ioBom.fromStream (ioIn);
+ return ioIn;
+}
+
+
+/** The disposer object function. */
+template <typename BOM>
+struct delete_disposer {
+ void operator() (BOM* oBOM_ptr) {
+ delete oBOM_ptr; oBOM_ptr = NULL;
+ }
+};
+
#endif // __INTRUSIVE_BOM_BOMABSTRACT_HPP
Modified: trunk/dsim/test/boost/intrusive/BomRoot.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomRoot.hpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/BomRoot.hpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -10,13 +10,29 @@
//
#include <test/boost/intrusive/BomAbstract.hpp>
-namespace intrusive {
+namespace stdair {
/** BomRoot. */
class BomRoot : public BomAbstract {
public:
BomRoot (const std::string& iKey) : BomAbstract (iKey) {}
BomRoot (const int idx) : BomAbstract (idx) {}
+
+ public:
+ // /////////// Display support methods /////////
+ /** Dump a Business Object into an output stream.
+ @param ostream& the output stream. */
+ void toStream (std::ostream& ioOut) const { ioOut << toString(); }
+
+ /** Read a Business Object from an input stream.
+ @param istream& the input stream. */
+ void fromStream (std::istream& ioIn) { }
+
+ /** Get the serialised version of the Business Object. */
+ std::string toString() const { return describeKey(); }
+
+ /** Get a string describing the key. */
+ const std::string describeKey() const { return _key; }
};
}
Modified: trunk/dsim/test/boost/intrusive/FlightDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -13,15 +13,34 @@
/** Alias for the boost::intrusive namespace. */
namespace bi = boost::intrusive;
-namespace intrusive {
+namespace stdair {
/** FlightDate. */
class FlightDate : public BomAbstract {
public:
+ /** Constructors. */
FlightDate (const std::string& iKey) : BomAbstract (iKey) {}
FlightDate (const int idx) : BomAbstract (idx) {}
+ /** Destructor. */
+ ~FlightDate() {}
bi::list_member_hook<> _childHook;
+
+ public:
+ // /////////// Display support methods /////////
+ /** Dump a Business Object into an output stream.
+ @param ostream& the output stream. */
+ void toStream (std::ostream& ioOut) const { ioOut << toString(); }
+
+ /** Read a Business Object from an input stream.
+ @param istream& the input stream. */
+ void fromStream (std::istream& ioIn) { }
+
+ /** Get the serialised version of the Business Object. */
+ std::string toString() const { return describeKey(); }
+
+ /** Get a string describing the key. */
+ const std::string describeKey() const { return _key; }
};
/** List of child-type FlightDate objects. */
Modified: trunk/dsim/test/boost/intrusive/LegDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/LegDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/LegDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -13,7 +13,7 @@
/** Alias for the boost::intrusive namespace. */
namespace bi = boost::intrusive;
-namespace intrusive {
+namespace stdair {
/** LegDate. */
class LegDate : public BomAbstract {
@@ -23,6 +23,22 @@
bi::list_member_hook<> _childHook;
bi::list_member_hook<> _siblingHook;
+
+ public:
+ // /////////// Display support methods /////////
+ /** Dump a Business Object into an output stream.
+ @param ostream& the output stream. */
+ void toStream (std::ostream& ioOut) const { ioOut << toString(); }
+
+ /** Read a Business Object from an input stream.
+ @param istream& the input stream. */
+ void fromStream (std::istream& ioIn) { }
+
+ /** Get the serialised version of the Business Object. */
+ std::string toString() const { return describeKey(); }
+
+ /** Get a string describing the key. */
+ const std::string describeKey() const { return _key; }
};
/** List of child-type LegDate objects. */
Modified: trunk/dsim/test/boost/intrusive/SegmentDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/SegmentDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/SegmentDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -13,7 +13,7 @@
/** Alias for the boost::intrusive namespace. */
namespace bi = boost::intrusive;
-namespace intrusive {
+namespace stdair {
/** SegmentDate. */
class SegmentDate : public BomAbstract {
@@ -23,6 +23,22 @@
bi::list_member_hook<> _childHook;
bi::list_member_hook<> _siblingHook;
+
+ public:
+ // /////////// Display support methods /////////
+ /** Dump a Business Object into an output stream.
+ @param ostream& the output stream. */
+ void toStream (std::ostream& ioOut) const { ioOut << toString(); }
+
+ /** Read a Business Object from an input stream.
+ @param istream& the input stream. */
+ void fromStream (std::istream& ioIn) { }
+
+ /** Get the serialised version of the Business Object. */
+ std::string toString() const { return describeKey(); }
+
+ /** Get a string describing the key. */
+ const std::string describeKey() const { return _key; }
};
/** List of child-type SegmentDate objects. */
Modified: trunk/dsim/test/boost/intrusive/bom.cpp
===================================================================
--- trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 16:51:17 UTC (rev 130)
+++ trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 19:54:29 UTC (rev 131)
@@ -14,36 +14,79 @@
// /////////////////////////// M A I N /////////////////////////
+/** Main.
+ <br>Run with the following command:
+ <tt>make check && ./bom && echo "Success"</tt>
+ <br>To run the program with Valgrind, type:
+ <tt>libtool --mode=execute valgrind --leak-check=full ./bom</tt>
+*/
int main (int argc, char* argv[]) {
+ //
+ typedef std::vector<stdair::FlightDate*> FlightDateVector_T;
+
+ // Standard STL container
+ FlightDateVector_T lFlightDateVector;
+
// Create several FlightDate objects, each one with a different value
- std::vector<intrusive::FlightDate> flightDateList;
- for (int i = 0; i < 100; ++i) {
- flightDateList.push_back (intrusive::FlightDate (i));
+ for (int idx = 0; idx < 100; ++idx) {
+ stdair::FlightDate* lFlightDate_ptr = new stdair::FlightDate (idx);
+ assert (lFlightDate_ptr != NULL);
+
+ lFlightDateVector.push_back (lFlightDate_ptr);
}
- intrusive::FlightDateChildren flightDateChildren;
+ // (Boost) Intrusive container
+ stdair::FlightDateChildren lFlightDateChildren;
// Now insert them in the same order as in vector in the member hook list
- for (std::vector<intrusive::FlightDate>::iterator it (flightDateList.begin()),
- itend (flightDateList.end()); it != itend; ++it) {
- flightDateChildren.push_back (*it);
+ for (FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
+ itend (lFlightDateVector.end()); itFlight != itend; ++itFlight) {
+ stdair::FlightDate* lFlightDate_ptr = *itFlight;
+ assert (lFlightDate_ptr != NULL);
+
+ lFlightDateChildren.push_back (*lFlightDate_ptr);
}
// Now test lists
{
- intrusive::FlightDateChildren::iterator mit (flightDateChildren.begin()),
- mitend (flightDateChildren.end());
- std::vector<intrusive::FlightDate>::iterator it (flightDateList.begin()),
- itend (flightDateList.end());
+ stdair::FlightDateChildren::iterator mit (lFlightDateChildren.begin()),
+ mitend (lFlightDateChildren.end());
+ FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
+ itend (lFlightDateVector.end());
// Test the objects inserted in the member hook list
- for (it = flightDateList.begin(); it != itend; ++it, ++mit) {
- if (&*mit != &*it) {
+ for (itFlight = lFlightDateVector.begin();
+ itFlight != itend; ++itFlight, ++mit) {
+ stdair::FlightDate* lFlightDate_ptr = *itFlight;
+ assert (lFlightDate_ptr != NULL);
+
+ if (&*mit != lFlightDate_ptr) {
return 1;
}
}
}
+ // Now, test iterator_to()
+ stdair::FlightDateChildren::iterator itChild (lFlightDateChildren.begin());
+ for (int idx = 0; idx < 100; ++idx, ++itChild) {
+ stdair::FlightDate* lFlightDate_ptr = lFlightDateVector.at(idx);
+ assert (lFlightDate_ptr != NULL);
+
+ if (lFlightDateChildren.iterator_to (*lFlightDate_ptr) != itChild ||
+ stdair::FlightDateChildren::s_iterator_to(*lFlightDate_ptr) != itChild){
+ return 1;
+ }
+ }
+
+ /** Some memory cleaning.
+ <br>Note: the FlightDate objects cannot be simply deleted (with the
+ delete opearator).
+ <br>See also, for more details:
+ - http://www.boost.org/doc/libs/1_44_0/doc/html/intrusive/usage.html#intrusive.usage.usage_lifetime
+ - http://www.boost.org/doc/libs/1_44_0/doc/html/intrusive/erasing_and_disposing.html
+ */
+ lFlightDateChildren.clear_and_dispose (delete_disposer<stdair::FlightDate>());
+
return 0;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|