core/vil/file_formats/vil_nitf2_field_definition.h
Go to the documentation of this file.
00001 // vil_nitf2: Written by Harry Voorhees (hlv@) and Rob Radtke (rob@) of
00002 // Stellar Science Ltd. Co. (stellarscience.com) for
00003 // Air Force Research Laboratory, 2005.
00004 
00005 #ifndef VIL_NITF2_FIELD_DEFINITION_H
00006 #define VIL_NITF2_FIELD_DEFINITION_H
00007 
00008 // Do not #include this file within another header file (to avoid
00009 // potential conflicts with the macro definitions below).
00010 
00011 // These macros make definitions more concise
00012 #define NITF_LOC  new vil_nitf2_location_formatter
00013 #define NITF_INT  new vil_nitf2_integer_formatter
00014 #define NITF_DBL  new vil_nitf2_double_formatter
00015 #define NITF_EXP  new vil_nitf2_exponential_formatter
00016 #define NITF_LONG new vil_nitf2_long_long_formatter
00017 #define NITF_CHAR new vil_nitf2_char_formatter
00018 #define NITF_BIN  new vil_nitf2_binary_formatter
00019 #define NITF_STR  new vil_nitf2_string_formatter
00020 #define NITF_ENUM new vil_nitf2_enum_string_formatter
00021 #define NITF_DAT  new vil_nitf2_date_time_formatter
00022 #define NITF_TRES new vil_nitf2_tagged_record_sequence_formatter
00023 
00024 #define NITF_STR_ECS(LEN)  NITF_STR(LEN, vil_nitf2_string_formatter::ECS)
00025 #define NITF_STR_ECSA(LEN) NITF_STR(LEN, vil_nitf2_string_formatter::ECSA)
00026 #define NITF_STR_BCS(LEN)  NITF_STR(LEN, vil_nitf2_string_formatter::BCS)
00027 #define NITF_STR_BCSA(LEN) NITF_STR(LEN, vil_nitf2_string_formatter::BCSA)
00028 
00029 #include <vcl_list.h>
00030 
00031 #include "vil_nitf2.h" // vil_nitf2_istream, vil_nitf2_ostream
00032 #include "vil_nitf2_field_functor.h"
00033 
00034 class vil_nitf2_field_definition;
00035 class vil_nitf2_field_definition_repeat_node;
00036 class vil_nitf2_field_formatter;
00037 
00038 //-----------------------------------------------------------------------------
00039 // An abstract base class that represents either
00040 // a field definition, condition, or repeat control.
00041 //
00042 class vil_nitf2_field_definition_node
00043 {
00044  public:
00045   enum node_type { type_field, type_repeat };
00046   vil_nitf2_field_definition_node(node_type type) : type(type) {}
00047   virtual ~vil_nitf2_field_definition_node() {}
00048 
00049   // Downcast test methods (for convenience)
00050   bool is_field_definition() const { return type==type_field; }
00051   bool is_repeat_node() const { return type==type_repeat; }
00052 
00053   // Downcast methods. Return 0 if conversion fails.
00054   vil_nitf2_field_definition* field_definition();
00055   vil_nitf2_field_definition_repeat_node* repeat_node();
00056 
00057   // Virtual copy method
00058   virtual vil_nitf2_field_definition_node* copy() const = 0;
00059 
00060   // Member variables
00061   node_type type;
00062 };
00063 
00064 //-----------------------------------------------------------------------------
00065 // Represents the definition of a particular field
00066 // (including a contiguous set of repeating fields) of a tagged record
00067 // extension, including the name, format, repeat count, or other condition.
00068 //
00069 class vil_nitf2_field_definition : public vil_nitf2_field_definition_node
00070 {
00071  public:
00072   // These members basically correspond to columns within a row of
00073   // a NITF tagged record spec. An instance of this class corresponds
00074   // to a row (or a sequence of contiguously repeating rows).
00075   vcl_string tag;
00076   vcl_string pretty_name;
00077   bool required;
00078   vil_nitf2_field_formatter* formatter;
00079   bool blanks_ok;
00080   vil_nitf2_field_functor<int>* width_functor;
00081   vil_nitf2_field_functor<bool>* condition_functor;
00082   vcl_string units;
00083   vcl_string description;
00084 
00085   bool is_required() const;
00086   bool is_variable_width() const;
00087 
00088   // Constructor. Assumes ownership of pointer arguments.
00089   vil_nitf2_field_definition(
00090     // field identifier, generally < 10 characters long
00091     vcl_string tag,
00092     // the field name, typically a few words long
00093     vcl_string pretty_name,
00094     // field type and format
00095     vil_nitf2_field_formatter* formatter,
00096     // whether this field may be unspecified (all blanks)
00097     bool blanks_ok = false,
00098     // function, when specified, that overrides formatter's field width.
00099     vil_nitf2_field_functor<int>* width_functor = 0,
00100     // conditional field predicate; 0 for required fields
00101     vil_nitf2_field_functor<bool>* condition_functor = 0,
00102     // additional documentation fields
00103     vcl_string units = "",
00104     vcl_string description = "");
00105 
00106   // Copy method
00107   vil_nitf2_field_definition_node* copy() const;
00108 
00109   // Destructor
00110   ~vil_nitf2_field_definition();
00111 };
00112 
00113 
00114 //-----------------------------------------------------------------------------
00115 // Represents the definition of a contiguous sequence of fields or nodes
00116 // containing other such sequences of fields. Methods field(), repeat(),
00117 // and cond() provide "syntactic sugar" for assembling the sequence.
00118 //
00119 class vil_nitf2_field_definitions : public vcl_list<vil_nitf2_field_definition_node*>
00120 {
00121  public:
00122   // Define a field and add it to this list of definitions, returning
00123   // the current list. Assumes ownership of pointer arguments.
00124   vil_nitf2_field_definitions& field(
00125     vcl_string tag,
00126     vcl_string pretty_name,
00127     vil_nitf2_field_formatter* formatter,
00128     // whether this field may be unspecified (all blank)
00129     bool blanks_ok = false,
00130     // function, when specified, that overrides formatter's field width
00131     vil_nitf2_field_functor<int>* width_functor = 0,
00132     // predicate that returns whether this conditional field is present;
00133     // 0 for required fields
00134     vil_nitf2_field_functor<bool>* condition_functor = 0,
00135     vcl_string units = "",
00136     vcl_string description = "");
00137 
00138   // Define a repeat node, with repeat count determined by repeat_functor,
00139   // and add it to this list of definitions, returning the current list.
00140   // Assumes ownership of pointer argument.
00141   vil_nitf2_field_definitions& repeat(vil_nitf2_field_functor<int>* repeat_functor,
00142                                       vil_nitf2_field_definitions& field_definitions);
00143 
00144   // Same as above where the repeat count is simply the value of a tag.
00145   vil_nitf2_field_definitions& repeat(vcl_string intTag,
00146                                       vil_nitf2_field_definitions& field_definitions)
00147   { return repeat(new vil_nitf2_field_value<int>(intTag), field_definitions); }
00148 
00149   // Copy constructor
00150   vil_nitf2_field_definitions(const vil_nitf2_field_definitions&);
00151 
00152   // Default constructor
00153   vil_nitf2_field_definitions() {}
00154 
00155   // Destructor
00156   virtual ~vil_nitf2_field_definitions();
00157 };
00158 
00159 //-----------------------------------------------------------------------------
00160 // Represents a sequence of fields that is repeated. The repeat count
00161 // is determined by a functor that evaluates previously processed tag(s).
00162 //
00163 class vil_nitf2_field_definition_repeat_node : public vil_nitf2_field_definition_node
00164 {
00165  public:
00166   // Construct a repeat node. Assumes ownership of pointer arguments.
00167   vil_nitf2_field_definition_repeat_node(vil_nitf2_field_functor<int>* repeat_functor,
00168                                          vil_nitf2_field_definitions* field_definitions)
00169     : vil_nitf2_field_definition_node(type_repeat),
00170       repeat_functor(repeat_functor),
00171       field_definitions(field_definitions) {}
00172 
00173   // Member variables
00174   vil_nitf2_field_functor<int>* repeat_functor;
00175   vil_nitf2_field_definitions* field_definitions;
00176 
00177   // Destructor
00178   ~vil_nitf2_field_definition_repeat_node();
00179 
00180   // Copy method
00181   vil_nitf2_field_definition_node* copy() const;
00182 };
00183 
00184 #endif // VIL_NITF2_FIELD_DEFINITION_H