MongoDB C++ Driver  legacy-1.1.2
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"
23 #include "mongo/geo/linestring.h"
25 #include "mongo/geo/multipoint.h"
26 #include "mongo/geo/multipolygon.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 
33 namespace mongo {
34 namespace geo {
35 
36 template <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 
64 template <typename TCoordinates>
65 GeoObjType Parser<TCoordinates>::stringToType(const StringData& typeStr) {
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
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: geometrycollection.h:37
Definition: geoobj.h:34
Definition: linestring.h:34
Definition: multipolygon.h:36
Definition: multilinestring.h:35
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:154
Definition: geometrycollection.h:40
BSONElement getField(const StringData &name) const
Get the field of the specified name.
std::string String() const
These functions, which start with a capital letter, throw a MsgAssertionException if the element is n...
Definition: bsonelement.h:62
Definition: multipoint.h:34
Represents a Point.
Definition: geometry.h:36
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
Definition: polygon.h:35
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
static GeoObj< TCoordinates > * parse(const BSONObj &bson)
Parse the given BSON into a geometry type.
Definition: parser-impl.h:37
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
character string, stored in utf8
Definition: bsontypes.h:46