libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNObject.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, 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: CRNObject.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNOBJECT_HEADER
23 #define CRNOBJECT_HEADER
24 
25 #include <CRN.h>
26 
27 namespace crn
28 {
29  class Object;
30  CRN_ALIAS_SMART_PTR(Object)
31 }
32 namespace crn
33 {
35  class Object
36  {
37  public:
38  virtual ~Object() = default;
39  protected:
40  };
41 
43  UObject Clone(const Object &obj);
44  UObject Clone(const UCObject &obj);
45  UObject Clone(const SCObject &obj);
46  class Int;
47  std::unique_ptr<Int> Clone(int i);
48  class Real;
49  std::unique_ptr<Real> Clone(double d);
50  class Prop3;
51  std::unique_ptr<Prop3> Clone(bool b);
52 
53  template<typename T, typename U> inline std::unique_ptr<T> CloneAs(const U &obj) { return std::unique_ptr<T>(dynamic_cast<T*>(Clone(obj).release())); }
54 
55  namespace xml
56  {
57  class Element;
58  }
60  void Deserialize(Object &obj, xml::Element &el);
61  void Deserialize(const UObject &obj, xml::Element &el);
62  void Deserialize(const SObject &obj, xml::Element &el);
64  xml::Element Serialize(const Object &obj, xml::Element &parent);
65  xml::Element Serialize(const UCObject &obj, xml::Element &parent);
66  xml::Element Serialize(const SCObject &obj, xml::Element &parent);
67 
69  double Distance(const Object &o1, const Object &o2);
71  double Distance(const UCObject &o1, const UCObject &o2);
73  double Distance(const SCObject &o1, const SCObject &o2);
74  // see CRNMath.h for Distance between numbers
75 
82  template<typename T> struct IsPOSet: public std::integral_constant<bool, traits::HasLT<T>::value && traits::HasGT<T>::value && traits::HasLE<T>::value && traits::HasGE<T>::value> {};
83 
84  // Metric objects
88  template<typename T> struct IsMetric: public std::integral_constant<bool, std::is_arithmetic<T>::value> {};
89  template<> struct IsMetric<Object>: public std::true_type {};
90  template<> struct IsMetric<UObject>: public std::true_type {};
91  template<> struct IsMetric<SObject>: public std::true_type {};
92 
97  template<typename T> struct IsMagma: public std::integral_constant<bool, traits::HasEquals<T>::value && traits::HasPlus<T>::value> {};
98 
103  template<typename T> struct IsGroup: public std::integral_constant<bool, IsMagma<T>::value && traits::HasMinus<T>::value> {};
104 
110  template<typename T> struct IsRing: public std::integral_constant<bool, IsGroup<T>::value && traits::HasInnerMult<T>::value> {};
111 
118  template<typename T> struct IsVectorOverR: public std::integral_constant<bool, IsGroup<T>::value && traits::HasRightOuterMult<T>::value && traits::HasLeftOuterMult<T>::value> {};
119 
127  template<typename T> struct IsAlgebra: public std::integral_constant<bool, IsRing<T>::value && IsVectorOverR<T>::value> {};
128 
137  template<typename T> struct IsField: public std::integral_constant<bool, IsAlgebra<T>::value && traits::HasDivide<T>::value> {};
138 
144  template<typename T> struct IsSerializable: public std::false_type {};
145  template<> struct IsSerializable<Object>: public std::true_type {};
146 
150  template<typename T> struct IsClonable: public std::false_type {};
151  template<> struct IsClonable<Object>: public std::true_type {};
152 
158  template<typename T> struct IsSavable: public std::false_type {};
159 }
160 
165 #define CRN_SERIALIZATION_CONSTRUCTOR(classname) public: classname(crn::xml::Element &el) { Deserialize(el); }
166 
173 #define CRN_DECLARE_CLASS_CONSTRUCTOR(classname) public:\
174  static void Initialize();\
175 private:\
176 struct Init { Init() { classname::Initialize(); } };\
177 static Init init;
178 
185 #define CRN_BEGIN_CLASS_CONSTRUCTOR(classname) void classname::Initialize()\
186 {\
187  static bool init_done = false;\
188  if (!init_done)\
189  {\
190  init_done = true;
191 
198 #define CRN_END_CLASS_CONSTRUCTOR(classname) }\
199 }\
200 classname::Init classname::init;
201 
202 #endif
203 
xml::Element Serialize(const Object &obj, xml::Element &parent)
Writes an object to XML if possible.
Definition: CRNObject.cpp:110
virtual ~Object()=default
Interface class for the metric real number class.
Definition: CRNReal.h:38
double Distance(const Int &i1, const Int &i2) noexcept
Definition: CRNInt.h:78
Interface class for the metric real number class.
Definition: CRNInt.h:40
CRN_ALIAS_SMART_PTR(ImageBW)
void Deserialize(Object &obj, xml::Element &el)
Reads an object from XML if possible.
Definition: CRNObject.cpp:80
UObject Clone(const Object &obj)
Clones an object if possible.
Definition: CRNObject.cpp:35
A ternary proposition.
Definition: CRNProp3.h:40
std::unique_ptr< T > CloneAs(const U &obj)
Definition: CRNObject.h:53