MongoDB C++ Driver  legacy-1.1.2
geoobj.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 <string>
21 #include <vector>
22 
23 #include "mongo/base/string_data.h"
24 #include "mongo/client/export_macros.h"
25 #include "mongo/db/jsobj.h"
26 #include "mongo/geo/boundingbox.h"
27 #include "mongo/geo/constants.h"
28 #include "mongo/util/assert_util.h"
29 
30 namespace mongo {
31 namespace geo {
32 
33 template <typename TCoordinates>
34 class GeoObj {
35 public:
36  virtual ~GeoObj() {}
37 
43  virtual BSONObj toBSON() const = 0;
44 
50  virtual BoundingBox<TCoordinates> getBoundingBox() const = 0;
51 
57  virtual GeoObjType getType() const = 0;
58 
59 protected:
60  static BSONObj validateType(const BSONObj& bson, const StringData& typeStr);
61 };
62 
63 template <typename TCoordinates>
64 BSONObj GeoObj<TCoordinates>::validateType(const BSONObj& bson, const StringData& typeStr) {
65  BSONElement typeField = bson.getField(kTypeFieldName);
66  uassert(0,
67  "bson must be a valid " + typeStr.toString() + " type",
68  !typeField.eoo() && ((typeField.type() == String) || (typeField.String() == typeStr)));
69  return bson;
70 }
71 
72 } // namespace geo
73 } // 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
BSON classes.
Definition: geoobj.h:34
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:154
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
virtual BSONObj toBSON() const =0
Obtain a BSON representation of this GeoObj.
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
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
virtual BoundingBox< TCoordinates > getBoundingBox() const =0
Obtain the bounding box of this GeoObj.
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78
virtual GeoObjType getType() const =0
Get the geometry type of this object.
Represents a bounding box.
Definition: boundingbox.h:67
character string, stored in utf8
Definition: bsontypes.h:46