core/vil/file_formats/vil_nitf2_field_sequence.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_SEQUENCE_H
00006 #define VIL_NITF2_FIELD_SEQUENCE_H
00007 
00008 #include <vcl_map.h>
00009 // not used? #include <vcl_iostream.h>
00010 #include <vcl_string.h>
00011 #include <vcl_vector.h>
00012 
00013 #include "vil_nitf2.h" // vil_nitf2_istream, vil_nitf2_ostream
00014 #include "vil_nitf2_index_vector.h"
00015 #include "vil_nitf2_field.h"
00016 
00017 class vil_nitf2_field_definition;
00018 class vil_nitf2_field_definitions;
00019 class vil_nitf2_location;
00020 class vil_nitf2_date_time;
00021 class vil_nitf2_tagged_record_sequence;
00022 
00023 //-----------------------------------------------------------------------------
00024 // vil_nitf2_field_sequence represents the values of a contiguous list of fields,
00025 // such as found in a NITF file header, image subheader, or tagged record extension.
00026 //
00027 class vil_nitf2_field_sequence
00028 {
00029  public:
00030   // Constructor
00031   vil_nitf2_field_sequence(const vil_nitf2_field_definitions& field_definitions)
00032     : m_field_definitions(&field_definitions) {}
00033 
00034   // Destructor
00035   virtual ~vil_nitf2_field_sequence();
00036 
00037   // Sets out_value to the value of field specified by tag.
00038   // Returns 0 if such a field is not found or is of the wrong type.
00039   bool get_value(vcl_string tag, int& out_value) const;
00040   bool get_value(vcl_string tag, double& out_value) const;
00041   bool get_value(vcl_string tag, char& out_value) const;
00042   bool get_value(vcl_string tag, void*& out_value) const;
00043   bool get_value(vcl_string tag, vcl_string& out_value) const;
00044   bool get_value(vcl_string tag, vil_nitf2_location*& out_value) const;
00045   bool get_value(vcl_string tag, vil_nitf2_date_time& out_value) const;
00046   bool get_value(vcl_string tag, vil_nitf2_tagged_record_sequence& out_value) const;
00047 #if VXL_HAS_INT_64
00048   bool get_value(vcl_string tag, vil_nitf2_long& out_value) const;
00049 #endif
00050 
00051   // Sets out_value to the value of the array field element specified by tag and index.
00052   // Returns 0 if such a field is not found or is of the wrong type. If ignore_extra_indexes
00053   // is true, then it trims indexes, if necessary to match the dimensionality of the vector.
00054   // This option is used by vil_nitf2_field_functor so that when in a repeat loop, the
00055   // value in the nearest enclosing scope will be used. For instance, if called with
00056   // indexes (i,j), and if tag dimensionality equals 1, then value(i) will be returned.
00057   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00058                  int& out_value, bool ignore_extra_indexes = false) const;
00059   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00060                  double& out_value, bool ignore_extra_indexes = false) const;
00061   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00062                  char& out_value, bool ignore_extra_indexes = false) const;
00063   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00064                  void*& out_value, bool ignore_extra_indexes = false) const;
00065   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00066                  vcl_string& out_value, bool ignore_extra_indexes = false) const;
00067   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00068                  vil_nitf2_location*& out_value, bool ignore_extra_indexes = false) const;
00069   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00070                  vil_nitf2_date_time& out_value, bool ignore_extra_indexes = false) const;
00071 #if VXL_HAS_INT_64
00072   bool get_value(vcl_string tag, const vil_nitf2_index_vector& indexes,
00073                  vil_nitf2_long& out_value, bool ignore_extra_indexes = false) const;
00074 #endif
00075 
00076   // Sets out_value to a flattened list of the values of the array field element
00077   // specified by tag and index. If index is the empty vector, then out_values
00078   // hold all instances of the field. If index partially specifies value instances,
00079   // the out_values hold all instances of the field selected by the partial index.
00080   // Appends to, instead of clearing, out_values if clear_out_values is false.
00081   // Returns 0 if such a field is not found or is of the wrong type.
00082   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00083                   vcl_vector<int>& out_values, bool clear_out_values = true) const;
00084   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00085                   vcl_vector<double>& out_values, bool clear_out_values = true) const;
00086   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00087                   vcl_vector<char>& out_values, bool clear_out_values = true) const;
00088   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00089                   vcl_vector<void*>& out_values, bool clear_out_values = true) const;
00090   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00091                   vcl_vector<vcl_string>& out_values, bool clear_out_values = true) const;
00092   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00093                   vcl_vector<vil_nitf2_location*>& out_values, bool clear_out_values = true) const;
00094   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00095                   vcl_vector<vil_nitf2_date_time>& out_values, bool clear_out_values = true) const;
00096 #if VXL_HAS_INT_64
00097   bool get_values(vcl_string tag, const vil_nitf2_index_vector& indexes,
00098                   vcl_vector<vil_nitf2_long>& out_values, bool clear_out_values = true) const;
00099 #endif
00100 
00101   // Convenience overload of preceding methods, that set out_values to all
00102   // instances of array field element.
00103   bool get_values(vcl_string tag, vcl_vector<int>& out_values) const;
00104   bool get_values(vcl_string tag, vcl_vector<double>& out_values) const;
00105   bool get_values(vcl_string tag, vcl_vector<char>& out_values) const;
00106   bool get_values(vcl_string tag, vcl_vector<void*>& out_values) const;
00107   bool get_values(vcl_string tag, vcl_vector<vcl_string>& out_values) const;
00108   bool get_values(vcl_string tag, vcl_vector<vil_nitf2_location*>& out_values) const;
00109   bool get_values(vcl_string tag, vcl_vector<vil_nitf2_date_time>& out_values) const;
00110 #if VXL_HAS_INT_64
00111   bool get_values(vcl_string tag, vcl_vector<vil_nitf2_long>& out_values) const;
00112 #endif
00113 
00114 #if 0 //Not yet implemented.
00115   // Sets the value of the integer-valued field specified by tag to value.
00116   bool set_value(vcl_string tag, int value) { return false; }
00117 #endif // 0
00118 
00119   // Returns a field with specified tag, or 0 if not found.
00120   vil_nitf2_field* get_field(vcl_string tag) const;
00121 
00122   // Removes the field with the specified tag, returning whether successful.
00123   // Not yet implemented.
00124   //bool remove_field(vcl_string tag) { return false; }
00125 
00126   // Read field definition sequence from input stream. If this is called within
00127   // a repeat node, then indexes must equal the indexes must equal the
00128   // current repeat iteration(s) and field_defs must equal the repeat node's
00129   // field definitions only.
00130   bool read(vil_nitf2_istream& input,
00131             const vil_nitf2_field_definitions* field_defs = 0,
00132             vil_nitf2_index_vector indexes = vil_nitf2_index_vector());
00133 
00134   // Attempts to write field sequence to the output stream. Arg
00135   // 'indexes' is used only during recursive calls to write nested sequences.
00136   virtual bool write(vil_nitf2_ostream&,
00137                      const vil_nitf2_field_definitions* field_defs = 0,
00138                      vil_nitf2_index_vector indexes = vil_nitf2_index_vector());
00139 
00140   // Create vector fields for the specified field definitions with specified
00141   // number of dimensions. Return success.
00142   bool create_array_fields(const vil_nitf2_field_definitions* field_defs,
00143                            int num_dimensions);
00144 
00145   // Recursively set the specified dimension for vector fields to repeat_count.
00146   void set_array_fields_dimension(const vil_nitf2_field_definitions* field_defs,
00147                                   const vil_nitf2_index_vector& index, int repeat_count);
00148 
00149   // Returns field definition if found
00150   vil_nitf2_field_definition* find_field_definition(vcl_string tag);
00151 
00152   // I allocate the return value, but you own it after I return it to you
00153   // so you need to delete it.  If you pass in 'tr', then I'll add my stuff to that.
00154   // otherwise I'll add a new one and return it.  Either way, you own it.
00155   virtual vil_nitf2_field::field_tree* get_tree( vil_nitf2_field::field_tree* tr = 0 ) const;
00156 
00157  private:
00158   void insert_field( const vcl_string& str, vil_nitf2_field* field);
00159 
00160   // Map of fields, indexed by field name
00161   typedef vcl_map<vcl_string, vil_nitf2_field*> field_map;
00162   field_map fields;
00163 
00164   // Keeps track of the order in which fields are inserted into fields_map
00165   vcl_vector<vil_nitf2_field*> fields_vector;
00166 
00167   const vil_nitf2_field_definitions* m_field_definitions;
00168 };
00169 
00170 #endif // VIL_NITF2_FIELD_SEQUENCE_H