core/vil/file_formats/vil_nitf2_field_definition.cxx
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 #include "vil_nitf2_field_definition.h"
00006 #include "vil_nitf2_field_formatter.h"
00007 
00008 #include <vcl_cassert.h>
00009 
00010 //==============================================================================
00011 // Class vil_nitf2_field_definition_node
00012 
00013 vil_nitf2_field_definition* 
00014 vil_nitf2_field_definition_node::field_definition() 
00015 { 
00016   return is_field_definition() ? (vil_nitf2_field_definition*)this : 0; 
00017 }
00018 
00019 vil_nitf2_field_definition_repeat_node* 
00020 vil_nitf2_field_definition_node::repeat_node() 
00021 { 
00022   return is_repeat_node() ? (vil_nitf2_field_definition_repeat_node*)this : 0; 
00023 }
00024 
00025 //==============================================================================
00026 // Class vil_nitf2_field_definition
00027 
00028 vil_nitf2_field_definition::
00029 vil_nitf2_field_definition(vcl_string tag,
00030                            vcl_string pretty_name,
00031                            vil_nitf2_field_formatter* formatter,
00032                            bool blanks_ok,
00033                            vil_nitf2_field_functor<int>* width_functor,
00034                            vil_nitf2_field_functor<bool>* condition_functor,
00035                            vcl_string units,
00036                            vcl_string description)
00037   : vil_nitf2_field_definition_node(type_field),
00038     tag(tag), 
00039     pretty_name(pretty_name),
00040     formatter(formatter),
00041     blanks_ok(blanks_ok),
00042     width_functor(width_functor),
00043     condition_functor(condition_functor),
00044     units(units), 
00045     description(description)
00046 {
00047   assert(!tag.empty() && "vil_nitf2_field_definition:: null tag");
00048   assert(formatter != 0 && "vil_nitf2_field_definition:: null formatter");
00049 }
00050 
00051 vil_nitf2_field_definition_node* vil_nitf2_field_definition::copy() const
00052 {
00053   return new vil_nitf2_field_definition(
00054     tag, 
00055     pretty_name, 
00056     formatter->copy(), 
00057     blanks_ok, 
00058     width_functor ? width_functor->copy() : 0,
00059     condition_functor ? condition_functor->copy() : 0,
00060     units, 
00061     description);
00062 }
00063 
00064 bool vil_nitf2_field_definition::is_required() const
00065 {
00066   return condition_functor == 0;
00067 }
00068 
00069 bool vil_nitf2_field_definition::is_variable_width() const
00070 {
00071   return width_functor != 0;
00072 }
00073 
00074 vil_nitf2_field_definition::~vil_nitf2_field_definition()
00075 {
00076   delete formatter;
00077   delete width_functor;
00078   delete condition_functor;
00079 }
00080 
00081 //==============================================================================
00082 // Class vil_nitf2_field_definitions
00083 
00084 vil_nitf2_field_definitions::vil_nitf2_field_definitions(
00085   const vil_nitf2_field_definitions& that)
00086   : vcl_list<vil_nitf2_field_definition_node*>()
00087 {
00088   for (vcl_list<vil_nitf2_field_definition_node*>::const_iterator it = that.begin();
00089        it != that.end(); ++it) 
00090   {
00091     push_back((*it)->copy());
00092   }
00093 }
00094 
00095 
00096 vil_nitf2_field_definitions& vil_nitf2_field_definitions::field(
00097     vcl_string tag,
00098     vcl_string pretty_name,
00099     vil_nitf2_field_formatter* formatter,
00100     bool blanks_ok,
00101     vil_nitf2_field_functor<int>* width_functor,
00102     vil_nitf2_field_functor<bool>* condition_functor,
00103     vcl_string units,
00104     vcl_string description)
00105 {
00106   push_back(new vil_nitf2_field_definition(
00107                   tag, pretty_name, formatter, blanks_ok, 
00108                   width_functor, condition_functor, units, description));
00109   return *this;
00110 }
00111 
00112 vil_nitf2_field_definitions& 
00113 vil_nitf2_field_definitions::repeat(vil_nitf2_field_functor<int>* repeat_functor, 
00114                                     vil_nitf2_field_definitions& field_definitions ) 
00115 {
00116   push_back(new vil_nitf2_field_definition_repeat_node( 
00117                   repeat_functor, 
00118                   new vil_nitf2_field_definitions(field_definitions)));
00119   return *this;
00120 }
00121 
00122 vil_nitf2_field_definitions::~vil_nitf2_field_definitions()
00123 {
00124   for (vcl_list<vil_nitf2_field_definition_node*>::iterator it = begin();
00125        it != end(); ++it)
00126   {
00127     delete *it;
00128   }
00129 }
00130 
00131 //==============================================================================
00132 // Class vil_nitf2_field_definition_repeat_node
00133 
00134 vil_nitf2_field_definition_node* 
00135 vil_nitf2_field_definition_repeat_node::copy() const 
00136 {
00137   return new vil_nitf2_field_definition_repeat_node(
00138     repeat_functor->copy(),
00139     new vil_nitf2_field_definitions(*field_definitions));
00140 };
00141 
00142 vil_nitf2_field_definition_repeat_node::~vil_nitf2_field_definition_repeat_node()
00143 {
00144   delete repeat_functor;
00145   delete field_definitions;
00146 }