Profile.hpp
00001 //
00002 // Laser stripe position detected in a Frame.
00003 // Copyright (C) 2008-2010 AQSENSE, S.L.
00004 //
00005 #if !defined (AQSENSE_SAL3DPP_PROFILE_HPP)
00006 #define AQSENSE_SAL3DPP_PROFILE_HPP
00007 
00008 #if defined (_MSC_VER) && (_MSC_VER >= 1020)
00009 #pragma once
00010 #endif // _MSC_VER && _MSC_VER >= 1020
00011 
00012 #include <sal3d/profile.h>
00013 #include <cstddef>
00014 #include <stdexcept>
00015 #include <sal3dpp/Common.hpp>
00016 
00017 namespace sal3d
00018 {
00052     class Profile
00053     {
00054         public:
00056             typedef float value_type;
00058             typedef value_type *iterator;
00060             typedef const value_type *const_iterator;
00061 
00071             Profile (value_type *start, int length):
00072                 _length(length),
00073                 _values(start)
00074             {
00075                 Error e;
00076                 sal3d_profile_new_from_memory(start, length, &_profile, &e.e);
00077                 if (e.e.value < 0)
00078                     throw e;
00079             }
00080 
00093             explicit Profile (sal3d_profile profile):
00094                 _length (sal3d_profile_get_length (profile)),
00095                 _profile (profile),
00096                 _values (0)
00097             {
00098                 Error e;
00099                 sal3d_profile_get_values (_profile, &_values, &e.e);
00100                 if (e.e.value < 0)
00101                     throw e;
00102             }
00103 
00109             Profile (const Profile &profile):
00110                 _length (profile._length),
00111                 _profile (profile._profile),
00112                 _values (profile._values)
00113             {
00114                 Error e;
00115                 sal3d_profile_share (_profile, &e.e);
00116                 if (e.e.value < 0)
00117                     throw e;
00118             }
00119 
00126             ~Profile ()
00127             {
00128                 sal3d_profile_release (_profile);
00129             }
00130 
00140             sal3d_profile
00141             c_profile () const
00142             {
00143                 return _profile;
00144             }
00145 
00151             const_iterator
00152             begin () const
00153             {
00154                 return _values;
00155             }
00156 
00162             iterator
00163             begin ()
00164             {
00165                 return const_cast<iterator>(
00166                         static_cast<const Profile &>(*this).begin() );
00167             }
00168 
00174             const_iterator
00175             end () const
00176             {
00177                 return _values + length ();
00178             }
00179 
00185             iterator
00186             end ()
00187             {
00188                 return const_cast<iterator> (
00189                         static_cast<const Profile &>(*this).end() );
00190             }
00191 
00197             int
00198             length () const
00199             {
00200                 return _length;
00201             }
00202 
00210             Profile &
00211             operator= (const Profile &rhs)
00212             {
00213                 Error e;
00214                 sal3d_profile_share (rhs._profile, &e.e);
00215                 if (e.e.value < 0)
00216                     throw e;
00217                 
00218                 sal3d_profile_release (_profile);
00219                 _length = rhs._length;
00220                 _profile = rhs._profile;
00221                 _values = rhs._values;
00222 
00223                 return *this;
00224             }
00225 
00238             const value_type &
00239             operator[] (int index) const
00240             {
00241                 if ( length () <= index )
00242                 {
00243                     throw std::out_of_range ("profile's index not valid");
00244                 }
00245                 return *(begin () + index);
00246             }
00247 
00248 
00260             value_type &
00261             operator[] (int index)
00262             {
00263                 return const_cast<value_type &> (
00264                         static_cast<const Profile &>(*this)[index] );
00265             }
00266 
00267         private:
00269             int _length;
00271             sal3d_profile _profile;
00273             iterator _values;
00274     };
00275 }
00276 
00277 #endif // !AQSENSE_SAL3DPP_PROFILE_HPP