Fawkes API Fawkes Development Version
unique.h
1
2/***************************************************************************
3 * unique.h - Uniqueness constraint
4 *
5 * Created: Sun Feb 24 13:07:25 2008
6 * Copyright 2007-2008 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_UNIQUE_H_
25#define _UTILS_CONSTRAINTS_UNIQUE_H_
26
27#include <core/exception.h>
28
29#include <cstddef>
30
31namespace fawkes {
32
33/** @class UniquenessViolationException <utils/constraints/unique.h>
34 * Uniqueness violation exception.
35 * Thrown if an operation is tried which would violate the uniqueness
36 * constraint.
37 * @see UniquenessConstraint
38 * @ingroup Exceptions
39 * @author Tim Niemueller
40 */
41
43{
44public:
45 /** Contructor.
46 * @param msg message
47 */
49 {
50 }
51};
52
53/** @class UniquenessConstraint <utils/constraints/unique.h>
54 * Uniqueness constraint.
55 * This constraint keeps track of a resource that may exist at most once.
56 *
57 * The resource can only be added if no resource has been added and not been
58 * removed before. A resource can always be removed.
59 *
60 * @author Tim Niemueller
61 */
62
63template <class ResourceType>
65{
66public:
68
69 void add(ResourceType *r);
70 void remove(ResourceType *p);
71
72 ResourceType *resource();
73
74private:
75 ResourceType *_resource;
76};
77
78/** Constructor. */
79template <class ResourceType>
81{
82 _resource = NULL;
83}
84
85/** Add resource.
86 * This will add the resources or throw an exception if there is already a resource.
87 * @param r resource object to add
88 * @exception UniquenessViolationException thrown, if a second resource is added
89 */
90template <class ResourceType>
91void
93{
94 if ((_resource != NULL) && (r != _resource)) {
95 throw UniquenessViolationException("Different resource has already been added.");
96 } else {
97 _resource = r;
98 }
99}
100
101/** Remove resource.
102 * @param r resource object to remove
103 */
104template <class ResourceType>
105void
107{
108 if (r == _resource)
109 _resource = NULL;
110}
111
112/** Get resource.
113 * @return resource if set, NULL otherwise
114 */
115template <class ResourceType>
116ResourceType *
118{
119 return _resource;
120}
121
122} // end namespace fawkes
123
124#endif
Base class for exceptions in Fawkes.
Definition: exception.h:36
Uniqueness constraint.
Definition: unique.h:65
ResourceType * resource()
Get resource.
Definition: unique.h:117
void remove(ResourceType *p)
Remove resource.
Definition: unique.h:106
void add(ResourceType *r)
Add resource.
Definition: unique.h:92
UniquenessConstraint()
Constructor.
Definition: unique.h:80
Uniqueness violation exception.
Definition: unique.h:43
UniquenessViolationException(const char *msg)
Contructor.
Definition: unique.h:48
Fawkes library namespace.