Fawkes API  Fawkes Development Version
dependency_onetomany.h
1 
2 /***************************************************************************
3  * dependency_onetomany.h - One-to-Many dependency constraint
4  *
5  * Created: Tue May 29 14:10:25 2007
6  * Copyright 2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_CONSTRAINTS_DEPENDENCY_ONETOMANY_H_
25 #define __UTILS_CONSTRAINTS_DEPENDENCY_ONETOMANY_H_
26 
27 #include <utils/constraints/dependency.h>
28 
29 #include <list>
30 
31 namespace fawkes {
32 
33 
34 /** @class OneToManyDependency <utils/constraints/dependency_onetomany.h>
35  * One-to-Many dependency constraint.
36  * This dependency constraint models a 1-to-n relationship. There is one
37  * object called provider, that any number of other objects (dependants)
38  * rely on.
39  *
40  * The provider is unique and only one provider may exist at any one time.
41  * There may be an arbitrary number of dependants. Dependants may only be
42  * added if there is already a provider.
43  *
44  * Dependants can always be removed. The provider can only be removed if
45  * there are no more dependants.
46  *
47  * @author Tim Niemueller
48  */
49 
50 template <class Provider, class Dependant>
52 {
53  public:
55  virtual ~OneToManyDependency();
56 
57  virtual void add(Provider *p);
58  virtual void add(Dependant *d);
59  virtual void remove(Provider *p);
60  virtual void remove(Dependant *d);
61 
62  virtual bool can_add(Provider *p);
63  virtual bool can_add(Dependant *d);
64  virtual bool can_remove(Provider *p);
65  virtual bool can_remove(Dependant *d);
66 
67  virtual Provider * provider();
68  virtual std::list<Dependant *> & dependants();
69 
70  private:
71  Provider *_provider;
72  std::list<Dependant *> _dependants;
73 };
74 
75 
76 /** Constructor. */
77 template <class Provider, class Dependant>
79 {
80  _provider = 0;
81  _dependants.clear();
82 }
83 
84 
85 /** Destructor. */
86 template <class Provider, class Dependant>
88 {
89  _dependants.clear();
90 }
91 
92 
93 /** Add provider object.
94  * This will add the provider to this dependency or throw an exception if there is
95  * already a provider.
96  * @param p provider object to add
97  * @exception DependencyViolationException thrown, if a second provider is added
98  */
99 template <class Provider, class Dependant>
100 void
102 {
103  if ( (_provider != 0) && (p != _provider) ) {
104  throw DependencyViolationException("Different provider already set");
105  } else {
106  _provider = p;
107  }
108 }
109 
110 
111 /** Add dependant object.
112  * This will add the dependant to this dependency or throw an exception if there is
113  * no provider.
114  * @param d dependant object to add
115  * @exception DependencyViolationException thrown, if no provider has been set
116  */
117 template <class Provider, class Dependant>
118 void
120 {
121  if (_provider == 0) {
122  throw DependencyViolationException("No provider set, cannot accept dependant");
123  } else {
124  _dependants.push_back(d);
125  }
126 }
127 
128 
129 /** Remove provider object.
130  * @param p provider object to remove
131  * @exception DependencyViolationException thrown, if the provider should be removed
132  * while there is still at least one dependant.
133  */
134 template <class Provider, class Dependant>
135 void
137 {
138  if ( ! _dependants.empty() ) {
139  throw DependencyViolationException("There are still dependants of provider, "
140  "cannot accept removal of provider");
141  }
142  if ( p == _provider ) _provider = 0;
143 }
144 
145 
146 /** Remove a depending object
147  * @param d depending object to remove
148  */
149 template <class Provider, class Dependant>
150 void
152 {
153  if ( d != 0 ) {
154  _dependants.remove(d);
155  }
156 }
157 
158 
159 /** Check if provider can be added.
160  * @param p provider object to add
161  * @return true, if add(p) would succeed, false otherwise
162  */
163 template <class Provider, class Dependant>
164 bool
166 {
167  return ( (_provider == 0) || (p == _provider) );
168 }
169 
170 
171 /** Check if dependant can be added.
172  * @param d dependant object to add
173  * @return true, if add(d) would succeed, false otherwise
174  */
175 template <class Provider, class Dependant>
176 bool
178 {
179  return (_provider != 0);
180 }
181 
182 
183 /** Check if provider can be removed.
184  * @param p provider object to remove
185  * @return true, if remove(p) would succeed, false otherwise
186  */
187 template <class Provider, class Dependant>
188 bool
190 {
191  return _dependants.empty();
192 }
193 
194 
195 /** Check if dependant can be removed.
196  * @param d depending object to remove
197  * @return always true
198  */
199 template <class Provider, class Dependant>
200 bool
202 {
203  return true;
204 }
205 
206 
207 /** Get provider.
208  * @return provider if set, 0 otherwise
209  */
210 template <class Provider, class Dependant>
211 Provider *
213 {
214  return _provider;
215 }
216 
217 
218 /** Get dependants.
219  * @return list of dependants.
220  */
221 template <class Provider, class Dependant>
222 std::list<Dependant *> &
224 {
225  return _dependants;
226 }
227 
228 
229 } // end namespace fawkes
230 
231 #endif
virtual void add(Provider *p)
Add provider object.
virtual bool can_remove(Provider *p)
Check if provider can be removed.
Fawkes library namespace.
virtual void remove(Provider *p)
Remove provider object.
virtual bool can_add(Provider *p)
Check if provider can be added.
Dependency violation exception.
Definition: dependency.h:32
virtual ~OneToManyDependency()
Destructor.
virtual Provider * provider()
Get provider.
One-to-Many dependency constraint.
virtual std::list< Dependant * > & dependants()
Get dependants.