| 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>
47 class ConstReference
48 {
49 template<typename>
50 friend class ConstReference;
51
52 template<typename>
53 friend class Reference;
54
55 public:
59 ConstReference() :
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
84 ConstReference(const ConstReference& other) :
85 ptr(other.ptr),
86 offset(other.offset)
87 {
88 // No-op.
89 }
90
99 template<typename T2>
100 ConstReference(const ConstReference<T2>& other) :
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
116 ConstReference& operator=(const ConstReference& other)
117 {
118 ptr = other.ptr;
119 offset = other.offset;
120
121 return *this;
122 }
123
132 template<typename T2>
133 ConstReference& operator=(const ConstReference<T2>& other)
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
150 ~ConstReference()
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:
205 Reference() :
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
261 Reference& operator=(const Reference& other)
262 {
263 ptr = other.ptr;
264 offset = other.offset;
265
266 return *this;
267 }
268
276 template<typename T2>
277 Reference& operator=(const Reference<T2>& other)
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
294 ~Reference()
295 {
296 // No-op.
297 }
298
306 template<typename T2>
307 operator ConstReference<T2>()
308 {
309 ConstReference<T2> cr;
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>
384 Reference<typename T::element_type> MakeReferenceFromSmartPointer(T ptr)
385 {
386 common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
387
388 Reference<typename T::element_type> res(impl);
389
390 impl->Swap(ptr);
391
392 return res;
393 }
394
409 template<typename T>
410 ConstReference<typename T::element_type> MakeConstReferenceFromSmartPointer(T ptr)
411 {
412 common::ReferenceSmartPointer<T>* impl = new common::ReferenceSmartPointer<T>();
413
414 ConstReference<typename T::element_type> res(impl);
415
416 impl->Swap(ptr);
417
418 return res;
419 }
420
430 template<typename T>
431 Reference<T> MakeReferenceFromCopy(const T& val)
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>
448 ConstReference<T> MakeConstReferenceFromCopy(const T& val)
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>
466 Reference<T> MakeReferenceFromOwningPointer(T* val)
467 {
468 common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
469
470 return Reference<T>(impl);
471 }
472
483 template<typename T>
484 ConstReference<T> MakeConstReferenceFromOwningPointer(T* val)
485 {
486 common::ReferenceOwningRawPointer<T>* impl = new common::ReferenceOwningRawPointer<T>(val);
487
488 return ConstReference<T>(impl);
489 }
490
501 template<typename T>
502 Reference<T> MakeReference(T& val)
503 {
504 common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(&val);
505
506 return Reference<T>(impl);
507 }
508
519 template<typename T>
520 Reference<T> MakeReference(T* val)
521 {
522 common::ReferenceNonOwningRawPointer<T>* impl = new common::ReferenceNonOwningRawPointer<T>(val);
523
524 return Reference<T>(impl);
525 }
526
537 template<typename T>
538 ConstReference<T> MakeConstReference(const T& val)
539 {
540 common::ConstReferenceNonOwningRawPointer<T>* impl = new common::ConstReferenceNonOwningRawPointer<T>(&val);
541
542 return ConstReference<T>(impl);
543 }
544
555 template<typename T>
556 ConstReference<T> MakeConstReference(const T* val)
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::Reference::operator=
Reference & operator=(const Reference< T2 > &other)
Assignment operator.
Definition: reference.h:277
ignite::ConstReference
Constant Reference class.
Definition: reference.h:47
ignite::ConstReference::ConstReference
ConstReference(const ConstReference< T2 > &other)
Copy constructor.
Definition: reference.h:100
ignite::ConstReference::ConstReference
ConstReference(const ConstReference &other)
Copy constructor.
Definition: reference.h:84
ignite::MakeReferenceFromCopy
Reference< T > MakeReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::Reference instance.
Definition: reference.h:431
ignite::Reference::Reference
Reference()
Default constructor.
Definition: reference.h:205
ignite::MakeReferenceFromSmartPointer
Reference< typename T::element_type > MakeReferenceFromSmartPointer(T ptr)
Make ignite::Reference instance out of smart pointer.
Definition: reference.h:384
ignite::MakeConstReference
ConstReference< T > MakeConstReference(const T &val)
Make ignite::ConstReference instance out of constant reference.
Definition: reference.h:538
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::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:331
ignite::ConstReference::IsNull
bool IsNull() const
Check if the pointer is null.
Definition: reference.h:173
ignite::Reference::operator=
Reference & operator=(const Reference &other)
Assignment operator.
Definition: reference.h:261
ignite::ConstReference::ConstReference
ConstReference()
Default constructor.
Definition: reference.h:59
ignite::Reference::Reference
Reference(const Reference &other)
Copy constructor.
Definition: reference.h:230
ignite::Reference::Reference
Reference(common::ReferenceImplBase *ptr, ptrdiff_t offset=0)
Constructor.
Definition: reference.h:218
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::MakeReferenceFromOwningPointer
Reference< T > MakeReferenceFromOwningPointer(T *val)
Make ignite::Reference instance out of pointer and pass its ownership.
Definition: reference.h:466
ignite::MakeConstReferenceFromOwningPointer
ConstReference< T > MakeConstReferenceFromOwningPointer(T *val)
Make ignite::ConstReference instance out of pointer and pass its ownership.
Definition: reference.h:484
ignite::Reference
Reference class.
Definition: reference.h:35
ignite::Reference::Reference
Reference(const Reference< T2 > &other)
Copy constructor.
Definition: reference.h:245
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference &other)
Assignment operator.
Definition: reference.h:116
ignite::ConstReference::operator=
ConstReference & operator=(const ConstReference< T2 > &other)
Assignment operator.
Definition: reference.h:133
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::MakeReference
Reference< T > MakeReference(T &val)
Make ignite::Reference instance out of reference.
Definition: reference.h:502
ignite::ConstReference::Get
const T * Get() const
Dereference the pointer.
Definition: reference.h:163
ignite::MakeConstReferenceFromSmartPointer
ConstReference< typename T::element_type > MakeConstReferenceFromSmartPointer(T ptr)
Make ignite::ConstReference instance out of smart pointer.
Definition: reference.h:410
ignite::MakeConstReferenceFromCopy
ConstReference< T > MakeConstReferenceFromCopy(const T &val)
Copy object and wrap it to make ignite::ConstReference instance.
Definition: reference.h:448
ignite::Reference::Get
T * Get()
Dereference the pointer.
Definition: reference.h:344