1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// RetainedObjectPointer.hpp
// Emojicode
//
// Created by Theo Weidmann on 26/05/2017.
// Copyright © 2017 Theo Weidmann. All rights reserved.
//
#ifndef RetainedObjectPointer_hpp
#define RetainedObjectPointer_hpp
#include <cstddef>
namespace Emojicode {
class Thread;
struct Object;
class RetainedObjectPointer {
public:
/// Constructs a RetainedObjectPointer that will always evaluate to @c nullptr.
explicit RetainedObjectPointer(std::nullptr_t n) : retainListPointer_(&nullobject) {}
friend Thread;
/// Accesses the object this pointer points to.
Object* operator->() const {
return *retainListPointer_;
}
/// Returns an ordinary pointer to the retained object.
/// @warning This pointer is only valid until the next garbage collector cycle. This does, of course, not pose a
/// problem if you store the pointer into another object that can be reached by the garbage collector or if you
/// return it.
Object* unretainedPointer() const {
return *retainListPointer_;
}
/// Sets the pointer to point and retain to @c object.
void operator=(Object *object) {
*retainListPointer_ = object;
}
void operator=(std::nullptr_t) = delete;
private:
static Object *nullobject;
explicit RetainedObjectPointer(Object **retainListPointer) : retainListPointer_(retainListPointer) {}
Object **retainListPointer_;
};
} // namespace Emojicode
#endif /* RetainedObjectPointer_hpp */