1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// This is mul/mbl/mbl_data_array_ptr_wrapper.txx
#ifndef mbl_data_array_ptr_wrapper_txx_
#define mbl_data_array_ptr_wrapper_txx_
//:
// \file
#include "mbl_data_array_ptr_wrapper.h"
#include <vcl_iostream.h>
#include <vcl_cstdlib.h>
#include <vcl_cassert.h>
//: Default constructor
template<class T>
mbl_data_array_ptr_wrapper<T>::mbl_data_array_ptr_wrapper()
: data_(0),n_(0),index_(0)
{
}
//: Constructor
template<class T>
mbl_data_array_ptr_wrapper<T>::mbl_data_array_ptr_wrapper(const T*const* data, unsigned long n)
{
set(data,n);
}
//: Constructor
// Sets up object to wrap a vcl_vector.
// The data must be kept in scope, this does not take a copy.
template<class T>
mbl_data_array_ptr_wrapper<T>::mbl_data_array_ptr_wrapper(const vcl_vector<const T* > &data)
{
// There is nothing in the STL standard that says that vector<> has
// to store its data in a contiguous memory block. However, most
// implementations do store data this way.
// Check this assumption holds.
assert(data.size() == 0 || &data[data.size() - 1] + 1 == &data[0] + data.size());
set(&data[0], data.size());
}
//: Initialise to return elements from data[i]
template<class T>
void mbl_data_array_ptr_wrapper<T>::set(const T*const* data, unsigned long n)
{
assert (n != ((unsigned long)-1)); // a common mistake
data_ = data;
n_ = n;
index_ = 0;
}
//: Default destructor
template<class T>
mbl_data_array_ptr_wrapper<T>::~mbl_data_array_ptr_wrapper()
{
}
//: Number of objects available
template<class T>
unsigned long mbl_data_array_ptr_wrapper<T>::size() const
{
return n_;
}
//: Reset so that current() returns first object
template<class T>
void mbl_data_array_ptr_wrapper<T>::reset()
{
index_=0;
}
//: Return current object
template<class T>
const T& mbl_data_array_ptr_wrapper<T>::current()
{
return *data_[index_];
}
//: Move to next object, returning true if is valid
template<class T>
bool mbl_data_array_ptr_wrapper<T>::next()
{
return ++index_<n_;
}
//: Return current index
template<class T>
unsigned long mbl_data_array_ptr_wrapper<T>::index() const
{
return index_;
}
//: Create copy on heap and return base pointer
template<class T>
mbl_data_wrapper< T >* mbl_data_array_ptr_wrapper<T>::clone() const
{
return new mbl_data_array_ptr_wrapper<T>(*this);
}
//: Move to element n
// First example has index 0
template<class T>
void mbl_data_array_ptr_wrapper<T>::set_index(unsigned long n)
{
assert(n != ((unsigned)-1));
if (n>=n_)
{
vcl_cerr<<"mbl_data_array_ptr_wrapper<T>::set_index(n) :\n"
<<" n = "<<n<<" not in range 0<=n<"<<size()<<vcl_endl;
vcl_abort();
}
index_=n;
}
template <class T>
bool mbl_data_array_ptr_wrapper<T>::is_class(vcl_string const& s) const
{
return s==is_a(); // no ref to parent's is_class() since that is pure virtual
}
#define MBL_DATA_ARRAY_PTR_WRAPPER_INSTANTIATE(T) \
VCL_DEFINE_SPECIALIZATION vcl_string mbl_data_array_ptr_wrapper<T >::is_a() const \
{ return vcl_string("mbl_data_array_ptr_wrapper<" #T ">"); } \
template class mbl_data_array_ptr_wrapper< T >
#endif // mbl_data_array_ptr_wrapper_txx_
|