|
From: <den...@us...> - 2010-09-11 16:51:23
|
Revision: 130
http://dsim.svn.sourceforge.net/dsim/?rev=130&view=rev
Author: denis_arnaud
Date: 2010-09-11 16:51:17 +0000 (Sat, 11 Sep 2010)
Log Message:
-----------
[Test] Added C++ classes to demonstrate how to use Boost.Intrusive for the BOM tree (there is still some work).
Modified Paths:
--------------
trunk/dsim/test/boost/intrusive/Makefile.am
Added 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
trunk/dsim/test/boost/intrusive/sources.mk
Property Changed:
----------------
trunk/dsim/test/boost/intrusive/
Property changes on: trunk/dsim/test/boost/intrusive
___________________________________________________________________
Modified: svn:ignore
- .deps
.libs
Makefile.in
Makefile
intrusive
+ .deps
.libs
Makefile.in
Makefile
intrusive
bom
Added: trunk/dsim/test/boost/intrusive/BomAbstract.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomAbstract.hpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,28 @@
+#ifndef __INTRUSIVE_BOM_BOMABSTRACT_HPP
+#define __INTRUSIVE_BOM_BOMABSTRACT_HPP
+
+// //////////////////////////////////////////////////////////////////////
+// Import section
+// //////////////////////////////////////////////////////////////////////
+// STL
+#include <cassert>
+#include <sstream>
+#include <string>
+
+namespace intrusive {
+
+ /** BomAbstract. */
+ class BomAbstract {
+ public:
+ BomAbstract (const std::string& iKey) : _key (iKey) {}
+ BomAbstract (const int idx) {
+ std::ostringstream oStr;
+ oStr << idx;
+ _key = oStr.str();
+ }
+ protected:
+ std::string _key;
+ };
+
+}
+#endif // __INTRUSIVE_BOM_BOMABSTRACT_HPP
Added: trunk/dsim/test/boost/intrusive/BomRoot.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomRoot.hpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/BomRoot.hpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,23 @@
+#ifndef __INTRUSIVE_BOM_BOMROOT_HPP
+#define __INTRUSIVE_BOM_BOMROOT_HPP
+
+// //////////////////////////////////////////////////////////////////////
+// Import section
+// //////////////////////////////////////////////////////////////////////
+// STL
+#include <cassert>
+#include <string>
+//
+#include <test/boost/intrusive/BomAbstract.hpp>
+
+namespace intrusive {
+
+ /** BomRoot. */
+ class BomRoot : public BomAbstract {
+ public:
+ BomRoot (const std::string& iKey) : BomAbstract (iKey) {}
+ BomRoot (const int idx) : BomAbstract (idx) {}
+ };
+
+}
+#endif // __INTRUSIVE_BOM_BOMROOT_HPP
Added: trunk/dsim/test/boost/intrusive/FlightDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/FlightDate.hpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,33 @@
+#ifndef __INTRUSIVE_BOM_FLIGHTDATE_HPP
+#define __INTRUSIVE_BOM_FLIGHTDATE_HPP
+
+// //////////////////////////////////////////////////////////////////////
+// Import section
+// //////////////////////////////////////////////////////////////////////
+// STL
+#include <cassert>
+#include <string>
+// Local
+#include <test/boost/intrusive/BomAbstract.hpp>
+
+/** Alias for the boost::intrusive namespace. */
+namespace bi = boost::intrusive;
+
+namespace intrusive {
+
+ /** FlightDate. */
+ class FlightDate : public BomAbstract {
+ public:
+ FlightDate (const std::string& iKey) : BomAbstract (iKey) {}
+ FlightDate (const int idx) : BomAbstract (idx) {}
+
+ bi::list_member_hook<> _childHook;
+ };
+
+ /** List of child-type FlightDate objects. */
+ typedef bi::member_hook <FlightDate, bi::list_member_hook<>,
+ &FlightDate::_childHook> FlightDateChildMemberOption;
+ typedef bi::list<FlightDate, FlightDateChildMemberOption> FlightDateChildren;
+
+}
+#endif // __INTRUSIVE_BOM_FLIGHTDATE_HPP
Added: trunk/dsim/test/boost/intrusive/LegDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/LegDate.hpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/LegDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,39 @@
+#ifndef __INTRUSIVE_BOM_LEGDATE_HPP
+#define __INTRUSIVE_BOM_LEGDATE_HPP
+
+// //////////////////////////////////////////////////////////////////////
+// Import section
+// //////////////////////////////////////////////////////////////////////
+// STL
+#include <cassert>
+#include <string>
+//
+#include <test/boost/intrusive/BomAbstract.hpp>
+
+/** Alias for the boost::intrusive namespace. */
+namespace bi = boost::intrusive;
+
+namespace intrusive {
+
+ /** LegDate. */
+ class LegDate : public BomAbstract {
+ public:
+ LegDate (const std::string& iKey) : BomAbstract (iKey) {}
+ LegDate (const int idx) : BomAbstract (idx) {}
+
+ bi::list_member_hook<> _childHook;
+ bi::list_member_hook<> _siblingHook;
+ };
+
+ /** List of child-type LegDate objects. */
+ typedef bi::member_hook <LegDate, bi::list_member_hook<>,
+ &LegDate::_childHook> LegDateChildMemberOption;
+ typedef bi::list<LegDate, LegDateChildMemberOption> LegDateChildren;
+
+ /** List of sibling-type LegDate objects. */
+ typedef bi::member_hook <LegDate, bi::list_member_hook<>,
+ &LegDate::_siblingHook> LegDateSiblingMemberOption;
+ typedef bi::list<LegDate, LegDateSiblingMemberOption> LegDateSiblings;
+
+}
+#endif // __INTRUSIVE_BOM_LEGDATE_HPP
Modified: trunk/dsim/test/boost/intrusive/Makefile.am
===================================================================
--- trunk/dsim/test/boost/intrusive/Makefile.am 2010-09-11 15:42:49 UTC (rev 129)
+++ trunk/dsim/test/boost/intrusive/Makefile.am 2010-09-11 16:51:17 UTC (rev 130)
@@ -1,5 +1,6 @@
## test/boost/intrusive sub-directory
include $(top_srcdir)/Makefile.common
+include $(srcdir)/sources.mk
MAINTAINERCLEANFILES = Makefile.in
@@ -9,12 +10,17 @@
EXTRA_DIST =
##
-check_PROGRAMS = intrusive
+check_PROGRAMS = intrusive bom
#TESTS = $(check_PROGRAMS)
TESTS =
XFAIL_TESTS = #IndexBuildingTestSuite
intrusive_SOURCES = intrusive.cpp
-intrusive_CXXFLAGS= $(BOOST_CFLAGS)
+intrusive_CXXFLAGS = $(BOOST_CFLAGS)
intrusive_LDFLAGS = $(BOOST_LIBS)
intrusive_LDADD =
+
+bom_SOURCES = $(intru_bom_h_sources) $(intru_bom_cc_sources)
+bom_CXXFLAGS = $(BOOST_CFLAGS)
+bom_LDFLAGS = $(BOOST_LIBS)
+bom_LDADD =
Added: trunk/dsim/test/boost/intrusive/SegmentDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/SegmentDate.hpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/SegmentDate.hpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,41 @@
+#ifndef __INTRUSIVE_BOM_SEGMENTDATE_HPP
+#define __INTRUSIVE_BOM_SEGMENTDATE_HPP
+
+// //////////////////////////////////////////////////////////////////////
+// Import section
+// //////////////////////////////////////////////////////////////////////
+// STL
+#include <cassert>
+#include <string>
+//
+#include <test/boost/intrusive/BomAbstract.hpp>
+
+/** Alias for the boost::intrusive namespace. */
+namespace bi = boost::intrusive;
+
+namespace intrusive {
+
+ /** SegmentDate. */
+ class SegmentDate : public BomAbstract {
+ public:
+ SegmentDate (const std::string& iKey) : BomAbstract (iKey) {}
+ SegmentDate (const int idx) : BomAbstract (idx) {}
+
+ bi::list_member_hook<> _childHook;
+ bi::list_member_hook<> _siblingHook;
+ };
+
+ /** List of child-type SegmentDate objects. */
+ typedef bi::member_hook <SegmentDate, bi::list_member_hook<>,
+ &SegmentDate::_childHook> SegmentDateChildMemberOption;
+ typedef bi::list<SegmentDate,
+ SegmentDateChildMemberOption> SegmentDateChildren;
+
+ /** List of sibling-type SegmentDate objects. */
+ typedef bi::member_hook <SegmentDate, bi::list_member_hook<>,
+ &SegmentDate::_siblingHook> SegmentDateSiblingMemberOption;
+ typedef bi::list<SegmentDate,
+ SegmentDateSiblingMemberOption> SegmentDateSiblings;
+
+}
+#endif // __INTRUSIVE_BOM_SEGMENTDATE_HPP
Copied: trunk/dsim/test/boost/intrusive/bom.cpp (from rev 129, trunk/dsim/test/boost/intrusive/intrusive.cpp)
===================================================================
--- trunk/dsim/test/boost/intrusive/bom.cpp (rev 0)
+++ trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,49 @@
+// STL
+#include <cassert>
+#include <sstream>
+#include <string>
+#include <vector>
+// Boost
+#include <boost/intrusive/list.hpp>
+// Local
+#include <test/boost/intrusive/FlightDate.hpp>
+
+/** Alias for the boost::intrusive namespace. */
+namespace bi = boost::intrusive;
+
+
+
+// /////////////////////////// M A I N /////////////////////////
+int main (int argc, char* argv[]) {
+
+ // 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));
+ }
+
+ intrusive::FlightDateChildren flightDateChildren;
+
+ // 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);
+ }
+
+ // Now test lists
+ {
+ intrusive::FlightDateChildren::iterator mit (flightDateChildren.begin()),
+ mitend (flightDateChildren.end());
+ std::vector<intrusive::FlightDate>::iterator it (flightDateList.begin()),
+ itend (flightDateList.end());
+
+ // Test the objects inserted in the member hook list
+ for (it = flightDateList.begin(); it != itend; ++it, ++mit) {
+ if (&*mit != &*it) {
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
Added: trunk/dsim/test/boost/intrusive/sources.mk
===================================================================
--- trunk/dsim/test/boost/intrusive/sources.mk (rev 0)
+++ trunk/dsim/test/boost/intrusive/sources.mk 2010-09-11 16:51:17 UTC (rev 130)
@@ -0,0 +1,8 @@
+intru_bom_h_sources = \
+ $(top_srcdir)/test/boost/intrusive/BomAbstract.hpp \
+ $(top_srcdir)/test/boost/intrusive/BomRoot.hpp \
+ $(top_srcdir)/test/boost/intrusive/FlightDate.hpp \
+ $(top_srcdir)/test/boost/intrusive/LegDate.hpp \
+ $(top_srcdir)/test/boost/intrusive/SegmentDate.hpp
+intru_bom_cc_sources = \
+ $(top_srcdir)/test/boost/intrusive/bom.cpp
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
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.
|
|
From: <den...@us...> - 2010-09-11 19:59:43
|
Revision: 132
http://dsim.svn.sourceforge.net/dsim/?rev=132&view=rev
Author: denis_arnaud
Date: 2010-09-11 19:59:37 +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/FlightDate.hpp
trunk/dsim/test/boost/intrusive/LegDate.hpp
trunk/dsim/test/boost/intrusive/SegmentDate.hpp
Modified: trunk/dsim/test/boost/intrusive/BomAbstract.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 19:54:29 UTC (rev 131)
+++ trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 19:59:37 UTC (rev 132)
@@ -17,13 +17,21 @@
/** BomAbstract. */
class BomAbstract {
public:
+ /** Constructors. */
BomAbstract (const std::string& iKey) : _key (iKey) {}
BomAbstract (const int idx) {
std::ostringstream oStr;
oStr << idx;
_key = oStr.str();
}
+ protected:
+ /** Default constructors.
+ <br>They are kept private, so as to forbid their use (only the
+ public constructors should be used). */
+ BomAbstract () {}
+ BomAbstract (const BomAbstract&) {}
+ public:
// Comparison operators
friend bool operator== (const BomAbstract &a, const BomAbstract &b) {
return a._key == b._key;
Modified: trunk/dsim/test/boost/intrusive/FlightDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
+++ trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 19:59:37 UTC (rev 132)
@@ -23,7 +23,14 @@
FlightDate (const int idx) : BomAbstract (idx) {}
/** Destructor. */
~FlightDate() {}
-
+ private:
+ /** Default constructors.
+ <br>They are kept private, so as to forbid their use (only the
+ public constructors should be used). */
+ FlightDate () {}
+ FlightDate (const FlightDate&) {}
+
+ public:
bi::list_member_hook<> _childHook;
public:
Modified: trunk/dsim/test/boost/intrusive/LegDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/LegDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
+++ trunk/dsim/test/boost/intrusive/LegDate.hpp 2010-09-11 19:59:37 UTC (rev 132)
@@ -20,7 +20,14 @@
public:
LegDate (const std::string& iKey) : BomAbstract (iKey) {}
LegDate (const int idx) : BomAbstract (idx) {}
-
+ private:
+ /** Default constructors.
+ <br>They are kept private, so as to forbid their use (only the
+ public constructors should be used). */
+ LegDate () {}
+ LegDate (const LegDate&) {}
+
+ public:
bi::list_member_hook<> _childHook;
bi::list_member_hook<> _siblingHook;
Modified: trunk/dsim/test/boost/intrusive/SegmentDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/SegmentDate.hpp 2010-09-11 19:54:29 UTC (rev 131)
+++ trunk/dsim/test/boost/intrusive/SegmentDate.hpp 2010-09-11 19:59:37 UTC (rev 132)
@@ -20,7 +20,14 @@
public:
SegmentDate (const std::string& iKey) : BomAbstract (iKey) {}
SegmentDate (const int idx) : BomAbstract (idx) {}
-
+ private:
+ /** Default constructors.
+ <br>They are kept private, so as to forbid their use (only the
+ public constructors should be used). */
+ SegmentDate () {}
+ SegmentDate (const SegmentDate&) {}
+
+ public:
bi::list_member_hook<> _childHook;
bi::list_member_hook<> _siblingHook;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <den...@us...> - 2010-09-11 22:48:47
|
Revision: 133
http://dsim.svn.sourceforge.net/dsim/?rev=133&view=rev
Author: denis_arnaud
Date: 2010-09-11 22:48:41 +0000 (Sat, 11 Sep 2010)
Log Message:
-----------
[Test] Added a sample for handling Boost.Intrusive Set, with the find() algorithm, to the Boost.Intrusive examples on StdAir typical use.
Modified Paths:
--------------
trunk/dsim/test/boost/intrusive/BomAbstract.hpp
trunk/dsim/test/boost/intrusive/FlightDate.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 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/BomAbstract.hpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -24,6 +24,11 @@
oStr << idx;
_key = oStr.str();
}
+ /** Get the key. */
+ const std::string& getKey() const {
+ return _key;
+ }
+
protected:
/** Default constructors.
<br>They are kept private, so as to forbid their use (only the
@@ -33,18 +38,26 @@
public:
// Comparison operators
- friend bool operator== (const BomAbstract &a, const BomAbstract &b) {
+ 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;
+ }
+
+ 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;
- }
+ 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);
- }
+ // The hash function
+ friend std::size_t hash_value (const BomAbstract& iBom) {
+ return boost::hash<std::string>() (iBom._key);
+ }
public:
// /////////// Display support methods /////////
@@ -56,7 +69,7 @@
@param istream& the input stream. */
virtual void fromStream (std::istream& ioIn) = 0;
- /** Get the serialised version of the Business Object. */
+ /** Get the serialised version of the Business Object. */
virtual std::string toString() const = 0;
protected:
@@ -117,4 +130,27 @@
}
};
+// These compare (STL strings) keys of BOM objects
+template <typename BOM>
+struct StrExpComp {
+ bool operator() (const std::string& iKey, const BOM& iBom) const {
+ return (iKey < iBom.getKey());
+ }
+
+ bool operator() (const BOM& iBom, const std::string& iKey) const {
+ return (iBom.getKey() < iKey);
+ }
+};
+
+template <typename BOM>
+struct StrExpEqual {
+ bool operator() (const std::string& iKey, const BOM& iBom) const {
+ return (iKey == iBom.getKey());
+ }
+
+ bool operator() (const BOM& iBom, const std::string& iKey) const {
+ return (iBom.getKey() == iKey);
+ }
+};
+
#endif // __INTRUSIVE_BOM_BOMABSTRACT_HPP
Modified: trunk/dsim/test/boost/intrusive/FlightDate.hpp
===================================================================
--- trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/FlightDate.hpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -7,6 +7,9 @@
// STL
#include <cassert>
#include <string>
+// Boost.Intrusive
+#include <boost/intrusive/list.hpp>
+#include <boost/intrusive/set.hpp>
// Local
#include <test/boost/intrusive/BomAbstract.hpp>
@@ -31,7 +34,8 @@
FlightDate (const FlightDate&) {}
public:
- bi::list_member_hook<> _childHook;
+ bi::list_member_hook<> _childListHook;
+ bi::set_member_hook<> _childSetHook;
public:
// /////////// Display support methods /////////
@@ -52,8 +56,11 @@
/** List of child-type FlightDate objects. */
typedef bi::member_hook <FlightDate, bi::list_member_hook<>,
- &FlightDate::_childHook> FlightDateChildMemberOption;
- typedef bi::list<FlightDate, FlightDateChildMemberOption> FlightDateChildren;
+ &FlightDate::_childListHook> FlightDateListMemberOption;
+ typedef bi::list<FlightDate, FlightDateListMemberOption> FlightDateChildList;
+ typedef bi::member_hook <FlightDate, bi::set_member_hook<>,
+ &FlightDate::_childSetHook> FlightDateSetMemberOption;
+ typedef bi::set<FlightDate, FlightDateSetMemberOption> FlightDateChildSet;
}
#endif // __INTRUSIVE_BOM_FLIGHTDATE_HPP
Modified: trunk/dsim/test/boost/intrusive/bom.cpp
===================================================================
--- trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 19:59:37 UTC (rev 132)
+++ trunk/dsim/test/boost/intrusive/bom.cpp 2010-09-11 22:48:41 UTC (rev 133)
@@ -1,5 +1,6 @@
// STL
#include <cassert>
+#include <iostream>
#include <sstream>
#include <string>
#include <vector>
@@ -12,11 +13,23 @@
namespace bi = boost::intrusive;
+// Optimized search functions
+stdair::FlightDate* getFromSet(const std::string& iKey,
+ stdair::FlightDateChildSet& ioFlightDateChildSet) {
+ stdair::FlightDate* oFlightDate_ptr = NULL;
+ stdair::FlightDateChildSet::iterator itFlight =
+ ioFlightDateChildSet.find (iKey, StrExpComp<stdair::FlightDate>());
+ if (itFlight == ioFlightDateChildSet.end()) {
+ return oFlightDate_ptr;
+ }
+ oFlightDate_ptr = &*itFlight;
+ return oFlightDate_ptr;
+}
// /////////////////////////// M A I N /////////////////////////
/** Main.
<br>Run with the following command:
- <tt>make check && ./bom && echo "Success"</tt>
+ <tt>make check && ((./bom && echo "Success") || echo "Failure")</tt>
<br>To run the program with Valgrind, type:
<tt>libtool --mode=execute valgrind --leak-check=full ./bom</tt>
*/
@@ -37,7 +50,8 @@
}
// (Boost) Intrusive container
- stdair::FlightDateChildren lFlightDateChildren;
+ stdair::FlightDateChildList lFlightDateChildList;
+ stdair::FlightDateChildSet lFlightDateChildSet;
// Now insert them in the same order as in vector in the member hook list
for (FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
@@ -45,13 +59,22 @@
stdair::FlightDate* lFlightDate_ptr = *itFlight;
assert (lFlightDate_ptr != NULL);
- lFlightDateChildren.push_back (*lFlightDate_ptr);
+ lFlightDateChildList.push_back (*lFlightDate_ptr);
+ lFlightDateChildSet.insert (*lFlightDate_ptr);
}
+ // DEBUG
+ /*
+ std::cout << "Size of the Boost.Intrusive list of FlightDate objects: "
+ << lFlightDateChildList.size() << std::endl;
+ std::cout << "Size of the Boost.Intrusive set of FlightDate objects: "
+ << lFlightDateChildSet.size() << std::endl;
+ */
+
// Now test lists
{
- stdair::FlightDateChildren::iterator mit (lFlightDateChildren.begin()),
- mitend (lFlightDateChildren.end());
+ stdair::FlightDateChildList::iterator mit (lFlightDateChildList.begin()),
+ mitend (lFlightDateChildList.end());
FlightDateVector_T::iterator itFlight (lFlightDateVector.begin()),
itend (lFlightDateVector.end());
@@ -68,16 +91,47 @@
}
// 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);
+ {
+ stdair::FlightDateChildList::iterator itChild(lFlightDateChildList.begin());
+ for (int idx = 0; idx < 100; ++idx, ++itChild) {
+ stdair::FlightDate* lFlightDate_ptr = lFlightDateVector.at(idx);
+ assert (lFlightDate_ptr != NULL);
+
+ if (lFlightDateChildList.iterator_to (*lFlightDate_ptr) != itChild ||
+ stdair::FlightDateChildList::s_iterator_to(*lFlightDate_ptr) != itChild) {
+ return 1;
+ }
+ }
+ }
- if (lFlightDateChildren.iterator_to (*lFlightDate_ptr) != itChild ||
- stdair::FlightDateChildren::s_iterator_to(*lFlightDate_ptr) != itChild){
- return 1;
+ // Now, test sets
+ {
+ stdair::FlightDateChildSet::iterator itChild (lFlightDateChildSet.begin()),
+ itChildEnd (lFlightDateChildSet.end());
+ for (; itChild != itChildEnd; ++itChild) {
+ const stdair::FlightDate& lFlightDate = *itChild;
+
+ const std::string& lKey = lFlightDate.getKey();
+ stdair::FlightDate* retrievedFlightDate_ptr =
+ getFromSet (lKey, lFlightDateChildSet);
+
+ // DEBUG
+ /*
+ std::cout << "Key = '" << lKey << "', itFD = "
+ << &lFlightDate << ", retrieved: " << retrievedFlightDate_ptr
+ << std::endl;
+ */
+
+ if (retrievedFlightDate_ptr == NULL ||
+ lFlightDateChildSet.iterator_to (lFlightDate) != itChild ||
+ lFlightDateChildSet.iterator_to (*retrievedFlightDate_ptr) != itChild ||
+ stdair::FlightDateChildSet::s_iterator_to (lFlightDate) != itChild ||
+ stdair::FlightDateChildSet::s_iterator_to(*retrievedFlightDate_ptr) != itChild) {
+ return 1;
+ }
}
}
+
/** Some memory cleaning.
<br>Note: the FlightDate objects cannot be simply deleted (with the
@@ -85,8 +139,12 @@
<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
+ <br>First, clear simply all the Boost.Intrusive containers but one. Then,
+ clear the last Boost.Intrusive container while deleting the corresponding
+ hooked objects.
*/
- lFlightDateChildren.clear_and_dispose (delete_disposer<stdair::FlightDate>());
+ lFlightDateChildSet.clear();
+ lFlightDateChildList.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.
|