/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2010-2016 Danny Robson */ #include "pascal.hpp" #include "debug.hpp" #include #include using cruft::parray; /////////////////////////////////////////////////////////////////////////////// template parray::parray (SizeT _size, DataT *_data): m_size (_size), m_data (_data) { ; } /////////////////////////////////////////////////////////////////////////////// template DataT& parray::operator[] (SizeT idx) { CHECK_LIMIT(idx, 0u, m_size); return data ()[idx]; } //----------------------------------------------------------------------------- template const DataT& parray::operator[] (SizeT idx) const { CHECK_LIMIT(idx, 0u, m_size); return data ()[idx]; } /////////////////////////////////////////////////////////////////////////////// template DataT& parray::at (SizeT idx) { if (idx >= size ()) throw std::out_of_range ("invalid index for parray"); return (*this)[idx]; } //----------------------------------------------------------------------------- template const DataT& parray::at (SizeT idx) const { if (idx >= size ()) throw std::out_of_range ("invalid index for parray"); return (*this)[idx]; } /////////////////////////////////////////////////////////////////////////////// template DataT* parray::begin (void) { return data (); } //----------------------------------------------------------------------------- template DataT* parray::end (void) { return data () + size (); } /////////////////////////////////////////////////////////////////////////////// template const DataT* parray::cbegin (void) const { return data (); } //----------------------------------------------------------------------------- template const DataT* parray::cend (void) const { return data () + size (); } /////////////////////////////////////////////////////////////////////////////// template const DataT* parray::data (void) const { return m_data; } //----------------------------------------------------------------------------- template DataT* parray::data (void) { return m_data; } //----------------------------------------------------------------------------- template SizeT parray::size (void) const { return m_size; } /////////////////////////////////////////////////////////////////////////////// template std::ostream& cruft::operator<< (std::ostream &os, parray p) { std::copy_n ( p.cbegin (), p.size (), std::ostream_iterator (os) ); return os; } //----------------------------------------------------------------------------- template std::ostream& cruft::operator<< (std::ostream &os, parray p) { std::copy_n ( p.cbegin (), p.size (), std::ostream_iterator (os) ); return os; } /////////////////////////////////////////////////////////////////////////////// template std::ostream& cruft::operator<< (std::ostream &os, parray p) { auto size = p.size (); uintptr_t ptr = reinterpret_cast (p.data ()); os << "[" << +size << ", " << std::hex << ptr << std::dec << "]"; return os; } /////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE_D_S(D,S) \ template std::ostream& cruft::operator<< (std::ostream&, parray< D,S>); \ template std::ostream& cruft::operator<< (std::ostream&, parray); \ \ template class cruft::parray< D,S>; \ template class cruft::parray; //----------------------------------------------------------------------------- #define INSTANTIATE_D(D) \ INSTANTIATE_D_S(D, uint16_t) \ INSTANTIATE_D_S(D, uint32_t) \ INSTANTIATE_D_S(D, uint64_t) //----------------------------------------------------------------------------- INSTANTIATE_D (char) INSTANTIATE_D (uint8_t) INSTANTIATE_D (uint16_t) INSTANTIATE_D (uint32_t) INSTANTIATE_D (uint64_t) INSTANTIATE_D (int8_t) INSTANTIATE_D (int16_t) INSTANTIATE_D (int32_t) INSTANTIATE_D (int64_t)