Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ObjectRef.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_RESOURCEREF_HPP
8 #define NAZARA_RESOURCEREF_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/Algorithm.hpp>
12 #include <Nazara/Core/RefCounted.hpp>
13 #include <type_traits>
14 
15 namespace Nz
16 {
17  template<typename T>
18  class ObjectRef
19  {
20  public:
21  ObjectRef();
22  ObjectRef(T* object);
23  ObjectRef(const ObjectRef& ref);
24  template<typename U> ObjectRef(const ObjectRef<U>& ref);
25  ObjectRef(ObjectRef&& ref) noexcept;
26  ~ObjectRef();
27 
28  T* Get() const;
29  bool IsValid() const;
30  T* Release();
31  bool Reset(T* object = nullptr);
32  ObjectRef& Swap(ObjectRef& ref);
33 
34  explicit operator bool() const;
35  operator T*() const;
36  T* operator->() const;
37 
38  ObjectRef& operator=(T* object);
39  ObjectRef& operator=(const ObjectRef& ref);
40  template<typename U> ObjectRef& operator=(const ObjectRef<U>& ref);
41  ObjectRef& operator=(ObjectRef&& ref) noexcept;
42 
43  private:
44  T* m_object;
45  };
46 
47  template<typename T> bool operator==(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
48  template<typename T> bool operator==(const T& lhs, const ObjectRef<T>& rhs);
49  template<typename T> bool operator==(const ObjectRef<T>& lhs, const T& rhs);
50 
51  template<typename T> bool operator!=(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
52  template<typename T> bool operator!=(const T& lhs, const ObjectRef<T>& rhs);
53  template<typename T> bool operator!=(const ObjectRef<T>& lhs, const T& rhs);
54 
55  template<typename T> bool operator<(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
56  template<typename T> bool operator<(const T& lhs, const ObjectRef<T>& rhs);
57  template<typename T> bool operator<(const ObjectRef<T>& lhs, const T& rhs);
58 
59  template<typename T> bool operator<=(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
60  template<typename T> bool operator<=(const T& lhs, const ObjectRef<T>& rhs);
61  template<typename T> bool operator<=(const ObjectRef<T>& lhs, const T& rhs);
62 
63  template<typename T> bool operator>(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
64  template<typename T> bool operator>(const T& lhs, const ObjectRef<T>& rhs);
65  template<typename T> bool operator>(const ObjectRef<T>& lhs, const T& rhs);
66 
67  template<typename T> bool operator>=(const ObjectRef<T>& lhs, const ObjectRef<T>& rhs);
68  template<typename T> bool operator>=(const T& lhs, const ObjectRef<T>& rhs);
69  template<typename T> bool operator>=(const ObjectRef<T>& lhs, const T& rhs);
70 
71 
72  template<typename T> struct PointedType<ObjectRef<T>> { typedef T type; };
73  template<typename T> struct PointedType<ObjectRef<T> const> { typedef T type; };
74 }
75 
76 #include <Nazara/Core/ObjectRef.inl>
77 
78 #endif // NAZARA_RESOURCEREF_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
T * Release()
Releases the handle of the pointer.
Definition: ObjectRef.inl:110
bool operator>(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1566
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
T * operator->() const
Dereferences the ObjectRef.
Definition: ObjectRef.inl:183
Core class that represents a reference to an object.
Definition: ObjectRef.hpp:18
bool Reset(T *object=nullptr)
Resets the content of the ObjectRef with another pointer.
Definition: ObjectRef.inl:126
ObjectRef & Swap(ObjectRef &ref)
Swaps the content of the two ObjectRef.
Definition: ObjectRef.inl:149
~ObjectRef()
Destructs the object (remove a reference to the object when shared)
Definition: ObjectRef.inl:79
ObjectRef()
Constructs a ObjectRef object by default.
Definition: ObjectRef.inl:20
T * Get() const
Gets the underlying pointer.
Definition: ObjectRef.inl:90
ObjectRef & operator=(T *object)
Assigns the object into this.
Definition: ObjectRef.inl:195
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469
bool operator>=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1580
bool IsValid() const
Checks whether the reference is valid.
Definition: ObjectRef.inl:100