libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNData.h
Go to the documentation of this file.
1 /* Copyright 2009-2016 INSA Lyon, Université Paris-Descartes, 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: CRNData.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNData_HEADER
23 #define CRNData_HEADER
24 
25 #include <vector>
26 #include <CRNData/CRNInt.h>
27 #include <CRNData/CRNReal.h>
28 #include <CRNString.h>
29 #include <CRNStringUTF8.h>
30 #include <CRNIO/CRNPath.h>
31 #include <memory>
32 #include <CRNException.h>
33 #include <CRNMath/CRNProp3.h>
34 #include <cstring>
35 
36 namespace crn
37 {
40  namespace Data
41  {
43  UInt ToCRN(int i);
45  UReal ToCRN(double d);
47  UString ToCRN(const String &str);
49  UStringUTF8 ToCRN(const StringUTF8 &str);
51  UPath ToCRN(const Path &str);
53  UProp3 ToCRN(const Prop3 &val);
54 
56  template<typename T> inline SObject ToCRN(std::shared_ptr<T> ptr)
57  {
58  SObject obj(std::dynamic_pointer_cast<Object>(ptr));
59  if (!obj)
60  throw ExceptionInvalidArgument("ToCRN(std::shared_ptr<T>): not a crn::Object.");
61  return obj;
62  }
63 
65  template<typename T> inline UObject ToCRN(const T* ptr)
66  {
67  return Clone(*ptr);
68  }
69 
71  template<typename T> inline UObject ToCRN(const T &obj)
72  {
73  return Clone(obj);
74  }
76  template<typename T> inline T Convert(const Object &obj)
77  {
78  throw ExceptionInvalidArgument("T Convert(const Object &obj): no way to convert object to T.");
79  }
85  template<> inline int Convert(const Object &obj)
86  {
87  const Int *i = dynamic_cast<const Int*>(&obj);
88  if (i)
89  return int(*i);
90  const String *s = dynamic_cast<const String*>(&obj);
91  if (s)
92  return s->ToInt();
93  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
94  if (su) // works for StringUTF8 and Path
95  return su->ToInt();
96  throw ExceptionInvalidArgument("Cannot convert between these types.");
97  }
103  template<> inline unsigned int Convert(const Object &obj)
104  {
105  const String *s = dynamic_cast<const String*>(&obj);
106  if (s)
107  return s->ToUInt();
108  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
109  if (su) // works for StringUTF8 and Path
110  return su->ToUInt();
111  throw ExceptionInvalidArgument("Cannot convert between these types.");
112  }
118  template<> inline double Convert(const Object &obj)
119  {
120  const Int *i = dynamic_cast<const Int*>(&obj);
121  if (i)
122  return int(*i);
123  const Real *r = dynamic_cast<const Real*>(&obj);
124  if (r)
125  return *r;
126  const String *s = dynamic_cast<const String*>(&obj);
127  if (s)
128  return s->ToDouble();
129  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
130  if (su) // works for StringUTF8 and Path
131  return su->ToDouble();
132  throw ExceptionInvalidArgument("Cannot convert between these types.");
133  }
138  template<> inline String Convert(const Object &obj)
139  {
140  const Int *i = dynamic_cast<const Int*>(&obj);
141  if (i)
142  return int(*i);
143  const Real *r = dynamic_cast<const Real*>(&obj);
144  if (r)
145  return double(*r);
146  const String *s = dynamic_cast<const String*>(&obj);
147  if (s)
148  return *s;
149  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
150  if (su) // works for StringUTF8 and Path
151  return *su;
152  throw ExceptionInvalidArgument("Cannot convert between these types.");
153  }
158  template<> inline StringUTF8 Convert(const Object &obj)
159  {
160  const Int *i = dynamic_cast<const Int*>(&obj);
161  if (i)
162  return int(*i);
163  const Real *r = dynamic_cast<const Real*>(&obj);
164  if (r)
165  return double(*r);
166  const String *s = dynamic_cast<const String*>(&obj);
167  if (s)
168  return s->CStr();
169  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
170  if (su) // works for StringUTF8 and Path
171  return *su;
172  throw ExceptionInvalidArgument("Cannot convert between these types.");
173  }
178  template<> inline Path Convert(const Object &obj)
179  {
180  const Int *i = dynamic_cast<const Int*>(&obj);
181  if (i)
182  return int(*i);
183  const String *s = dynamic_cast<const String*>(&obj);
184  if (s)
185  return s->CStr();
186  const StringUTF8 *su = dynamic_cast<const StringUTF8*>(&obj);
187  if (su) // works for StringUTF8 and Path
188  return *su;
189  throw ExceptionInvalidArgument("Cannot convert between these types.");
190  }
196  template<> inline Prop3 Convert(const Object &obj)
197  {
198  const Prop3 *p = dynamic_cast<const Prop3*>(&obj);
199  if (p)
200  return *p;
201  throw ExceptionInvalidArgument("Cannot convert between these types.");
202  }
208  template<> inline bool Convert(const Object &obj)
209  {
210  const Int *i = dynamic_cast<const Int*>(&obj);
211  if (i)
212  return *i != 0;
213  const Prop3 *p = dynamic_cast<const Prop3*>(&obj);
214  if (p)
215  return p->IsTrue();
216  throw ExceptionInvalidArgument("Cannot convert between these types.");
217  }
218 
220  crn::StringUTF8 ASCII85Encode(const uint8_t * const data, size_t len);
222  std::vector<uint8_t> ASCII85Decode(const crn::StringUTF8 &s);
229  template<typename T> std::vector<T> ASCII85Decode(const crn::StringUTF8 &s)
230  {
231  const auto deco = ASCII85Decode(s);
232  const auto nelem = deco.size() / sizeof(T);
233  if (nelem * sizeof(T) != deco.size())
234  throw ExceptionDimension{StringUTF8("ASCII85Decode<") + typeid(T).name() + StringUTF8(">(): invalid size.")};
235  auto res = std::vector<T>(nelem);
236  std::copy(deco.begin(), deco.end(), reinterpret_cast<uint8_t*>(res.data()));
237  return res;
238  }
239 
240  }
242 }
243 
244 #endif
245 
246 
unsigned int ToUInt() const
Conversion to unsigned int.
Definition: CRNString.h:136
const char * CStr() const
Conversion to UTF8 cstring.
Definition: CRNString.cpp:167
double ToDouble() const
Conversion to double.
Definition: CRNString.h:144
int ToInt() const
Conversion to int.
bool IsTrue() const noexcept
Is true?
Definition: CRNProp3.cpp:175
A UTF32 character string class.
Definition: CRNString.h:61
UInt ToCRN(int i)
Converts an int to CRN data.
Definition: CRNData.cpp:33
Interface class for the metric real number class.
Definition: CRNReal.h:38
A convenience class for file paths.
Definition: CRNPath.h:39
A dimension error.
Definition: CRNException.h:119
int ToInt() const
Conversion to int.
Definition: CRNString.h:134
double ToDouble() const
Conversion to double.
T Convert(const Object &obj)
Converts from a CRN object to another type.
Definition: CRNData.h:76
unsigned int ToUInt() const
Conversion to unsigned int.
std::vector< uint8_t > ASCII85Decode(const crn::StringUTF8 &s)
Decodes an ASCII85 string to a vector of bytes.
Definition: CRNData.h:229
Interface class for the metric real number class.
Definition: CRNInt.h:40
A character string class.
Definition: CRNStringUTF8.h:49
UObject Clone(const Object &obj)
Clones an object if possible.
Definition: CRNObject.cpp:35
A ternary proposition.
Definition: CRNProp3.h:40
crn::StringUTF8 ASCII85Encode(const uint8_t *const data, size_t len)
Encodes any data into a printable string.
Definition: CRNData.cpp:109
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107