Gorgon Game Engine
Base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Containers/Collection.h"
4 #include "../SGuid.h"
5 
6 #include "GID.h"
7 #include "Writer.h"
8 
9 namespace Gorgon {
10  class Window;
11 
13  namespace Resource {
14 
15  class File;
16 
20  class Base {
21  friend class File;
22  public:
23 
25  Base();
26 
27 
29  virtual GID::Type GetGID() const = 0;
30 
31 
35  virtual void Resolve(File &file);
36 
40  virtual void Prepare();
41 
45  virtual void Discard();
46 
47 
49  virtual bool IsEqual(const SGuid &guid) const { return guid==this->guid; }
50 
51 
53  virtual SGuid GetGuid() const { return guid; }
54 
56  virtual void SetGuid(SGuid guid) { this->guid=guid; }
57 
58 
61  const std::string &GetName() const { return name; }
62 
64  virtual void SetName(const std::string &name) { this->name=name; }
65 
67  bool HasParent() const { return parent!=nullptr; }
68 
70  Base &GetParent() const {
71  if(!parent)
72  throw std::runtime_error("This object has no parent");
73 
74  return *parent;
75  }
76 
78  Base *GetParentPtr() const {
79  return parent;
80  }
81 
85  const Base &GetRoot() const {
86  if(!root) {
87  if(!parent) {
88  root=this;
89  }
90  else {
91  root=parent->root;
92  }
93  }
94 
95  return *root;
96  }
97 
98 
101  return Children.begin();
102  }
103 
106  return Children.end();
107  }
108 
111  return Children.begin();
112  }
113 
116  return Children.end();
117  }
118 
121  bool DeleteResource();
122 
124  void Save(Writer &writer);
125 
126 
132 
137  unsigned long refcount=1;
138 
139  protected:
141  virtual ~Base();
142 
143  virtual void save(Writer &writer) const = 0;
144 
146  void destroychildren();
147 
149  void setparenttonullptr(Base &base) { base.parent=nullptr; base.root=nullptr; }
150 
153 
155  std::string name;
156 
158  Base *parent=nullptr;
159 
163 
165  mutable const Base *root=nullptr;
166  };
167 
168  }
169 
170 }
Gorgon::Resource::Base::Resolve
virtual void Resolve(File &file)
This function shall resolve links or similar constructs.
Definition: Base.cpp:6
Gorgon::Resource::Base::DeleteResource
bool DeleteResource()
Safely deletes the resource.
Definition: Base.cpp:34
Gorgon::Resource::Base::setparenttonullptr
void setparenttonullptr(Base &base)
Sets the parent of an object to nullptr, provides access.
Definition: Base.h:149
Gorgon::Resource::GID::File
constexpr Type File
File.
Definition: GID.h:84
Gorgon::Resource::Base::name
std::string name
Name of this resource object, may not be loaded.
Definition: Base.h:155
Gorgon::Resource::Base::destroychildren
void destroychildren()
Destroys the children of this resource.
Definition: Base.cpp:43
Gorgon::Resource::Base::GetGID
virtual GID::Type GetGID() const =0
This function shall return Gorgon ID of this resource.
Gorgon::SGuid::New
void New()
Generates a new GUID and assign that GUID to this one.
Definition: SGuid.h:113
Gorgon::SGuid::IsEmpty
bool IsEmpty() const
Returns whether this GUID is empty.
Definition: SGuid.h:158
Base.h
Gorgon::Resource::Base::IsEqual
virtual bool IsEqual(const SGuid &guid) const
This function tests whether this object has the given SGuid.
Definition: Base.h:49
Gorgon::Resource::Base::Children
const Containers::Collection< Base > & Children
The children this object have.
Definition: Base.h:131
Gorgon::Resource::Base::GetRoot
const Base & GetRoot() const
Returns the root of this resource.
Definition: Base.h:85
Gorgon::Resource::Base::Base
Base()
Default constructor.
Definition: Base.cpp:28
Gorgon::UI::Organizers::Base::IsAttached
bool IsAttached() const
Returns if this organizer is attached to a container.
Definition: Base.h:32
Gorgon::Resource::Base::root
const Base * root
Root of this resource.
Definition: Base.h:165
Gorgon::Resource::Base::Prepare
virtual void Prepare()
This function shall prepare this resource to be used after resource is loaded.
Definition: Base.cpp:16
GID.h
contains Gorgon IDs
Gorgon::UI::Organizers::Base::attachmentchanged
virtual void attachmentchanged()
Called when the attachment of the organizer is changed.
Definition: Base.h:50
Gorgon::Resource::Base::end
const Containers::Collection< Base >::ConstIterator end() const
Allows easy iteration through range based fors.
Definition: Base.h:105
Gorgon::Resource::Base::begin
const Containers::Collection< Base >::ConstIterator begin() const
Allows easy iteration through range based fors.
Definition: Base.h:100
Gorgon::Resource::Writer::IsGood
bool IsGood() const
Checks if the stream is open and it can be written to.
Definition: Writer.h:104
Gorgon::Resource::Base::refcount
unsigned long refcount
INTERNAL, Reference count, used in linking mechanism.
Definition: Base.h:137
Gorgon::Resource::Base::parent
Base * parent
Immediate parent of this resource.
Definition: Base.h:158
Gorgon::Resource::Base::SetName
virtual void SetName(const std::string &name)
Sets the name of the object.
Definition: Base.h:64
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::UI::Organizers::Base::reorganize
virtual void reorganize()=0
Should reorganize the contents of the organizer.
Gorgon::Resource::Base::Save
void Save(Writer &writer)
Saves this object into the given writer. The writer should be open prior to this call.
Definition: Base.cpp:57
Gorgon::Resource::Base::GetParentPtr
Base * GetParentPtr() const
Returns the pointer to the parent. This function may return nullptr.
Definition: Base.h:78
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
Gorgon::Resource::File
This class represents a logical resource file.
Definition: File.h:53
Gorgon::Resource::Base
This class is the base for all Gorgon Resources.
Definition: Base.h:20
Gorgon::UI::Organizers::Base::RemoveFrom
void RemoveFrom()
Removes the organizer from the container.
Definition: Base.cpp:17
Gorgon::Resource::Base::save
virtual void save(Writer &writer) const =0
Gorgon::Resource::Base::SetGuid
virtual void SetGuid(SGuid guid)
Changes the guid of the object.
Definition: Base.h:56
Gorgon::Containers::Collection::ConstIterator
Const iterator allows iteration of const collections.
Definition: Collection.h:137
Gorgon::Resource::Base::Discard
virtual void Discard()
This function shall discard any transitional data which is not vital after Prepare function is issued...
Definition: Base.cpp:22
Gorgon::Resource::Writer
This class allows resource objects to save their data to a stream.
Definition: Writer.h:59
Gorgon::UI::Organizers::Base::AttachTo
void AttachTo(WidgetContainer &container)
Attaches this organizer to a container.
Definition: Base.cpp:6
Gorgon::Resource::Base::cbegin
const Containers::Collection< Base >::ConstIterator cbegin() const
Beginning of children.
Definition: Base.h:110
Writer.h
Gorgon::UI::Organizers::Base::Reorganize
void Reorganize()
Reorganizes the widgets that are organized by this organizer.
Definition: Base.cpp:31
Gorgon::Resource::Base::GetGuid
virtual SGuid GetGuid() const
Returns the guid of the object.
Definition: Base.h:53
Gorgon::Resource::Base::HasParent
bool HasParent() const
Returns whether this object has a parent.
Definition: Base.h:67
Gorgon::UI::WidgetContainer::AttachOrganizer
void AttachOrganizer(Organizers::Base &organizer)
Attaches an organizer to this container.
Definition: WidgetContainer.cpp:452
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::SGuid
This class represents a short globally unique identifier.
Definition: SGuid.h:22
Gorgon::Resource::Base::children
Containers::Collection< Base > children
Child objects that this resource object have.
Definition: Base.h:162
Gorgon::UI::WidgetContainer
This class is the base class for all widget containers.
Definition: WidgetContainer.h:37
Gorgon::UI::WidgetContainer::RemoveOrganizer
void RemoveOrganizer()
Removes the organizer from this container.
Definition: WidgetContainer.cpp:436
Gorgon::Resource::Base::GetParent
Base & GetParent() const
Returns the parent. If no parent set, this function throws std::runtime_error.
Definition: Base.h:70
Gorgon::Resource::Base::cend
const Containers::Collection< Base >::ConstIterator cend() const
End of children.
Definition: Base.h:115
Gorgon::Resource::Base::GetName
const std::string & GetName() const
Returns the name of this object.
Definition: Base.h:61
Gorgon::Resource::Base::~Base
virtual ~Base()
Destructor, Always children gets destroyed first.
Definition: Base.cpp:30
Gorgon::Resource::Base::guid
SGuid guid
SGuid to identify this resource object.
Definition: Base.h:152