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
31namespace 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
49template <class Provider, class Dependant>
51{
52public:
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
69private:
70 Provider * _provider;
71 std::list<Dependant *> _dependants;
72};
73
74/** Constructor. */
75template <class Provider, class Dependant>
77{
78 _provider = 0;
79 _dependants.clear();
80}
81
82/** Destructor. */
83template <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 */
95template <class Provider, class Dependant>
96void
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 */
112template <class Provider, class Dependant>
113void
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 */
128template <class Provider, class Dependant>
129void
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 */
143template <class Provider, class Dependant>
144void
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 */
156template <class Provider, class Dependant>
157bool
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 */
167template <class Provider, class Dependant>
168bool
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 */
178template <class Provider, class Dependant>
179bool
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 */
189template <class Provider, class Dependant>
190bool
192{
193 return true;
194}
195
196/** Get provider.
197 * @return provider if set, 0 otherwise
198 */
199template <class Provider, class Dependant>
200Provider *
202{
203 return _provider;
204}
205
206/** Get dependants.
207 * @return list of dependants.
208 */
209template <class Provider, class Dependant>
210std::list<Dependant *> &
212{
213 return _dependants;
214}
215
216} // end namespace fawkes
217
218#endif
Dependency violation exception.
Definition: dependency.h:32
One-to-Many dependency constraint.
virtual bool can_add(Dependant *d)
Check if dependant can be added.
virtual bool can_remove(Provider *p)
Check if provider can be removed.
virtual bool can_remove(Dependant *d)
Check if dependant can be removed.
virtual bool can_add(Provider *p)
Check if provider can be added.
virtual void add(Dependant *d)
Add dependant object.
virtual void remove(Dependant *d)
Remove a depending object.
virtual void remove(Provider *p)
Remove provider object.
virtual Provider * provider()
Get provider.
virtual void add(Provider *p)
Add provider object.
virtual std::list< Dependant * > & dependants()
Get dependants.
virtual ~OneToManyDependency()
Destructor.
Fawkes library namespace.