contrib/mul/mbl/mbl_selected_data_wrapper.txx
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_selected_data_wrapper.txx
00002 #ifndef mbl_selected_data_wrapper_txx_
00003 #define mbl_selected_data_wrapper_txx_
00004 //:
00005 // \file
00006 
00007 #include "mbl_selected_data_wrapper.h"
00008 
00009 #include <vcl_iostream.h>
00010 #include <vcl_cstdlib.h>
00011 #include <vcl_cassert.h>
00012 
00013 //: Default constructor.
00014 template<class T>
00015 mbl_selected_data_wrapper<T>::mbl_selected_data_wrapper():
00016   data_(0), index_(0)
00017 {
00018 }
00019 
00020 //: Constructor.
00021 template<class T>
00022 mbl_selected_data_wrapper<T>::mbl_selected_data_wrapper(
00023   const mbl_data_wrapper<T>& data, const vcl_vector<unsigned> &selection)
00024   : data_(0)
00025 {
00026   set(data, selection);
00027 }
00028 
00029 //: Copy constructor.
00030 template<class T>
00031 mbl_selected_data_wrapper<T>::mbl_selected_data_wrapper(
00032   const mbl_selected_data_wrapper<T>& p)
00033   : mbl_data_wrapper<T>(), data_(0), index_(0)
00034 {
00035   *this = p;
00036 }
00037 
00038 template<class T>
00039 void mbl_selected_data_wrapper<T>::set(const mbl_data_wrapper<T>& data,
00040                                        const vcl_vector<unsigned> &selection)
00041 {
00042   delete data_;
00043   data_ = data.clone();
00044   selection_ = selection;
00045   reset();
00046 }
00047 
00048 //: Default destructor
00049 template<class T>
00050 mbl_selected_data_wrapper<T>::~mbl_selected_data_wrapper()
00051 {
00052   delete data_;
00053 }
00054 
00055 //: Copy operator
00056 template<class T>
00057 mbl_selected_data_wrapper<T>& mbl_selected_data_wrapper<T>::operator=(
00058                                        const mbl_selected_data_wrapper<T>& b)
00059 {
00060   if (this==&b) return *this;
00061 
00062   delete data_; data_=0;
00063   if (b.data_!=0)
00064     data_=b.data_->clone();
00065 
00066   index_ = b.index_;
00067   selection_ = b.selection_;
00068 
00069   return *this;
00070 }
00071 
00072 
00073 //: Number of objects available
00074 template<class T>
00075 unsigned long mbl_selected_data_wrapper<T>::size() const
00076 {
00077   return selection_.size();
00078 }
00079 
00080 //: Reset so that current() returns first object
00081 template<class T>
00082 void mbl_selected_data_wrapper<T>::reset()
00083 {
00084   index_ = 0;
00085   if (size() > 0)
00086     data_->set_index(selection_[index_]);
00087 }
00088 
00089 //: Return current object
00090 template<class T>
00091 const T& mbl_selected_data_wrapper<T>::current()
00092 {
00093   return data_->current();
00094 }
00095 
00096 //: Move to next object, returning true if is valid
00097 template<class T>
00098 bool mbl_selected_data_wrapper<T>::next()
00099 {
00100   if (index_ < size()-1)
00101   {
00102     index_++;
00103     data_->set_index(selection_[index_]);
00104     return true;
00105   }
00106   else
00107     return false;
00108 }
00109 
00110 //: Return current index
00111 template<class T>
00112 unsigned long mbl_selected_data_wrapper<T>::index() const
00113 {
00114   return index_;
00115 }
00116 
00117 //: Create copy on heap and return base pointer
00118 template<class T>
00119 mbl_data_wrapper<T>* mbl_selected_data_wrapper<T>::clone() const
00120 {
00121   return (mbl_data_wrapper<T> *)
00122     new mbl_selected_data_wrapper<T>(*this);
00123 }
00124 
00125 //: Move to element n
00126 //  First example has index 0
00127 template<class T>
00128 void mbl_selected_data_wrapper<T>::set_index(unsigned long n)
00129 {
00130   assert(n != ((unsigned)-1));
00131   if (n>=size())
00132   {
00133     vcl_cerr<<"mbl_selected_data_wrapper<T>::set_index(n) :\n"
00134             <<"  n = "<<n<<" not in range 0<=n<"<<size()<<vcl_endl;
00135     vcl_abort();
00136   }
00137   index_=n;
00138   data_->set_index(selection_[index_]);
00139 }
00140 
00141 template <class T>
00142 bool mbl_selected_data_wrapper<T>::is_class(vcl_string const& s) const
00143 {
00144   return s==is_a(); // no ref to parent's is_class() since that is pure virtual
00145 }
00146 
00147 
00148 #define MBL_SELECTED_DATA_WRAPPER_INSTANTIATE(T) \
00149 VCL_DEFINE_SPECIALIZATION vcl_string mbl_selected_data_wrapper<T >::is_a() const \
00150 { return vcl_string("mbl_selected_data_wrapper<" #T ">"); } \
00151 template class mbl_selected_data_wrapper< T >
00152 
00153 #endif // mbl_selected_data_wrapper_txx_