/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2010-2016 Danny Robson */ #include "pascal.hpp" #include "debug.hpp" #include #include using util::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& util::operator<< (std::ostream &os, parray p) { std::copy_n ( p.cbegin (), p.size (), std::ostream_iterator (os) ); return os; } //----------------------------------------------------------------------------- template std::ostream& util::operator<< (std::ostream &os, parray p) { std::copy_n ( p.cbegin (), p.size (), std::ostream_iterator (os) ); return os; } /////////////////////////////////////////////////////////////////////////////// template std::ostream& util::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& util::operator<< (std::ostream&, parray< D,S>); \ template std::ostream& util::operator<< (std::ostream&, parray); \ \ template class util::parray< D,S>; \ template class util::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)