-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjsonactioniohandler.hpp
More file actions
80 lines (77 loc) · 4.43 KB
/
jsonactioniohandler.hpp
File metadata and controls
80 lines (77 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include <fea/config.hpp>
#include <fea/ui/actiontrigger.hpp>
#include <fea/ui/inputfilenotfoundexception.hpp>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
namespace fea
{
template<class Action>
class FEA_API JsonActionIOHandler
{
public:
void loadBindingsFile(const std::string& path, std::function<std::string(Action)> stringToAction = [] (Action a) { return a;});
void saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions) const;
void saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions, const std::map<ActionTrigger, Action>& secondaryActions) const;
const std::map<ActionTrigger, Action>& getPrimaryBindings() const;
const std::map<ActionTrigger, Action>& getSecondaryBindings() const;
private:
std::map<ActionTrigger, Action> mPrimaryBindings;
std::map<ActionTrigger, Action> mSecondaryBindings;
};
#include <fea/ui/jsonactioniohandler.inl>
/** @addtogroup UserInterface
*@{
* @class JsonActionIOHandler
*@}
***
* @class JsonActionIOHandler
* @brief This class is for loading and saving json files describing Action bindings.
*
* The output of the loading functions of this class can directly be passed to the ActionHandler to bind the actions saved. To bind the actions, first load a json file using JsonActionIOHandler::loadBindingsFile. Then pass the output of JsonActionIOHandler::getPrimaryBindings into ActionHandler::setPrimaryBindings and JsonActionIOHandler::getSecondaryBindings into ActionHandler::setSecondaryBindings if needed.
*
* The type used as actions must support string serialisations using the << operator for this to work.
*
* @tparam Type used as actions.
***
* @fn void JsonActionIOHandler::loadBindingsFile(const std::string& path, std::function<std::string(Action)> stringToAction = [] (Action a) { return a;});
* @brief Load a json file defining bindings for actions.
*
* Use this function to load an attribute file. When the bindings are loaded, they can be accessed with JsonActionIOHandler::getPrimaryBindings and JsonActionIOHandler::getSecondaryBindings.
* Throws FileNotFoundException when the file does not exist.
*
* Since actions can be of an arbitrary type, a converter function must be supplied that converts the action type from an std::string (since they are saved as string in the json file). The default value of this parameter is sufficient if std::string is used as the action type.
* @param path File to open.
* @param stringToAction Function to use for the string to action conversion. Default works for std::string as the action type.
***
* @fn void JsonActionIOHandler::saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions) const
* @brief Save action bindings to a json formatted file.
*
* @param path File to save the bindings in.
* @param primaryActions Map of primary actions. Obtainable through the ActionHandler.
***
* @fn void JsonActionIOHandler::saveBindingsFile(const std::string& path, const std::map<ActionTrigger, Action>& primaryActions, const std::map<ActionTrigger, Action>& secondaryActions) const
* @brief Save action bindings to a json formatted file.
*
* @param path File to save the bindings in.
* @param primaryActions Map of primary actions. Obtainable through the ActionHandler.
* @param secondaryActions Map of secondary actions. Obtainable through the ActionHandler.
***
* @fn const std::map<ActionTrigger, Action>& JsonActionIOHandler::getPrimaryBindings() const
* @brief Retrieve loaded primary bindings.
*
* JsonActionIOHandler::loadBindingsFile must be called before this function or the returned map will be empty.
* @return Map containing primary bindings.
***
* @fn const std::map<ActionTrigger, Action>& JsonActionIOHandler::getSecondaryBindings() const
* @brief Retrieve loaded secondary bindings.
*
* JsonActionIOHandler::loadBindingsFile must be called before this function or the returned map will be empty.
* @return Map containing secondary bindings.
**/
}