libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNDataFactory.cpp
Go to the documentation of this file.
1 /* Copyright 2008-2016 INSA Lyon, CoReNum
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: CRNDataFactory.cpp
19  * \author Yann LEYDIER
20  */
21 
22 #include <CRNData/CRNDataFactory.h>
23 #include <CRNException.h>
24 #include <CRNString.h>
25 #include <CRNIO/CRNIO.h>
26 #include <CRNi18n.h>
27 
28 using namespace crn;
29 
30 DataFactory& DataFactory::getInstance()
31 {
32  static DataFactory fac;
33  return fac;
34 }
35 
47 {
48  if (!el)
49  {
50  throw ExceptionInvalidArgument(StringUTF8("SObject DataFactory::CreateData(xml::Element &el): ") +
51  _("Null XML element."));
52  }
53  const StringUTF8 typ = el.GetValue();
54  if (typ.IsEmpty())
55  {
56  throw ExceptionRuntime(StringUTF8("SObject DataFactory::CreateData(xml::Element &el): ") + _("Cannot read element."));
57  }
58  return getInstance().createObject(typ, el); // may throw
59 }
60 
68 bool DataFactory::RegisterClass(const String &name, std::unique_ptr<DataFactoryElementBase> &&cstr)
69 {
70  if (getInstance().data.find(name) == getInstance().data.end())
71  {
72  getInstance().data[name] = std::move(cstr);
73  return true;
74  }
75  else
76  return false;
77 }
78 
85 {
86  String s;
87 
88  for (auto & elem : getInstance().data)
89  {
90  s += (elem).first + U"\n";
91  }
92  return s;
93 }
94 
98 DataFactory::DataFactory()
99 {
100 }
101 
106 {
107 }
108 
117 UObject DataFactory::createObject(const String &name, xml::Element &el)
118 {
119  if (data.find(name) != data.end())
120  return data[name]->Create(el);
121  else
122  {
123  throw ExceptionNotFound(StringUTF8("SObject CRNDataFactory::createObject("
124  "const String &name, xml::Element &el): " + StringUTF8(_("Unknown type: "))) +
125  name.CStr() + StringUTF8("."));
126  }
127 }
128 
~DataFactory()
Destructor.
XML element.
Definition: CRNXml.h:135
A generic runtime error.
Definition: CRNException.h:131
#define _(String)
Definition: CRNi18n.h:51
const char * CStr() const
Conversion to UTF8 cstring.
Definition: CRNString.cpp:167
static bool RegisterClass(const String &name, std::unique_ptr< DataFactoryElementBase > &&cstr)
Registers a class in the factory.
Produces CRNData objects from XML.
static UObject CreateData(xml::Element &el)
Creates and initializes a SObject from an XML element.
A UTF32 character string class.
Definition: CRNString.h:61
bool IsEmpty() const noexcept
Checks if the string is empty.
virtual StringUTF8 GetValue() const
Gets the content of the node.
Definition: CRNXml.cpp:171
static String GetKnownTypes()
Returns the list of registered classes.
A character string class.
Definition: CRNStringUTF8.h:49
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
An item was not found in a container.
Definition: CRNException.h:95