Gorgon Game Engine
Instance.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Animation.h"
4 
5 namespace Gorgon { namespace Animation {
6 
10  template<class T_>
12  };
13 
19  template<class A_>
20  class basic_Instance : public virtual Base, public basic_InstanceInjection<A_> {
21  public:
22 
24  basic_Instance() = default;
25 
27  basic_Instance(A_ &instance, bool owner = true) : instance(&instance), isowned(owner) { }
28 
30  basic_Instance(const basic_Instance &) = delete;
31 
33  basic_Instance(basic_Instance &&other) : instance(other.instance), isowned(other.isowned) {
34  other.isowned = false;
35  other.instance = nullptr;
36  }
37 
40 
43  Remove();
44  isowned = other.isowned;
45  instance = other.instance;
46  other.isowned = false;
47  other.instance = nullptr;
48 
49  return *this;
50  }
51 
54  basic_Instance &operator =(A_ &instance) {
55  Remove();
56  isowned = true;
57  this->instance = &instance;
58 
59  return *this;
60  }
61 
63  bool HasAnimation() const {
64  return instance != nullptr;
65  }
66 
69  A_ &GetAnimation() const {
70  if(instance)
71  return *instance;
72  else
73  throw std::runtime_error("Instance contains no animation");
74  }
75 
77  A_ &operator *() const {
78  return GetAnimation();
79  }
80 
82  A_ *operator ->() const {
83  return &GetAnimation();
84  }
85 
87  void SetAnimation(A_ &value, bool owner = true) {
88  Remove();
89 
90  instance = &value;
91  this->isowned = owner;
92  }
93 
97  void Remove() {
98  if(isowned)
99  instance->DeleteAnimation();
100 
101  isowned = false;
102  instance = nullptr;
103  }
104 
106  A_ *Release() {
107  auto temp = instance;
108 
109  isowned = false;
110 
111  Remove();
112 
113  return temp;
114  }
115 
117  bool IsOwner() const {
118  return isowned;
119  }
120 
121 
122  private:
123  A_ *instance = nullptr;
124  bool isowned = false;
125  };
126 
127 
129  template<class Target_, class Original_>
132 
133  if(!original.HasAnimation())
134  return target;
135 
136  bool owned = original.IsOwner();
137  Target_ *anim = dynamic_cast<Target_*>(original.Release());
138  if(!anim)
139  throw std::runtime_error("Animation types are not compatible");
140 
141  target.SetAnimation(*anim, owned);
142 
143  return target;
144  }
145 
148 
149 } }
Gorgon::Animation::AnimationCast
basic_Instance< Target_ > AnimationCast(basic_Instance< Original_ > &&original)
Moves one type of animation into another.
Definition: Instance.h:130
Gorgon::Animation::basic_Instance
This class allows storing an animation instance regarless of its underlying type as a value.
Definition: Instance.h:20
Gorgon::Animation::basic_Instance::Remove
void Remove()
Removes the animation stored in the container, if the container owns the animation,...
Definition: Instance.h:97
Gorgon::Animation::basic_Instance::GetAnimation
A_ & GetAnimation() const
Returns the animation stored in the object.
Definition: Instance.h:69
Gorgon::Animation::basic_Instance::Release
A_ * Release()
Removes the animation from the storage without destroying it.
Definition: Instance.h:106
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Animation::basic_Instance::IsOwner
bool IsOwner() const
Whether the stored animation is owned by this container.
Definition: Instance.h:117
Gorgon::Animation::basic_Instance::basic_Instance
basic_Instance(basic_Instance &&other)
Move constructor.
Definition: Instance.h:33
Gorgon::Animation::Base
This is the base class for all animations.
Definition: Animation.h:306
Gorgon::Animation::basic_Instance::basic_Instance
basic_Instance()=default
Empty constructor.
Gorgon::Animation::basic_Instance::SetAnimation
void SetAnimation(A_ &value, bool owner=true)
Sets the animation stored in this container.
Definition: Instance.h:87
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Animation::basic_Instance::operator*
A_ & operator*() const
Alias for GetAnimation.
Definition: Instance.h:77
Gorgon::Animation::basic_Instance::basic_Instance
basic_Instance(const basic_Instance &)=delete
Copy constructor is disabled for ownership reasons.
Gorgon::Animation::basic_Instance::basic_Instance
basic_Instance(A_ &instance, bool owner=true)
Filling constructor.
Definition: Instance.h:27
Gorgon::Animation::basic_Instance::operator=
basic_Instance & operator=(const basic_Instance &)=delete
Copy assignment.
Gorgon::Animation::basic_Instance::operator->
A_ * operator->() const
Alias for GetAnimation.
Definition: Instance.h:82
Gorgon::Animation::basic_Instance::HasAnimation
bool HasAnimation() const
Check if this instance has an animation.
Definition: Instance.h:63
Gorgon::Animation::basic_InstanceInjection
Specializing this class allows code injection to animation instances.
Definition: Instance.h:11