added 2 useful methods to quickly save or load one Serializable object
This commit is contained in:
parent
128b88838c
commit
7666f39406
@ -1,6 +1,7 @@
|
|||||||
#include "serializationmanager.h"
|
#include "serializationmanager.h"
|
||||||
#include "serializable.h"
|
#include "serializable.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
void ObjectSaver::addObject(Serializable *object)
|
void ObjectSaver::addObject(Serializable *object)
|
||||||
{
|
{
|
||||||
@ -375,3 +376,35 @@ void ObjectLoader::buildReferences()
|
|||||||
*(ref.ptr) = m_objects[ref.type][ref.id];
|
*(ref.ptr) = m_objects[ref.type][ref.id];
|
||||||
m_references.clear();
|
m_references.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ObjectSaver::quickSave(std::string filename, Serializable* object, bool binary)
|
||||||
|
{
|
||||||
|
std::fstream file;
|
||||||
|
ObjectSaver saver;
|
||||||
|
saver.addObject(object);
|
||||||
|
auto mode = binary ? std::ios_base::out | std::ios_base::binary : std::ios_base::out;
|
||||||
|
file.open(filename, mode);
|
||||||
|
if(file.is_open())
|
||||||
|
{
|
||||||
|
saver.saveAscii(file);
|
||||||
|
file.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serializable* ObjectLoader::quickLoad(std::string filename, std::string type, bool binary)
|
||||||
|
{
|
||||||
|
std::fstream file;
|
||||||
|
auto mode = binary ? std::ios_base::in | std::ios_base::binary : std::ios_base::in;
|
||||||
|
file.open(filename, mode);
|
||||||
|
ObjectLoader loader;
|
||||||
|
loader.loadAscii(file);
|
||||||
|
file.close();
|
||||||
|
const std::vector<Serializable*>& vec = loader.getObjects(type);
|
||||||
|
if(vec.size() > 0)
|
||||||
|
return vec[0];
|
||||||
|
else
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
@ -23,6 +23,8 @@ public:
|
|||||||
|
|
||||||
std::ostream& saveBinary(std::ostream& os);
|
std::ostream& saveBinary(std::ostream& os);
|
||||||
std::ostream& saveAscii(std::ostream& os);
|
std::ostream& saveAscii(std::ostream& os);
|
||||||
|
|
||||||
|
static bool quickSave(std::string filename, Serializable* object, bool binary = true);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ObjectLoader
|
class ObjectLoader
|
||||||
@ -45,6 +47,10 @@ public:
|
|||||||
const std::vector<Serializable*>& getObjects(const std::string &type) { return m_objects[type]; }
|
const std::vector<Serializable*>& getObjects(const std::string &type) { return m_objects[type]; }
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const std::vector<T*>& getObjects() { return (const std::vector<T*>&)(m_objects[T::getStaticType()]); }
|
const std::vector<T*>& getObjects() { return (const std::vector<T*>&)(m_objects[T::getStaticType()]); }
|
||||||
|
|
||||||
|
static Serializable* quickLoad(std::string filename, std::string type, bool binary = true);
|
||||||
|
template <typename T>
|
||||||
|
static T* quickLoad(std::string filename, bool binary = true) { return (T*)(quickLoad(filename, T::getStaticType(), binary)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SERIALIZATIONMANAGER_H
|
#endif // SERIALIZATIONMANAGER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user