Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef VIENNADATA_KEY_VALUE_MANAGER_HPP
00016 #define VIENNADATA_KEY_VALUE_MANAGER_HPP
00017
00018 #include <map>
00019 #include <vector>
00020 #include <list>
00021 #include <set>
00022 #include <iostream>
00023 #include <algorithm>
00024 #include <memory>
00025
00026 #include "viennadata/data_container.hpp"
00027 #include "viennadata/forwards.h"
00028
00033 namespace viennadata
00034 {
00036
00043 template <typename ObjectType>
00044 class key_value_pair_interface
00045 {
00046 public:
00048 virtual ~key_value_pair_interface() {};
00049
00051 virtual void copy(ObjectType const & src, ObjectType const & dest) const = 0;
00052
00054 virtual void erase(ObjectType const & src) const = 0;
00055
00057 virtual bool operator==(key_value_pair_interface<ObjectType> const &) const = 0;
00058 };
00059
00060
00069 template <typename KeyType,
00070 typename DataType,
00071 typename ObjectType>
00072 class key_value_pair_wrapper : public key_value_pair_interface<ObjectType>
00073 {
00074 typedef key_value_pair_wrapper<KeyType, DataType, ObjectType> self_type;
00075 typedef key_value_pair_wrapper<KeyType, all, ObjectType> self_type_key_all;
00076 typedef key_value_pair_wrapper<all, DataType, ObjectType> self_type_all_value;
00077 typedef key_value_pair_wrapper<all, all, ObjectType> self_type_all_all;
00078
00079 public:
00081 void copy(ObjectType const & src,
00082 ObjectType const & dest) const
00083 {
00084 data_container<KeyType, DataType, ObjectType>::instance().copy(src, dest);
00085 }
00086
00088 void erase(ObjectType const & src) const
00089 {
00090 data_container<KeyType, DataType, ObjectType>::instance().erase(src);
00091 }
00092
00102 bool operator==(key_value_pair_interface<ObjectType> const & other) const
00103 {
00104 return (dynamic_cast< self_type const *>( &other ) != NULL
00105 || dynamic_cast< self_type_key_all const *>( &other ) != NULL
00106 || dynamic_cast< self_type_all_value const *>( &other ) != NULL
00107 || dynamic_cast< self_type_all_all const *>( &other ) != NULL);
00108 }
00109
00110 };
00111
00112
00119 template <typename ObjectType>
00120 class key_value_pair
00121 {
00122 public:
00123 key_value_pair() : key_value(NULL), owner(false) {};
00124
00126 key_value_pair(key_value_pair const & other)
00127 {
00128 key_value = other.key_value;
00129 owner = true;
00130 other.owner = false;
00131 }
00132
00134 ~key_value_pair()
00135 {
00136 if (owner && key_value != NULL)
00137 delete key_value;
00138 }
00139
00141 bool operator==(key_value_pair<ObjectType> const & other) const
00142 {
00143 return *key_value == *(other.key_value);
00144 }
00145
00151 template <typename KeyType, typename DataType>
00152 void add()
00153 {
00154 assert(key_value == NULL);
00155 key_value = new key_value_pair_wrapper<KeyType, DataType, ObjectType>();
00156 }
00157
00163 void copy(ObjectType const & src,
00164 ObjectType const & dest) const
00165 {
00166 key_value->copy(src, dest);
00167 }
00168
00170 void erase(ObjectType const & el) const
00171 {
00172 key_value->erase(el);
00173 }
00174
00175 private:
00176 key_value_pair_interface<ObjectType> * key_value;
00177 mutable bool owner;
00178 };
00179
00180
00181
00182 }
00183
00184 #endif