12 #ifndef ECL_CONTAINERS_ARRAY_DYNAMIC_MEM_CHECK_HPP_ 13 #define ECL_CONTAINERS_ARRAY_DYNAMIC_MEM_CHECK_HPP_ 22 #include <ecl/config/macros.hpp> 23 #include <ecl/utilities/blueprints.hpp> 24 #include "../definitions.hpp" 25 #include "../initialiser.hpp" 26 #include "../stencil.hpp" 41 namespace blueprints {
43 template <
typename Type>
class ConstantDynamicArray;
47 namespace formatters {
49 template <
typename Type,
size_t N>
class ArrayFormatter;
61 template<
typename Type>
72 static blueprints::ConstantDynamicArray<Type> Constant(
size_t size,
const Type &value) {
73 return blueprints::ConstantDynamicArray<Type>(size,value);
75 virtual ~BluePrintFactory() {};
127 template<
typename Type>
133 typedef Type value_type;
134 typedef Type* iterator;
135 typedef const Type* const_iterator;
136 typedef Type& reference;
137 typedef const Type& const_reference;
138 typedef std::size_t size_type;
139 typedef std::ptrdiff_t difference_type;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
142 typedef formatters::ArrayFormatter<Type,DynamicStorage>
Formatter;
145 typedef BluePrintFactory< Array<Type,DynamicStorage> > Factory;
155 explicit Array() : buffer_size(0), underrun(NULL), buffer(NULL), overrun(NULL) {}
165 buffer_size(reserve_size),
170 underrun = (
char*) malloc(buffer_size*
sizeof(Type)+bufferUnderrunLength()+bufferOverrunLength());
172 buffer = (Type*) (underrun+bufferUnderrunLength());
173 overrun = (
char*) underrun+bufferUnderrunLength()+buffer_size*
sizeof(Type);
178 initialiseMagicSections();
187 Array(
const Array<Type,DynamicStorage>& array) : buffer_size(0), underrun(NULL), buffer(NULL), overrun(NULL) {
188 if ( array.size() != 0 ) {
190 std::copy(array.begin(),array.end(),begin());
214 Array(
const blueprints::ArrayBluePrint< T > &blueprint) : buffer_size(0), underrun(NULL), buffer(NULL), overrun(NULL) {
220 blueprint.implementApply(*
this);
221 initialiseMagicSections();
230 if ( underrun != NULL ) {
253 containers::BoundedListInitialiser<value_type,iterator,DynamicStorage>
operator<<(
const Type &value) {
254 return containers::BoundedListInitialiser<value_type,iterator,DynamicStorage>(value,buffer,buffer_size);
257 void operator=(
const Array<Type,DynamicStorage>& array) {
258 if ( array.size() == 0 ) {
262 std::copy(array.begin(),array.end(),begin());
297 return buffer+buffer_size;
307 return buffer+buffer_size;
317 return reverse_iterator(end());
327 return const_reverse_iterator(end());
337 return reverse_iterator(begin());
347 return const_reverse_iterator(begin());
381 return buffer[buffer_size-1];
391 return buffer[buffer_size-1];
408 Stencil< Array<Type,DynamicStorage> > stencil(
const unsigned int& start_index,
const unsigned int& n)
ecl_assert_throw_decl(StandardException) {
411 return Stencil< Array<Type,DynamicStorage> >(*
this, begin()+start_index, begin()+start_index+n);
451 reference at(size_type i)
throw(StandardException) {
452 if ( i>=buffer_size ) {
469 const_reference at(size_type i)
const throw(StandardException) {
470 if ( i>=buffer_size ) {
486 size_type
size()
const {
return buffer_size; }
497 if ( underrun != NULL ) {
501 underrun = (
char*) malloc(buffer_size*
sizeof(Type)+bufferUnderrunLength()+bufferOverrunLength());
503 buffer = (Type*) (underrun+bufferUnderrunLength());
504 overrun = (
char*) underrun+bufferUnderrunLength()+buffer_size*
sizeof(Type);
509 initialiseMagicSections();
517 if ( underrun != NULL ) {
536 template <
typename OutputStream,
typename ElementType>
537 friend OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array);
542 bool bufferUnderRun();
543 bool bufferOverRun();
544 bool bufferOverFlow();
547 void initialiseMagicSections();
549 static const unsigned int& bufferOverrunLength() {
550 static const unsigned int buffer_overrun_length = 4;
551 return buffer_overrun_length;
553 static const unsigned int& bufferUnderrunLength() {
554 static const unsigned int buffer_underrun_length = 4;
555 return buffer_underrun_length;
557 static const char& magicChar() {
558 static const char magic_char = 0xCC;
561 unsigned int buffer_size;
576 template<
typename Type>
580 for (
unsigned int i = 0; i < bufferUnderrunLength(); ++i ) {
581 *(underrun+i) = magicChar();
583 for (
unsigned int i = 0; i < bufferOverrunLength(); ++i ) {
584 *(overrun+i) = magicChar();
595 template<
typename Type>
597 if ( underrun != NULL ) {
598 for (
unsigned int i = 0; i < bufferUnderrunLength(); ++i ) {
599 if ( *(underrun+i) != magicChar() ) {
615 template<
typename Type>
618 if ( overrun != NULL ) {
619 for (
unsigned int i = 0; i < bufferOverrunLength(); ++i ) {
620 if ( *(overrun+i) != magicChar() ) {
637 template<
typename Type>
639 return ( bufferOverRun() || bufferUnderRun() );
647 template <
typename OutputStream,
typename ElementType>
648 OutputStream& operator<<(OutputStream &ostream , const Array<ElementType,DynamicStorage> &array) {
651 for(
size_t i = 0; i < array.buffer_size; ++i )
653 ostream << array[i] <<
" ";
665 namespace blueprints {
678 template <
typename Type>
679 class ConstantDynamicArray:
public ArrayBluePrint< ConstantDynamicArray<Type> > {
690 ConstantDynamicArray() {};
701 ConstantDynamicArray(
size_t size,
const Type &value ) :
706 virtual ~ConstantDynamicArray() {};
718 base_type instantiate() {
720 std::fill_n(array.
begin(),reserve_size,val);
732 void apply(base_type& array)
const {
733 array.resize(reserve_size);
734 std::fill_n(array.begin(),reserve_size,val);
Fixed size container with a few bells and whistles.
bool bufferOverRun()
Manual handle to check for buffer overruns.
ecl_geometry_PUBLIC void resize(Trajectory2D &trajectory, const int &size)
#define LOC
Stringify the line of code you are at.
OutputStream & operator<<(OutputStream &ostream, const Void void_object)
Output stream operator for Void objects.
bool bufferOverFlow()
Manual handle to check for buffer under/over runs simultaneously.
void initialiseMagicSections()
Prepares the array for overrun and underrun detection.
#define ecl_assert_throw(expression, exception)
Debug mode throw with a logical condition check.
ecl_geometry_PUBLIC int size(const Trajectory2D &trajectory)
#define ecl_assert_throw_decl(exception)
Assure throw exception declaration.
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
bool bufferUnderRun()
Manual handle to check for buffer underruns.
MatrixFormatter< Derived, Scalar > Formatter