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 /** @class OneToManyDependency <utils/constraints/dependency_onetomany.h>
34  * One-to-Many dependency constraint.
35  * This dependency constraint models a 1-to-n relationship. There is one
36  * object called provider, that any number of other objects (dependants)
37  * rely on.
38  *
39  * The provider is unique and only one provider may exist at any one time.
40  * There may be an arbitrary number of dependants. Dependants may only be
41  * added if there is already a provider.
42  *
43  * Dependants can always be removed. The provider can only be removed if
44  * there are no more dependants.
45  *
46  * @author Tim Niemueller
47  */
48 
49 template <class Provider, class Dependant>
50 class OneToManyDependency
51 {
52 public:
55 
56  virtual void add(Provider *p);
57  virtual void add(Dependant *d);
58  virtual void remove(Provider *p);
59  virtual void remove(Dependant *d);
60 
61  virtual bool can_add(Provider *p);
62  virtual bool can_add(Dependant *d);
63  virtual bool can_remove(Provider *p);
64  virtual bool can_remove(Dependant *d);
65 
66  virtual Provider * provider();
67  virtual std::list<Dependant *> &dependants();
68 
69 private:
70  Provider * _provider;
71  std::list<Dependant *> _dependants;
72 };
73 
74 /** Constructor. */
75 template <class Provider, class Dependant>
77 {
78  _provider = 0;
79  _dependants.clear();
80 }
81 
82 /** Destructor. */
83 template <class Provider, class Dependant>
85 {
86  _dependants.clear();
87 }
88 
89 /** Add provider object.
90  * This will add the provider to this dependency or throw an exception if there is
91  * already a provider.
92  * @param p provider object to add
93  * @exception DependencyViolationException thrown, if a second provider is added
94  */
95 template <class Provider, class Dependant>
96 void
98 {
99  if ((_provider != 0) && (p != _provider)) {
100  throw DependencyViolationException("Different provider already set");
101  } else {
102  _provider = p;
103  }
104 }
105 
106 /** Add dependant object.
107  * This will add the dependant to this dependency or throw an exception if there is
108  * no provider.
109  * @param d dependant object to add
110  * @exception DependencyViolationException thrown, if no provider has been set
111  */
112 template <class Provider, class Dependant>
113 void
115 {
116  if (_provider == 0) {
117  throw DependencyViolationException("No provider set, cannot accept dependant");
118  } else {
119  _dependants.push_back(d);
120  }
121 }
122 
123 /** Remove provider object.
124  * @param p provider object to remove
125  * @exception DependencyViolationException thrown, if the provider should be removed
126  * while there is still at least one dependant.
127  */
128 template <class Provider, class Dependant>
129 void
131 {
132  if (!_dependants.empty()) {
133  throw DependencyViolationException("There are still dependants of provider, "
134  "cannot accept removal of provider");
135  }
136  if (p == _provider)
137  _provider = 0;
138 }
139 
140 /** Remove a depending object
141  * @param d depending object to remove
142  */
143 template <class Provider, class Dependant>
144 void
146 {
147  if (d != 0) {
148  _dependants.remove(d);
149  }
150 }
151 
152 /** Check if provider can be added.
153  * @param p provider object to add
154  * @return true, if add(p) would succeed, false otherwise
155  */
156 template <class Provider, class Dependant>
157 bool
159 {
160  return ((_provider == 0) || (p == _provider));
161 }
162 
163 /** Check if dependant can be added.
164  * @param d dependant object to add
165  * @return true, if add(d) would succeed, false otherwise
166  */
167 template <class Provider, class Dependant>
168 bool
170 {
171  return (_provider != 0);
172 }
173 
174 /** Check if provider can be removed.
175  * @param p provider object to remove
176  * @return true, if remove(p) would succeed, false otherwise
177  */
178 template <class Provider, class Dependant>
179 bool
181 {
182  return _dependants.empty();
183 }
184 
185 /** Check if dependant can be removed.
186  * @param d depending object to remove
187  * @return always true
188  */
189 template <class Provider, class Dependant>
190 bool
192 {
193  return true;
194 }
195 
196 /** Get provider.
197  * @return provider if set, 0 otherwise
198  */
199 template <class Provider, class Dependant>
200 Provider *
202 {
203  return _provider;
204 }
205 
206 /** Get dependants.
207  * @return list of dependants.
208  */
209 template <class Provider, class Dependant>
210 std::list<Dependant *> &
212 {
213  return _dependants;
214 }
215 
216 } // end namespace fawkes
217 
218 #endif
fawkes::OneToManyDependency::remove
virtual void remove(Provider *p)
Remove provider object.
Definition: dependency_onetomany.h:134
fawkes::OneToManyDependency::add
virtual void add(Provider *p)
Add provider object.
Definition: dependency_onetomany.h:101
fawkes::OneToManyDependency::OneToManyDependency
OneToManyDependency()
Constructor.
Definition: dependency_onetomany.h:80
fawkes::OneToManyDependency::~OneToManyDependency
virtual ~OneToManyDependency()
Destructor.
Definition: dependency_onetomany.h:88
fawkes::OneToManyDependency::provider
virtual Provider * provider()
Get provider.
Definition: dependency_onetomany.h:205
fawkes
fawkes::OneToManyDependency::can_add
virtual bool can_add(Provider *p)
Check if provider can be added.
Definition: dependency_onetomany.h:162
fawkes::OneToManyDependency::dependants
virtual std::list< Dependant * > & dependants()
Get dependants.
Definition: dependency_onetomany.h:215
fawkes::OneToManyDependency::can_remove
virtual bool can_remove(Provider *p)
Check if provider can be removed.
Definition: dependency_onetomany.h:184
fawkes::DependencyViolationException
Definition: dependency.h:35