MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
parser-impl.h
Go to the documentation of this file.
1/* Copyright 2014 MongoDB Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
18#pragma once
19
20#include "mongo/base/string_data.h"
21#include "mongo/geo/constants.h"
22#include "mongo/geo/geometry.h"
27#include "mongo/geo/geoobj.h"
28#include "mongo/geo/point.h"
29#include "mongo/geo/polygon.h"
30#include "mongo/geo/parser.h"
31#include "mongo/util/assert_util.h"
32
33namespace mongo {
34namespace geo {
35
36template <typename TCoordinates>
38 BSONElement typeField = bson.getField(kTypeFieldName);
39
40 uassert(0,
41 "bson argument must have field \"type\" that has value of type string.",
42 !typeField.eoo() && typeField.type() == String);
43
44 switch (stringToType(typeField.String())) {
45 case GeoObjType_Point:
46 return new Point<TCoordinates>(bson);
47 case GeoObjType_MultiPoint:
48 return new MultiPoint<TCoordinates>(bson);
49 case GeoObjType_LineString:
50 return new LineString<TCoordinates>(bson);
51 case GeoObjType_MultiLineString:
52 return new MultiLineString<TCoordinates>(bson);
53 case GeoObjType_Polygon:
54 return new Polygon<TCoordinates>(bson);
55 case GeoObjType_MultiPolygon:
56 return new MultiPolygon<TCoordinates>(bson);
57 case GeoObjType_GeometryCollection:
58 return new GeometryCollection<TCoordinates>(bson);
59 default:
60 uassert(0, "bson must contain a type supported by MongoDB.", false);
61 }
62}
63
64template <typename TCoordinates>
66 if (typeStr == kPointTypeStr)
67 return GeoObjType_Point;
68 if (typeStr == kLineStringTypeStr)
69 return GeoObjType_LineString;
70 if (typeStr == kPolygonTypeStr)
71 return GeoObjType_Polygon;
72 if (typeStr == kMultiPointTypeStr)
73 return GeoObjType_MultiPoint;
74 if (typeStr == kMultiLineStringTypeStr)
75 return GeoObjType_MultiLineString;
76 if (typeStr == kMultiPolygonTypeStr)
77 return GeoObjType_MultiPolygon;
78 if (typeStr == kGeometryCollectionTypeStr)
79 return GeoObjType_GeometryCollection;
80
81 uassert(0, "typeStr must contain a GeoJSON type supported by MongoDB", false);
82}
83
84} // namespace geo
85} // namespace mongo
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object.
Definition bsonelement.h:172
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:154
std::string String() const
These functions, which start with a capital letter, throw a MsgAssertionException if the element is n...
Definition bsonelement.h:62
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
BSONElement getField(const StringData &name) const
Get the field of the specified name.
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.
Definition string_data.h:43
Definition geoobj.h:34
Definition geometrycollection.h:40
Definition linestring.h:34
Definition multilinestring.h:35
Definition multipoint.h:34
Definition multipolygon.h:36
Definition parser.h:30
static GeoObj< TCoordinates > * parse(const BSONObj &bson)
Parse the given BSON into a geometry type.
Definition parser-impl.h:37
Represents a Point.
Definition point.h:52
Definition polygon.h:35
GeoObjType
An enum to represent the type of geometry of a Geo object.
Definition constants.h:45
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
@ String
character string, stored in utf8
Definition bsontypes.h:46