00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __UTILS_CONSTRAINTS_DEPENDENCY_ONETOMANY_H_
00025 #define __UTILS_CONSTRAINTS_DEPENDENCY_ONETOMANY_H_
00026
00027 #include <utils/constraints/dependency.h>
00028
00029 #include <list>
00030
00031 namespace fawkes {
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 template <class Provider, class Dependant>
00051 class OneToManyDependency
00052 {
00053 public:
00054 OneToManyDependency();
00055 virtual ~OneToManyDependency();
00056
00057 virtual void add(Provider *p);
00058 virtual void add(Dependant *d);
00059 virtual void remove(Provider *p);
00060 virtual void remove(Dependant *d);
00061
00062 virtual bool can_add(Provider *p);
00063 virtual bool can_add(Dependant *d);
00064 virtual bool can_remove(Provider *p);
00065 virtual bool can_remove(Dependant *d);
00066
00067 virtual Provider * provider();
00068 virtual std::list<Dependant *> & dependants();
00069
00070 private:
00071 Provider *_provider;
00072 std::list<Dependant *> _dependants;
00073 };
00074
00075
00076
00077 template <class Provider, class Dependant>
00078 OneToManyDependency<Provider, Dependant>::OneToManyDependency()
00079 {
00080 _provider = 0;
00081 _dependants.clear();
00082 }
00083
00084
00085
00086 template <class Provider, class Dependant>
00087 OneToManyDependency<Provider, Dependant>::~OneToManyDependency()
00088 {
00089 _dependants.clear();
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099 template <class Provider, class Dependant>
00100 void
00101 OneToManyDependency<Provider, Dependant>::add(Provider *p)
00102 {
00103 if ( (_provider != 0) && (p != _provider) ) {
00104 throw DependencyViolationException("Different provider already set");
00105 } else {
00106 _provider = p;
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117 template <class Provider, class Dependant>
00118 void
00119 OneToManyDependency<Provider, Dependant>::add(Dependant *d)
00120 {
00121 if (_provider == 0) {
00122 throw DependencyViolationException("No provider set, cannot accept dependant");
00123 } else {
00124 _dependants.push_back(d);
00125 }
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 template <class Provider, class Dependant>
00135 void
00136 OneToManyDependency<Provider, Dependant>::remove(Provider *p)
00137 {
00138 if ( ! _dependants.empty() ) {
00139 throw DependencyViolationException("There are still dependants of provider, "
00140 "cannot accept removal of provider");
00141 }
00142 if ( p == _provider ) _provider = 0;
00143 }
00144
00145
00146
00147
00148
00149 template <class Provider, class Dependant>
00150 void
00151 OneToManyDependency<Provider, Dependant>::remove(Dependant *d)
00152 {
00153 if ( d != 0 ) {
00154 _dependants.remove(d);
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163 template <class Provider, class Dependant>
00164 bool
00165 OneToManyDependency<Provider, Dependant>::can_add(Provider *p)
00166 {
00167 return ( (_provider == 0) || (p == _provider) );
00168 }
00169
00170
00171
00172
00173
00174
00175 template <class Provider, class Dependant>
00176 bool
00177 OneToManyDependency<Provider, Dependant>::can_add(Dependant *d)
00178 {
00179 return (_provider != 0);
00180 }
00181
00182
00183
00184
00185
00186
00187 template <class Provider, class Dependant>
00188 bool
00189 OneToManyDependency<Provider, Dependant>::can_remove(Provider *p)
00190 {
00191 return _dependants.empty();
00192 }
00193
00194
00195
00196
00197
00198
00199 template <class Provider, class Dependant>
00200 bool
00201 OneToManyDependency<Provider, Dependant>::can_remove(Dependant *d)
00202 {
00203 return true;
00204 }
00205
00206
00207
00208
00209
00210 template <class Provider, class Dependant>
00211 Provider *
00212 OneToManyDependency<Provider, Dependant>::provider()
00213 {
00214 return _provider;
00215 }
00216
00217
00218
00219
00220
00221 template <class Provider, class Dependant>
00222 std::list<Dependant *> &
00223 OneToManyDependency<Provider, Dependant>::dependants()
00224 {
00225 return _dependants;
00226 }
00227
00228
00229 }
00230
00231 #endif