Sleeping Wombat GUI  0.100
swGUI
ResourcePtr.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 
6 class ResourceObject;
7 
8 
17 template< typename ResourceType >
19 {
20 private:
21  ResourceType* m_resource;
22 
23 public:
25  {
26  static_assert( std::is_base_of< ResourceObject, ResourceType >::value, "Template parameter type must inherit from ResourceObject" );
27  m_resource = nullptr;
28  }
29 
30  ResourcePtr( ResourceType* ptr )
31  {
32  static_assert( std::is_base_of< ResourceObject, ResourceType >::value, "Template parameter type must inherit from ResourceObject" );
33  m_resource = ptr;
34  }
35 
37  {
39  }
40 
41  ResourcePtr( const ResourcePtr& other )
42  {
43  m_resource = nullptr;
44  AssignPointer( other.m_resource );
45  }
46 
48  {
49  if( this != &other)
50  {
51  m_resource = other.m_resource;
52  other.m_resource = nullptr;
53  }
54  }
55 
56  void operator=( ResourceType* ptr )
57  {
59  AssignPointer( ptr );
60  }
61 
63  {
66  }
67 
68  operator void*() const
69  {
70  return m_resource;
71  }
72 
73  ResourceType* operator*()
74  {
75  return m_resource;
76  }
77 
78  ResourceType* operator->()
79  {
80  return m_resource;
81  }
82 
83  const ResourceType* operator*() const
84  {
85  return m_resource;
86  }
87 
88  const ResourceType* operator->() const
89  {
90  return m_resource;
91  }
92 
94  {
95  if( m_resource )
96  m_resource->DeleteObjectReference();
97  m_resource = nullptr;
98  }
99 
100  void AssignPointer( ResourceType* ptr )
101  {
102  if( ptr )
103  {
104  m_resource = ptr;
105  m_resource->AddObjectReference();
106  }
107  }
108 
109  ResourceType* Ptr () const
110  { return m_resource; }
111 
112 };
113 
114 
115 
116 namespace rttr
117 {
118 
119 // This part of code enables ResourcePtr as wrapper for rttr system.
120 
121 
122 template< typename T >
123 struct wrapper_mapper< ResourcePtr< T > >
124 {
125  using wrapped_type = decltype( std::declval< ResourcePtr< T > >().Ptr() );
127 
128  inline static wrapped_type get ( const type& obj )
129  {
130  return obj.Ptr();
131  }
132 
133  inline static type create ( const wrapped_type& value )
134  {
135  return ResourcePtr< T >( value );
136  }
137 };
138 
139 
140 } // end namespace rttr
Definition: ResourcePtr.h:116
ResourcePtr(const ResourcePtr &other)
Definition: ResourcePtr.h:41
ResourcePtr(ResourceType *ptr)
Definition: ResourcePtr.h:30
ResourceType * Ptr() const
Definition: ResourcePtr.h:109
void operator=(const ResourcePtr< ResourceType > &ptr)
Definition: ResourcePtr.h:62
ResourceType * operator*()
Definition: ResourcePtr.h:73
const ResourceType * operator->() const
Definition: ResourcePtr.h:88
ResourceType * operator->()
Definition: ResourcePtr.h:78
static type create(const wrapped_type &value)
Definition: ResourcePtr.h:133
void operator=(ResourceType *ptr)
Definition: ResourcePtr.h:56
void AssignPointer(ResourceType *ptr)
Definition: ResourcePtr.h:100
ResourcePtr(ResourcePtr &&other)
Definition: ResourcePtr.h:47
Wrapper for low level resources and high level assets.
Definition: ResourcePtr.h:18
ResourcePtr()
Definition: ResourcePtr.h:24
~ResourcePtr()
Definition: ResourcePtr.h:36
std::shared_ptr< PtrType > Ptr
Definition: TypesDefinitions.h:55
void ReleaseResource()
Definition: ResourcePtr.h:93
decltype(std::declval< ResourcePtr< T > >().Ptr()) wrapped_type
Definition: ResourcePtr.h:125
Klasa ułatwiająca zarządzanie odwołaniami do assetów.Obiekty assetów (np. MaterialObject, TextureObject, VertexShader, PixelShader itp.) wymagają jakiegoś systemu zapewniającego współdzielenie między innymi obiektami.
Definition: ResourceObject.h:30
ResourceType * m_resource
Definition: ResourcePtr.h:21
const ResourceType * operator*() const
Definition: ResourcePtr.h:83