From b263bbef0a081548050fefecf4fd1a72a456ee19 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 17 Mar 2016 18:10:46 +1100 Subject: [PATCH] pascal: make size/data private --- pascal.cpp | 150 +++++++++++++++++++++++++++++++++-------------------- pascal.hpp | 23 +++++--- 2 files changed, 110 insertions(+), 63 deletions(-) diff --git a/pascal.cpp b/pascal.cpp index 8e20bdb6..4ac5af3b 100644 --- a/pascal.cpp +++ b/pascal.cpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ #include "pascal.hpp" @@ -21,44 +21,38 @@ using util::parray; -//----------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template parray::parray (size_t _size, T *_data): - size (_size), - data (_data) + m_size (_size), + m_data (_data) { ; } -//----------------------------------------------------------------------------- +/////////////////////////////////////////////////////////////////////////////// template T& -parray::operator[] (size_t idx) { - return data[idx]; -} - - -template -const T& -parray::operator[] (size_t idx) const { - return data[idx]; +parray::operator[] (size_t idx) +{ + return data ()[idx]; } //----------------------------------------------------------------------------- template -T& -parray::at (size_t idx) { - if (idx >= size) - throw std::out_of_range ("invalid index for parray"); - - return (*this)[idx]; +const T& +parray::operator[] (size_t idx) const +{ + return data ()[idx]; } +/////////////////////////////////////////////////////////////////////////////// template -const T& -parray::at (size_t idx) const { - if (idx >= size) +T& +parray::at (size_t idx) +{ + if (idx >= size ()) throw std::out_of_range ("invalid index for parray"); return (*this)[idx]; @@ -67,43 +61,90 @@ parray::at (size_t idx) const { //----------------------------------------------------------------------------- template -T* -parray::begin (void) const { - return data; +const T& +parray::at (size_t idx) const +{ + if (idx >= size ()) + throw std::out_of_range ("invalid index for parray"); + + return (*this)[idx]; } +/////////////////////////////////////////////////////////////////////////////// template T* -parray::end (void) const { - return data + size; +parray::begin (void) +{ + return data (); +} + + +//----------------------------------------------------------------------------- +template +T* +parray::end (void) +{ + return data () + size (); +} + + +/////////////////////////////////////////////////////////////////////////////// +template +const T* +parray::cbegin (void) const +{ + return data (); } //----------------------------------------------------------------------------- template const T* -parray::cbegin (void) const { - return data; +parray::cend (void) const +{ + return data () + size (); } +/////////////////////////////////////////////////////////////////////////////// template const T* -parray::cend (void) const { - return data + size; +parray::data (void) const +{ + return m_data; } //----------------------------------------------------------------------------- template +T* +parray::data (void) +{ + return m_data; +} + + +//----------------------------------------------------------------------------- +template +size_t +parray::size (void) const +{ + return m_size; +} + + +/////////////////////////////////////////////////////////////////////////////// +template std::ostream& -util::operator<< (std::ostream &os, parray p) { - os << "[" << p.size << ", " << std::hex << p.data << std::dec << "]"; +util::operator<< (std::ostream &os, parray p) +{ + os << "[" << p.size () << ", " << std::hex << p.data () << std::dec << "]"; return os; } +//----------------------------------------------------------------------------- template std::ostream& util::operator<< (std::ostream&, parray); template std::ostream& util::operator<< (std::ostream&, parray); template std::ostream& util::operator<< (std::ostream&, parray); @@ -112,28 +153,27 @@ template std::ostream& util::operator<< (std::ostream&, parray); template std::ostream& util::operator<< (std::ostream&, parray); template std::ostream& util::operator<< (std::ostream&, parray); -//----------------------------------------------------------------------------- -namespace util { - template struct parray; - template struct parray; - template struct parray; - template struct parray; - template struct parray; - template struct parray; +/////////////////////////////////////////////////////////////////////////////// +template class util::parray; +template class util::parray; - template struct parray; - template struct parray; - template struct parray; - template struct parray; +template class util::parray; +template class util::parray; +template class util::parray; +template class util::parray; - template struct parray; - template struct parray; - template struct parray; - template struct parray; +template class util::parray; +template class util::parray; +template class util::parray; +template class util::parray; - template struct parray; - template struct parray; - template struct parray; - template struct parray; -} +template class util::parray; +template class util::parray; +template class util::parray; +template class util::parray; + +template class util::parray; +template class util::parray; +template class util::parray; +template class util::parray; diff --git a/pascal.hpp b/pascal.hpp index ce84f22d..93fb217c 100644 --- a/pascal.hpp +++ b/pascal.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010 Danny Robson + * Copyright 2010-2016 Danny Robson */ #ifndef __UTIL_PASCAL_HPP @@ -22,27 +22,34 @@ namespace util { template - struct parray { + class parray { + public: parray (size_t size, T *data); - size_t size; - T *data; - T& operator[] (size_t idx); const T& operator[] (size_t idx) const; T& at (size_t idx); const T& at (size_t idx) const; - T* begin (void) const; - T* end (void) const; + T* begin (void); + T* end (void); const T* cbegin (void) const; const T* cend (void) const; + + const T* data (void) const; + T* data (void); + size_t size (void) const; + + private: + size_t m_size; + T *m_data; }; template - std::ostream& operator<< (std::ostream&, util::parray); + std::ostream& + operator<< (std::ostream&, util::parray); }