libspatialindex API Reference  (git-trunk)
Tools.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libspatialindex - A C++ library for spatial indexing
3  * Author: Marios Hadjieleftheriou, mhadji@gmail.com
4  ******************************************************************************
5  * Copyright (c) 2004, Marios Hadjieleftheriou
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26 ******************************************************************************/
27 
28 #pragma once
29 
30 
31 #if (defined _WIN32 || defined _WIN64 || defined WIN32 || defined WIN64) && (defined _MSC_VER) && (_MSC_VER < 1900) && !defined __GNUC__
32  typedef __int8 int8_t;
33  typedef __int16 int16_t;
34  typedef __int32 int32_t;
35  typedef __int64 int64_t;
36  typedef unsigned __int8 uint8_t;
37  typedef unsigned __int16 uint16_t;
38  typedef unsigned __int32 uint32_t;
39  typedef unsigned __int64 uint64_t;
40 
41 #else
42  #include <cstdint>
43 #endif
44 
45 #if (defined _WIN32 || defined _WIN64 || defined WIN32 || defined WIN64) && !defined __GNUC__
46  #ifdef SIDX_DLL_EXPORT
47  #define SIDX_DLL __declspec(dllexport)
48  #else
49  #define SIDX_DLL
50  #endif
51 
52  // Nuke this annoying warning. See http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html
53 #pragma warning( disable: 4251 )
54 
55 #else
56  #define SIDX_DLL
57 #endif
58 
59 #include <cassert>
60 #include <iostream>
61 #include <iomanip>
62 #include <iterator>
63 #include <string>
64 #include <sstream>
65 #include <fstream>
66 #include <queue>
67 #include <vector>
68 #include <map>
69 #include <set>
70 #include <stack>
71 #include <list>
72 #include <algorithm>
73 #include <cwchar>
74 
75 #include "PointerPool.h"
76 #include "PoolPointer.h"
77 
78 namespace Tools
79 {
81  {
82  IT_RIGHTOPEN = 0x0,
86  };
87 
89  {
90  VT_LONG = 0x0,
107  };
108 
109  enum FileMode
110  {
111  APPEND = 0x0,
113  };
114 
115  //
116  // Exceptions
117  //
119  {
120  public:
121  virtual std::string what() = 0;
122  virtual ~Exception() = default;
123  };
124 
126  {
127  public:
128  IndexOutOfBoundsException(size_t i);
129  ~IndexOutOfBoundsException() override = default;
130  std::string what() override;
131 
132  private:
133  std::string m_error;
134  }; // IndexOutOfBoundsException
135 
137  {
138  public:
139  IllegalArgumentException(std::string s);
140  ~IllegalArgumentException() override = default;
141  std::string what() override;
142 
143  private:
144  std::string m_error;
145  }; // IllegalArgumentException
146 
148  {
149  public:
150  IllegalStateException(std::string s);
151  ~IllegalStateException() override = default;
152  std::string what() override;
153 
154  private:
155  std::string m_error;
156  }; // IllegalStateException
157 
159  {
160  public:
161  EndOfStreamException(std::string s);
162  ~EndOfStreamException() override = default;
163  std::string what() override;
164 
165  private:
166  std::string m_error;
167  }; // EndOfStreamException
168 
170  {
171  public:
172  ResourceLockedException(std::string s);
173  ~ResourceLockedException() override = default;
174  std::string what() override;
175 
176  private:
177  std::string m_error;
178  }; // ResourceLockedException
179 
181  {
182  public:
183  NotSupportedException(std::string s);
184  ~NotSupportedException() override = default;
185  std::string what() override;
186 
187  private:
188  std::string m_error;
189  }; // NotSupportedException
190 
191  //
192  // Interfaces
193  //
195  {
196  public:
197  virtual ~IInterval() = default;
198 
199  virtual double getLowerBound() const = 0;
200  virtual double getUpperBound() const = 0;
201  virtual void setBounds(double, double) = 0;
202  virtual bool intersectsInterval(const IInterval&) const = 0;
203  virtual bool intersectsInterval(IntervalType type, const double start, const double end) const = 0;
204  virtual bool containsInterval(const IInterval&) const = 0;
205  virtual IntervalType getIntervalType() const = 0;
206  }; // IInterval
207 
209  {
210  public:
211  virtual ~IObject() = default;
212 
213  virtual IObject* clone() = 0;
214  // return a new object that is an exact copy of this one.
215  // IMPORTANT: do not return the this pointer!
216  }; // IObject
217 
219  {
220  public:
221  virtual ~ISerializable() = default;
222 
223  virtual uint32_t getByteArraySize() = 0;
224  // returns the size of the required uint8_t array.
225  virtual void loadFromByteArray(const uint8_t* data) = 0;
226  // load this object using the uint8_t array.
227  virtual void storeToByteArray(uint8_t** data, uint32_t& length) = 0;
228  // store this object in the uint8_t array.
229  };
230 
232  {
233  public:
234  virtual ~IComparable() = default;
235 
236  virtual bool operator<(const IComparable& o) const = 0;
237  virtual bool operator>(const IComparable& o) const = 0;
238  virtual bool operator==(const IComparable& o) const = 0;
239  }; //IComparable
240 
242  {
243  public:
244  virtual ~IObjectComparator() = default;
245 
246  virtual int compare(IObject* o1, IObject* o2) = 0;
247  }; // IObjectComparator
248 
250  {
251  public:
252  virtual ~IObjectStream() = default;
253 
254  virtual IObject* getNext() = 0;
255  // returns a pointer to the next entry in the
256  // stream or 0 at the end of the stream.
257 
258  virtual bool hasNext() = 0;
259  // returns true if there are more items in the stream.
260 
261  virtual uint32_t size() = 0;
262  // returns the total number of entries available in the stream.
263 
264  virtual void rewind() = 0;
265  // sets the stream pointer to the first entry, if possible.
266  }; // IObjectStream
267 
268  //
269  // Classes & Functions
270  //
271 
273  {
274  public:
275  Variant();
276 
277  VariantType m_varType{VT_EMPTY};
278 
279  union
280  {
281  int16_t iVal; // VT_SHORT
282  int32_t lVal; // VT_LONG
283  int64_t llVal; // VT_LONGLONG
284  uint8_t bVal; // VT_BYTE
285  float fltVal; // VT_FLOAT
286  double dblVal; // VT_DOUBLE
287  char cVal; // VT_CHAR
288  uint16_t uiVal; // VT_USHORT
289  uint32_t ulVal; // VT_ULONG
290  uint64_t ullVal; // VT_ULONGLONG
291  bool blVal; // VT_BOOL
292  char* pcVal; // VT_PCHAR
293  void* pvVal; // VT_PVOID
294  wchar_t* pwcVal;
295  } m_val;
296  }; // Variant
297 
299  SIDX_DLL std::ostream& operator<<(std::ostream& os, const Tools::PropertySet& p);
300 
302  {
303  public:
304  PropertySet();
305  PropertySet(const uint8_t* data);
306  ~PropertySet() override;
307 
308  Variant getProperty(std::string property) const;
309  void setProperty(std::string property, Variant const& v);
310  void removeProperty(std::string property);
311 
312  uint32_t getByteArraySize() override;
313  void loadFromByteArray(const uint8_t* data) override;
314  void storeToByteArray(uint8_t** data, uint32_t& length) override;
315 
316  private:
317  std::map<std::string, Variant> m_propertySet;
318 // #ifdef HAVE_PTHREAD_H
319 // pthread_rwlock_t m_rwLock;
320 // #else
321 // bool m_rwLock;
322 // #endif
323  friend SIDX_DLL std::ostream& operator<<(std::ostream& os, const Tools::PropertySet& p);
324  }; // PropertySet
325 
326  // does not support degenerate intervals.
327  class SIDX_DLL Interval : public IInterval
328  {
329  public:
330  Interval();
331  Interval(IntervalType, double, double);
332  Interval(double, double);
333  Interval(const Interval&);
334  ~Interval() override = default;
335  virtual IInterval& operator=(const IInterval&);
336 
337  virtual bool operator==(const Interval&) const;
338  virtual bool operator!=(const Interval&) const;
339  double getLowerBound() const override;
340  double getUpperBound() const override;
341  void setBounds(double, double) override;
342  bool intersectsInterval(const IInterval&) const override;
343  bool intersectsInterval(IntervalType type, const double start, const double end) const override;
344  bool containsInterval(const IInterval&) const override;
345  IntervalType getIntervalType() const override;
346 
348  double m_low{0.0};
349  double m_high{0.0};
350  }; // Interval
351 
352  SIDX_DLL std::ostream& operator<<(std::ostream& os, const Tools::Interval& iv);
353 
355  {
356  public:
357  Random();
358  Random(uint32_t seed, uint16_t xsubi0);
359  virtual ~Random();
360 
361  int32_t nextUniformLong();
362  // returns a uniformly distributed long.
363  uint32_t nextUniformUnsignedLong();
364  // returns a uniformly distributed unsigned long.
365  int32_t nextUniformLong(int32_t low, int32_t high);
366  // returns a uniformly distributed long in the range [low, high).
367  uint32_t nextUniformUnsignedLong(uint32_t low, uint32_t high);
368  // returns a uniformly distributed unsigned long in the range [low, high).
369  int64_t nextUniformLongLong();
370  // returns a uniformly distributed long long.
371  uint64_t nextUniformUnsignedLongLong();
372  // returns a uniformly distributed unsigned long long.
373  int64_t nextUniformLongLong(int64_t low, int64_t high);
374  // returns a uniformly distributed unsigned long long in the range [low, high).
375  uint64_t nextUniformUnsignedLongLong(uint64_t low, uint64_t high);
376  // returns a uniformly distributed unsigned long long in the range [low, high).
377  int16_t nextUniformShort();
378  // returns a uniformly distributed short.
379  uint16_t nextUniformUnsignedShort();
380  // returns a uniformly distributed unsigned short.
381  double nextUniformDouble();
382  // returns a uniformly distributed double in the range [0, 1).
383  double nextUniformDouble(double low, double high);
384  // returns a uniformly distributed double in the range [low, high).
385 
386  bool flipCoin();
387 
388  private:
389  void initDrand(uint32_t seed, uint16_t xsubi0);
390 
391  uint16_t* m_pBuffer;
392  }; // Random
393 
394  #if HAVE_PTHREAD_H
395  class SIDX_DLL LockGuard
396  {
397  public:
398  LockGuard(pthread_mutex_t* pLock);
399  ~LockGuard();
400 
401  private:
402  pthread_mutex_t* m_pLock;
403  }; // LockGuard
404  #endif
405 
407  {
408  public:
409  BufferedFile(uint32_t u32BufferSize = 16384);
410  virtual ~BufferedFile();
411 
412  virtual void close();
413  virtual bool eof();
414  virtual void rewind() = 0;
415  virtual void seek(std::fstream::off_type offset) = 0;
416 
417  protected:
418  std::fstream m_file;
419  char* m_buffer;
420  uint32_t m_u32BufferSize;
421  bool m_bEOF{true};
422  };
423 
425  {
426  public:
428  BufferedFileReader(const std::string& sFileName, uint32_t u32BufferSize = 32768);
429  ~BufferedFileReader() override;
430 
431  virtual void open(const std::string& sFileName);
432  void rewind() override;
433  void seek(std::fstream::off_type offset) override;
434 
435  virtual uint8_t readUInt8();
436  virtual uint16_t readUInt16();
437  virtual uint32_t readUInt32();
438  virtual uint64_t readUInt64();
439  virtual float readFloat();
440  virtual double readDouble();
441  virtual bool readBoolean();
442  virtual std::string readString();
443  virtual void readBytes(uint32_t u32Len, uint8_t** pData);
444  };
445 
447  {
448  public:
450  BufferedFileWriter(const std::string& sFileName, FileMode mode = CREATE, uint32_t u32BufferSize = 32768);
451  ~BufferedFileWriter() override;
452 
453  virtual void open(const std::string& sFileName, FileMode mode = CREATE);
454  void rewind() override;
455  void seek(std::fstream::off_type offset) override;
456 
457  virtual void write(uint8_t i);
458  virtual void write(uint16_t i);
459  virtual void write(uint32_t i);
460  virtual void write(uint64_t i);
461  virtual void write(float i);
462  virtual void write(double i);
463  virtual void write(bool b);
464  virtual void write(const std::string& s);
465  virtual void write(uint32_t u32Len, uint8_t* pData);
466  };
467 
469  {
470  public:
471  TemporaryFile();
472  virtual ~TemporaryFile();
473 
474  void rewindForReading();
475  void rewindForWriting();
476  bool eof();
477  std::string getFileName() const;
478 
479  uint8_t readUInt8();
480  uint16_t readUInt16();
481  uint32_t readUInt32();
482  uint64_t readUInt64();
483  float readFloat();
484  double readDouble();
485  std::string readString();
486  void readBytes(uint32_t u32Len, uint8_t** pData);
487 
488  void write(uint8_t i);
489  void write(uint16_t i);
490  void write(uint32_t i);
491  void write(uint64_t i);
492  void write(float i);
493  void write(double i);
494  void write(const std::string& s);
495  void write(uint32_t u32Len, uint8_t* pData);
496 
497  private:
498  std::string m_sFile;
499  BufferedFile* m_pFile;
500  };
501 }
uint32_t ulVal
Definition: Tools.h:289
FileMode
Definition: Tools.h:109
uint64_t ullVal
Definition: Tools.h:290
std::fstream m_file
Definition: Tools.h:418
wchar_t * pwcVal
Definition: Tools.h:294
VariantType
Definition: Tools.h:88
char * pcVal
Definition: Tools.h:292
uint8_t bVal
Definition: Tools.h:284
char * m_buffer
Definition: Tools.h:419
uint16_t uiVal
Definition: Tools.h:288
int64_t llVal
Definition: Tools.h:283
float fltVal
Definition: Tools.h:285
char cVal
Definition: Tools.h:287
SIDX_DLL std::ostream & operator<<(std::ostream &os, const Tools::PropertySet &p)
Definition: Tools.cc:647
class SIDX_DLL PropertySet
Definition: Tools.h:298
void * pvVal
Definition: Tools.h:293
int32_t lVal
Definition: Tools.h:282
#define SIDX_DLL
Definition: Tools.h:56
double dblVal
Definition: Tools.h:286
uint32_t m_u32BufferSize
Definition: Tools.h:420
int16_t iVal
Definition: Tools.h:281
bool blVal
Definition: Tools.h:291
IntervalType
Definition: Tools.h:80