libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNDataFactory.h
Go to the documentation of this file.
1 /* Copyright 2008-2016 INSA Lyon, CoReNum, ENS-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: CRNDataFactory.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNDATAFACTORY_HEADER
23 #define CRNDATAFACTORY_HEADER
24 
25 #include <CRNObject.h>
26 #include <CRNXml/CRNXml.h>
27 #include <CRNProtocols.h>
28 #include <map>
29 
30 #ifdef RegisterClass
31 # undef RegisterClass
32 #endif
33 
34 namespace crn
35 {
36  class String;
37 
40  {
41  public:
44  virtual UObject Create(xml::Element &el) const = 0;
45  };
47  template<
48  class T,
49  typename std::enable_if<IsSerializable<T>::value, int>::type = 0
50  >
52  {
53  public:
54  virtual ~DataFactoryElement() override {}
56  virtual UObject Create(xml::Element &el) const override { return std::make_unique<T>(el); }
57  };
58  /****************************************************************************/
69  {
70  public:
71  DataFactory(const DataFactory &) = delete;
72  DataFactory(DataFactory &&) = delete;
73  DataFactory& operator=(const DataFactory &) = delete;
74  DataFactory& operator=(DataFactory &&) = delete;
75 
77  static UObject CreateData(xml::Element &el);
79  static bool RegisterClass(const String &name, std::unique_ptr<DataFactoryElementBase> &&cstr);
81  static String GetKnownTypes();
83  ~DataFactory();
84 
85  private:
87  DataFactory();
89  UObject createObject(const String &name, xml::Element &el);
91  static DataFactory& getInstance();
92 
93  std::map<String, std::unique_ptr<DataFactoryElementBase> > data;
95  };
96 }
97 
106 #define CRN_DATA_FACTORY_REGISTER(elemname, classname) \
107  crn::DataFactory::RegisterClass(elemname, std::make_unique<crn::DataFactoryElement<classname>>());\
108  crn::Serializer::Register<classname>();
109 
110 #endif
Factory element.
~DataFactory()
Destructor.
XML element.
Definition: CRNXml.h:135
virtual UObject Create(xml::Element &el) const =0
Creates an object.
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
virtual ~DataFactoryElement() override
DataFactory & operator=(const DataFactory &)=delete
Base class for factory elements.
static String GetKnownTypes()
Returns the list of registered classes.
virtual UObject Create(xml::Element &el) const override
Creates an object.