Gorgon Game Engine
Storage.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 
5 #include "../Animation.h"
6 
7 namespace Gorgon { namespace Animation {
8 
12  template<class T_>
14  };
15 
20  template<class A_>
21  class basic_Storage : public virtual Provider, public basic_StorageInjection<A_> {
22  public:
23 
25  basic_Storage() = default;
26 
28  basic_Storage(A_ &anim, bool owner = false) : anim(&anim), isowned(owner) { }
29 
31  basic_Storage(const basic_Storage &) = delete;
32 
34  basic_Storage(basic_Storage &&other) : anim(other.anim), isowned(other.isowned) {
35  other.isowned = false;
36  other.anim = nullptr;
37  }
38 
39  //types are derived not to type the same code for every class
40  virtual auto MoveOutProvider() -> decltype(*this) override {
41  auto ret = new basic_Storage(std::move(*this));
42 
43  return *ret;
44  }
45 
48 
51  Remove();
52  isowned = other.isowned;
53  anim = other.anim;
54  other.isowned = false;
55  other.anim = nullptr;
56 
57  return *this;
58  }
59 
61  bool HasAnimation() const {
62  return anim != nullptr;
63  }
64 
67  A_ &GetAnimation() const {
68  if(anim)
69  return *anim;
70  else
71  throw std::runtime_error("Storage contains no animation");
72  }
73 
75  A_ &operator *() const {
76  return GetAnimation();
77  }
78 
80  A_ *operator ->() const {
81  return &GetAnimation();
82  }
83 
85  void SetAnimation(A_ &value, bool owner = false) {
86  Remove();
87 
88  anim = &value;
89  this->isowned = owner;
90  }
91 
93  void SetAnimation(A_ &&value) {
94  Remove();
95 
96  anim = &value.MoveOutProvider();
97  this->isowned = true;
98  }
99 
103  void Remove() {
104  if(isowned)
105  delete anim;
106 
107  isowned = false;
108  anim = nullptr;
109  }
110 
112  A_ *Release() {
113  auto temp = anim;
114 
115  isowned = false;
116 
117  Remove();
118 
119  return temp;
120  }
121 
123  bool IsOwner() const {
124  return isowned;
125  }
126 
129  virtual typename A_::AnimationType &CreateAnimation(ControllerBase &timer) const override {
130  if(anim)
131  return dynamic_cast<typename A_::AnimationType &>(anim->CreateAnimation(timer));
132  else
133  throw std::runtime_error("Storage contains no animation");
134  }
135 
138  virtual typename A_::AnimationType &CreateAnimation(bool create=true) const override {
139  if(anim)
140  return dynamic_cast<typename A_::AnimationType &>(anim->CreateAnimation(create));
141  else
142  throw std::runtime_error("Storage contains no animation");
143  }
144 
145  private:
146  A_ *anim = nullptr;
147  bool isowned = false;
148  };
149 
151  template<class Target_, class Original_>
153  basic_Storage<Target_> target;
154 
155  if(!original.HasAnimation())
156  return target;
157 
158 
159  bool owned = original.IsOwner();
160  Target_ *anim = dynamic_cast<Target_*>(original.Release());
161  if(!anim)
162  throw std::runtime_error("Animation types are not compatible");
163 
164  target.SetAnimation(*anim, owned);
165 
166  return target;
167  }
168 
171 
172 } }
Gorgon::Animation::basic_Storage
This class stores animations as a part of itself so that it can be moved around as a value rather tha...
Definition: Storage.h:21
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_Storage::CreateAnimation
virtual A_::AnimationType & CreateAnimation(bool create=true) const override
This function creates a new animation from the stored animation provider.
Definition: Storage.h:138
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Animation::basic_Storage::IsOwner
bool IsOwner() const
Whether the stored animation is owned by this container.
Definition: Storage.h:123
Gorgon::Animation::basic_Storage::SetAnimation
void SetAnimation(A_ &&value)
Sets the animation stored in this container.
Definition: Storage.h:93
Gorgon::Animation::basic_Storage::basic_Storage
basic_Storage(basic_Storage &&other)
Move constructor.
Definition: Storage.h:34
Gorgon::Animation::basic_Storage::Remove
void Remove()
Removes the animation stored in the container, if the container owns the animation,...
Definition: Storage.h:103
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Animation::Provider
This interface marks a class as animation provider.
Definition: Animation.h:283
Gorgon::Animation::basic_Storage::operator->
A_ * operator->() const
Alias for GetAnimation.
Definition: Storage.h:80
Gorgon::Animation::basic_Storage::Release
A_ * Release()
Removes the animation from the storage without destroying it.
Definition: Storage.h:112
Gorgon::Animation::basic_Storage::basic_Storage
basic_Storage(const basic_Storage &)=delete
Copy constructor is disabled for ownership reasons.
Gorgon::Animation::basic_Storage::SetAnimation
void SetAnimation(A_ &value, bool owner=false)
Sets the animation stored in this container.
Definition: Storage.h:85
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Animation::basic_StorageInjection
Specializing this class allows code injection to animation storages.
Definition: Storage.h:13
Gorgon::Animation::basic_Storage::HasAnimation
bool HasAnimation() const
Check if this storage has an animation.
Definition: Storage.h:61
Gorgon::Animation::basic_Storage::CreateAnimation
virtual A_::AnimationType & CreateAnimation(ControllerBase &timer) const override
This function creates a new animation from the stored animation provider.
Definition: Storage.h:129
Gorgon::Animation::basic_Storage::operator*
A_ & operator*() const
Alias for GetAnimation.
Definition: Storage.h:75
Gorgon::Animation::basic_Storage::basic_Storage
basic_Storage(A_ &anim, bool owner=false)
Filling constructor.
Definition: Storage.h:28
Gorgon::Animation::basic_Storage::operator=
basic_Storage & operator=(const basic_Storage &)=delete
Copy assignment.
Gorgon::Animation::basic_Storage::basic_Storage
basic_Storage()=default
Empty constructor.
Gorgon::Animation::basic_Storage::MoveOutProvider
virtual auto MoveOutProvider() -> decltype(*this) override
This function moves this animation provider into a new provider.
Definition: Storage.h:40
Gorgon::Animation::basic_Storage::GetAnimation
A_ & GetAnimation() const
Returns the animation stored in the object.
Definition: Storage.h:67