1 #ifndef MOBI_MTLD_COMMON 2 #define MOBI_MTLD_COMMON 11 #include <unordered_map> 13 #include <sys/types.h> 16 #include <mtld/util.h> 17 #include <mtld/exception.h> 20 #if defined(WIN32) || defined(WIN64) 21 #define strcasecmp _stricmp 22 #define strncasecmp _strnicmp 23 #define strtok_r strtok_s 24 #define strtoll _strtoi64 25 #define strtoull _strtoui64 26 #define snprintf _snprintf 33 namespace Mobi {
namespace Mtld {
namespace Da {
43 struct attrvec :
public std::unordered_map<size_t, const Value *> {
44 attrvec() : std::unordered_map<size_t, const Value *>() {}
64 NoSuchProperty(std::string name) { *
this <<
"no such property \"" << name <<
"\""; }
65 NoSuchProperty(
int idx) { *
this <<
"no property at index " << idx; }
105 *
this <<
": " << typec;
127 enum PropertyType { Integer, String, Bool, Float, Set };
137 typedef std::vector<Value *> valueset;
140 operator<<(ostream &,
const valueset &);
157 const char *strval()
const {
162 float floatval()
const {
167 const valueset setval()
const {
172 long longval()
const {
177 operator bool()
const;
178 operator std::string()
const;
179 operator long()
const;
180 operator float()
const;
181 int compare(
const Value &)
const;
183 std::string toString()
const {
return std::string(*
this); }
200 template<
typename signedT>
201 static signedT atoi(
const char *);
202 template<
typename unsignedT>
203 static unsignedT atou(
const char *);
204 template<
typename realT>
205 static realT atof(
const char *);
209 bool operator()(
const std::string & a,
const std::string & b)
const {
210 return strncasecmp(a.c_str(), b.c_str(), b.size()) < 0;
214 typedef std::map<std::string, std::string, CaseInsensitiveLess> HeaderMap;
223 DA_EXPORT
extern const char **defaultHeaderPriorities[];
241 typedef std::vector<const Property *> propvec;
245 std::shared_ptr<Values> values;
255 class constcharless {
257 bool operator()(
const char *l,
const char *r)
const {
258 return strcmp(l, r) < 0;
264 typedef std::map<const char *, const Property *, constcharless> propByNameMap;
265 propByNameMap propertiesByName;
269 propvec &getProperties() {
return properties; }
270 const propvec &getProperties()
const {
return properties; }
273 const Property &addProperty(
const char *name, PropertyType type);
274 const Property *findPropertyDescr(
const char *p)
const;
276 const Property &getPropertyDescr(
const char *p)
const;
277 const Property &getPropertyDescr(
size_t off)
const;
279 size_t getPropertyCount()
const {
return getProperties().size(); }
287 __attribute__((malloc))
char *strdup(
const char *);
288 __attribute__((malloc))
char *strdup(
const char *,
size_t);
291 virtual ~CommonDeviceAtlas() {}
292 virtual int getDataRevision()
const = 0;
293 virtual time_t getDataGeneration()
const = 0;
308 const Value **globalAttrs;
314 __attribute__((malloc))
char *strdup(
const char *from,
size_t len = -1);
316 virtual size_t size()
const = 0;
317 virtual bool empty() = 0;
325 void getProperty(
const Property &prop,
int &value);
326 void getProperty(
const Property &prop,
const char *&value);
327 void getProperty(
const Property &prop,
bool &value);
329 virtual const Value *findProperty(
const Property &p)
const = 0;
330 virtual const Value *findProperty(
size_t)
const = 0;
331 virtual void setProperty(
const Property &,
const Value *) = 0;
332 virtual void setProperty(
size_t,
const Value *) = 0;
333 virtual const Property &getPropertyDescr(
const char *, PropertyType *) = 0;
334 virtual const Property &getPropertyDescr(
size_t idx)
const = 0;
339 void getProperty(
const char *prop,
const Value *&value)
340 { getProperty(getPropertyDescr(prop, 0), value); }
341 void getProperty(
const char *prop,
int &value)
342 { getProperty(getPropertyDescr(prop, 0), value); }
343 void getProperty(
const char *prop,
const char *&value)
344 { getProperty(getPropertyDescr(prop, 0), value); }
345 void getProperty(
const char *prop,
bool &value)
346 { getProperty(getPropertyDescr(prop, 0), value); }
348 bool hasProperty(
const Property &prop) {
return findProperty(prop.index) != 0; }
356 template <
typename T>
357 bool contains(
const char *propname, T tocheck);
361 virtual const Value *operator[](
const char *propname)
const = 0;
363 DA_EXPORT std::ostream &operator<<(std::ostream &,
const PropertyType &);
364 DA_EXPORT std::ostream &operator<<(std::ostream &,
const Property &);
365 static inline std::ostream &operator << (std::ostream &os,
const Value &v)
366 {
return os << std::string(v); }
368 inline bool CommonDeliveryContext::contains<const char *>(
const char *,
const char *);
370 #include <mtld/common.tpp>
Definition: exception.h:16
const propvec & getPropertyNames() const
Returns the Json's properties list.
Definition: common.h:284