contrib/mul/vpdfl/vpdfl_builder_base.cxx
Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_builder_base.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Tim Cootes
00008 // \date 12-Apr-2001
00009 // \brief Base for classes to build vpdfl_pdf_base objects.
00010 
00011 #include "vpdfl_builder_base.h"
00012 
00013 #include <vsl/vsl_indent.h>
00014 #include <vsl/vsl_binary_loader.h>
00015 
00016 #include <vpdfl/vpdfl_axis_gaussian_builder.h>
00017 #include <vpdfl/vpdfl_gaussian_kernel_pdf_builder.h>
00018 #include <vpdfl/vpdfl_gaussian_builder.h>
00019 
00020 #include <mbl/mbl_read_props.h>
00021 #include <mbl/mbl_cloneables_factory.h>
00022 #include <mbl/mbl_parse_block.h>
00023 #include <mbl/mbl_exception.h>
00024 
00025 //=======================================================================
00026 
00027 short vpdfl_builder_base::version_no() const
00028 {
00029   return 1;
00030 }
00031 
00032 //=======================================================================
00033 
00034 void vsl_add_to_binary_loader(const vpdfl_builder_base& b)
00035 {
00036   vsl_binary_loader<vpdfl_builder_base>::instance().add(b);
00037 }
00038 
00039 //=======================================================================
00040 
00041 vcl_string vpdfl_builder_base::is_a() const
00042 {
00043   return vcl_string("vpdfl_builder_base");
00044 }
00045 
00046 //=======================================================================
00047 
00048 bool vpdfl_builder_base::is_class(vcl_string const& s) const
00049 {
00050   return s==vpdfl_builder_base::is_a();
00051 }
00052 
00053 //: Create a vpdfl_builder_base object given a config stream
00054 // \throw vcl_runtime_exception if parse error.
00055 vcl_auto_ptr<vpdfl_builder_base> vpdfl_builder_base::new_builder_from_stream(vcl_istream &is)
00056 {
00057   // This function should really be replaced by a general loader scheme
00058   // Ask Ian for examples from Manchester's private code base.
00059 
00060   //: This will store the constructed builder.
00061   vcl_auto_ptr<vpdfl_builder_base> builder;
00062 
00063   vcl_string type;
00064   is >> type;
00065 
00066   if (type == "vpdfl_axis_gaussian_builder")
00067   {
00068     builder = vcl_auto_ptr<vpdfl_builder_base>(new vpdfl_axis_gaussian_builder());
00069   }
00070   else if (type == "vpdfl_gaussian_kernel_pdf_builder")
00071   {
00072     builder = vcl_auto_ptr<vpdfl_builder_base>(new vpdfl_gaussian_kernel_pdf_builder());
00073   }
00074   else if (type == "vpdfl_gaussian_builder")
00075   {
00076     builder = vcl_auto_ptr<vpdfl_builder_base>(new vpdfl_gaussian_builder());
00077   }
00078   else
00079     mbl_exception_error(mbl_exception_no_name_in_factory(type,
00080       "vpdfl_axis_gaussian_builder, vpdfl_gaussian_kernel_pdf_builder, vpdfl_gaussian_builder"));
00081 
00082   //: Incoming properties
00083   mbl_read_props_type props(mbl_read_props(is));
00084   // which should be empty (until we add some relevant parameters)
00085   mbl_read_props_look_for_unused_props("vpdfl_builder_base::new_builder_from_stream", props, mbl_read_props_type());
00086   return builder;
00087 }
00088 
00089 //=======================================================================
00090 
00091 void vsl_b_write(vsl_b_ostream& bfs, const vpdfl_builder_base& b)
00092 {
00093   b.b_write(bfs);
00094 }
00095 
00096 //=======================================================================
00097 
00098 void vsl_b_read(vsl_b_istream& bfs, vpdfl_builder_base& b)
00099 {
00100   b.b_read(bfs);
00101 }
00102 
00103 //=======================================================================
00104 
00105 void vsl_print_summary(vcl_ostream& os,const vpdfl_builder_base& b)
00106 {
00107   os << b.is_a() << ": ";
00108   vsl_indent_inc(os);
00109   b.print_summary(os);
00110   vsl_indent_dec(os);
00111 }
00112 
00113 //=======================================================================
00114 
00115 void vsl_print_summary(vcl_ostream& os,const vpdfl_builder_base* b)
00116 {
00117   if (b)
00118     vsl_print_summary(os, *b);
00119   else
00120     os << "No vpdfl_builder_base defined.";
00121 }
00122 
00123 //=======================================================================
00124 
00125 //: Stream output operator for class reference
00126 vcl_ostream& operator<<(vcl_ostream& os,const vpdfl_builder_base& b)
00127 {
00128   vsl_print_summary(os,b);
00129   return os;
00130 }
00131 
00132 //=======================================================================
00133 
00134 //: Stream output operator for class pointer
00135 vcl_ostream& operator<<(vcl_ostream& os,const vpdfl_builder_base* b)
00136 {
00137   vsl_print_summary(os,b);
00138   return os;
00139 }
00140 
00141 //=======================================================================
00142 //: Create a vpdfl_builder_base object given a config stream (recursive style)
00143 //  Creates object, then uses config_from_stream(is) to set up internals
00144 vcl_auto_ptr<vpdfl_builder_base> vpdfl_builder_base::
00145   new_pdf_builder_from_stream(vcl_istream &is)
00146 {
00147   vcl_string name;
00148   is >> name;
00149   vcl_auto_ptr<vpdfl_builder_base> builder;
00150   try
00151   {
00152     builder = mbl_cloneables_factory<vpdfl_builder_base>::get_clone(name);
00153   }
00154   catch (const mbl_exception_no_name_in_factory & e)
00155   {
00156     throw (mbl_exception_parse_error( e.what() ));
00157   }
00158   builder->config_from_stream(is);
00159   return builder;
00160 }
00161 
00162 //=======================================================================
00163 //: Read initialisation settings from a stream.
00164 // The default implementation merely checks that no properties have
00165 // been specified.
00166 void vpdfl_builder_base::config_from_stream(
00167   vcl_istream & is)
00168 {
00169   vcl_string s = mbl_parse_block(is);
00170   if (s.empty() || s=="{}") return;
00171 
00172   throw mbl_exception_parse_error(
00173     this->is_a() + " expects no properties in initialisation,\n"
00174     "But the following properties were given:\n" + s);
00175 }
00176