pascal: make size/data private

This commit is contained in:
Danny Robson 2016-03-17 18:10:46 +11:00
parent 61e1d6a303
commit b263bbef0a
2 changed files with 110 additions and 63 deletions

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "pascal.hpp"
@ -21,44 +21,38 @@
using util::parray;
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
template <typename T>
parray<T>::parray (size_t _size, T *_data):
size (_size),
data (_data)
m_size (_size),
m_data (_data)
{ ; }
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
template <typename T>
T&
parray<T>::operator[] (size_t idx) {
return data[idx];
}
template <typename T>
const T&
parray<T>::operator[] (size_t idx) const {
return data[idx];
parray<T>::operator[] (size_t idx)
{
return data ()[idx];
}
//-----------------------------------------------------------------------------
template <typename T>
T&
parray<T>::at (size_t idx) {
if (idx >= size)
throw std::out_of_range ("invalid index for parray");
return (*this)[idx];
const T&
parray<T>::operator[] (size_t idx) const
{
return data ()[idx];
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
const T&
parray<T>::at (size_t idx) const {
if (idx >= size)
T&
parray<T>::at (size_t idx)
{
if (idx >= size ())
throw std::out_of_range ("invalid index for parray");
return (*this)[idx];
@ -67,43 +61,90 @@ parray<T>::at (size_t idx) const {
//-----------------------------------------------------------------------------
template <typename T>
T*
parray<T>::begin (void) const {
return data;
const T&
parray<T>::at (size_t idx) const
{
if (idx >= size ())
throw std::out_of_range ("invalid index for parray");
return (*this)[idx];
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
T*
parray<T>::end (void) const {
return data + size;
parray<T>::begin (void)
{
return data ();
}
//-----------------------------------------------------------------------------
template <typename T>
T*
parray<T>::end (void)
{
return data () + size ();
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
const T*
parray<T>::cbegin (void) const
{
return data ();
}
//-----------------------------------------------------------------------------
template <typename T>
const T*
parray<T>::cbegin (void) const {
return data;
parray<T>::cend (void) const
{
return data () + size ();
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
const T*
parray<T>::cend (void) const {
return data + size;
parray<T>::data (void) const
{
return m_data;
}
//-----------------------------------------------------------------------------
template <typename T>
T*
parray<T>::data (void)
{
return m_data;
}
//-----------------------------------------------------------------------------
template <typename T>
size_t
parray<T>::size (void) const
{
return m_size;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
std::ostream&
util::operator<< (std::ostream &os, parray<T> p) {
os << "[" << p.size << ", " << std::hex << p.data << std::dec << "]";
util::operator<< (std::ostream &os, parray<T> p)
{
os << "[" << p.size () << ", " << std::hex << p.data () << std::dec << "]";
return os;
}
//-----------------------------------------------------------------------------
template std::ostream& util::operator<< (std::ostream&, parray<uint16_t>);
template std::ostream& util::operator<< (std::ostream&, parray<uint32_t>);
template std::ostream& util::operator<< (std::ostream&, parray<uint64_t>);
@ -112,28 +153,27 @@ template std::ostream& util::operator<< (std::ostream&, parray<const uint16_t>);
template std::ostream& util::operator<< (std::ostream&, parray<const uint32_t>);
template std::ostream& util::operator<< (std::ostream&, parray<const uint64_t>);
//-----------------------------------------------------------------------------
namespace util {
template struct parray<char>;
template struct parray<const char>;
template struct parray<int8_t>;
template struct parray<int16_t>;
template struct parray<int32_t>;
template struct parray<int64_t>;
///////////////////////////////////////////////////////////////////////////////
template class util::parray<char>;
template class util::parray<const char>;
template struct parray<uint8_t>;
template struct parray<uint16_t>;
template struct parray<uint32_t>;
template struct parray<uint64_t>;
template class util::parray<int8_t>;
template class util::parray<int16_t>;
template class util::parray<int32_t>;
template class util::parray<int64_t>;
template struct parray<const int8_t>;
template struct parray<const int16_t>;
template struct parray<const int32_t>;
template struct parray<const int64_t>;
template class util::parray<uint8_t>;
template class util::parray<uint16_t>;
template class util::parray<uint32_t>;
template class util::parray<uint64_t>;
template struct parray<const uint8_t>;
template struct parray<const uint16_t>;
template struct parray<const uint32_t>;
template struct parray<const uint64_t>;
}
template class util::parray<const int8_t>;
template class util::parray<const int16_t>;
template class util::parray<const int32_t>;
template class util::parray<const int64_t>;
template class util::parray<const uint8_t>;
template class util::parray<const uint16_t>;
template class util::parray<const uint32_t>;
template class util::parray<const uint64_t>;

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_PASCAL_HPP
@ -22,27 +22,34 @@
namespace util {
template <typename T>
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 <typename T>
std::ostream& operator<< (std::ostream&, util::parray<T>);
std::ostream&
operator<< (std::ostream&, util::parray<T>);
}