-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
137 changed files
with
5,773 additions
and
951 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2015 MongoDB Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
#include <bsoncxx/builder/stream/document.hpp> | ||
#include <bsoncxx/decimal128.hpp> | ||
#include <bsoncxx/exception/exception.hpp> | ||
#include <bsoncxx/types.hpp> | ||
|
||
using namespace bsoncxx; | ||
|
||
int main(int, char**) { | ||
// Convert a string to BSON Decimal128. | ||
decimal128 d128; | ||
try { | ||
d128 = decimal128{"1.234E+3456"}; | ||
} catch (const bsoncxx::exception& e) { | ||
// The example won't fail, but in general, arbitrary strings | ||
// might not convert properly. | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// Add it to a BSON document. | ||
builder::stream::document build_doc; | ||
build_doc << "counter" << d128; | ||
auto doc = document::value{build_doc.extract()}; | ||
|
||
// Extract a BSON Decimal128 from a document view. | ||
auto view = doc.view(); | ||
auto d128copy = view["counter"].get_decimal128().value; | ||
|
||
// Convert it back to a string. | ||
std::cout << "Counter is " << d128copy.to_string() << std::endl; | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2016 MongoDB Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <bsoncxx/decimal128.hpp> | ||
#include <bsoncxx/exception/error_code.hpp> | ||
#include <bsoncxx/exception/exception.hpp> | ||
#include <bsoncxx/stdx/string_view.hpp> | ||
|
||
#include <bson.h> | ||
|
||
#include <bsoncxx/config/private/prelude.hh> | ||
|
||
namespace bsoncxx { | ||
BSONCXX_INLINE_NAMESPACE_BEGIN | ||
|
||
decimal128::decimal128(stdx::string_view str) { | ||
bson_decimal128_t d128; | ||
if (!bson_decimal128_from_string(str.to_string().c_str(), &d128)) { | ||
throw bsoncxx::exception{error_code::k_invalid_decimal128}; | ||
} | ||
_high = d128.high; | ||
_low = d128.low; | ||
} | ||
|
||
std::string decimal128::to_string() const { | ||
bson_decimal128_t d128; | ||
d128.high = _high; | ||
d128.low = _low; | ||
char str[BSON_DECIMAL128_STRING]; | ||
bson_decimal128_to_string(&d128, str); | ||
return {str}; | ||
} | ||
|
||
bool operator==(const decimal128& lhs, const decimal128& rhs) { | ||
return lhs._high == rhs._high && lhs._low == rhs._low; | ||
} | ||
|
||
bool operator!=(const decimal128& lhs, const decimal128& rhs) { | ||
return !(lhs == rhs); | ||
} | ||
|
||
BSONCXX_INLINE_NAMESPACE_END | ||
} // namespace bsoncxx |
Oops, something went wrong.