Gorgon Game Engine
Folder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 
6 #include "Base.h"
7 
8 namespace Gorgon { namespace Resource {
9  class File;
10  class Reader;
11  class Folder;
12 
14  class Folder : public Base {
15  friend class File;
16  public:
17 
19  Folder() { }
20 
23  Folder(File &file);
24 
25 
26  Folder(std::weak_ptr<File> file);
27 
29  virtual ~Folder() { }
30 
31 
33  virtual GID::Type GetGID() const override { return GID::Folder; }
34 
35 
39 
41  void Add(Base &resource) { children.Add(resource); }
42 
44  void Add(Base *resource) {
45  children.Add(resource); }
46 
48  void Insert(Base &resource, long before) {
49  children.Insert(resource, before);
50  }
51 
53  void Insert(Base *resource, long before) {
54  children.Insert(resource, before);
55  }
56 
59  void MoveBefore(long index, long before) {
60  children.MoveBefore(index, before);
61  }
62 
65  void MoveBefore(Base &item, long before) {
66  children.MoveBefore(item, before);
67  }
68 
70  void Remove(Base &resource) { children.Remove(resource); }
71 
73  void Delete(Base &resource) {
74  if(children.FindLocation(resource)==-1)
75  return;
76 
77  children.Remove(resource);
78 
79  if(resource.GetParentPtr()==this)
80  setparenttonullptr(resource);
81 
82  resource.DeleteResource();
83  }
84 
86  int GetCount() const { return children.GetCount(); }
87 
89  Base &GetItem(int Index) const { return children[Index]; }
90 
92  Base &GetItemPtr(int Index) const { return children[Index]; }
93 
95  Base &operator [](int Index) const { return (children[Index]); }
96 
98  bool Exists(int index) const {
99  return index>=0 && index<children.GetCount();
100  }
101 
103  bool Exists(const std::string &name) const {
104  return namedlist.count(name)>0;
105  }
106 
108 
109 
113 
117  template <typename T_>
118  T_ &Get(int index) const {
119  return dynamic_cast<T_&>(children[index]);
120  }
121 
126  template <typename T_>
127  T_ &Get(const std::string &name) const {
128  if(namedlist.count(name)>0 && dynamic_cast<T_*>(namedlist.at(name)))
129  return *dynamic_cast<T_*>(namedlist.at(name));
130  else
131  throw std::runtime_error("Requested item cannot be found");
132  }
133 
136  template <typename T_>
137  T_ *GetPtr(int index) {
138  return dynamic_cast<T_*>(&children[index]);
139  }
140 
144  template <typename T_>
145  T_ *GetPtr(const std::string &name) const {
146  if(namedlist.count(name)==0)
147  return nullptr;
148  else
149  return dynamic_cast<T_*>(namedlist.at(name));
150  }
151 
153 
154 
157  bool Load(bool shallow=false);
158 
160  bool IsLoaded() const {
161  return fullyloaded;
162  }
163 
165  virtual void Prepare() override;
166 
168  static Folder *LoadResource(std::weak_ptr<File> file, std::shared_ptr<Reader> data, unsigned long size);
169 
170  protected:
171 
174  bool load(std::shared_ptr<Reader> data, unsigned long size, bool first, bool shallow, bool load);
175 
176  void save(Writer &writer) const override;
177 
180  unsigned long entrypoint = -1;
181 
183  bool reallyloadnames = false;
184 
186  std::map<std::string, Base*> namedlist;
187 
189  bool fullyloaded = false;
190 
193  std::weak_ptr<File> file;
194 
196  std::shared_ptr<Reader> reader;
197 
198 
199  };
200 
201 } }
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::Folder::Add
void Add(Base &resource)
Adds a the given resource to this folder.
Definition: Folder.h:41
Gorgon::Resource::Folder::namedlist
std::map< std::string, Base * > namedlist
A map to bind items to their names.
Definition: Folder.h:186
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::Folder::GetItem
Base & GetItem(int Index) const
Returns an item with the given index.
Definition: Folder.h:89
Gorgon::Resource::Folder::~Folder
virtual ~Folder()
Destructor.
Definition: Folder.h:29
File.h
Gorgon::Resource::Folder::Exists
bool Exists(int index) const
Checks whether an item in the given index is present.
Definition: Folder.h:98
Base.h
Gorgon::Resource::LoadError::NoFileObject
@ NoFileObject
There is no file object associated with the resource. Generally thrown during late loading.
Definition: Reader.h:43
Gorgon::Resource::Folder::load
bool load(std::shared_ptr< Reader > data, unsigned long size, bool first, bool shallow, bool load)
This is the actual load function.
Definition: Folder.cpp:72
Gorgon::Resource::Writer::WriteChunkHeader
void WriteChunkHeader(GID::Type type, unsigned long size)
Writes the header of a chunk.
Definition: Writer.h:364
Gorgon::Resource::Folder::Exists
bool Exists(const std::string &name) const
Checks whether an item with the given name is present.
Definition: Folder.h:103
Gorgon::Resource::Folder::fullyloaded
bool fullyloaded
Whether the contents of this folder is fully loaded.
Definition: Folder.h:189
Gorgon::Resource::Folder::GetPtr
T_ * GetPtr(const std::string &name) const
Returns the item at the given index performing dynamic_cast to the given type.
Definition: Folder.h:145
Gorgon::Resource::Base::Prepare
virtual void Prepare()
This function shall prepare this resource to be used after resource is loaded.
Definition: Base.cpp:16
Gorgon::Resource::GID::Folder_Props
constexpr Type Folder_Props
Properties of a folder.
Definition: GID.h:97
Gorgon::Resource::Writer::WriteObjectStart
Marker WriteObjectStart(const Base &base)
Writes the start of an object. Should have a matching WriteEnd with the returned marker.
Definition: File.cpp:211
Gorgon::Resource::GID::Folder
constexpr Type Folder
Folder resource Gorgon::Resource::Folder.
Definition: GID.h:91
Gorgon::Resource::Folder::save
void save(Writer &writer) const override
Definition: Folder.cpp:137
Gorgon::Resource::Base::begin
const Containers::Collection< Base >::ConstIterator begin() const
Allows easy iteration through range based fors.
Definition: Base.h:100
Gorgon::Resource::Folder::reader
std::shared_ptr< Reader > reader
This is the reader used to read this folder. Might be empty if the folder is loaded completely.
Definition: Folder.h:196
Gorgon::Resource::Folder::entrypoint
unsigned long entrypoint
Entry point of this resource within the physical file.
Definition: Folder.h:180
Gorgon::Resource::Folder::Remove
void Remove(Base &resource)
Removes the given item.
Definition: Folder.h:70
Gorgon::Resource::Folder::Add
void Add(Base *resource)
Adds a the given resource to this folder.
Definition: Folder.h:44
Gorgon::Resource::Folder::Get
T_ & Get(const std::string &name) const
Returns the item at the given index performing dynamic_cast to the given type.
Definition: Folder.h:127
Gorgon::Resource::Folder::Folder
Folder()
Default constructor.
Definition: Folder.h:19
Gorgon::Resource::Folder::Delete
void Delete(Base &resource)
Deletes the given item properly, minding any links.
Definition: Folder.h:73
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Resource::Folder::MoveBefore
void MoveBefore(Base &item, long before)
Moves the given item to the given position.
Definition: Folder.h:65
Gorgon::Resource::Folder::GetGID
virtual GID::Type GetGID() const override
01010000h, (System, Folder)
Definition: Folder.h:33
Gorgon::Resource::Folder::Get
T_ & Get(int index) const
Returns the item at the given index performing dynamic_cast to the given type.
Definition: Folder.h:118
Gorgon::Resource::Folder::GetCount
int GetCount() const
Returns the number of items contained.
Definition: Folder.h:86
Gorgon::Resource::Folder::Insert
void Insert(Base &resource, long before)
Inserts a the given resource to this folder before the given index.
Definition: Folder.h:48
Gorgon::Resource::Base::GetParentPtr
Base * GetParentPtr() const
Returns the pointer to the parent. This function may return nullptr.
Definition: Base.h:78
Gorgon::Resource::Folder::operator[]
Base & operator[](int Index) const
Returns an item with the given index.
Definition: Folder.h:95
Gorgon::Resource::File
This class represents a logical resource file.
Definition: File.h:53
Gorgon::Resource::Folder::file
std::weak_ptr< File > file
The file object that is used to load this folder.
Definition: Folder.h:193
Gorgon::Resource::Folder::IsLoaded
bool IsLoaded() const
Returns whether this resource is loaded.
Definition: Folder.h:160
Gorgon::Resource::Base
This class is the base for all Gorgon Resources.
Definition: Base.h:20
Gorgon::Resource::Folder::Insert
void Insert(Base *resource, long before)
Inserts a the given resource to this folder before the given index.
Definition: Folder.h:53
Gorgon::Resource::Writer
This class allows resource objects to save their data to a stream.
Definition: Writer.h:59
Gorgon::Resource::Folder::Load
bool Load(bool shallow=false)
Loads this resource if it is not loaded yet.
Definition: Folder.cpp:24
Gorgon::Resource::LoadError
This class represents a loading error.
Definition: Reader.h:16
Gorgon::Resource::Writer::WriteEnd
void WriteEnd(Marker &marker)
This function performs writes necessary to end a chunk that is represented by the marker.
Definition: Writer.h:373
Gorgon::Resource::Folder::MoveBefore
void MoveBefore(long index, long before)
Moves the given item to the given position.
Definition: Folder.h:59
Gorgon::Resource::Folder::GetItemPtr
Base & GetItemPtr(int Index) const
Returns an item with the given index.
Definition: Folder.h:92
Gorgon::Resource::Folder::GetPtr
T_ * GetPtr(int index)
Returns the item at the given index performing dynamic_cast to the given type.
Definition: Folder.h:137
Gorgon::Resource::Folder::reallyloadnames
bool reallyloadnames
Names will only be loaded if the variable is set.
Definition: Folder.h:183
Gorgon::Resource::Folder::LoadResource
static Folder * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > data, unsigned long size)
Definition: Folder.cpp:150
Folder.h
Gorgon::Resource::Writer::WriteBool
void WriteBool(bool value)
Writes a boolean value. In resource 1.0, booleans are stored as 32bit integers.
Definition: Writer.h:220
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::Resource::Base::children
Containers::Collection< Base > children
Child objects that this resource object have.
Definition: Base.h:162
Gorgon::Resource::Folder::Prepare
virtual void Prepare() override
Prepares children to be used.
Definition: Folder.cpp:12
Gorgon::Resource::Folder
This is basic folder resource, it contains other resources.
Definition: Folder.h:14
Gorgon::Resource::GID::Folder_Names
constexpr Type Folder_Names
Definition: GID.h:93