Bounded.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2010 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00035 #ifndef KEYVALUE_KEY_GENERIC_BOUNDED_H_
00036 #define KEYVALUE_KEY_GENERIC_BOUNDED_H_
00037 
00038 #include "keyvalue/key/converter/StdSingle.h"
00039 #include "keyvalue/key/generic/bound/NoBound.h"
00040 #include "keyvalue/key/Traits.h"
00041 
00042 namespace keyvalue {
00043 namespace key {
00044 
00106 template<typename ElementType, template <typename> class Bound1,
00107   template <typename> class Bound2 = NoBound>
00108 class Bounded: public Traits<ElementType, StdSingle> {
00109 
00110 public:
00111 
00120   Bounded(const string& name, const ElementType& bound1);
00121 
00132   Bounded(const string& name, const ElementType& bound1,
00133     const ElementType& bound2);
00134 
00142   void checkOutput(ElementType element) const;
00143 
00144 private:
00145 
00146   const Bound1<ElementType> bound1_;
00147   const Bound2<ElementType> bound2_;
00148 
00149 }; // class Bounded
00150 
00151 /*--------------------------------------------------------------------------
00152  * Bounded()
00153  *------------------------------------------------------------------------*/
00154 
00155 template<typename ElementType, template <typename> class Bound1,
00156   template <typename> class Bound2>
00157 Bounded<ElementType, Bound1, Bound2>::Bounded(const string& name,
00158   const ElementType& bound1) :
00159   Bounded::Traits_(name),
00160   bound1_(bound1),
00161   bound2_() {
00162 }
00163 
00164 template<typename ElementType, template <typename> class Bound1,
00165   template <typename> class Bound2>
00166 Bounded<ElementType, Bound1, Bound2>::Bounded(const string& name,
00167   const ElementType& bound1, const ElementType& bound2) :
00168   Bounded::Traits_(name),
00169   bound1_(bound1),
00170   bound2_(bound2) {
00171 }
00172 
00173 /*--------------------------------------------------------------------------
00174  * checkOutput()
00175  *------------------------------------------------------------------------*/
00176 
00177 template<typename ElementType, template <typename> class Bound1,
00178   template <typename> class Bound2>
00179 void
00180 Bounded<ElementType, Bound1, Bound2>::checkOutput(const ElementType element)
00181   const {
00182 
00183   if (!bound1_.check(element))
00184     throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00185       "be " & bound1_.getName() & " " & bound1_.getBound() & ". Got " &
00186       element & "!";
00187 
00188   if (!bound2_.check(element))
00189     throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00190       "be " & bound2_.getName() & " " & bound2_.getBound() & ". Got " &
00191       element & "!";
00192 }
00193 
00194 } // namespace key
00195 } // namespace keyvalue
00196 
00197 #endif // KEYVALUE_KEY_GENERIC_BOUNDED_H_