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_TYPED_SCALAR_FIELD_H 00006 #define VIL_NITF2_TYPED_SCALAR_FIELD_H 00007 00008 #include "vil_nitf2_scalar_field.h" 00009 #include "vil_nitf2_tagged_record.h" 00010 #include "vil_nitf2.h" 00011 #include <vcl_iosfwd.h> 00012 00013 // Typed concrete class for scalar NITF fields. 00014 // During file reading, this class is instantiated only for non-blank 00015 // fields that are successfully parsed. Thus, every instance of 00016 // vil_nitf2_scalar_field represents a valid value. 00017 // 00018 template<class T> 00019 class vil_nitf2_typed_scalar_field : public vil_nitf2_scalar_field 00020 { 00021 public: 00022 // Constructor 00023 vil_nitf2_typed_scalar_field(T value, vil_nitf2_field_definition* definition) 00024 : vil_nitf2_scalar_field(definition), m_value(value) {} 00025 00026 // Destructor 00027 ~vil_nitf2_typed_scalar_field() {} 00028 00029 // Set out_value to my value and return true. 00030 // (This is a partial override of overloaded method 00031 // vil_nitf2_scalar_field::value() for my specific type.) 00032 virtual bool value(T& out_value) const { 00033 out_value = m_value; 00034 return true; 00035 } 00036 00037 // Return my value 00038 // (was named value(), renamed to avoid an internal bcc compiler error) 00039 T get_value() const { return m_value; } 00040 00041 // Set value 00042 void set_value(const T& value) { m_value = value; } 00043 00044 // Output to stream. Overload as necessary. 00045 virtual vcl_ostream& output(vcl_ostream& os) const { return os << m_value; } 00046 00047 virtual field_tree* get_tree() const { return vil_nitf2_scalar_field::get_tree(); } 00048 private: 00049 T m_value; 00050 }; 00051 00052 //============================================================================== 00053 // implementation 00054 //============================================================================== 00055 00056 #include "vil_nitf2_compound_field_value.h" 00057 00058 // Overload for vil_nitf2_location* (Necessary because it's a pointer.) 00059 template<> 00060 inline vcl_ostream& vil_nitf2_typed_scalar_field<vil_nitf2_location*>::output(vcl_ostream& os) const 00061 { 00062 if (m_value==0) { 00063 os << m_value; 00064 } 00065 else { 00066 os << *m_value; 00067 } 00068 return os; 00069 } 00070 00071 template<> 00072 inline vil_nitf2_field::field_tree* 00073 vil_nitf2_typed_scalar_field<vil_nitf2_tagged_record_sequence>::get_tree() const 00074 { 00075 field_tree* tr = new field_tree; 00076 tr->columns.push_back( "TREs" ); 00077 vil_nitf2_tagged_record_sequence::const_iterator it; 00078 for ( it = m_value.begin() ; it != m_value.end() ; it++ ) { 00079 tr->children.push_back( (*it)->get_tree() ); 00080 } 00081 return tr; 00082 } 00083 00084 template<> 00085 inline vil_nitf2_typed_scalar_field<void*>::~vil_nitf2_typed_scalar_field() 00086 { 00087 // vector delete corresponds to new char[] of binary data 00088 delete[] (char*) m_value; 00089 } 00090 00091 template<> 00092 inline vil_nitf2_typed_scalar_field<vil_nitf2_location*>::~vil_nitf2_typed_scalar_field() 00093 { 00094 delete m_value; 00095 } 00096 00097 #endif // VIL_NITF2_TYPED_SCALAR_FIELD_H