Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * unique.h - Uniqueness constraint 00004 * 00005 * Created: Sun Feb 24 13:07:25 2008 00006 * Copyright 2007-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #ifndef __UTILS_CONSTRAINTS_UNIQUE_H_ 00025 #define __UTILS_CONSTRAINTS_UNIQUE_H_ 00026 00027 #include <core/exception.h> 00028 #include <cstddef> 00029 00030 namespace fawkes { 00031 00032 00033 /** @class UniquenessViolationException <utils/constraints/unique.h> 00034 * Uniqueness violation exception. 00035 * Thrown if an operation is tried which would violate the uniqueness 00036 * constraint. 00037 * @see UniquenessConstraint 00038 * @ingroup Exceptions 00039 * @author Tim Niemueller 00040 */ 00041 00042 class UniquenessViolationException : public Exception 00043 { 00044 public: 00045 /** Contructor. 00046 * @param msg message 00047 */ 00048 UniquenessViolationException(const char *msg) : Exception(msg) {} 00049 }; 00050 00051 00052 /** @class UniquenessConstraint <utils/constraints/unique.h> 00053 * Uniqueness constraint. 00054 * This constraint keeps track of a resource that may exist at most once. 00055 * 00056 * The resource can only be added if no resource has been added and not been 00057 * removed before. A resource can always be removed. 00058 * 00059 * @author Tim Niemueller 00060 */ 00061 00062 template <class ResourceType> 00063 class UniquenessConstraint 00064 { 00065 public: 00066 UniquenessConstraint(); 00067 00068 void add(ResourceType *r); 00069 void remove(ResourceType *p); 00070 00071 ResourceType * resource(); 00072 00073 private: 00074 ResourceType *_resource; 00075 }; 00076 00077 00078 /** Constructor. */ 00079 template <class ResourceType> 00080 UniquenessConstraint<ResourceType>::UniquenessConstraint() 00081 { 00082 _resource = NULL; 00083 } 00084 00085 00086 /** Add resource. 00087 * This will add the resources or throw an exception if there is already a resource. 00088 * @param r resource object to add 00089 * @exception UniquenessViolationException thrown, if a second resource is added 00090 */ 00091 template <class ResourceType> 00092 void 00093 UniquenessConstraint<ResourceType>::add(ResourceType *r) 00094 { 00095 if ( (_resource != NULL) && (r != _resource) ) { 00096 throw UniquenessViolationException("Different resource has already been added."); 00097 } else { 00098 _resource = r; 00099 } 00100 } 00101 00102 00103 /** Remove resource. 00104 * @param r resource object to remove 00105 */ 00106 template <class ResourceType> 00107 void 00108 UniquenessConstraint<ResourceType>::remove(ResourceType *r) 00109 { 00110 if ( r == _resource ) _resource = NULL; 00111 } 00112 00113 /** Get resource. 00114 * @return resource if set, NULL otherwise 00115 */ 00116 template <class ResourceType> 00117 ResourceType * 00118 UniquenessConstraint<ResourceType>::resource() 00119 { 00120 return _resource; 00121 } 00122 00123 00124 } // end namespace fawkes 00125 00126 #endif