libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNDefaultAction.cpp
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, INSA-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNDefaultAction.cpp
19  * \author Yann LEYDIER
20  */
21 
23 #include <CRNXml/CRNXml.h>
24 #include <CRNi18n.h>
25 
26 using namespace crn;
27 
29 {
30  deserialize(el);
31  for (auto sel = el.BeginElement(); sel != el.EndElement(); ++sel)
32  if (sel.GetAttribute<crn::StringUTF8>("role", false) == "userdata")
33  {
34  UserData.Deserialize(sel);
35  break;
36  }
37 }
39 {
40  auto el = serialize(parent);
41  if (!UserData.IsEmpty())
42  {
43  auto mel = UserData.Serialize(el);
44  mel.SetAttribute("role", "userdata");
45  }
46  return el;
47 }
48 
50 {
51  if (el.GetName() != GetClassName())
52  throw ExceptionInvalidArgument(StringUTF8("void Action::Deserialize(xml::Element &el): ") + _("Wrong XML element."));
53 }
54 
56 {
57  return parent.PushBackElement(GetClassName());
58 }
59 
63 DefaultAction& DefaultAction::getInstance()
64 {
65  static DefaultAction actions;
66  return actions;
67 }
68 
74 SAction DefaultAction::GetAction(const String &name)
75 {
76  Map &act(getInstance().actions);
77  auto it = act.Find(name);
78  if (it != act.end())
79  return std::static_pointer_cast<Action>(it->second);
80  else
81  return nullptr;
82 }
83 
89 void DefaultAction::SetAction(const String &name, SAction action)
90 {
91  getInstance().actions.Set(name, action);
92 }
93 
104 void DefaultAction::Load(const Path &filename)
105 {
106  getInstance().actions.Load(filename);
107 }
108 
117 void DefaultAction::Save(const Path &filename)
118 {
119  getInstance().actions.Save(filename);
120 }
121 
126 std::vector<String> DefaultAction::GetActionList()
127 {
128  std::vector<String> lst;
129  Map &act(getInstance().actions);
130  for (auto & elem : act)
131  {
132  lst.push_back(elem.first);
133  }
134  return lst;
135 }
136 
iterator Find(const String &key)
Returns an iterator to a specific key.
Definition: CRNMap.h:90
XML element.
Definition: CRNXml.h:135
StringUTF8 GetName() const
Gets the label of the element.
Definition: CRNXml.h:146
#define _(String)
Definition: CRNi18n.h:51
void Set(const String &key, SObject value)
Sets a value for a key with constraints check.
Definition: CRNMap.cpp:92
void Deserialize(xml::Element &el)
Reads from an XML node if applicable.
Definition: CRNMap.cpp:187
void Deserialize(xml::Element &el)
xml::Element Serialize(xml::Element &parent) const
void Load(const Path &fname)
Definition: CRNMap.cpp:282
Element BeginElement()
Gets the first child element.
Definition: CRNXml.h:174
A UTF32 character string class.
Definition: CRNString.h:61
static void Load(const Path &filename)
Loads a set of default actions.
void Save(const Path &fname) const
Definition: CRNMap.cpp:293
static SAction GetAction(const String &name)
Gets a default action.
A convenience class for file paths.
Definition: CRNPath.h:39
xml::Element Serialize(xml::Element &parent) const
Dumps to an XML node if applicable.
Definition: CRNMap.cpp:220
virtual xml::Element serialize(xml::Element &parent) const
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
Definition: CRNXml.cpp:595
virtual StringUTF8 GetClassName() const =0
virtual void deserialize(xml::Element &el)
static void Save(const Path &filename)
Saves the set of default actions.
static std::vector< String > GetActionList()
Gets the list of default actions.
bool IsEmpty() const noexcept
Tests if the map is empty.
Definition: CRNMap.h:58
A class containing functors.
Data map class.
Definition: CRNMap.h:42
A character string class.
Definition: CRNStringUTF8.h:49
crn::Map UserData
iterator end()
Returns an iterator after the last element.
Definition: CRNMap.h:88
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Definition: CRNXml.cpp:355
Element EndElement()
Gets a null node.
Definition: CRNXml.h:176
static void SetAction(const String &name, SAction action)
Sets a default action.
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107