Lugdunum  0.1.0
Resource.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <type_traits>
5 #include <string>
6 
8 
9 namespace lug {
10 namespace Graphics {
11 
12 class ResourceManager;
13 
19 
20 public:
24  enum class Type : uint8_t {
25  Scene,
26  SceneNode,
27  Material,
28  Mesh,
29  Light,
30  Texture,
31  Pipeline,
32  Camera,
33  SkyBox
34  };
35 
41  struct Handle {
42  union {
43  struct {
44  uint32_t type : 8;
45  uint32_t index : 24;
46  };
47 
48  uint32_t value;
49  };
50 
51  explicit operator uint32_t() {
52  return value;
53  }
54 
55  bool operator==(const Handle& other) const {
56  return value == other.value;
57  }
58  };
59 
65  template <typename T>
66  class SharedPtr {
67  template <typename RhsT>
68  friend class SharedPtr;
69 
70  static_assert(
71  std::is_base_of<Resource, T>::value,
72  "T must inherit from Resource"
73  );
74 
75  public:
76  constexpr SharedPtr(T* pointer = nullptr);
77 
78  SharedPtr(const SharedPtr<T>& rhs);
79  SharedPtr(SharedPtr<T>&& rhs);
80 
81  SharedPtr<T>& operator=(const SharedPtr<T>& rhs);
82  SharedPtr<T>& operator=(SharedPtr<T>&& rhs);
83 
84  ~SharedPtr();
85 
86  T& operator*() const;
87  T* operator->() const;
88 
89  explicit operator bool() const {
90  return _resource != nullptr;
91  }
92 
93  T* get() const;
94 
95  public:
101  template <typename RhsT>
102  static SharedPtr<T> cast(const SharedPtr<RhsT>& rhs);
103 
104  private:
105  T* _resource{nullptr};
106  };
107 
113  template <typename T>
114  class WeakPtr {
115  template <typename RhsT>
116  friend class WeakPtr;
117 
118  static_assert(
119  std::is_base_of<Resource, T>::value,
120  "T must inherit from Resource"
121  );
122 
123  public:
124  constexpr WeakPtr(T* pointer = nullptr);
125  constexpr WeakPtr(const SharedPtr<T>& rhs);
126 
127  WeakPtr(const WeakPtr<T>& rhs);
128  WeakPtr(WeakPtr<T>&& rhs);
129 
130  WeakPtr<T>& operator=(const WeakPtr<T>& rhs);
131  WeakPtr<T>& operator=(WeakPtr<T>&& rhs);
132 
133  ~WeakPtr();
134 
138  SharedPtr<T> lock() const;
139 
140  public:
146  template <typename RhsT>
147  static WeakPtr<T> cast(const WeakPtr<RhsT>& rhs);
148 
149  private:
150  T* _resource{nullptr};
151  };
152 
153 public:
154  Resource(Type type, const std::string& name);
155 
156  Resource(const Resource&) = delete;
157  Resource(Resource&&) = delete;
158 
159  Resource& operator=(const Resource&) = delete;
160  Resource& operator=(Resource&&) = delete;
161 
162  virtual ~Resource() = default;
163 
169  Type getType() const;
170 
176  Handle getHandle() const;
177 
183  const std::string& getName() const;
184 
190  void setName(const std::string &name);
191 
192 protected:
193  std::string _name;
194 
195 private:
197 };
198 
199 #include <lug/Graphics/Resource.inl>
200 
201 } // Graphics
202 } // lug
bool operator==(const Handle &other) const
Definition: Resource.hpp:55
uint32_t value
Access of the raw value of the above bytefield.
Definition: Resource.hpp:48
#define LUG_GRAPHICS_API
Definition: Export.hpp:11
Dummy class for a weak ptr.
Definition: Resource.hpp:114
Dummy class for a shared pointer.
Definition: Resource.hpp:66
Class for resource.
Definition: Resource.hpp:17
Handle of the resource. It contains informations such as the type and the index in the ResourceManage...
Definition: Resource.hpp:41
Class for resource manager. The ResourceManager allows the user to load resources and store them...
Type
Type of the resource.
Definition: Resource.hpp:24
Matrix< Rows, Columns, T > operator*(const Matrix< Rows, Columns, T > &lhs, T rhs)
uint32_t type
Type of the ressource.
Definition: Resource.hpp:44
uint32_t index
Index of the Resource in the ResourceManager&#39;s internal storage.
Definition: Resource.hpp:45