-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entity no longer contains the map since it was adding overhead to anything that derived from it. The create_map function has now been replaced with a map function that returns a mapping object. Added << operator overloads to allow for a tidier mapping syntax along with macros that help with automatic naming. Moved the "to_tree" and "from_tree" to "to" and "from" overloads. Const'd a lot of stuff to make it easier to pass temporaries about. Renamed the map.h to vmap.h to match the class names inside.
- Loading branch information
Showing
11 changed files
with
205 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,48 @@ | ||
#pragma once | ||
|
||
#include <entity/map.h> | ||
#include <entity/tree.h> | ||
#include <entity/mapping.h> | ||
|
||
|
||
namespace ent | ||
{ | ||
// Utility macro to invoke the map function using the name of | ||
// the parameter as the mapping name. | ||
#define automap(reference) map(#reference, reference) | ||
|
||
|
||
// The abstract base class for serialisable objects. | ||
// Inherit from this, implement the create_map function correctly, | ||
// Inherit from this, implement the map function correctly, | ||
// and your class will become serialisable. | ||
class entity | ||
{ | ||
public: | ||
|
||
// Convenience function that invokes the appropriate | ||
// methods to serialise this instance. | ||
template <class T> std::string to(bool pretty = false) | ||
// Uses the mapping to convert this object instance | ||
// into a tree structure for easier parsing. | ||
tree to() | ||
{ | ||
return this->to_tree().to<T>(pretty); | ||
return this->map().to(); | ||
} | ||
|
||
// Convenience function that invokes the appropriate | ||
// methods to deserialise this instance. | ||
template <class T> void from(const std::string &value) | ||
// Convenience functions to serialise this instance. | ||
template <class T> std::string to(bool pretty = false) | ||
{ | ||
tree t = T::from(value); | ||
this->from_tree(t); | ||
return this->map().to().to<T>(pretty); | ||
} | ||
|
||
// Uses the mapping to convert this object instance | ||
// into a tree structure for easier parsing. | ||
tree to_tree(); | ||
|
||
// Uses the mapping to pull the relevant values | ||
// out of a tree and into this object instance. | ||
void from_tree(tree &tree); | ||
|
||
|
||
protected: | ||
|
||
// Create a mapping instance for a singular value | ||
template <class T> void map(std::string name, T &reference) | ||
void from(const tree &tree) | ||
{ | ||
this->mapping[name] = std::make_shared<vmap<T>>(reference); | ||
return this->map().from(tree); | ||
} | ||
|
||
// Create a mapping instance for an array | ||
template <class T> void map(std::string name, std::vector<T> &reference) | ||
// Convenience function to deserialise this instance. | ||
template <class T> void from(const std::string &value) | ||
{ | ||
this->mapping[name] = std::make_shared<vmap<T>>(reference); | ||
this->map().from(T::from(value)); | ||
} | ||
|
||
// Create a mapping instance for a dictionary | ||
template <class T> void map(std::string name, std::map<std::string, T> &reference) | ||
{ | ||
this->mapping[name] = std::make_shared<vmap<T>>(reference); | ||
} | ||
|
||
protected: | ||
|
||
// Abstract function that must be implemented by | ||
// any objects to be serialised/deserialised. | ||
virtual void create_map() = 0; | ||
|
||
|
||
private: | ||
|
||
// This holds the map that links object property names to | ||
// actual instance members. | ||
std::map<std::string, std::shared_ptr<vmapbase>> mapping; | ||
virtual mapping map() = 0; | ||
}; | ||
|
||
// Special case to bypass the vector<T> template | ||
template <> void entity::map(std::string name, std::vector<byte> &reference); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#include <entity/vmap.h> | ||
#include <entity/tree.h> | ||
|
||
|
||
namespace ent | ||
{ | ||
// Utility macros for invoking the appropriate reference constructor | ||
// If one argument is passed then string name of that argument is used as the mapping key | ||
// If two arguments are passed then the supplied name is used as the mapping key | ||
#ifndef ent_ref | ||
#define ent_get_ref(_1, _2, name, ...) name | ||
#define ent_auto_ref(item) mapping::reference(#item, item) | ||
#define ent_man_ref(name, item) mapping::reference(name, item) | ||
#define ent_ref(...) ent_get_ref(__VA_ARGS__, ent_man_ref, ent_auto_ref)(__VA_ARGS__) | ||
|
||
// Concise call to ent_ref, disable if it conflicts and use ent_ref instead | ||
#define ref ent_ref | ||
#endif | ||
|
||
|
||
// Storage for the mapping lookup table | ||
class mapping | ||
{ | ||
public: | ||
|
||
// Reference to a mapped variable | ||
struct reference | ||
{ | ||
std::string name; | ||
std::shared_ptr<vmapbase> item; | ||
|
||
// Create a mapping reference for a singular value | ||
template <class T> reference(std::string name, T &item) : name(name), item(std::make_shared<vmap<T>>(item)) {} | ||
|
||
// Create a mapping reference for an array (except vector<byte>) | ||
template <class T, class = typename std::enable_if<!std::is_same<T, byte>::value>::type> | ||
reference(std::string name, std::vector<T> &item) : name(name), item(std::make_shared<vmap<T>>(item)) {} | ||
|
||
// Create a mapping reference for a dictionary | ||
template <class T> reference(std::string name, std::map<std::string, T> &item) : name(name), item(std::make_shared<vmap<T>>(item)) {} | ||
}; | ||
|
||
|
||
// Default | ||
mapping() {} | ||
|
||
// Construct from a single reference | ||
mapping(const reference &item); | ||
|
||
// Construct from a list of references | ||
mapping(std::initializer_list<reference> items); | ||
|
||
// Add a reference to the mapping | ||
mapping &operator<<(const reference &item); | ||
|
||
// Add a list of references to the mapping | ||
mapping &operator<<(std::initializer_list<reference> items); | ||
|
||
// Traverses the map to generate a tree | ||
tree to(); | ||
|
||
// Traverses the map and updates with values from the tree | ||
void from(const tree &tree); | ||
|
||
private: | ||
|
||
// This holds the map that links object property names to | ||
// actual instance members. | ||
std::map<std::string, std::shared_ptr<vmapbase>> lookup; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.