Fawkes API Fawkes Development Version

unique.h

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 
00029 namespace fawkes {
00030 
00031 
00032 /** @class UniquenessViolationException <utils/constraints/unique.h>
00033  * Uniqueness violation exception.
00034  * Thrown if an operation is tried which would violate the uniqueness
00035  * constraint.
00036  * @see UniquenessConstraint
00037  * @ingroup Exceptions
00038  * @author Tim Niemueller
00039  */
00040 
00041 class UniquenessViolationException : public Exception
00042 {
00043  public:
00044   /** Contructor.
00045    * @param msg message
00046    */
00047   UniquenessViolationException(const char *msg) : Exception(msg) {}
00048 };
00049 
00050 
00051 /** @class UniquenessConstraint <utils/constraints/unique.h>
00052  * Uniqueness constraint.
00053  * This constraint keeps track of a resource that may exist at most once.
00054  *
00055  * The resource can only be added if no resource has been added and not been
00056  * removed before. A resource can always be removed.
00057  *
00058  * @author Tim Niemueller
00059  */
00060 
00061 template <class ResourceType>
00062   class UniquenessConstraint
00063 {
00064  public:
00065   UniquenessConstraint();
00066 
00067   void add(ResourceType *r);
00068   void remove(ResourceType *p);
00069 
00070   ResourceType *             resource();
00071 
00072  private:
00073   ResourceType *_resource;
00074 };
00075 
00076 
00077 /** Constructor. */
00078 template <class ResourceType>
00079   UniquenessConstraint<ResourceType>::UniquenessConstraint()
00080 {
00081   _resource = 0;
00082 }
00083 
00084 
00085 /** Add resource.
00086  * This will add the resources or throw an exception if there is already a resource.
00087  * @param r resource object to add
00088  * @exception UniquenessViolationException thrown, if a second resource is added
00089  */
00090 template <class ResourceType>
00091 void
00092 UniquenessConstraint<ResourceType>::add(ResourceType *r)
00093 {
00094   if ( (_resource != 0) && (r != _resource) ) {
00095     throw UniquenessViolationException("Different resource has already been added.");
00096   } else {
00097     _resource = r;
00098   }
00099 }
00100 
00101 
00102 /** Remove resource.
00103  * @param r resource object to remove
00104  */
00105 template <class ResourceType>
00106 void
00107 UniquenessConstraint<ResourceType>::remove(ResourceType *r)
00108 {
00109   if ( r == _resource )  _resource = 0;
00110 }
00111 
00112 /** Get resource.
00113  * @return resource if set, NULL otherwise
00114  */
00115 template <class ResourceType>
00116 ResourceType *
00117 UniquenessConstraint<ResourceType>::resource()
00118 {
00119   return _resource;
00120 }
00121 
00122 
00123 } // end namespace fawkes
00124 
00125 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends