summaryrefslogtreecommitdiff
path: root/project2/xmlStorage.h
diff options
context:
space:
mode:
Diffstat (limited to 'project2/xmlStorage.h')
-rw-r--r--project2/xmlStorage.h112
1 files changed, 76 insertions, 36 deletions
diff --git a/project2/xmlStorage.h b/project2/xmlStorage.h
index 9263b48..73fce0a 100644
--- a/project2/xmlStorage.h
+++ b/project2/xmlStorage.h
@@ -3,75 +3,115 @@
#include "sourceObject.h"
#include "exceptions.h"
+#include <set>
+#include <list>
+#include <map>
#include <boost/intrusive_ptr.hpp>
-#include <boost/multi_index_container.hpp>
-#include <boost/multi_index/member.hpp>
-#include <boost/multi_index/ordered_index.hpp>
SimpleMessageException(StoreFailed);
-struct bySOName { };
-struct bySOOrder { };
+#define STORAGEOF(X) \
+ std::map<std::string, boost::intrusive_ptr<X> >
+#define ANONORDEREDSTORAGEOF(X) \
+ std::list<boost::intrusive_ptr<X> >
+#define ANONSTORAGEOF(X) \
+ std::set<boost::intrusive_ptr<X> >
-template <class X>
-class Storage {
- public:
- typedef boost::multi_index::multi_index_container<
- boost::intrusive_ptr<X>,
- boost::multi_index::indexed_by<
- boost::multi_index::ordered_unique<
- boost::multi_index::tag<bySOOrder>, BOOST_MULTI_INDEX_MEMBER(SourceObject, const unsigned int, order)>,
- boost::multi_index::ordered_unique<
- boost::multi_index::tag<bySOName>, BOOST_MULTI_INDEX_MEMBER(SourceObject, const std::string, name)>
- > > Objects;
- typedef Objects * ObjectsPtr;
-};
class Storer;
typedef boost::intrusive_ptr<Storer> StorerPtr;
class Storer : public virtual IntrusivePtrBase {
public:
template <class X>
- static StorerPtr into(X * map);
+ static StorerPtr into(STORAGEOF(X) * map);
+ template <class X>
+ static StorerPtr into(ANONSTORAGEOF(X) * set);
+ template <class X>
+ static StorerPtr into(ANONORDEREDSTORAGEOF(X) * list);
- virtual bool save(SourceObjectPtr o, const xmlpp::Element *) const = 0;
+ virtual bool save(SourceObjectPtr o, const xmlpp::Element *) = 0;
};
-template <class X>
+template <class X, class M = STORAGEOF(X)>
class StorerBase : public Storer {
public:
- typedef typename Storage<X>::ObjectsPtr Map;
- StorerBase()
- {
- }
-
- bool save(SourceObjectPtr obj, const xmlpp::Element * p) const {
+ typedef M * Map;
+ bool save(SourceObjectPtr obj, const xmlpp::Element * p) {
boost::intrusive_ptr<X> O = boost::dynamic_pointer_cast<X>(obj);
if (O) {
- if (getMap(p)->insert(O).second) {
+ if (insert(p, O)) {
return true;
}
throw StoreFailed(obj->name);
}
return false;
}
- virtual Map getMap(const xmlpp::Element *) const = 0;
+ virtual bool insert(const xmlpp::Element *, boost::intrusive_ptr<X>) = 0;
};
+template <class X, class M = STORAGEOF(X)>
+class StorerImpl : public StorerBase<X, M> {
+ public:
+ StorerImpl(M * m);
+ bool insert(const xmlpp::Element *, boost::intrusive_ptr<X> O);
+};
template <class X>
-class StorerImpl : public StorerBase<X> {
+class StorerImpl<X, STORAGEOF(X)> : public StorerBase<X, STORAGEOF(X)> {
public:
- StorerImpl(typename StorerBase<X>::Map m) : map(m)
+ typedef STORAGEOF(X) Map;
+ StorerImpl(STORAGEOF(X) * m) : map(m)
{
}
-
- typename StorerBase<X>::Map getMap(const xmlpp::Element *) const { return map; }
- typename StorerBase<X>::Map map;
+ bool insert(const xmlpp::Element *, boost::intrusive_ptr<X> O)
+ {
+ return map->insert(typename Map::value_type(O->name, O)).second;
+ }
+ Map * map;
+};
+template <class X>
+class StorerImpl<X, ANONSTORAGEOF(X)> : public StorerBase<X, ANONSTORAGEOF(X)> {
+ public:
+ typedef ANONSTORAGEOF(X) Map;
+ StorerImpl(ANONSTORAGEOF(X) * m) : map(m)
+ {
+ }
+ bool insert(const xmlpp::Element *, boost::intrusive_ptr<X> O)
+ {
+ map->insert(O);
+ return true;
+ }
+ Map * map;
+};
+template <class X>
+class StorerImpl<X, ANONORDEREDSTORAGEOF(X)> : public StorerBase<X, ANONORDEREDSTORAGEOF(X)> {
+ public:
+ typedef ANONORDEREDSTORAGEOF(X) Map;
+ StorerImpl(ANONORDEREDSTORAGEOF(X) * m) : map(m)
+ {
+ }
+ bool insert(const xmlpp::Element *, boost::intrusive_ptr<X> O)
+ {
+ map->push_back(O);
+ return true;
+ }
+ Map * map;
};
template <class X>
StorerPtr
-Storer::into(X * map) {
- return new StorerImpl<typename X::value_type::element_type>(map);
+Storer::into(STORAGEOF(X) * map) {
+ return new StorerImpl<X, STORAGEOF(X)>(map);
+}
+
+template <class X>
+StorerPtr
+Storer::into(ANONSTORAGEOF(X) * set) {
+ return new StorerImpl<X, ANONSTORAGEOF(X)>(set);
+}
+
+template <class X>
+StorerPtr
+Storer::into(ANONORDEREDSTORAGEOF(X) * list) {
+ return new StorerImpl<X, ANONORDEREDSTORAGEOF(X)>(list);
}
#endif