Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ObjectHandle.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_OBJECTHANDLE_HPP
8 #define NAZARA_OBJECTHANDLE_HPP
9 
10 #include <Nazara/Core/Algorithm.hpp>
11 #include <ostream>
12 
13 namespace Nz
14 {
15  template<typename T> class HandledObject;
16 
17  template<typename T>
18  class ObjectHandle
19  {
20  friend HandledObject<T>;
21 
22  public:
23  ObjectHandle();
24  explicit ObjectHandle(T* object);
25  ObjectHandle(const ObjectHandle& handle);
26  ObjectHandle(ObjectHandle&& handle) noexcept;
27  ~ObjectHandle();
28 
29  T* GetObject() const;
30 
31  bool IsValid() const;
32 
33  void Reset(T* object = nullptr);
34  void Reset(const ObjectHandle& handle);
35  void Reset(ObjectHandle&& handle) noexcept;
36 
37  ObjectHandle& Swap(ObjectHandle& handle);
38 
39  Nz::String ToString() const;
40 
41  explicit operator bool() const;
42  operator T*() const;
43  T* operator->() const;
44 
45  ObjectHandle& operator=(T* object);
46  ObjectHandle& operator=(const ObjectHandle& handle);
47  ObjectHandle& operator=(ObjectHandle&& handle) noexcept;
48 
49  static const ObjectHandle InvalidHandle;
50 
51  protected:
52  void OnObjectDestroyed();
53  void OnObjectMoved(T* newObject);
54 
55  T* m_object;
56  };
57 
58  template<typename T> std::ostream& operator<<(std::ostream& out, const ObjectHandle<T>& handle);
59 
60  template<typename T> bool operator==(const ObjectHandle<T>& lhs, const ObjectHandle<T>& rhs);
61  template<typename T> bool operator==(const T& lhs, const ObjectHandle<T>& rhs);
62  template<typename T> bool operator==(const ObjectHandle<T>& lhs, const T& rhs);
63 
64  template<typename T> bool operator!=(const ObjectHandle<T>& lhs, const ObjectHandle<T>& rhs);
65  template<typename T> bool operator!=(const T& lhs, const ObjectHandle<T>& rhs);
66  template<typename T> bool operator!=(const ObjectHandle<T>& lhs, const T& rhs);
67 
68  template<typename T> bool operator<(const ObjectHandle<T>& lhs, const ObjectHandle<T>& rhs);
69  template<typename T> bool operator<(const T& lhs, const ObjectHandle<T>& rhs);
70  template<typename T> bool operator<(const ObjectHandle<T>& lhs, const T& rhs);
71 
72  template<typename T> bool operator<=(const ObjectHandle<T>&, const ObjectHandle<T>& rhs);
73  template<typename T> bool operator<=(const T& lhs, const ObjectHandle<T>& rhs);
74  template<typename T> bool operator<=(const ObjectHandle<T>& lhs, const T& rhs);
75 
76  template<typename T> bool operator>(const ObjectHandle<T>& lhs, const ObjectHandle<T>& rhs);
77  template<typename T> bool operator>(const T& lhs, const ObjectHandle<T>& rhs);
78  template<typename T> bool operator>(const ObjectHandle<T>& lhs, const T& rhs);
79 
80  template<typename T> bool operator>=(const ObjectHandle<T>& lhs, const ObjectHandle<T>& rhs);
81  template<typename T> bool operator>=(const T& lhs, const ObjectHandle<T>& rhs);
82  template<typename T> bool operator>=(const ObjectHandle<T>& lhs, const T& rhs);
83 
84  template<typename T> struct PointedType<ObjectHandle<T>> { typedef T type; };
85  template<typename T> struct PointedType<const ObjectHandle<T>> { typedef T type; };
86 }
87 
88 namespace std
89 {
90  template<typename T>
91  void swap(Nz::ObjectHandle<T>& lhs, Nz::ObjectHandle<T>& rhs);
92 }
93 
94 #include <Nazara/Core/ObjectHandle.inl>
95 
96 #endif // NAZARA_OBJECTHANDLE_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
bool operator>(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1566
Core class that represents a string.
Definition: String.hpp:22
STL namespace.
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
~ObjectHandle()
Destructs the object and calls reset with nullptr.
Definition: ObjectHandle.inl:69
void Reset(T *object=nullptr)
Resets the content of the ObjectHandle with another object.
Definition: ObjectHandle.inl:100
ObjectHandle & operator=(T *object)
Assigns the entity into this.
Definition: ObjectHandle.inl:232
T * operator->() const
Dereferences the ObjectHandle.
Definition: ObjectHandle.inl:220
void OnObjectDestroyed()
Action to do on object destruction.
Definition: ObjectHandle.inl:271
ObjectHandle & Swap(ObjectHandle &handle)
Swaps the content of the two ObjectHandle.
Definition: ObjectHandle.inl:152
bool IsValid() const
Checks whether the object is valid.
Definition: ObjectHandle.inl:89
T * GetObject() const
Gets the underlying object.
Definition: ObjectHandle.inl:79
ObjectHandle()
Constructs a ObjectHandle object by default.
Definition: ObjectHandle.inl:22
Core class that represents a object handle.
Definition: HandledObject.hpp:16
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469
Nz::String ToString() const
Gives a string representation.
Definition: ObjectHandle.inl:179
bool operator>=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1580
void OnObjectMoved(T *newObject)
Action to do on object move.
Definition: ObjectHandle.inl:281