00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #ifndef KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00030 #define KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00031
00032 #include "keyvalue/key/converter/StdVector.h"
00033 #include "keyvalue/key/generic/bound/NoBound.h"
00034 #include "keyvalue/key/Traits.h"
00035
00036 namespace keyvalue {
00037 namespace key {
00038
00113 template <typename ElementType, typename Monotone,
00114 template <typename> class Bound1 = NoBound,
00115 template <typename> class Bound2 = NoBound>
00116 class MonotoneBoundedVector: public Traits<ElementType, StdVector, NoMap> {
00117
00118 public:
00119
00129 explicit
00130 MonotoneBoundedVector(const string& name, size_t size = 0);
00131
00142 MonotoneBoundedVector(const string& name, const ElementType& bound1,
00143 size_t size = 0);
00144
00156 MonotoneBoundedVector(const string& name, const ElementType& bound1,
00157 const ElementType& bound2, size_t size = 0);
00158
00167 void
00168 checkSoFar(const StdVector<ElementType>& container) const;
00169
00179 void
00180 checkSize(size_t size) const;
00181
00182 private:
00183
00184 const size_t size_;
00185 const Bound1<ElementType> bound1_;
00186 const Bound2<ElementType> bound2_;
00187
00188 };
00189
00190
00191
00192
00193
00194 template <typename ElementType, typename Monotone,
00195 template <typename> class Bound1, template <typename> class Bound2>
00196 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00197 Bound2>::MonotoneBoundedVector(const string& name, size_t size)
00198 : MonotoneBoundedVector::Traits_(name), size_(size), bound1_(),
00199 bound2_() {
00200 }
00201
00202 template <typename ElementType, typename Monotone,
00203 template <typename> class Bound1, template <typename> class Bound2>
00204 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00205 Bound2>::MonotoneBoundedVector(const string& name,
00206 const ElementType& bound1, size_t size)
00207 : MonotoneBoundedVector::Traits_(name), size_(size), bound1_(bound1),
00208 bound2_() {
00209 }
00210
00211 template <typename ElementType, typename Monotone,
00212 template <typename> class Bound1, template <typename> class Bound2>
00213 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00214 Bound2>::MonotoneBoundedVector(const string& name,
00215 const ElementType& bound1, const ElementType& bound2, size_t size)
00216 : MonotoneBoundedVector::Traits_(name), size_(size), bound1_(bound1),
00217 bound2_(bound2) {
00218 }
00219
00220
00221
00222
00223
00224 template <typename ElementType, typename Monotone,
00225 template <typename> class Bound1, template <typename> class Bound2>
00226 void
00227 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSoFar(
00228 const StdVector<ElementType>& container) const {
00229
00230 const std::vector<ElementType>& vector(*container.getOutput());
00231 const size_t size(vector.size());
00232
00233
00234 if (size > 1 && !Monotone::check(vector[size-2], vector[size-1]))
00235 throw RuntimeError() & "Values for key '" & Key::getName() &
00236 "' must be in " & Monotone::getName() & " order. Got " &
00237 vector[size-2] & " followed by " & vector[size-1] & "!";
00238
00239
00240 if (!bound1_.check(vector.back()))
00241 throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00242 "be " & bound1_.getName() & " " & bound1_.getBound() & ". Got " &
00243 vector.back() & "!";
00244
00245 if (!bound2_.check(vector.back()))
00246 throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00247 "be " & bound2_.getName() & " " & bound2_.getBound() & ". Got " &
00248 vector.back() & "!";
00249 }
00250
00251
00252
00253
00254
00255 template <typename ElementType, typename Monotone,
00256 template <typename> class Bound1, template <typename> class Bound2>
00257 void
00258 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSize(
00259 size_t size) const {
00260
00261 if (size_ != 0 && size_ != size)
00262 throw RuntimeError() & "Invalid number of values assigned to key '" &
00263 Key::getName() & "'! Expecting " & size_ & " got " & size & "!";
00264 }
00265
00266 }
00267 }
00268
00269 #endif // KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_