00001
00002
00003
00004
00005
00006
00007
00008 #ifndef IMP_CONTAINER_MACROS_H
00009 #define IMP_CONTAINER_MACROS_H
00010
00011 #include "RefCounted.h"
00012 #include "VectorOfRefCounted.h"
00013 #include "internal/Vector.h"
00014 #include "macros.h"
00015
00016
00017
00018
00019
00020 #ifdef IMP_SWIG_WRAPPER
00021 #define IMP_PROTECTION(protection) public:
00022 #else
00023 #define IMP_PROTECTION(protection) protection:
00024 #endif
00025
00026 #ifndef SWIG
00027
00028 #define IMP_EXPOSE_ITERATORS(ContainerType, container_name, Ucname, lcname) \
00029 IMP_NO_DOXYGEN(typedef ContainerType::iterator Ucname##Iterator;) \
00030 IMP_NO_DOXYGEN(typedef ContainerType::const_iterator \
00031 Ucname##ConstIterator;) \
00032 IMP_ONLY_DOXYGEN(class Ucname##Iterator; class Ucname##ConstIterator;) \
00033 Ucname##Iterator lcname##s_begin() {return container_name.begin();} \
00034 Ucname##Iterator lcname##s_end() {return container_name.end();} \
00035 Ucname##ConstIterator lcname##s_begin() const { \
00036 return container_name.begin(); \
00037 } \
00038 Ucname##ConstIterator lcname##s_end() const { \
00039 return container_name.end();} \
00040
00041 #else
00042 #define IMP_EXPOSE_ITERATORS(ContainerType, container_name, Ucname, lcname) \
00043 IMP_NO_DOXYGEN(typedef ContainerType::iterator Ucname##Iterator;) \
00044 Ucname##Iterator lcname##s_begin() {return container_name.begin();} \
00045 Ucname##Iterator lcname##s_end() {return container_name.end();}
00046 #endif // SWIG
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #define IMP_LIST(protection, Ucname, lcname, Data, PluralData) \
00085 IMP_PROTECTION(protection) \
00086 \
00087 void remove_##lcname(Data d); \
00088 \
00089 template <class F> \
00090 void remove_##lcname##s_if(const F &f) { \
00091 for (Ucname##Iterator it= lcname##s_begin(); it != lcname##s_end(); \
00092 ++it) { \
00093 if (f(*it)) handle_remove(*it); \
00094 } \
00095 IMP::internal::remove_if(lcname##_vector_, f); \
00096 } \
00097 \
00098 void remove_##lcname##s(const PluralData& d); \
00099
00100 \
00101 void set_##lcname##s(const PluralData &ps) { \
00102
00103 \
00104 clear_##lcname##s(); \
00105 add_##lcname##s(ps); \
00106 } \
00107
00108 \
00109 unsigned int add_##lcname(Data obj); \
00110
00111
00112 \
00113 void add_##lcname##s(const PluralData& obj); \
00114 void clear_##lcname##s(); \
00115 unsigned int get_number_of_##lcname##s() const { \
00116 return lcname##_vector_.size();} \
00117 \
00118 bool get_has_##lcname##s() const { \
00119 return !lcname##_vector_.empty();} \
00120
00121
00122 \
00123 Data get_##lcname(unsigned int i) const { \
00124 return lcname##_vector_[i]; \
00125 } \
00126 void reserve_##lcname##s(unsigned int sz) { \
00127 lcname##_vector_.reserve(sz); \
00128 } \
00129 IMP_EXPOSE_ITERATORS(IMP::VectorOfRefCounted<Data>, \
00130 lcname##_vector_, Ucname, lcname); \
00131 private: \
00132 const PluralData &access_##lcname##s() const {return lcname##_vector_;} \
00133 void handle_remove(Data d); \
00134 IMP_NO_DOXYGEN(PluralData lcname##_vector_;) \
00135 IMP_PROTECTION(protection) \
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 #define IMP_LIST_IMPL(Class, Ucname, lcname, Data, PluralData, OnAdd, \
00157 OnChanged, OnRemoved) \
00158 void Class::handle_remove( Data obj){ \
00159 if (0) std::cout << obj; \
00160 OnRemoved; \
00161 } \
00162 unsigned int Class::add_##lcname(Data obj) { \
00163 unsigned int index= lcname##_vector_.size(); \
00164 lcname##_vector_.push_back(obj); \
00165 OnAdd; \
00166 OnChanged; \
00167 if (false) {index=index; obj=obj;}; \
00168 return index; \
00169 } \
00170 void Class::add_##lcname##s(const PluralData &objs) { \
00171 unsigned int osz= lcname##_vector_.size(); \
00172 lcname##_vector_.insert(lcname##_vector_.end(), objs.begin(), \
00173 objs.end()); \
00174 for (unsigned int i=0; i< objs.size(); ++i) { \
00175 Data obj= lcname##_vector_[osz+i]; \
00176 unsigned int index(osz+i); \
00177 OnAdd; \
00178 if (false) {obj=obj; index=index;} \
00179 } \
00180 OnChanged; \
00181 } \
00182 void Class::remove_##lcname##s(const PluralData& d) { \
00183 std::vector<Data> ds(d.begin(), d.end()); \
00184 std::sort(ds.begin(), ds.end()); \
00185 for (unsigned int i=0; i< ds.size(); ++i) { \
00186 handle_remove(ds[i]); \
00187 } \
00188 IMP::internal::remove_if(lcname##_vector_, \
00189 ::IMP::internal::list_contains(ds)); \
00190 } \
00191 void Class::clear_##lcname##s(){ \
00192 lcname##_vector_.clear(); \
00193 OnChanged; \
00194 } \
00195 void Class::remove_##lcname(Data d) { \
00196 for (Ucname##Iterator it= lcname##s_begin(); \
00197 it != lcname##s_end(); ++it) { \
00198 if (*it == d) { \
00199 handle_remove(*it); \
00200 lcname##_vector_.erase(it); break; \
00201 } \
00202 } \
00203 OnChanged; \
00204 } \
00205
00206 #endif