00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #ifndef KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00036 #define KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_
00037
00038 #include "keyvalue/key/converter/StdVector.h"
00039 #include "keyvalue/key/generic/bound/NoBound.h"
00040 #include "keyvalue/key/Traits.h"
00041
00042 namespace keyvalue {
00043 namespace key {
00044
00111 template <typename ElementType, typename Monotone,
00112 template <typename> class Bound1 = NoBound,
00113 template <typename> class Bound2 = NoBound>
00114 class MonotoneBoundedVector: public Traits<ElementType, StdVector, NoMap> {
00115
00116 public:
00117
00127 explicit
00128 MonotoneBoundedVector(const string& name, size_t size = 0);
00129
00140 MonotoneBoundedVector(const string& name, const ElementType& bound1,
00141 size_t size = 0);
00142
00154 MonotoneBoundedVector(const string& name, const ElementType& bound1,
00155 const ElementType& bound2, size_t size = 0);
00156
00165 void
00166 checkSoFar(const StdVector<ElementType>& container) const;
00167
00177 void
00178 checkSize(size_t size) const;
00179
00180 private:
00181
00182 const size_t size_;
00183 const Bound1<ElementType> bound1_;
00184 const Bound2<ElementType> bound2_;
00185
00186 };
00187
00188
00189
00190
00191
00192 template <typename ElementType, typename Monotone,
00193 template <typename> class Bound1, template <typename> class Bound2>
00194 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00195 Bound2>::MonotoneBoundedVector(const string& name, size_t size) :
00196 MonotoneBoundedVector::Traits_(name),
00197 size_(size),
00198 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),
00208 size_(size),
00209 bound1_(bound1),
00210 bound2_() {
00211 }
00212
00213 template <typename ElementType, typename Monotone,
00214 template <typename> class Bound1, template <typename> class Bound2>
00215 MonotoneBoundedVector<ElementType, Monotone, Bound1,
00216 Bound2>::MonotoneBoundedVector(const string& name,
00217 const ElementType& bound1, const ElementType& bound2, size_t size) :
00218 MonotoneBoundedVector::Traits_(name),
00219 size_(size),
00220 bound1_(bound1),
00221 bound2_(bound2) {
00222 }
00223
00224
00225
00226
00227
00228 template <typename ElementType, typename Monotone,
00229 template <typename> class Bound1, template <typename> class Bound2>
00230 void
00231 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSoFar(
00232 const StdVector<ElementType>& container) const {
00233
00234 const vector<ElementType>& vector(*container.getOutput());
00235 const size_t size(vector.size());
00236
00237
00238 if (size > 1 && !Monotone::check(vector[size-2], vector[size-1]))
00239 throw RuntimeError() & "Values for key '" & Key::getName() &
00240 "' must be in " & Monotone::getName() & " order. Got " &
00241 vector[size-2] & " followed by " & vector[size-1] & "!";
00242
00243
00244 if (!bound1_.check(vector.back()))
00245 throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00246 "be " & bound1_.getName() & " " & bound1_.getBound() & ". Got " &
00247 vector.back() & "!";
00248
00249 if (!bound2_.check(vector.back()))
00250 throw RuntimeError() & "Value for key '" & Key::getName() & "' must "
00251 "be " & bound2_.getName() & " " & bound2_.getBound() & ". Got " &
00252 vector.back() & "!";
00253 }
00254
00255
00256
00257
00258
00259 template <typename ElementType, typename Monotone,
00260 template <typename> class Bound1, template <typename> class Bound2>
00261 void
00262 MonotoneBoundedVector<ElementType, Monotone, Bound1, Bound2>::checkSize(
00263 size_t size) const {
00264
00265 if (size_ != 0 && size_ != size)
00266 throw RuntimeError() & "Invalid number of values assigned to key '" &
00267 Key::getName() & "'! Expecting " & size_ & " got " & size & "!";
00268 }
00269
00270 }
00271 }
00272
00273 #endif // KEYVALUE_KEY_GENERIC_MONOTONEBOUNDEDVECTOR_H_