1 /******************************************************************************
2  * $Id: ogr_mem.h 92caa0d0f67bdff72b5f272d1558b1909bdbeaea 2021-03-31 14:38:01 +0200 Even Rouault $
3  *
4  * Project:  OpenGIS Simple Features Reference Implementation
5  * Purpose:  Private definitions within the OGR Memory driver.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
10  * Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  ****************************************************************************/
30 
31 #ifndef OGRMEM_H_INCLUDED
32 #define OGRMEM_H_INCLUDED
33 
34 #include "ogrsf_frmts.h"
35 
36 #include <map>
37 
38 /************************************************************************/
39 /*                             OGRMemLayer                              */
40 /************************************************************************/
41 class OGRMemDataSource;
42 
43 class IOGRMemLayerFeatureIterator;
44 
45 class OGRMemLayer CPL_NON_FINAL: public OGRLayer
46 {
47     CPL_DISALLOW_COPY_ASSIGN(OGRMemLayer)
48 
49     typedef std::map<GIntBig, OGRFeature*>           FeatureMap;
50     typedef std::map<GIntBig, OGRFeature*>::iterator FeatureIterator;
51 
52     OGRFeatureDefn     *m_poFeatureDefn;
53 
54     GIntBig             m_nFeatureCount;
55 
56     GIntBig             m_iNextReadFID;
57     GIntBig             m_nMaxFeatureCount;  // Max size of papoFeatures.
58     OGRFeature        **m_papoFeatures;
59     bool                m_bHasHoles;
60 
61     FeatureMap          m_oMapFeatures;
62     FeatureIterator     m_oMapFeaturesIter;
63 
64     GIntBig             m_iNextCreateFID;
65 
66     bool                m_bUpdatable;
67     bool                m_bAdvertizeUTF8;
68 
69     bool                m_bUpdated;
70 
71     // Only use it in the lifetime of a function where the list of features
72     // doesn't change.
73     IOGRMemLayerFeatureIterator* GetIterator();
74 
75   public:
76                         OGRMemLayer( const char * pszName,
77                                      OGRSpatialReference *poSRS,
78                                      OGRwkbGeometryType eGeomType );
79     virtual            ~OGRMemLayer();
80 
81     void                ResetReading() override;
82     OGRFeature *        GetNextFeature() override;
83     virtual OGRErr      SetNextByIndex( GIntBig nIndex ) override;
84 
85     OGRFeature         *GetFeature( GIntBig nFeatureId ) override;
86     OGRErr              ISetFeature( OGRFeature *poFeature ) override;
87     OGRErr              ICreateFeature( OGRFeature *poFeature ) override;
88     virtual OGRErr      DeleteFeature( GIntBig nFID ) override;
89 
GetLayerDefn()90     OGRFeatureDefn *    GetLayerDefn() override { return m_poFeatureDefn; }
91 
92     GIntBig             GetFeatureCount( int ) override;
93 
94     virtual OGRErr      CreateField( OGRFieldDefn *poField,
95                                      int bApproxOK = TRUE ) override;
96     virtual OGRErr      DeleteField( int iField ) override;
97     virtual OGRErr      ReorderFields( int* panMap ) override;
98     virtual OGRErr      AlterFieldDefn( int iField,
99                                         OGRFieldDefn* poNewFieldDefn,
100                                         int nFlags ) override;
101     virtual OGRErr      CreateGeomField( OGRGeomFieldDefn *poGeomField,
102                                          int bApproxOK = TRUE ) override;
103 
104     int                 TestCapability( const char * ) override;
105 
IsUpdatable()106     bool                IsUpdatable() const { return m_bUpdatable; }
SetUpdatable(bool bUpdatableIn)107     void                SetUpdatable( bool bUpdatableIn )
108         { m_bUpdatable = bUpdatableIn; }
SetAdvertizeUTF8(bool bAdvertizeUTF8In)109     void                SetAdvertizeUTF8( bool bAdvertizeUTF8In )
110         { m_bAdvertizeUTF8 = bAdvertizeUTF8In; }
111 
HasBeenUpdated()112     bool                HasBeenUpdated() const { return m_bUpdated; }
SetUpdated(bool bUpdated)113     void                SetUpdated(bool bUpdated) { m_bUpdated = bUpdated; }
114 
GetNextReadFID()115     GIntBig             GetNextReadFID() { return m_iNextReadFID; }
116 };
117 
118 /************************************************************************/
119 /*                           OGRMemDataSource                           */
120 /************************************************************************/
121 
122 class OGRMemDataSource CPL_NON_FINAL: public OGRDataSource
123 {
124     CPL_DISALLOW_COPY_ASSIGN(OGRMemDataSource)
125 
126     OGRMemLayer       **papoLayers;
127     int                 nLayers;
128 
129     char                *pszName;
130 
131   public:
132                         OGRMemDataSource( const char *, char ** );
133                         virtual ~OGRMemDataSource();
134 
GetName()135     const char          *GetName() override { return pszName; }
GetLayerCount()136     int                 GetLayerCount() override { return nLayers; }
137     OGRLayer            *GetLayer( int ) override;
138 
139     virtual OGRLayer    *ICreateLayer( const char *,
140                                        OGRSpatialReference * = nullptr,
141                                        OGRwkbGeometryType = wkbUnknown,
142                                        char ** = nullptr ) override;
143     OGRErr              DeleteLayer( int iLayer ) override;
144 
145     int                 TestCapability( const char * ) override;
146 
147     bool                AddFieldDomain(std::unique_ptr<OGRFieldDomain>&& domain,
148                                        std::string& failureReason) override;
149 };
150 
151 /************************************************************************/
152 /*                             OGRMemDriver                             */
153 /************************************************************************/
154 
155 class OGRMemDriver final: public OGRSFDriver
156 {
157   public:
158     virtual ~OGRMemDriver();
159 
160     const char *GetName() override;
161     OGRDataSource *Open( const char *, int ) override;
162 
163     virtual OGRDataSource *CreateDataSource( const char *pszName,
164                                              char ** = nullptr ) override;
165 
166     int TestCapability( const char * ) override;
167 };
168 
169 #endif  // ndef OGRMEM_H_INCLUDED
170