📄 l3_array_base_s.h
字号:
#ifndef _INCLUDED_L3_ARRAY_BASE_S_H#define _INCLUDED_L3_ARRAY_BASE_S_H// Copyright (C) Krzysztof Bosak, 1999-07-22...2001-01-10.// All rights reserved.// kbosak@box43.pl// http://www.kbosak.prv.pl#include "l3_array_base.h"/////////////////////////////////////////////////////////////////////////////template<class type>class basearray_s: public basearray<type> // non-public is a must, but impossible with MSVC!{ // Same restrictions as for basearray<type>. // Last position is guarded by two sentinels, overwriting is detected. // Destructor is aggressive. // Useful for creating an array of chars and passing it as argument for // old C-style text management functions. // A must for true debugging. // Corresponds to some sort of basic C-style string with reliable error testing. // Is EXCEPTION NEUTRAL.private: type _sentinel_a; type _sentinel_b;public: explicit basearray_s(int requested_size=0); // EXCEPTION NEUTRAL basearray_s(int requested_size, const type * const arr); // EXCEPTION NEUTRAL basearray_s(const type& value, int requested_size); // EXCEPTION NEUTRAL inline type & operator[](int element_index) // EXCEPTION NEUTRAL { // Returns object contained in array by reference, changes are possible SENTINEL_TEST; INDEXING_TEST(_size-2); return _data[element_index]; } inline const type & operator[](int element_index) const // EXCEPTION NEUTRAL { // Returns object contained in array by reference, no changes to data SENTINEL_TEST; INDEXING_TEST(_size-2); return _data[element_index]; } inline void clear() // EXCEPTION NEUTRAL { SENTINEL_TEST; setsize(0); } inline int size() const// EXCEPTION NEUTRAL { // Returns current size of array SENTINEL_TEST; return _size-2; } inline int max_size() const // NOTHROW { // Returns maximal allowed size of array // Don't do SENTINEL_TEST; return basearray<type>::max_size()-2; } inline bool empty() const // EXCEPTION NEUTRAL { // Is array empty? SENTINEL_TEST; return size()==0; } inline int capacity() const // NOTHROW { // Returns current capacity of array SENTINEL_TEST; return capacity()-2; } void resize(int requested_size); // EXCEPTION NEUTRAL void reserve(int reserve_size); // EXCEPTION NEUTRAL void swap(basearray_s<type>& other); // EXCEPTION NEUTRAL // Efficient nonstandard extensions: void setsize(int requested_size); // not STL // EXCEPTION NEUTRAL inline void erase() // not STL // NOTHROW { assert(_data!=NULL); if(_size>2) { memset(_data, 0, sizeof(type)*(_size-2)); } }};template<class type>basearray_s<type>::basearray_s(int requested_size) // EXCEPTION NEUTRAL : basearray<type>(requested_size+2), _sentinel_a((type)('x')), _sentinel_b((type)('\0')){ // Constructor and not-so-trivial default constructor assert(requested_size>=0); assert(requested_size<=max_size()); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b; SENTINEL_TEST;}template<class type>basearray_s<type>::basearray_s(int requested_size, const type * const arr) // EXCEPTION NEUTRAL : basearray<type>(requested_size+2){ // Constructor from const C array _GarbageFillLoop(&_sentinel_a, 1); _GarbageFillLoop(&_sentinel_b, 1); assert(requested_size>=0); assert(requested_size<=max_size()); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b; assert(arr!=NULL); memcpy(_data, arr, (_size-2)*sizeof(type)); SENTINEL_TEST;}template<class type>basearray_s<type>::basearray_s(const type& value, int requested_size) // EXCEPTION NEUTRAL : basearray<type>(requested_size+2){ // Filling constructor _GarbageFillLoop(&_sentinel_a, 1); _GarbageFillLoop(&_sentinel_b, 1); assert(requested_size>=0); assert(requested_size<=max_size()); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b; _UnrolledFillLoop(_data, value, _size-2); SENTINEL_TEST;}template<class type>void basearray_s<type>::resize(int requested_size) // EXCEPTION NEUTRAL{ // Resizes and prevents content of array SENTINEL_TEST; assert(requested_size>=0); assert(requested_size<=max_size()); basearray<type>::resize(requested_size+2); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b;}template<class type>void basearray_s<type>::reserve(int reserve_size) // EXCEPTION NEUTRAL{ // Sets _capacity for max(reserve_size, _capacity), will ensure quick resizing in future for up to reserve_size array size SENTINEL_TEST; assert(reserve_size>=0); assert(reserve_size+2<=max_size()); if(reserve_size<=size()-2) { return; } basearray<type>::reserve(reserve_size+2); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b;}template<class type>void basearray_s<type>::swap(basearray_s<type>& other) // EXCEPTION NEUTRAL{ // Quick swap of two contents. SENTINEL_TEST; basearray<type>::swap(other); type temp=_sentinel_a; _sentinel_a=other._sentinel_a; other._sentinel_a=temp; temp=_sentinel_b; _sentinel_b=other._sentinel_b; other._sentinel_b=temp;}// Efficient nonstandard extensions:template<class type>void basearray_s<type>::setsize(int requested_size) // not STL // EXCEPTION NEUTRAL{ // Resizes, but does not prevents content of array, may preserwe some old, unused memory for future SENTINEL_TEST; assert(requested_size>=0); assert(requested_size<=max_size()); basearray<type>::setsize(requested_size+2); _data[_size-2]=_sentinel_a; _data[_size-1]=_sentinel_b;}/////////////////////////////////////////////////////////////////////////////#endif //_INCLUDED_L3_ARRAY_BASE_S_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -