GridGain C++
reference.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #ifndef _IGNITE_COMMON_REFERENCE
23 #define _IGNITE_COMMON_REFERENCE
24 
25 #include <cstddef>
26 
27 #include <ignite/common/common.h>
28 #include <ignite/common/concurrent.h>
29 #include <ignite/common/reference_impl.h>
30 
31 namespace ignite
32 {
33  template<typename T>
34  class Reference;
35 
45  template<typename T>
47  {
48  template<typename>
49  friend class ConstReference;
50 
51  template<typename>
52  friend class Reference;
53 
54  public:
59  ptr(),
60  offset(0)
61  {
62  // No-op.
63  }
64 
71  explicit ConstReference(common::ConstReferenceImplBase* ptr, ptrdiff_t offset = 0) :
72  ptr(ptr),
73  offset(offset)
74  {
75  // No-op.
76  }
77 
84  ptr(other.ptr),
85  offset(other.offset)
86  {
87  // No-op.
88  }
89 
98  template<typename T2>
100  ptr(other.ptr),
101  offset(other.offset)
102  {
103  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
104  T* p1 = static_cast<T*>(p0);
105 
106  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
107  offset += diff;
108  }
109 
116  {
117  ptr = other.ptr;
118  offset = other.offset;
119 
120  return *this;
121  }
122 
131  template<typename T2>
133  {
134  ptr = other.ptr;
135  offset = other.offset;
136 
137  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
138  T* p1 = static_cast<T*>(p0);
139 
140  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
141  offset += diff;
142 
143  return *this;
144  }
145 
150  {
151  // No-op.
152  }
153 
162  const T* Get() const
163  {
164  return reinterpret_cast<const T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
165  }
166 
172  bool IsNull() const
173  {
174  const common::ConstReferenceImplBase* raw = ptr.Get();
175 
176  return !raw || !raw->Get();
177  }
178 
179  private:
181  common::concurrent::SharedPointer<common::ConstReferenceImplBase> ptr;
182 
184  ptrdiff_t offset;
185  };
186 
195  template<typename T>
196  class Reference
197  {
198  template<typename>
199  friend class Reference;
200  public:
205  ptr(),
206  offset(0)
207  {
208  // No-op.
209  }
210 
217  explicit Reference(common::ReferenceImplBase* ptr, ptrdiff_t offset = 0) :
218  ptr(ptr),
219  offset(offset)
220  {
221  // No-op.
222  }
223 
229  Reference(const Reference& other) :
230  ptr(other.ptr),
231  offset(other.offset)
232  {
233  // No-op.
234  }
235 
243  template<typename T2>
244  Reference(const Reference<T2>& other) :
245  ptr(other.ptr),
246  offset(other.offset)
247  {
248  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
249  T* p1 = static_cast<T*>(p0);
250 
251  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
252  offset += diff;
253  }
254 
261  {
262  ptr = other.ptr;
263  offset = other.offset;
264 
265  return *this;
266  }
267 
275  template<typename T2>
277  {
278  ptr = other.ptr;
279  offset = other.offset;
280 
281  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
282  T* p1 = static_cast<T*>(p0);
283 
284  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
285  offset += diff;
286 
287  return *this;
288  }
289 
294  {
295  // No-op.
296  }
297 
305  template<typename T2>
307  {
309 
310  cr.ptr = ptr;
311  cr.offset = offset;
312 
313  T2* p0 = reinterpret_cast<T2*>(common::POINTER_CAST_MAGIC_NUMBER);
314  const T* p1 = static_cast<T*>(p0);
315 
316  ptrdiff_t diff = reinterpret_cast<ptrdiff_t>(p1) - reinterpret_cast<ptrdiff_t>(p0);
317  cr.offset -= diff;
318 
319  return cr;
320  }
321 
330  const T* Get() const
331  {
332  return reinterpret_cast<const T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
333  }
334 
343  T* Get()
344  {
345  return reinterpret_cast<T*>(reinterpret_cast<ptrdiff_t>(ptr.Get()->Get()) + offset);
346  }
347 
353  bool IsNull() const
354  {
355  const common::ReferenceImplBase* raw = ptr.Get();
356 
357  return !raw || !raw->Get();
358  }
359 
360  private:
362  common::concurrent::SharedPointer<common::ReferenceImplBase> ptr;
363 
365  ptrdiff_t offset;
366  };
367 
382  template<typename T>
384  {
385  common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
386 
388 
389  impl->Swap(ptr);
390 
391  return res;
392  }
393 
408  template<typename T>
410  {
411  common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
412 
414 
415  impl->Swap(ptr);
416 
417  return res;
418  }
419 
429  template<typename T>
431  {
432  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(new T(val));
433 
434  return Reference<T>(impl);
435  }
436 
446  template<typename T>
448  {
449  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(new T(val));
450 
451  return ConstReference<T>(impl);
452  }
453 
464  template<typename T>
466  {
467  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
468 
469  return Reference<T>(impl);
470  }
471 
482  template<typename T>
484  {
485  common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
486 
487  return ConstReference<T>(impl);
488  }
489 
500  template<typename T>
502  {
503  common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(&val);
504 
505  return Reference<T>(impl);
506  }
507 
518  template<typename T>
520  {
521  common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(val);
522 
523  return Reference<T>(impl);
524  }
525 
536  template<typename T>
538  {
539  common::ConstReferenceNonOwningRawPointer<T>* impl = new common::ConstReferenceNonOwningRawPointer<T>(&val);
540 
541  return ConstReference<T>(impl);
542  }
543 
554  template<typename T>
556  {
557  common::ConstReferenceNonOwningRawPointer<T>* impl = new common::ConstReferenceNonOwningRawPointer<T>(val);
558 
559  return ConstReference<T>(impl);
560  }
561 }
562 
563 #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:483
ignite
Ignite API.
Definition: cache.h:47
ignite::ConstReference::ConstReference
ConstReference(common::ConstReferenceImplBase *ptr, ptrdiff_t offset=0)
Constructor.
Definition: reference.h:71
ignite::Reference::~Reference
~Reference()
Destructor.
Definition: reference.h:293
ignite::MakeReference
Reference< T > MakeReference(T &val)
Make ignite::Reference instance out of reference.
Definition: reference.h:501
ignite::ConstReference::ConstReference
ConstReference()
Default constructor.
Definition: reference.h:58
ignite::MakeConstReference
ConstReference< T > MakeConstReference(const T &val)
Make ignite::ConstReference instance out of constant reference.
Definition: reference.h:537
ignite::MakeReferenceFromSmartPointer
Reference< typename T::element_type > MakeReferenceFromSmartPointer(T ptr)
Make ignite::Reference instance out of smart pointer.
Definition: reference.h:383
ignite::ConstReference::ConstReference
ConstReference(const ConstReference &other)
Copy constructor.
Definition: reference.h:83
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference &other)
Assignment operator.
Definition: reference.h:115
ignite::Reference::operator=
Reference & operator=(const Reference< T2 > &other)
Assignment operator.
Definition: reference.h:276
ignite::Reference::Reference
Reference(const Reference &other)
Copy constructor.
Definition: reference.h:229
ignite::Reference::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:330
ignite::Reference::Reference
Reference()
Default constructor.
Definition: reference.h:204
ignite::MakeConstReferenceFromSmartPointer
ConstReference< typename T::element_type > MakeConstReferenceFromSmartPointer(T ptr)
Make ignite::ConstReference instance out of smart pointer.
Definition: reference.h:409
ignite::MakeReferenceFromCopy
Reference< T > MakeReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::Reference instance.
Definition: reference.h:430
ignite::Reference::Get
T * Get()
Dereference the pointer.
Definition: reference.h:343
ignite::Reference::Reference
Reference(const Reference< T2 > &other)
Copy constructor.
Definition: reference.h:244
ignite::MakeConstReferenceFromCopy
ConstReference< T > MakeConstReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::ConstReference instance.
Definition: reference.h:447
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference< T2 > &other)
Assignment operator.
Definition: reference.h:132
ignite::ConstReference::IsNull
bool IsNull() const
Check if the pointer is null.
Definition: reference.h:172
ignite::ConstReference::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:162
ignite::Reference::IsNull
bool IsNull() const
Check if the pointer is null.
Definition: reference.h:353
ignite::ConstReference::~ConstReference
~ConstReference()
Destructor.
Definition: reference.h:149
ignite::Reference
Reference class.
Definition: reference.h:34
ignite::Reference::operator=
Reference & operator=(const Reference &other)
Assignment operator.
Definition: reference.h:260
ignite::MakeReferenceFromOwningPointer
Reference< T > MakeReferenceFromOwningPointer(T *val)
Make ignite::Reference instance out of pointer and pass its ownership.
Definition: reference.h:465
ignite::ConstReference
Constant Reference class.
Definition: reference.h:46
ignite::ConstReference::ConstReference
ConstReference(const ConstReference< T2 > &other)
Copy constructor.
Definition: reference.h:99
ignite::Reference::Reference
Reference(common::ReferenceImplBase *ptr, ptrdiff_t offset=0)
Constructor.
Definition: reference.h:217