activemq-cpp-3.8.2
ArrayPointer.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_LANG_ARRAYPOINTER_H_
19 #define _DECAF_LANG_ARRAYPOINTER_H_
20 
21 #include <decaf/util/Config.h>
22 #include <decaf/lang/System.h>
27 #include <decaf/util/Comparator.h>
28 #include <decaf/util/Arrays.h>
29 #include <memory>
30 #include <typeinfo>
31 #include <algorithm>
32 
33 namespace decaf {
34 namespace lang {
35 
50  template< typename T >
51  class ArrayPointer {
52  private:
53 
54  struct ArrayData {
55  private:
56 
57  ArrayData(const ArrayData&);
58  ArrayData& operator= (const ArrayData&);
59 
60  public:
61 
62  T* value;
63  int length;
65 
66  ArrayData() : value(NULL), length(0), refs(1) {}
67  ArrayData(T* value, int length) : value(value), length(length), refs(1) {
68  if( value != NULL && length <= 0 ) {
70  __FILE__, __LINE__, "Non-NULL array pointer cannot have a size <= zero" );
71  }
72 
73  if( value == NULL && length > 0 ) {
75  __FILE__, __LINE__, "NULL array pointer cannot have a size > zero" );
76  }
77  }
78 
79  bool release() {
80  if (this->refs.decrementAndGet() < 1) {
81  return true;
82  }
83  return false;
84  }
85  };
86 
87  typedef void (*deletionFuncPtr)(ArrayData* p);
88 
89  private:
90 
91  ArrayData* array;
92 
93  // Pointer to our internal delete function, allows incompletes.
94  deletionFuncPtr onDelete;
95 
96  public:
97 
98  typedef T* PointerType; // type returned by operator->
99  typedef T& ReferenceType; // type returned by operator*
100  typedef const T& ConstReferenceType; // type returned by const operator*
101 
102  public:
103 
110  ArrayPointer() : array(new ArrayData()), onDelete(onDeleteFunc) {}
111 
119  ArrayPointer(int size) : array(NULL), onDelete(onDeleteFunc) {
120 
121  if (size == 0) {
122  return;
123  }
124 
125  try {
126  T* value = new T[size];
127  this->array = new ArrayData(value, size);
128  decaf::util::Arrays::fill(value, size, 0, size, T());
129  } catch (std::exception& ex) {
130  throw ex;
131  } catch (...) {
132  throw std::bad_alloc();
133  }
134  }
135 
146  ArrayPointer(int size, const T& fillWith) : array(NULL), onDelete(onDeleteFunc) {
147 
148  if (size == 0) {
149  return;
150  }
151 
152  try {
153  T* value = new T[size];
154  decaf::util::Arrays::fill(value, size, 0, size, fillWith);
155  this->array = new ArrayData(value, size);
156  } catch (std::exception& ex) {
157  throw ex;
158  } catch (...) {
159  throw std::bad_alloc();
160  }
161  }
162 
172  explicit ArrayPointer(const PointerType value, int size) : array(NULL), onDelete(onDeleteFunc) {
173 
174  try {
175  this->array = new ArrayData(value, size);
176  } catch (std::exception& ex) {
177  throw ex;
178  } catch (...) {
179  throw std::bad_alloc();
180  }
181  }
182 
187  ArrayPointer(const ArrayPointer& value) : array(value.array), onDelete(onDeleteFunc) {
188  this->array->refs.incrementAndGet();
189  }
190 
191  virtual ~ArrayPointer() {
192  if (this->array->release() == true) {
193  onDelete(this->array);
194  }
195  }
196 
208  void reset(T* value, int size = 0) {
209  ArrayPointer(value, size).swap(*this);
210  }
211 
223  T* release() {
224  T* temp = this->array->value;
225  this->array->value = NULL;
226  this->array->length = 0;
227  this->array->refs.set(1);
228  return temp;
229  }
230 
239  PointerType get() const {
240  return this->array->value;
241  }
242 
249  int length() const {
250  return this->array->length;
251  }
252 
257  void swap(ArrayPointer& value) {
258  std::swap(this->array, value.array);
259  }
260 
267  ArrayPointer clone() const {
268 
269  if (this->array->length == 0) {
270  return ArrayPointer();
271  }
272 
273  ArrayPointer copy(this->array->length);
274  decaf::lang::System::arraycopy(this->array->value, 0, copy.get(), 0, this->array->length);
275  return copy;
276  }
277 
283  if (this == (void*) &right) {
284  return *this;
285  }
286 
287  ArrayPointer temp(right);
288  temp.swap(*this);
289  return *this;
290  }
291  template< typename T1>
293  if (this == (void*) &right) {
294  return *this;
295  }
296 
297  ArrayPointer temp(right);
298  temp.swap(*this);
299  return *this;
300  }
301 
310  ReferenceType operator[](int index) {
311  if (this->array->value == NULL) {
313  __FILE__, __LINE__, "ArrayPointer operator& - Pointee is NULL." );
314  }
315 
316  if (index < 0 || this->array->length <= index) {
318  __FILE__, __LINE__, "Array Index %d is out of bounds for this array.", this->array->length );
319  }
320 
321  return this->array->value[index];
322  }
323  ConstReferenceType operator[](int index) const {
324  if( this->array->value == NULL ) {
326  __FILE__, __LINE__, "ArrayPointer operator& - Pointee is NULL." );
327  }
328 
329  if( index < 0 || this->array->length <= index ) {
331  __FILE__, __LINE__, "Array Index %d is out of bounds for this array.", this->array->length );
332  }
333 
334  return this->array->value[index];
335  }
336 
337  bool operator!() const {
338  return this->array->value == NULL;
339  }
340 
341  inline friend bool operator==(const ArrayPointer& left, const T* right) {
342  return left.get() == right;
343  }
344 
345  inline friend bool operator==(const T* left, const ArrayPointer& right) {
346  return left == right.get();
347  }
348 
349  inline friend bool operator!=(const ArrayPointer& left, const T* right) {
350  return left.get() != right;
351  }
352 
353  inline friend bool operator!=(const T* left, const ArrayPointer& right) {
354  return left != right.get();
355  }
356 
357  template<typename T1>
358  bool operator==(const ArrayPointer<T1>& right) const {
359  return this->array->value == right.get();
360  }
361 
362  template<typename T1>
363  bool operator!=(const ArrayPointer<T1>& right) const {
364  return this->array->value != right.get();
365  }
366 
367  private:
368 
369  // Internal Static deletion function.
370  static void onDeleteFunc(ArrayData* value) {
371  delete [] value->value;
372  delete value;
373  }
374 
375  };
376 
378  template<typename T, typename U>
379  inline bool operator==(const ArrayPointer<T>& left, const U* right) {
380  return left.get() == right;
381  }
382 
384  template<typename T, typename U>
385  inline bool operator==(const U* left, const ArrayPointer<T>& right) {
386  return right.get() == left;
387  }
388 
390  template<typename T, typename U>
391  inline bool operator!=(const ArrayPointer<T>& left, const U* right) {
392  return !(left.get() == right);
393  }
394 
396  template<typename T, typename U>
397  inline bool operator!=(const U* left, const ArrayPointer<T>& right) {
398  return right.get() != left;
399  }
400 
410  template< typename T >
411  class ArrayPointerComparator : public decaf::util::Comparator< ArrayPointer<T> > {
412  public:
413 
415 
416  // Allows for operator less on types that implement Comparable or provide
417  // a workable operator <
418  virtual bool operator() ( const ArrayPointer<T>& left, const ArrayPointer<T>& right ) const {
419  return left.get() < right.get();
420  }
421 
422  // Requires that the type in the pointer is an instance of a Comparable.
423  virtual int compare(const ArrayPointer<T>& left, const ArrayPointer<T>& right) const {
424  return left.get() < right.get() ? -1 : right.get() < left.get() ? 1 : 0;
425  }
426 
427  };
428 
429 }}
430 
432 namespace std{
433 
438  template< typename T >
439  struct less< decaf::lang::ArrayPointer<T> > {
440 
443  typedef bool result_type;
444 
446  const decaf::lang::ArrayPointer<T>& right) const {
447  return less<T*>()(left.get(), right.get());
448  }
449  };
450 }
451 
452 #endif /*_DECAF_LANG_ARRAYPOINTER_H_*/
friend bool operator==(const T *left, const ArrayPointer &right)
Definition: ArrayPointer.h:345
ArrayPointer & operator=(const ArrayPointer &right)
Assigns the value of right to this Pointer and increments the reference Count.
Definition: ArrayPointer.h:282
ArrayPointer(int size, const T &fillWith)
Create a new ArrayPointer instance and allocates an internal array that is sized using the passed in ...
Definition: ArrayPointer.h:146
virtual ~ArrayPointerComparator()
Definition: ArrayPointer.h:414
A comparison function, which imposes a total ordering on some collection of objects.
Definition: Comparator.h:40
virtual int compare(const ArrayPointer< T > &left, const ArrayPointer< T > &right) const
Definition: ArrayPointer.h:423
bool operator==(const ArrayPointer< T1 > &right) const
Definition: ArrayPointer.h:358
bool operator!() const
Definition: ArrayPointer.h:337
int decrementAndGet()
Atomically decrements by one the current value.
#define NULL
Definition: Config.h:33
Definition: ArrayPointer.h:432
ArrayPointer(const ArrayPointer &value)
Copy constructor.
Definition: ArrayPointer.h:187
static void arraycopy(const char *src, std::size_t srcPos, char *dest, std::size_t destPos, std::size_t length)
Copies the number of elements specified by length from the source array starting at the given source ...
friend bool operator!=(const T *left, const ArrayPointer &right)
Definition: ArrayPointer.h:353
static void fill(E *array, int size, const E &value)
Fills an array with the specified element.
Definition: Arrays.h:57
const T & ConstReferenceType
Definition: ArrayPointer.h:100
ArrayPointer()
Default Constructor.
Definition: ArrayPointer.h:110
Definition: IndexOutOfBoundsException.h:31
ArrayPointer(const PointerType value, int size)
Explicit Constructor, creates an ArrayPointer that contains value with a single reference.
Definition: ArrayPointer.h:172
int length() const
Returns the current size of the contained array or zero if the array is NULL.
Definition: ArrayPointer.h:249
An int value that may be updated atomically.
Definition: AtomicInteger.h:37
PointerType get() const
Gets the real array pointer that is contained within this Pointer.
Definition: ArrayPointer.h:239
decaf::lang::ArrayPointer< T > first_argument_type
Definition: ArrayPointer.h:441
ArrayPointer & operator=(const ArrayPointer< T1 > &right)
Definition: ArrayPointer.h:292
void reset(T *value, int size=0)
Resets the ArrayPointer to hold the new value.
Definition: ArrayPointer.h:208
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: ArrayPointer.h:51
Definition: IllegalArgumentException.h:31
bool operator()(const decaf::lang::ArrayPointer< T > &left, const decaf::lang::ArrayPointer< T > &right) const
Definition: ArrayPointer.h:445
ReferenceType operator[](int index)
Dereference Operator, returns a reference to the Contained value.
Definition: ArrayPointer.h:310
ConstReferenceType operator[](int index) const
Definition: ArrayPointer.h:323
friend bool operator==(const ArrayPointer &left, const T *right)
Definition: ArrayPointer.h:341
ArrayPointer(int size)
Create a new ArrayPointer instance and allocates an internal array that is sized using the passed in ...
Definition: ArrayPointer.h:119
Definition: NullPointerException.h:32
ArrayPointer clone() const
Creates a new ArrayPointer instance that is a clone of the value contained in this ArrayPointer...
Definition: ArrayPointer.h:267
This implementation of Comparator is designed to allows objects in a Collection to be sorted or teste...
Definition: ArrayPointer.h:411
void swap(ArrayPointer &value)
Exception Safe Swap Function.
Definition: ArrayPointer.h:257
T * PointerType
Definition: ArrayPointer.h:98
T & ReferenceType
Definition: ArrayPointer.h:99
bool result_type
Definition: ArrayPointer.h:443
friend bool operator!=(const ArrayPointer &left, const T *right)
Definition: ArrayPointer.h:349
bool operator!=(const ArrayPointer< T1 > &right) const
Definition: ArrayPointer.h:363
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
decaf::lang::ArrayPointer< T > second_argument_type
Definition: ArrayPointer.h:442
T * release()
Releases the Pointer held and resets the internal pointer value to Null.
Definition: ArrayPointer.h:223
virtual ~ArrayPointer()
Definition: ArrayPointer.h:191