Apache Ignite C++
reference.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 
23 #ifndef _IGNITE_COMMON_REFERENCE
24 #define _IGNITE_COMMON_REFERENCE
25 
26 #include <cstddef>
27 
28 #include <ignite/common/common.h>
29 #include <ignite/common/concurrent.h>
30 #include <ignite/common/reference_impl.h>
31 
32 namespace ignite
33 {
34  template<typename T>
35  class Reference;
36 
46  template<typename T>
48  {
49  template<typename>
50  friend class ConstReference;
51 
52  template<typename>
53  friend class Reference;
54 
55  public:
60  ptr(),
61  offset(0)
62  {
63  // No-op.
64  }
65 
72  explicit ConstReference(common::ConstReferenceImplBase* ptr, ptrdiff_t offset = 0) :
73  ptr(ptr),
74  offset(offset)
75  {
76  // No-op.
77  }
78 
85  ptr(other.ptr),
86  offset(other.offset)
87  {
88  // No-op.
89  }
90 
99  template<typename T2>
101  ptr(other.ptr),
102  offset(other.offset)
103  {
104  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
105  T* p1 = static_cast<T*>(p0);
106 
107  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
108  offset += diff;
109  }
110 
117  {
118  ptr = other.ptr;
119  offset = other.offset;
120 
121  return *this;
122  }
123 
132  template<typename T2>
134  {
135  ptr = other.ptr;
136  offset = other.offset;
137 
138  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
139  T* p1 = static_cast<T*>(p0);
140 
141  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
142  offset += diff;
143 
144  return *this;
145  }
146 
151  {
152  // No-op.
153  }
154 
163  const T* Get() const
164  {
165  return reinterpret_cast<const T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
166  }
167 
173  bool IsNull() const
174  {
175  const common::ConstReferenceImplBase* raw = ptr.Get();
176 
177  return !raw || !raw->Get();
178  }
179 
180  private:
182  common::concurrent::SharedPointer<common::ConstReferenceImplBase> ptr;
183 
185  ptrdiff_t offset;
186  };
187 
196  template<typename T>
197  class Reference
198  {
199  template<typename>
200  friend class Reference;
201  public:
206  ptr(),
207  offset(0)
208  {
209  // No-op.
210  }
211 
218  explicit Reference(common::ReferenceImplBase* ptr, ptrdiff_t offset = 0) :
219  ptr(ptr),
220  offset(offset)
221  {
222  // No-op.
223  }
224 
230  Reference(const Reference& other) :
231  ptr(other.ptr),
232  offset(other.offset)
233  {
234  // No-op.
235  }
236 
244  template<typename T2>
245  Reference(const Reference<T2>& other) :
246  ptr(other.ptr),
247  offset(other.offset)
248  {
249  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
250  T* p1 = static_cast<T*>(p0);
251 
252  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
253  offset += diff;
254  }
255 
262  {
263  ptr = other.ptr;
264  offset = other.offset;
265 
266  return *this;
267  }
268 
276  template<typename T2>
278  {
279  ptr = other.ptr;
280  offset = other.offset;
281 
282  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
283  T* p1 = static_cast<T*>(p0);
284 
285  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
286  offset += diff;
287 
288  return *this;
289  }
290 
295  {
296  // No-op.
297  }
298 
306  template<typename T2>
308  {
310 
311  cr.ptr = ptr;
312  cr.offset = offset;
313 
314  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
315  const T* p1 = static_cast<T*>(p0);
316 
317  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
318  cr.offset -= diff;
319 
320  return cr;
321  }
322 
331  const T* Get() const
332  {
333  return reinterpret_cast<const T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
334  }
335 
344  T* Get()
345  {
346  return reinterpret_cast<T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
347  }
348 
354  bool IsNull() const
355  {
356  const common::ReferenceImplBase* raw = ptr.Get();
357 
358  return !raw || !raw->Get();
359  }
360 
361  private:
363  common::concurrent::SharedPointer<common::ReferenceImplBase> ptr;
364 
366  ptrdiff_t offset;
367  };
368 
383  template<typename T>
385  {
386  common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
387 
389 
390  impl->Swap(ptr);
391 
392  return res;
393  }
394 
409  template<typename T>
411  {
412  common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
413 
415 
416  impl->Swap(ptr);
417 
418  return res;
419  }
420 
430  template<typename T>
432  {
433  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(new T(val));
434 
435  return Reference<T>(impl);
436  }
437 
447  template<typename T>
449  {
450  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(new T(val));
451 
452  return ConstReference<T>(impl);
453  }
454 
465  template<typename T>
467  {
468  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
469 
470  return Reference<T>(impl);
471  }
472 
483  template<typename T>
485  {
486  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
487 
488  return ConstReference<T>(impl);
489  }
490 
501  template<typename T>
503  {
504  common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(&val);
505 
506  return Reference<T>(impl);
507  }
508 
519  template<typename T>
521  {
522  common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(val);
523 
524  return Reference<T>(impl);
525  }
526 
537  template<typename T>
539  {
540  common::ConstReferenceNonOwningRawPointer<T>* impl = new common::ConstReferenceNonOwningRawPointer<T>(&val);
541 
542  return ConstReference<T>(impl);
543  }
544 
555  template<typename T>
557  {
558  common::ConstReferenceNonOwningRawPointer<T>* impl = new common::ConstReferenceNonOwningRawPointer<T>(val);
559 
560  return ConstReference<T>(impl);
561  }
562 }
563 
564 #endif //_IGNITE_COMMON_REFERENCE
ignite::MakeConstReferenceFromOwningPointer
ConstReference< T > MakeConstReferenceFromOwningPointer(T *val)
Make ignite::ConstReference instance out of pointer and pass its ownership.
Definition: reference.h:484
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::ConstReference::ConstReference
ConstReference(common::ConstReferenceImplBase *ptr, ptrdiff_t offset=0)
Constructor.
Definition: reference.h:72
ignite::Reference::~Reference
~Reference()
Destructor.
Definition: reference.h:294
ignite::MakeReference
Reference< T > MakeReference(T &val)
Make ignite::Reference instance out of reference.
Definition: reference.h:502
ignite::ConstReference::ConstReference
ConstReference()
Default constructor.
Definition: reference.h:59
ignite::MakeConstReference
ConstReference< T > MakeConstReference(const T &val)
Make ignite::ConstReference instance out of constant reference.
Definition: reference.h:538
ignite::MakeReferenceFromSmartPointer
Reference< typename T::element_type > MakeReferenceFromSmartPointer(T ptr)
Make ignite::Reference instance out of smart pointer.
Definition: reference.h:384
ignite::ConstReference::ConstReference
ConstReference(const ConstReference &other)
Copy constructor.
Definition: reference.h:84
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference &other)
Assignment operator.
Definition: reference.h:116
ignite::Reference::operator=
Reference & operator=(const Reference< T2 > &other)
Assignment operator.
Definition: reference.h:277
ignite::Reference::Reference
Reference(const Reference &other)
Copy constructor.
Definition: reference.h:230
ignite::Reference::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:331
ignite::Reference::Reference
Reference()
Default constructor.
Definition: reference.h:205
ignite::MakeConstReferenceFromSmartPointer
ConstReference< typename T::element_type > MakeConstReferenceFromSmartPointer(T ptr)
Make ignite::ConstReference instance out of smart pointer.
Definition: reference.h:410
ignite::MakeReferenceFromCopy
Reference< T > MakeReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::Reference instance.
Definition: reference.h:431
ignite::Reference::Get
T * Get()
Dereference the pointer.
Definition: reference.h:344
ignite::Reference::Reference
Reference(const Reference< T2 > &other)
Copy constructor.
Definition: reference.h:245
ignite::MakeConstReferenceFromCopy
ConstReference< T > MakeConstReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::ConstReference instance.
Definition: reference.h:448
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference< T2 > &other)
Assignment operator.
Definition: reference.h:133
ignite::ConstReference::IsNull
bool IsNull() const
Check if the pointer is null.
Definition: reference.h:173
ignite::ConstReference::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:163
ignite::Reference::IsNull
bool IsNull() const
Check if the pointer is null.
Definition: reference.h:354
ignite::ConstReference::~ConstReference
~ConstReference()
Destructor.
Definition: reference.h:150
ignite::Reference
Reference class.
Definition: reference.h:35
ignite::Reference::operator=
Reference & operator=(const Reference &other)
Assignment operator.
Definition: reference.h:261
ignite::MakeReferenceFromOwningPointer
Reference< T > MakeReferenceFromOwningPointer(T *val)
Make ignite::Reference instance out of pointer and pass its ownership.
Definition: reference.h:466
ignite::ConstReference
Constant Reference class.
Definition: reference.h:47
ignite::ConstReference::ConstReference
ConstReference(const ConstReference< T2 > &other)
Copy constructor.
Definition: reference.h:100
ignite::Reference::Reference
Reference(common::ReferenceImplBase *ptr, ptrdiff_t offset=0)
Constructor.
Definition: reference.h:218