BARE2D
XMLDataManager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <glm/glm.hpp>
5 #include <rapidxml/rapidxml.hpp>
6 #include <string>
7 
8 #include "XMLDataTypes.hpp"
9 
10 namespace BARE2D {
11 
12 /**
13  * @class XMLDataManager
14  * @brief A singleton class that statically manages all XML data, including read and write operations.
15  */
17 {
18  friend class XMLData;
19 
20  public:
21  /**
22  * @brief Loads all of the files from the filepath that end in .xml to the caches. Does not clear caches.
23  * @param filepath The path of the folder of files to load, or the path of an actual singular file to load.
24  */
25  static void loadXML(std::string filepath);
26 
27  /**
28  * @brief Writes all of the currently cached data to the data's respective files in a folder at filepath. Does not
29  * clear caches.
30  * @param filepath The path of the folder of files to save.
31  */
32  static void saveXML(std::string filepath);
33 
34  /**
35  * @brief Adds a type of data that can be read. Make sure that the custom data type's nodeName is set.
36  */
37  template <typename T> static void addDataType();
38 
39  /**
40  * @brief Adds a piece of data to the cache, in the appropriate sub-cache.
41  * @param data A pointer to the data to be added. data->nodeName must be set for appropriate sub-caching.
42  */
43  static void addData(XMLData* data);
44 
45  /**
46  * @brief Adds or overwrites data in the cache/sub-cache
47  * @param data A pointer to the data to be added. data->nodeName must be set for appropriate sub-caching
48  */
49  static void setData(XMLData* data);
50 
51  /**
52  * @brief Finds some data in the subcache.
53  * @param dataType The string type of the data to get.
54  * @param key The ID of the data.
55  * @return A copy of the data, as nobody should be modifying the cached data.
56  */
57  template <typename T> static T getData(std::string dataType, unsigned int key);
58 
59  /**
60  * @param dataType The string type of the data to get info on.
61  * @return The number of elements of the type of data.
62  */
63  static unsigned int getDataCount(std::string dataType);
64 
65  /**
66  * @brief Clears the various caches and subcaches that the class uses. Useful for refreshes.
67  */
68  static void clearCache();
69 
70  /**
71  * @brief Provides a copy of some data type added by the addDataType function
72  * @param dataType The string identifier for the type of data. In other words, nodeName
73  * @return A copy of some type of data, complete with nodeName and attributes set.
74  */
75  static XMLData getDataType(std::string dataType);
76 
77  private:
78  /**
79  * @brief Writes a single subcache of data to a single file
80  * @param file The file to write the data to
81  * @param data A pointer to the subcache to write
82  */
83  static void writeXMLData(std::ofstream& file, Cache<unsigned int, XMLData>* data);
84  /**
85  * @brief Reads a single subcache from a single file.
86  * @param file The file to read from.
87  * @param dataType The nodeName of the data to read. This will be the key for the cache.
88  */
89  static void readXMLData(std::ifstream& file, std::string dataType);
90 
91  /**
92  * @brief Reads a value from a node. Templated for almost any primitive.
93  * @param parent A pointer to the node of the parent data.
94  * @param valueName The "nodeName" of the value to be read.
95  * @param variable The return variable.
96  * @return True on success, false on failure.
97  */
98  template <typename T> static bool readValue(rapidxml::xml_node<>* parent, std::string valueName, T& variable);
99 
100  /**
101  * @brief Returns a cache for a certain type of data
102  * @param dataType The string type of the data
103  * @return A pointer to the subcache.
104  */
105  static Cache<unsigned int, XMLData>* getDataCache(std::string dataType);
106 
108  static std::unordered_map<std::string, std::function<XMLData*()>> m_dataTypingFunctions;
109 };
110 
111 } // namespace BARE2D
112 
113 #include "XMLDataManager.tcc"
BARE2D::XMLDataManager::readValue
static bool readValue(rapidxml::xml_node<> *parent, std::string valueName, T &variable)
Reads a value from a node. Templated for almost any primitive.
BARE2D
Definition: App.cpp:13
BARE2D::XMLDataManager::getDataType
static XMLData getDataType(std::string dataType)
Provides a copy of some data type added by the addDataType function.
BARE2D::XMLDataManager::setData
static void setData(XMLData *data)
Adds or overwrites data in the cache/sub-cache.
Definition: XMLDataManager.cpp:173
BARE2D::XMLDataManager::getDataCache
static Cache< unsigned int, XMLData > * getDataCache(std::string dataType)
Returns a cache for a certain type of data.
Definition: XMLDataManager.cpp:482
XMLDataTypes.hpp
BARE2D::XMLDataManager::m_storedData
static Cache< std::string, Cache< unsigned int, XMLData > > m_storedData
Definition: XMLDataManager.hpp:107
BARE2D::XMLDataManager::loadXML
static void loadXML(std::string filepath)
Loads all of the files from the filepath that end in .xml to the caches. Does not clear caches.
Definition: XMLDataManager.cpp:18
XMLDataManager.tcc
BARE2D::XMLDataManager::m_dataTypingFunctions
static std::unordered_map< std::string, std::function< XMLData *()> > m_dataTypingFunctions
Definition: XMLDataManager.hpp:108
BARE2D::XMLDataManager::addData
static void addData(XMLData *data)
Adds a piece of data to the cache, in the appropriate sub-cache.
Definition: XMLDataManager.cpp:158
BARE2D::XMLDataManager::getDataCount
static unsigned int getDataCount(std::string dataType)
Definition: XMLDataManager.cpp:153
BARE2D::XMLDataManager
A singleton class that statically manages all XML data, including read and write operations.
Definition: XMLDataManager.hpp:16
BARE2D::XMLDataManager::clearCache
static void clearCache()
Clears the various caches and subcaches that the class uses. Useful for refreshes.
Definition: XMLDataManager.cpp:494
BARE2D::Cache
This is a skeleton cache class. This can only be used by the ResourceManager or other classes who act...
Definition: Cache.hpp:15
BARE2D::XMLDataManager::addDataType
static void addDataType()
Adds a type of data that can be read. Make sure that the custom data type's nodeName is set.
BARE2D::XMLDataManager::writeXMLData
static void writeXMLData(std::ofstream &file, Cache< unsigned int, XMLData > *data)
Writes a single subcache of data to a single file.
Definition: XMLDataManager.cpp:130
BARE2D::XMLDataManager::getData
static T getData(std::string dataType, unsigned int key)
Finds some data in the subcache.
BARE2D::XMLDataManager::readXMLData
static void readXMLData(std::ifstream &file, std::string dataType)
Reads a single subcache from a single file.
Definition: XMLDataManager.cpp:57
BARE2D::XMLDataManager::saveXML
static void saveXML(std::string filepath)
Writes all of the currently cached data to the data's respective files in a folder at filepath....
Definition: XMLDataManager.cpp:103
BARE2D::XMLData
Holds all the very basic information for XML data. Designed to be a base class from which a user can ...
Definition: XMLDataTypes.hpp:108