00001
00002
00003
00004
00005 #ifndef VIL_NITF2_FIELD_FUNCTOR_H
00006 #define VIL_NITF2_FIELD_FUNCTOR_H
00007
00008 class vil_nitf2_field;
00009 class vil_nitf2_enum_values;
00010 class vil_nitf2_index_vector;
00011
00012 #include <vcl_string.h>
00013 #include <vcl_vector.h>
00014 #include <vcl_map.h>
00015 #include "vil_nitf2_field_sequence.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 template<typename T>
00045 class vil_nitf2_field_functor
00046 {
00047 public:
00048 virtual bool operator() (vil_nitf2_field_sequence* record,
00049 const vil_nitf2_index_vector& indexes, T& out_value) = 0;
00050 virtual ~vil_nitf2_field_functor() {}
00051
00052
00053 virtual vil_nitf2_field_functor<T>* copy() const = 0;
00054 };
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 template<typename T>
00071 class vil_nitf2_field_value : public vil_nitf2_field_functor<T>
00072 {
00073 public:
00074 vil_nitf2_field_value(vcl_string tag) : tag(tag) {}
00075
00076 vil_nitf2_field_value(vcl_string tag, vcl_map<T, T> overrideMap )
00077 : tag(tag), overrides( overrideMap ) {}
00078
00079 virtual vil_nitf2_field_functor<T>* copy() const {
00080 return new vil_nitf2_field_value(tag, overrides); }
00081
00082 bool operator() (vil_nitf2_field_sequence* record,
00083 const vil_nitf2_index_vector& indexes, T& value)
00084 {
00085 bool success = record->get_value(tag, indexes, value, true);
00086 if( success ) {
00087
00088 typename vcl_map<T, T>::const_iterator it = overrides.find( value );
00089 if( it != overrides.end() ){
00090
00091 value = (*it).second;
00092 }
00093 }
00094 return success;
00095 }
00096
00097 private:
00098 vcl_string tag;
00099 vcl_map<T, T> overrides;
00100 };
00101
00102
00103
00104
00105
00106
00107
00108 class vil_nitf2_multiply_field_values : public vil_nitf2_field_functor<int>
00109 {
00110 public:
00111 vil_nitf2_multiply_field_values(const vcl_string& tag_1,
00112 const vcl_string& tag_2,
00113 bool use_zero_if_tag_not_found = false)
00114 : tag_1(tag_1),
00115 tag_2(tag_2),
00116 use_zero_if_tag_not_found(use_zero_if_tag_not_found) {}
00117
00118 vil_nitf2_field_functor<int>* copy() const {
00119 return new vil_nitf2_multiply_field_values(
00120 tag_1, tag_2, use_zero_if_tag_not_found);
00121 }
00122
00123 bool operator() (vil_nitf2_field_sequence* record,
00124 const vil_nitf2_index_vector& indexes, int& value);
00125
00126 private:
00127 vcl_string tag_1;
00128 vcl_string tag_2;
00129 bool use_zero_if_tag_not_found;
00130 };
00131
00132
00133
00134
00135
00136
00137
00138 class vil_nitf2_max_field_value_plus_offset_and_threshold : public vil_nitf2_field_functor<int>
00139 {
00140 public:
00141 vil_nitf2_max_field_value_plus_offset_and_threshold(
00142 vcl_string tag, int offset, int min_threshold = 0, int tag_factor = 1 )
00143 : tag(tag), offset(offset), min_threshold(min_threshold), tag_factor( tag_factor ) {}
00144
00145 vil_nitf2_field_functor<int>* copy() const {
00146 return new vil_nitf2_max_field_value_plus_offset_and_threshold(
00147 tag, offset, min_threshold, tag_factor); }
00148
00149 bool operator() (vil_nitf2_field_sequence* record,
00150 const vil_nitf2_index_vector& indexes, int& value);
00151
00152 private:
00153 vcl_string tag;
00154 int offset;
00155 int min_threshold;
00156 int tag_factor;
00157 };
00158
00159
00160
00161
00162
00163
00164
00165 template<typename T>
00166 class vil_nitf2_field_value_greater_than: public vil_nitf2_field_functor<bool>
00167 {
00168 public:
00169 vil_nitf2_field_value_greater_than(vcl_string tag, T threshold)
00170 : tag(tag), threshold(threshold) {}
00171
00172 vil_nitf2_field_functor<bool>* copy() const {
00173 return new vil_nitf2_field_value_greater_than(tag, threshold); }
00174
00175 bool operator() (vil_nitf2_field_sequence* record,
00176 const vil_nitf2_index_vector& indexes, bool& result) {
00177 T value;
00178 if (record->get_value(tag, indexes, value, true)) {
00179 result = value > threshold;
00180 return true;
00181 } else {
00182 return false;
00183 }
00184 }
00185 private:
00186 vcl_string tag;
00187 T threshold;
00188 };
00189
00190
00191
00192
00193
00194 class vil_nitf2_field_specified: public vil_nitf2_field_functor<bool>
00195 {
00196 public:
00197 vil_nitf2_field_specified(vcl_string tag) : tag(tag) {}
00198
00199 vil_nitf2_field_functor<bool>* copy() const {
00200 return new vil_nitf2_field_specified(tag); }
00201
00202 bool operator() (vil_nitf2_field_sequence* record,
00203 const vil_nitf2_index_vector& indexes, bool& result);
00204
00205 private:
00206 vcl_string tag;
00207 };
00208
00209
00210
00211
00212
00213
00214 template<typename T>
00215 class vil_nitf2_field_value_one_of: public vil_nitf2_field_functor<bool>
00216 {
00217 public:
00218
00219 vil_nitf2_field_value_one_of(vcl_string tag, vcl_vector<T> acceptable_values)
00220 : tag(tag), acceptable_values(acceptable_values) {};
00221
00222
00223 vil_nitf2_field_value_one_of(vcl_string tag, T acceptable_value)
00224 : tag(tag), acceptable_values(1, acceptable_value) {}
00225
00226 vil_nitf2_field_functor<bool>* copy() const {
00227 return new vil_nitf2_field_value_one_of(tag, acceptable_values); }
00228
00229 bool operator() (vil_nitf2_field_sequence* record,
00230 const vil_nitf2_index_vector& indexes, bool& result)
00231 {
00232 result = false;
00233 T val;
00234 if (record->get_value(tag, indexes, val, true)) {
00235 typename vcl_vector<T>::iterator it;
00236 for (it = acceptable_values.begin(); it != acceptable_values.end(); ++it) {
00237 if ((*it) == val) {
00238 result = true;
00239 break;
00240 }
00241 }
00242 return true;
00243 }
00244
00245 return false;
00246 }
00247
00248 protected:
00249 vcl_string tag;
00250 vcl_vector<T> acceptable_values;
00251 };
00252
00253
00254
00255
00256
00257
00258
00259
00260 template<typename T>
00261 class vil_nitf2_choose_field_value : public vil_nitf2_field_functor<T>
00262 {
00263 public:
00264
00265 vil_nitf2_choose_field_value(const vcl_string& tag_1, const vcl_string& tag_2,
00266 vil_nitf2_field_functor<bool>* choose_tag_1_predicate)
00267 : tag_1(tag_1), tag_2(tag_2), choose_tag_1_predicate(choose_tag_1_predicate) {}
00268
00269 vil_nitf2_field_functor<T>* copy() const {
00270 return new vil_nitf2_choose_field_value(
00271 tag_1, tag_2, choose_tag_1_predicate->copy()); }
00272
00273 virtual ~vil_nitf2_choose_field_value() {
00274 if (choose_tag_1_predicate) delete choose_tag_1_predicate;
00275 }
00276
00277 bool operator() (vil_nitf2_field_sequence* record,
00278 const vil_nitf2_index_vector& indexes, T& value) {
00279 bool choose_tag_1;
00280 if ((*choose_tag_1_predicate)(record, indexes, choose_tag_1)) {
00281 if (choose_tag_1) return record->get_value(tag_1, indexes, value, true);
00282 else return record->get_value(tag_2, indexes, value, true);
00283 } else return false;
00284 }
00285 private:
00286 vcl_string tag_1;
00287 vcl_string tag_2;
00288 vil_nitf2_field_functor<bool>* choose_tag_1_predicate;
00289 };
00290
00291
00292
00293
00294
00295 template<typename T>
00296 class vil_nitf2_constant_functor : public vil_nitf2_field_functor<T>
00297 {
00298 public:
00299 vil_nitf2_constant_functor(T value) : value_(value) {}
00300
00301 virtual vil_nitf2_constant_functor* copy() const {
00302 return new vil_nitf2_constant_functor(value_);
00303 }
00304
00305 virtual ~vil_nitf2_constant_functor() {}
00306
00307 bool operator() (vil_nitf2_field_sequence* ,
00308 const vil_nitf2_index_vector& , T& value) {
00309 value = value_;
00310 return true;
00311 }
00312
00313 private:
00314 T value_;
00315 };
00316
00317 #endif // VIL_NITF2_FIELD_FUNCTOR_H