MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
point.h
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
27#pragma once
28
29#include <algorithm>
30#include <boost/scoped_ptr.hpp>
31
32#include "mongo/client/export_macros.h"
33#include "mongo/db/jsobj.h"
35#include "mongo/geo/constants.h"
36#include "mongo/geo/geometry.h"
37
38namespace mongo {
39namespace geo {
40
51template <typename TCoordinates>
52class Point : public Geometry<TCoordinates> {
53public:
65 explicit Point(const BSONObj& bson);
66
77 explicit Point(const TCoordinates& coords);
78
79 Point(const Point<TCoordinates>& other);
80 Point& operator=(Point<TCoordinates> other);
81
96 virtual BSONObj toBSON() const {
97 return _bson;
98 }
99
114
133 virtual GeoObjType getType() const {
134 return GeoObjType_Point;
135 }
136
151 TCoordinates getCoordinates() const {
152 return _coords;
153 }
154
170 double operator[](size_t dimension) const {
171 return _coords[dimension];
172 }
173
174private:
175 static BSONObj createBSON(const TCoordinates& coords);
176
177 BSONObj _bson;
178 TCoordinates _coords;
179 mutable boost::scoped_ptr<BoundingBox<TCoordinates> > _boundingBox;
180
181 BoundingBox<TCoordinates>* computeBoundingBox() const;
182};
183
184template <typename TCoordinates>
186 : _bson(GeoObj<TCoordinates>::validateType(bson, kPointTypeStr)),
187 _coords(Geometry<TCoordinates>::parseCoords(bson)),
188 _boundingBox(Geometry<TCoordinates>::parseBoundingBox(bson)) {}
189
190template <typename TCoordinates>
191Point<TCoordinates>::Point(const TCoordinates& coords)
192 : _bson(createBSON(coords)), _coords(coords) {}
193
194template <typename TCoordinates>
196 : _bson(other._bson), _coords(other._coords) {
197 // TODO: consider refactoring this to not make deep copies,
198 // and instead use a boost::shared_ptr to share the same bounding
199 // box across all copies of a Point. This would also let the
200 // compiler generate copy and assignment constructors, so we can drop
201 // them from the implementation.
202 if (other._boundingBox)
203 _boundingBox.reset(new BoundingBox<TCoordinates>(*other._boundingBox));
204}
205
206template <typename TCoordinates>
207Point<TCoordinates>& Point<TCoordinates>::operator=(Point<TCoordinates> other) {
208 using std::swap;
209 swap(_bson, other._bson);
210 swap(_coords, other._coords);
211 swap(_boundingBox, other._boundingBox);
212 return *this;
213}
214
215template <typename TCoordinates>
217 if (!_boundingBox)
218 _boundingBox.reset(computeBoundingBox());
219 return *_boundingBox.get();
220}
221
222template <typename TCoordinates>
223BSONObj Point<TCoordinates>::createBSON(const TCoordinates& coords) {
224 BSONObjBuilder bob;
225 return bob.append(kTypeFieldName, kPointTypeStr).appendElements(coords.toBSON()).obj();
226}
227
228template <typename TCoordinates>
229BoundingBox<TCoordinates>* Point<TCoordinates>::computeBoundingBox() const {
230 return new BoundingBox<TCoordinates>(_coords, _coords);
231}
232
233} // namespace geo
234} // namespace mongo
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition bsonobjbuilder.h:124
BSONObjBuilder & appendElements(BSONObj x)
add all the fields from the object specified to this object
BSONObj obj()
destructive The returned BSONObj will free the buffer when it is finished.
Definition bsonobjbuilder.h:618
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
Represents a bounding box.
Definition boundingbox.h:67
Definition geoobj.h:34
Definition geometry.h:39
Represents a Point.
Definition point.h:52
Point(const BSONObj &bson)
Point constructor.
Definition point.h:185
double operator[](size_t dimension) const
Get the position of this point in the given dimension.
Definition point.h:170
TCoordinates getCoordinates() const
Get the coordinates of this point.
Definition point.h:151
virtual BSONObj toBSON() const
Obtain a BSON representation of this point.
Definition point.h:96
virtual GeoObjType getType() const
Get the geometry type of this object.
Definition point.h:133
virtual BoundingBox< TCoordinates > getBoundingBox() const
Get the bounding box of this point.
Definition point.h:216
GeoObjType
An enum to represent the type of geometry of a Geo object.
Definition constants.h:45
BSON classes.
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20