libspatialindex API Reference  (git-trunk)
CustomStorage.cc
Go to the documentation of this file.
1 /******************************************************************************
2  * Project: libsidx - A C API wrapper around libspatialindex
3  * Purpose: C++ object to implement the custom storage manager.
4  * Author: Matthias (nitro), nitro@dr-code.org
5  ******************************************************************************
6  * Copyright (c) 2010, Matthias (nitro)
7  *
8  * All rights reserved.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27 ******************************************************************************/
28 
30 
31 using namespace SpatialIndex;
32 using namespace SpatialIndex::StorageManager;
33 
34 
36 {
38  return sm;
39 }
40 
42 {
43  Tools::Variant var;
44  var = ps.getProperty("CustomStorageCallbacks");
45 
46  if (var.m_varType != Tools::VT_EMPTY)
47  {
48  if (var.m_varType != Tools::VT_PVOID)
49  throw Tools::IllegalArgumentException("CustomStorageManager: Property CustomStorageCallbacks must be Tools::VT_PVOID");
50 
51  if (!var.m_val.pvVal)
52  throw Tools::IllegalArgumentException("CustomStorageManager: Property CustomStorageCallbacks must not be 0.");
53 
54  // we already checked for validity in IndexProperty_SetCustomStorageCallbacks
55  CustomStorageManagerCallbacks* callbackArray = static_cast<CustomStorageManagerCallbacks*>(var.m_val.pvVal);
56  callbacks = *callbackArray;
57  }
58 
59  int errorCode( NoError );
60  if ( callbacks.createCallback ) callbacks.createCallback( callbacks.context, &errorCode );
61  processErrorCode( errorCode, NewPage );
62 }
63 
65 {
66  int errorCode( NoError );
67  if ( callbacks.destroyCallback ) callbacks.destroyCallback( callbacks.context, &errorCode );
68  processErrorCode( errorCode, NewPage );
69 }
70 
72 {
73  int errorCode( NoError );
74  if ( callbacks.flushCallback ) callbacks.flushCallback( callbacks.context, &errorCode );
75  processErrorCode( errorCode, NewPage );
76 }
77 
78 void CustomStorageManager::loadByteArray(const id_type page, uint32_t& len, uint8_t** data)
79 {
80  int errorCode( NoError );
81  if ( callbacks.loadByteArrayCallback ) callbacks.loadByteArrayCallback( callbacks.context, page, &len, data, &errorCode );
82  processErrorCode( errorCode, page );
83 }
84 
85 void CustomStorageManager::storeByteArray(id_type& page, const uint32_t len, const uint8_t* const data)
86 {
87  int errorCode( NoError );
88  if ( callbacks.storeByteArrayCallback ) callbacks.storeByteArrayCallback( callbacks.context, &page, len, data, &errorCode );
89  processErrorCode( errorCode, page );
90 }
91 
93 {
94  int errorCode( NoError );
95  if ( callbacks.deleteByteArrayCallback ) callbacks.deleteByteArrayCallback( callbacks.context, page, &errorCode );
96  processErrorCode( errorCode, page );
97 }
98 
99 inline void CustomStorageManager::processErrorCode(int errorCode, const id_type page)
100 {
101  switch (errorCode)
102  {
103  case NoError:
104  break;
105 
106  case InvalidPageError:
107  throw InvalidPageException( page );
108  break;
109 
110  case IllegalStateError:
111  throw Tools::IllegalStateException( "CustomStorageManager: Error in user implementation." );
112  break;
113 
114  default:
115  throw Tools::IllegalStateException( "CustomStorageManager: Unknown error." );
116  }
117 }
VariantType m_varType
Definition: Tools.h:277
virtual void loadByteArray(const id_type page, uint32_t &len, uint8_t **data)
virtual void storeByteArray(id_type &page, const uint32_t len, const uint8_t *const data)
void(* loadByteArrayCallback)(const void *context, const id_type page, uint32_t *len, uint8_t **data, int *errorCode)
Definition: CustomStorage.h:52
void(* storeByteArrayCallback)(const void *context, id_type *page, const uint32_t len, const uint8_t *const data, int *errorCode)
Definition: CustomStorage.h:53
void(* createCallback)(const void *context, int *errorCode)
Definition: CustomStorage.h:49
void(* flushCallback)(const void *context, int *errorCode)
Definition: CustomStorage.h:51
IStorageManager * returnCustomStorageManager(Tools::PropertySet &in)
void(* destroyCallback)(const void *context, int *errorCode)
Definition: CustomStorage.h:50
union Tools::Variant::@0 m_val
void * pvVal
Definition: Tools.h:293
virtual void deleteByteArray(const id_type page)
void(* deleteByteArrayCallback)(const void *context, const id_type page, int *errorCode)
Definition: CustomStorage.h:54
int64_t id_type
Definition: SpatialIndex.h:41
Variant getProperty(std::string property) const
Definition: Tools.cc:346