Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
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
#include <cstddef>
29
30
namespace
fawkes {
31
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
42
class
UniquenessViolationException
:
public
Exception
43
{
44
public
:
45
/** Contructor.
46
* @param msg message
47
*/
48
UniquenessViolationException
(
const
char
*msg) :
Exception
(msg) {}
49
};
50
51
52
/** @class UniquenessConstraint <utils/constraints/unique.h>
53
* Uniqueness constraint.
54
* This constraint keeps track of a resource that may exist at most once.
55
*
56
* The resource can only be added if no resource has been added and not been
57
* removed before. A resource can always be removed.
58
*
59
* @author Tim Niemueller
60
*/
61
62
template
<
class
ResourceType>
63
class
UniquenessConstraint
64
{
65
public
:
66
UniquenessConstraint
();
67
68
void
add
(ResourceType *r);
69
void
remove
(ResourceType *p);
70
71
ResourceType *
resource
();
72
73
private
:
74
ResourceType *_resource;
75
};
76
77
78
/** Constructor. */
79
template
<
class
ResourceType>
80
UniquenessConstraint<ResourceType>::UniquenessConstraint
()
81
{
82
_resource = NULL;
83
}
84
85
86
/** Add resource.
87
* This will add the resources or throw an exception if there is already a resource.
88
* @param r resource object to add
89
* @exception UniquenessViolationException thrown, if a second resource is added
90
*/
91
template
<
class
ResourceType>
92
void
93
UniquenessConstraint<ResourceType>::add
(ResourceType *r)
94
{
95
if
( (_resource != NULL) && (r != _resource) ) {
96
throw
UniquenessViolationException
(
"Different resource has already been added."
);
97
}
else
{
98
_resource = r;
99
}
100
}
101
102
103
/** Remove resource.
104
* @param r resource object to remove
105
*/
106
template
<
class
ResourceType>
107
void
108
UniquenessConstraint<ResourceType>::remove
(ResourceType *r)
109
{
110
if
( r == _resource ) _resource = NULL;
111
}
112
113
/** Get resource.
114
* @return resource if set, NULL otherwise
115
*/
116
template
<
class
ResourceType>
117
ResourceType *
118
UniquenessConstraint<ResourceType>::resource
()
119
{
120
return
_resource;
121
}
122
123
124
}
// end namespace fawkes
125
126
#endif
src
libs
utils
constraints
unique.h
Generated by
1.8.1.2