From 43894dccdf87fd14257c10940a011afb80b55c3d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:07 -0500 Subject: [PATCH 01/26] Silence common warnings in API examples --- examples/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index bcc3d56f1e..aaa51cf09b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -134,6 +134,16 @@ file(GLOB_RECURSE examples_headers foreach(source ${api_examples_sources}) add_examples_executable(${source} LIBRARIES mongocxx_target) + + # Silence warnings for common patterns in API examples. + set_property (SOURCE ${source} APPEND PROPERTY COMPILE_OPTIONS + # Allow unused variables. + $<$:-Wno-unused> + $<$:/wd4189> + + # Allow simpler switch statements. + $<$:-Wno-switch> + ) endforeach() foreach(source ${bsoncxx_examples_sources}) From 0b7f138cca5f95d7da4a5aadc61c817c9a0b649b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:08 -0500 Subject: [PATCH 02/26] Add bsoncxx and mongocxx topic pages for guides and explanations --- src/bsoncxx/include/bsoncxx/doc.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 4d42d18b1d..8e0f7579a0 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -88,3 +88,23 @@ /// @namespace std /// The top-level namespace reserved for the C++ standard library. /// + +/// +/// @page topic-bsoncxx-examples Using the bsoncxx Library +/// @brief Examples of how to use the bsoncxx library. +/// + +/// +/// @page topic-mongocxx-examples Using the mongocxx Library +/// @brief Examples of how to use the bsoncxx library. +/// + +/// +/// @page topic-bsoncxx-about About the bsoncxx Library +/// @brief Design decisions, history, and other technical details about the bsoncxx library. +/// + +/// +/// @page topic-mongocxx-about About the mongocxx Library +/// @brief Design decisions, history, and other technical details about the mongocxx library. +/// From d55c71ffcfa7b1a98da109b0343f44ae52cf81cf Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:09 -0500 Subject: [PATCH 03/26] Add bsoncxx topic page for using BSON documents --- src/bsoncxx/include/bsoncxx/doc.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 8e0f7579a0..b9f6504ace 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -92,6 +92,7 @@ /// /// @page topic-bsoncxx-examples Using the bsoncxx Library /// @brief Examples of how to use the bsoncxx library. +/// @li @subpage topic-bsoncxx-examples-bson-documents /// /// @@ -99,6 +100,12 @@ /// @brief Examples of how to use the bsoncxx library. /// +/// +/// @page topic-bsoncxx-examples-bson-documents BSON Documents +/// @brief How to create and use BSON documents. +/// @tableofcontents +/// + /// /// @page topic-bsoncxx-about About the bsoncxx Library /// @brief Design decisions, history, and other technical details about the bsoncxx library. From 96d6e50c24b20380d1253507917f9e274953a445 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:09 -0500 Subject: [PATCH 04/26] Add BSON document usage examples --- .../examples/bson_documents/access_array.md | 25 ++++ .../examples/bson_documents/access_doc.md | 25 ++++ .../examples/bson_documents/create_array.md | 71 ++++++++++++ .../examples/bson_documents/create_doc.md | 71 ++++++++++++ .../examples/bson_documents/elements.md | 31 +++++ .../bsoncxx/examples/bson_documents/json.md | 9 ++ .../bsoncxx/examples/bson_documents/values.md | 39 +++++++ .../access_array/algorithms.cpp | 50 ++++++++ .../bson_documents/access_array/basic.cpp | 53 +++++++++ .../bson_documents/access_array/find.cpp | 46 ++++++++ .../bson_documents/access_array/iterators.cpp | 62 ++++++++++ .../bson_documents/access_array/subscript.cpp | 46 ++++++++ .../bson_documents/access_doc/algorithms.cpp | 53 +++++++++ .../bson_documents/access_doc/basic.cpp | 52 +++++++++ .../bson_documents/access_doc/find.cpp | 46 ++++++++ .../bson_documents/access_doc/iterators.cpp | 62 ++++++++++ .../bson_documents/access_doc/subscript.cpp | 46 ++++++++ .../create_array/builder_append.cpp | 46 ++++++++ .../create_array/builder_basic.cpp | 44 +++++++ .../create_array/builder_bson_type.cpp | 49 ++++++++ .../create_array/builder_bson_value.cpp | 57 +++++++++ .../create_array/builder_make_document.cpp | 43 +++++++ .../create_array/builder_raw_value.cpp | 54 +++++++++ .../create_array/builder_raw_view.cpp | 46 ++++++++ .../create_array/builder_reset.cpp | 54 +++++++++ .../create_array/builder_sub_array.cpp | 46 ++++++++ .../create_array/builder_sub_document.cpp | 42 +++++++ .../create_array/builder_value_type.cpp | 47 ++++++++ .../create_array/json_basic.cpp | 43 +++++++ .../create_array/json_extended.cpp | 47 ++++++++ .../create_array/json_sub_array.cpp | 44 +++++++ .../create_array/json_sub_document.cpp | 43 +++++++ .../bson_documents/create_array/json_udl.cpp | 39 +++++++ .../create_doc/builder_append.cpp | 49 ++++++++ .../create_doc/builder_basic.cpp | 47 ++++++++ .../create_doc/builder_bson_type.cpp | 54 +++++++++ .../create_doc/builder_bson_value.cpp | 57 +++++++++ .../create_doc/builder_make_document.cpp | 46 ++++++++ .../create_doc/builder_raw_value.cpp | 52 +++++++++ .../create_doc/builder_raw_view.cpp | 44 +++++++ .../create_doc/builder_reset.cpp | 57 +++++++++ .../create_doc/builder_sub_array.cpp | 51 ++++++++ .../create_doc/builder_sub_document.cpp | 43 +++++++ .../create_doc/builder_value_type.cpp | 50 ++++++++ .../bson_documents/create_doc/json_basic.cpp | 47 ++++++++ .../create_doc/json_extended.cpp | 47 ++++++++ .../create_doc/json_sub_array.cpp | 44 +++++++ .../create_doc/json_sub_document.cpp | 43 +++++++ .../bson_documents/create_doc/json_udl.cpp | 39 +++++++ .../bson_documents/elements/arr_multi.cpp | 72 ++++++++++++ .../bson_documents/elements/arr_single.cpp | 50 ++++++++ .../elements/cmp_bson_value.cpp | 56 +++++++++ .../bson_documents/elements/cmp_type.cpp | 49 ++++++++ .../bson_documents/elements/doc_multi.cpp | 71 ++++++++++++ .../bson_documents/elements/doc_single.cpp | 49 ++++++++ .../examples/bson_documents/json/array.cpp | 105 +++++++++++++++++ .../examples/bson_documents/json/document.cpp | 109 ++++++++++++++++++ .../bson_documents/values/arr_value.cpp | 49 ++++++++ .../bson_documents/values/arr_view.cpp | 58 ++++++++++ .../bson_documents/values/bson_type_value.cpp | 49 ++++++++ .../bson_documents/values/bson_type_view.cpp | 54 +++++++++ .../bson_documents/values/doc_value.cpp | 49 ++++++++ .../bson_documents/values/doc_view.cpp | 57 +++++++++ .../bson_documents/values/make_value.cpp | 62 ++++++++++ .../bson_documents/values/value_type.cpp | 53 +++++++++ src/bsoncxx/include/bsoncxx/doc.hpp | 17 ++- 66 files changed, 3305 insertions(+), 5 deletions(-) create mode 100644 docs/api/bsoncxx/examples/bson_documents/access_array.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/access_doc.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/create_array.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/create_doc.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/elements.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/json.md create mode 100644 docs/api/bsoncxx/examples/bson_documents/values.md create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/json/array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/json/document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp diff --git a/docs/api/bsoncxx/examples/bson_documents/access_array.md b/docs/api/bsoncxx/examples/bson_documents/access_array.md new file mode 100644 index 0000000000..53961db315 --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/access_array.md @@ -0,0 +1,25 @@ +# Access an Array Element + +## By Iteration + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/access_array/basic.cpp Example + +### Using Iterators + +@snippet api/bsoncxx/examples/bson_documents/access_array/iterators.cpp Example + +### Using Algorithms + +@snippet api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp Example + +## By Key + +### Using find() + +@snippet api/bsoncxx/examples/bson_documents/access_array/find.cpp Example + +### Using the Subscript Operator + +@snippet api/bsoncxx/examples/bson_documents/access_array/subscript.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/access_doc.md b/docs/api/bsoncxx/examples/bson_documents/access_doc.md new file mode 100644 index 0000000000..2388edc3d1 --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/access_doc.md @@ -0,0 +1,25 @@ +# Access a Document Element + +## By Iteration + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/access_doc/basic.cpp Example + +### Using Iterators + +@snippet api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp Example + +### Using Algorithms + +@snippet api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp Example + +## By Key + +### Using find() + +@snippet api/bsoncxx/examples/bson_documents/access_doc/find.cpp Example + +### Using the Subscript Operator + +@snippet api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/create_array.md b/docs/api/bsoncxx/examples/bson_documents/create_array.md new file mode 100644 index 0000000000..0b188a4139 --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/create_array.md @@ -0,0 +1,71 @@ +# Create an Array + +## From a JSON String + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp Example + +### With Extended JSON + +@snippet api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp Example + +### With a Sub-Document + +@snippet api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp Example + +### With a Sub-Array + +@snippet api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp Example + +### With a User-Defined Literal + +@snippet api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp Example + +## Using the Basic Builder + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp Example + +### With make_document + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp Example + +### With Multiple Appends + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp Example + +### To Create Multiple Documents + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp Example + +### With a Value Type + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp Example + +### With a BSON Type + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp Example + +### With a BSON Value + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp Example + +### With a Sub-Document + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp Example + +### With a Sub-Array + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp Example + +## From Raw Bytes + +### As a View + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp Example + +### As a Value + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/create_doc.md b/docs/api/bsoncxx/examples/bson_documents/create_doc.md new file mode 100644 index 0000000000..1f4cafabc3 --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/create_doc.md @@ -0,0 +1,71 @@ +# Create a Document + +## From a JSON String + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp Example + +### With Extended JSON + +@snippet api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp Example + +### With a Sub-Document + +@snippet api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp Example + +### With a Sub-Array + +@snippet api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp Example + +### With a User-Defined Literal + +@snippet api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp Example + +## Using the Basic Builder + +### Basic Example + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp Example + +### With make_document + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp Example + +### With Multiple Appends + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp Example + +### To Create Multiple Documents + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp Example + +### With a Value Type + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp Example + +### With a BSON Type + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp Example + +### With a BSON Value + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp Example + +### With a Sub-Document + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp Example + +### With a Sub-Array + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp Example + +## From Raw Bytes + +### As a View + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp Example + +### As a Value + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/elements.md b/docs/api/bsoncxx/examples/bson_documents/elements.md new file mode 100644 index 0000000000..6819e767ab --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/elements.md @@ -0,0 +1,31 @@ +# Query an Element + +## In a Document + +### For a Single Type + +@snippet api/bsoncxx/examples/bson_documents/elements/doc_single.cpp Example + +### For Multiple Types + +@snippet api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp Example + +## In an Array + +### For Single Type + +@snippet api/bsoncxx/examples/bson_documents/elements/arr_single.cpp Example + +### For Multiple Types + +@snippet api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp Example + +## Comparison + +### By Type + +@snippet api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp Example + +### By BSON Value + +@snippet api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/json.md b/docs/api/bsoncxx/examples/bson_documents/json.md new file mode 100644 index 0000000000..e54c2d8efa --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/json.md @@ -0,0 +1,9 @@ +# Convert to a JSON String + +## From a Document + +@snippet api/bsoncxx/examples/bson_documents/json/document.cpp Example + +## From an Array + +@snippet api/bsoncxx/examples/bson_documents/json/array.cpp Example diff --git a/docs/api/bsoncxx/examples/bson_documents/values.md b/docs/api/bsoncxx/examples/bson_documents/values.md new file mode 100644 index 0000000000..6e864d5adb --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_documents/values.md @@ -0,0 +1,39 @@ +# Obtain a BSON Value + +## From a BSON Type + +### As a View + +@snippet api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp Example + +### As a Value + +@snippet api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp Example + +## From a Document Element + +### As a View + +@snippet api/bsoncxx/examples/bson_documents/values/doc_view.cpp Example + +### As a Value + +@snippet api/bsoncxx/examples/bson_documents/values/doc_value.cpp Example + +## From an Array Element + +### As a View + +@snippet api/bsoncxx/examples/bson_documents/values/arr_view.cpp Example + +### As a Value + +@snippet api/bsoncxx/examples/bson_documents/values/arr_value.cpp Example + +## From a Value Type + +@snippet api/bsoncxx/examples/bson_documents/values/value_type.cpp Example + +## With make_value + +@snippet api/bsoncxx/examples/bson_documents/values/make_value.cpp Example diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp new file mode 100644 index 0000000000..367b064264 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2.0, "three"] +void example(bsoncxx::array::view arr) { + assert(std::distance(arr.begin(), arr.end()) == 3); + + std::vector elements; + + std::copy_if( + arr.begin(), arr.end(), std::back_inserter(elements), [](const bsoncxx::array::element& e) { + return e.key() == "0" || e.type() == bsoncxx::type::k_string; + }); + + assert(elements.size() == 2u); + assert(elements[0].key() == "0"); + assert(elements[1].key() == "2"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp new file mode 100644 index 0000000000..fea4ee62da --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2.0, "three"] +void example(bsoncxx::array::view arr) { + for (bsoncxx::array::element e : arr) { + switch (e.type()) { + case bsoncxx::type::k_int32: + assert(e.key() == "0"); + assert(e.get_int32().value == 1); + break; + case bsoncxx::type::k_double: + assert(e.key() == "1"); + assert(e.get_double().value == 2.0); + break; + case bsoncxx::type::k_string: + assert(e.key() == "2"); + assert(e.get_string().value == "three"); + break; + } + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { // clang-format off + example(bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})")["v"].get_array().value); + +} // clang-format on diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp new file mode 100644 index 0000000000..90d3d59ab7 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2] +void example(bsoncxx::array::view arr) { + assert(arr.find(0) == arr.begin()); + + { + auto iter = arr.find(1); + + assert(iter != arr.end()); + assert(iter->key() == "1"); + assert(iter->get_int32().value == 2); + } + + assert(arr.find(2) == arr.end()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp new file mode 100644 index 0000000000..c6f9a25582 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp @@ -0,0 +1,62 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2] +void example(bsoncxx::array::view arr) { + assert(arr.begin() != arr.end()); + + auto iter = arr.begin(); + assert(iter == arr.begin()); + + { + bsoncxx::array::element e = *iter; + + assert(e.key() == "0"); + assert(e.get_int32().value == 1); + } + + ++iter; + + assert(iter->key() == "1"); + assert(iter->get_int32().value == 2); + + { + auto iter_copy = iter++; + + assert(iter_copy != iter); + assert(iter_copy->key() == "1"); + assert(iter_copy->get_int32() == 2); + } + + assert(iter == arr.end()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp new file mode 100644 index 0000000000..4c0d1cadd1 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2] +void example(bsoncxx::array::view arr) { + assert(arr[0]); + + { + bsoncxx::array::element e = arr[1]; + + assert(e.key() == "1"); + assert(e.get_int32().value == 2); + } + + assert(!arr[2]); // Invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp new file mode 100644 index 0000000000..ab1073fb94 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 +#include +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2.0, "c": "three"} +void example(bsoncxx::document::view doc) { + assert(std::distance(doc.begin(), doc.end()) == 3); + + std::vector elements; + + std::copy_if(doc.begin(), + doc.end(), + std::back_inserter(elements), + [](const bsoncxx::document::element& e) { + return e.key() == "a" || e.type() == bsoncxx::type::k_string; + }); + + assert(elements.size() == 2u); + assert(elements[0].key() == "a"); + assert(elements[1].key() == "c"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})")); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp new file mode 100644 index 0000000000..8129589a61 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp @@ -0,0 +1,52 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2.0, "c": "three"} +void example(bsoncxx::document::view doc) { + for (bsoncxx::document::element e : doc) { + switch (e.type()) { + case bsoncxx::type::k_int32: + assert(e.key() == "a"); + assert(e.get_int32().value == 1); + break; + case bsoncxx::type::k_double: + assert(e.key() == "b"); + assert(e.get_double().value == 2.0); + break; + case bsoncxx::type::k_string: + assert(e.key() == "c"); + assert(e.get_string().value == "three"); + break; + } + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})")); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp new file mode 100644 index 0000000000..250fb66757 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2} +void example(bsoncxx::document::view doc) { + assert(doc.find("a") == doc.begin()); + + { + auto iter = doc.find("b"); + + assert(iter != doc.end()); + assert(iter->key() == "b"); + assert(iter->get_int32().value == 2); + } + + assert(doc.find("x") == doc.end()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp new file mode 100644 index 0000000000..3c9db0a4ba --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp @@ -0,0 +1,62 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2} +void example(bsoncxx::document::view doc) { + assert(doc.begin() != doc.end()); + + auto iter = doc.begin(); + assert(iter == doc.begin()); + + { + bsoncxx::document::element e = *iter; + + assert(e.key() == "a"); + assert(e.get_int32().value == 1); + } + + ++iter; + + assert(iter->key() == "b"); + assert(iter->get_int32().value == 2); + + { + auto iter_copy = iter++; + + assert(iter_copy != iter); + assert(iter_copy->key() == "b"); + assert(iter_copy->get_int32() == 2); + } + + assert(iter == doc.end()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp new file mode 100644 index 0000000000..9d32daabfa --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2} +void example(bsoncxx::document::view doc) { + assert(doc["a"]); + + { + bsoncxx::document::element e = doc["b"]; + + assert(e.key() == "b"); + assert(e.get_int32().value == 2); + } + + assert(!doc["c"]); // Invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp new file mode 100644 index 0000000000..b84b091da5 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::builder::basic::array builder; + builder.append(std::int32_t{1}); + builder.append(2.0); + builder.append("three"); + bsoncxx::array::value owner = builder.extract(); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_double().value == 2.0); + assert(arr[2].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp new file mode 100644 index 0000000000..e58ab188b2 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::builder::basic::array builder; + builder.append(std::int32_t{1}, 2.0, "three"); + bsoncxx::array::value owner = builder.extract(); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_double().value == 2.0); + assert(arr[2].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp new file mode 100644 index 0000000000..22271873fc --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::b_int32 a{1}; + bsoncxx::types::b_double b{2.0}; + bsoncxx::types::b_string c{"three"}; + + bsoncxx::array::value owner = bsoncxx::builder::basic::make_array(a, b, c); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].type() == bsoncxx::type::k_int32); + assert(arr[1].type() == bsoncxx::type::k_double); + assert(arr[2].type() == bsoncxx::type::k_string); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_double().value == 2.0); + assert(arr[2].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp new file mode 100644 index 0000000000..db9fb201b2 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::types::bson_value::value values[]{ + std::int32_t{1}, + 2.0, + "three", + }; + + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array(values[0], values[1], values[2]); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].type() == bsoncxx::type::k_int32); + assert(arr[1].type() == bsoncxx::type::k_double); + assert(arr[2].type() == bsoncxx::type::k_string); + + assert(arr[0].get_value() == values[0]); + assert(arr[1].get_value() == values[1]); + assert(arr[2].get_value() == values[2]); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp new file mode 100644 index 0000000000..8638211c2d --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_double().value == 2.0); + assert(arr[2].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp new file mode 100644 index 0000000000..d0b8e5674a --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2] +void example(const std::uint8_t* data, std::size_t length) { + using deleter_type = bsoncxx::array::value::deleter_type; + + std::uint8_t* raw = new std::uint8_t[length]; + std::copy_n(data, length, raw); + + deleter_type deleter = [](std::uint8_t* data) { delete[] data; }; + bsoncxx::array::value owner{raw, length, deleter}; + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_int32().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + bsoncxx::document::value owner = bsoncxx::from_json(R"({"v": [1, 2]})"); + bsoncxx::array::view arr = owner.view()["v"].get_array().value; + + example(arr.data(), arr.length()); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp new file mode 100644 index 0000000000..4eca5d7908 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2] +void example(const std::uint8_t* data, std::size_t length) { + bsoncxx::array::view arr{data, length}; + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_int32().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + bsoncxx::document::value owner = bsoncxx::from_json(R"({"v": [1, 2]})"); + bsoncxx::array::view arr = owner.view()["v"].get_array().value; + + example(arr.data(), arr.length()); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp new file mode 100644 index 0000000000..a9b7d966c2 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::builder::basic::array builder; + + builder.append(std::int32_t{1}); + bsoncxx::array::value a_owner = builder.extract(); + + builder.clear(); + + builder.append(std::int64_t{2}); + bsoncxx::array::value b_owner = builder.extract(); + + bsoncxx::array::view a = a_owner.view(); + bsoncxx::array::view b = b_owner.view(); + + assert(a[0].type() == bsoncxx::type::k_int32); + assert(b[0].type() == bsoncxx::type::k_int64); + + assert(a[0].get_int32().value == 1); + assert(b[0].get_int64().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp new file mode 100644 index 0000000000..677ffb8ab3 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array([](bsoncxx::builder::basic::sub_array arr) { + arr.append(std::int32_t{1}, std::int64_t{2}); + }); + bsoncxx::array::view v = owner.view()[0].get_array().value; + + assert(v[0].type() == bsoncxx::type::k_int32); + assert(v[1].type() == bsoncxx::type::k_int64); + + assert(v[0].get_int32().value == 1); + assert(v[1].get_int64().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp new file mode 100644 index 0000000000..0cbe7119bd --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -0,0 +1,42 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::array::value owner = bsoncxx::builder::basic::make_array( + [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); }); + bsoncxx::document::view v = owner.view()[0].get_document().value; + + assert(v["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp new file mode 100644 index 0000000000..4643ea3219 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp @@ -0,0 +1,47 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); + bsoncxx::array::view arr = owner.view(); + + assert(arr[0].type() == bsoncxx::type::k_int32); + assert(arr[1].type() == bsoncxx::type::k_double); + assert(arr[2].type() == bsoncxx::type::k_string); + + assert(arr[0].get_int32().value == 1); + assert(arr[1].get_double().value == 2.0); + assert(arr[2].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp new file mode 100644 index 0000000000..c24fbd9b38 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + [1, 2.0, "three"] + )"); + bsoncxx::document::view doc = owner.view(); + + assert(doc["0"].get_int32().value == 1); + assert(doc["1"].get_double().value == 2.0); + assert(doc["2"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp new file mode 100644 index 0000000000..d4a05882c6 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp @@ -0,0 +1,47 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + [ + {"$numberInt": "1"}, + {"$numberLong": "2"}, + {"$numberDouble": "3"} + ] + )"); + bsoncxx::document::view doc = owner.view(); + + assert(doc["0"].get_int32().value == 1); + assert(doc["1"].get_int64().value == 2); + assert(doc["2"].get_double().value == 3.0); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp new file mode 100644 index 0000000000..7470aee536 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + [ + [1, 2] + ] + )"); + bsoncxx::array::view sub = owner.view()["0"].get_array().value; + + assert(sub[0].get_int32().value == 1); + assert(sub[1].get_int32().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp new file mode 100644 index 0000000000..dde4bda28a --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + [ + {"key": "value"} + ] + )"); + bsoncxx::document::view v = owner.view()["0"].get_document().value; + + assert(v["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp new file mode 100644 index 0000000000..20e1482959 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp @@ -0,0 +1,39 @@ +// Copyright 2009-present 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 + +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using namespace bsoncxx; + + bsoncxx::document::value a = R"([1, 2])"_bson; + bsoncxx::document::value b = bsoncxx::from_json(R"([1, 2])"); + + assert(a == b); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp new file mode 100644 index 0000000000..161abc2102 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + builder.append(kvp("a", std::int32_t{1})); + builder.append(kvp("b", 2.0)); + builder.append(kvp("c", "three")); + bsoncxx::document::value owner = builder.extract(); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_double().value == 2.0); + assert(doc["c"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp new file mode 100644 index 0000000000..7f5c7d72e7 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp @@ -0,0 +1,47 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + builder.append(kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); + bsoncxx::document::value owner = builder.extract(); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_double().value == 2.0); + assert(doc["c"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp new file mode 100644 index 0000000000..86f96eaf29 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::types::b_int32 a{1}; + bsoncxx::types::b_double b{2.0}; + bsoncxx::types::b_string c{"three"}; + + bsoncxx::document::value owner = + bsoncxx::builder::basic::make_document(kvp("a", a), kvp("b", b), kvp("c", c)); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].type() == bsoncxx::type::k_int32); + assert(doc["b"].type() == bsoncxx::type::k_double); + assert(doc["c"].type() == bsoncxx::type::k_string); + + assert(doc["a"].get_int32() == a); + assert(doc["b"].get_double() == b); + assert(doc["c"].get_string() == c); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp new file mode 100644 index 0000000000..87254af5eb --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::types::bson_value::value values[]{ + std::int32_t{1}, + 2.0, + "three", + }; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("a", values[0]), kvp("b", values[1]), kvp("c", values[2])); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].type() == bsoncxx::type::k_int32); + assert(doc["b"].type() == bsoncxx::type::k_double); + assert(doc["c"].type() == bsoncxx::type::k_string); + + assert(doc["a"].get_value() == values[0]); + assert(doc["b"].get_value() == values[1]); + assert(doc["c"].get_value() == values[2]); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp new file mode 100644 index 0000000000..124ad3ccd1 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_double().value == 2.0); + assert(doc["c"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp new file mode 100644 index 0000000000..2fb68a0be8 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp @@ -0,0 +1,52 @@ +// Copyright 2009-present 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 +#include +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"key": "value"} +void example(const std::uint8_t* data, std::size_t length) { + using deleter_type = bsoncxx::document::value::deleter_type; + + std::uint8_t* raw = new std::uint8_t[length]; + std::copy_n(data, length, raw); + + deleter_type deleter = [](std::uint8_t* data) { delete[] data; }; + bsoncxx::document::value owner{raw, length, deleter}; + bsoncxx::document::view doc = owner.view(); + + assert(doc["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + bsoncxx::document::value owner = bsoncxx::from_json(R"({"key": "value"})"); + bsoncxx::document::view doc = owner.view(); + + example(doc.data(), doc.length()); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp new file mode 100644 index 0000000000..caea6eed52 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"key": "value"} +void example(const std::uint8_t* data, std::size_t length) { + bsoncxx::document::view doc{data, length}; + + assert(doc["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + bsoncxx::document::value owner = bsoncxx::from_json(R"({"key": "value"})"); + bsoncxx::document::view doc = owner.view(); + + example(doc.data(), doc.length()); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp new file mode 100644 index 0000000000..1056d27163 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + + builder.append(kvp("v", std::int32_t{1})); + bsoncxx::document::value a_owner = builder.extract(); + + builder.clear(); + + builder.append(kvp("v", std::int64_t{2})); + bsoncxx::document::value b_owner = builder.extract(); + + bsoncxx::document::view a = a_owner.view(); + bsoncxx::document::view b = b_owner.view(); + + assert(a["v"].type() == bsoncxx::type::k_int32); + assert(b["v"].type() == bsoncxx::type::k_int64); + + assert(a["v"].get_int32().value == 1); + assert(b["v"].get_int64().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp new file mode 100644 index 0000000000..e9bc6cdcc2 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp @@ -0,0 +1,51 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::document::value owner = + bsoncxx::builder::basic::make_document(kvp("v", [](bsoncxx::builder::basic::sub_array arr) { + arr.append(std::int32_t{1}, std::int64_t{2}); + })); + bsoncxx::array::view v = owner.view()["v"].get_array().value; + + assert(v[0].type() == bsoncxx::type::k_int32); + assert(v[1].type() == bsoncxx::type::k_int64); + + assert(v[0].get_int32().value == 1); + assert(v[1].get_int64().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp new file mode 100644 index 0000000000..b9f2e5fd9c --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document(kvp( + "v", [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); })); + bsoncxx::document::view v = owner.view()["v"].get_document().value; + + assert(v["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp new file mode 100644 index 0000000000..fe236f70c9 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].type() == bsoncxx::type::k_int32); + assert(doc["b"].type() == bsoncxx::type::k_double); + assert(doc["c"].type() == bsoncxx::type::k_string); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_double().value == 2.0); + assert(doc["c"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp new file mode 100644 index 0000000000..a6897486fa --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp @@ -0,0 +1,47 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + { + "a": 1, + "b": 2.0, + "c": "three" + } + )"); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_double().value == 2.0); + assert(doc["c"].get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp new file mode 100644 index 0000000000..39c9a7c11d --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp @@ -0,0 +1,47 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + { + "a": {"$numberInt": "1"}, + "b": {"$numberLong": "2"}, + "c": {"$numberDouble": "3"} + } + )"); + bsoncxx::document::view doc = owner.view(); + + assert(doc["a"].get_int32().value == 1); + assert(doc["b"].get_int64().value == 2); + assert(doc["c"].get_double().value == 3.0); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp new file mode 100644 index 0000000000..313e12b1c7 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value owner = bsoncxx::from_json(R"( + { + "v": [1, 2] + } + )"); + bsoncxx::array::view sub = owner.view()["v"].get_array().value; + + assert(sub[0].get_int32().value == 1); + assert(sub[1].get_int32().value == 2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp new file mode 100644 index 0000000000..eb04204b6f --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value doc = bsoncxx::from_json(R"( + { + "v": {"key": "value"} + } + )"); + bsoncxx::document::view v = doc["v"].get_document().value; + + assert(v["key"].get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp new file mode 100644 index 0000000000..cc7d0a58c9 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp @@ -0,0 +1,39 @@ +// Copyright 2009-present 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 + +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using namespace bsoncxx; + + bsoncxx::document::value a = R"({"key": "value"})"_bson; + bsoncxx::document::value b = bsoncxx::from_json(R"({"key": "value"})"); + + assert(a == b); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp new file mode 100644 index 0000000000..e0089cde79 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp @@ -0,0 +1,72 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2.0, "three"] +void example(bsoncxx::array::element e) { + switch (e.type()) { + case bsoncxx::type::k_int32: { + assert(e.key() == "0"); + + bsoncxx::types::b_int32 v = e.get_int32(); + + assert(v.type_id == bsoncxx::type::k_int32); + assert(v.value == 1); + + break; + } + case bsoncxx::type::k_double: { + assert(e.key() == "1"); + + bsoncxx::types::b_double v = e.get_double(); + + assert(v.type_id == bsoncxx::type::k_double); + assert(v.value == 2.0); + + break; + } + case bsoncxx::type::k_string: { + assert(e.key() == "2"); + + bsoncxx::types::b_string v = e.get_string(); + + assert(v.type_id == bsoncxx::type::k_string); + assert(v.value == "three"); + + break; + } + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); + const auto arr = doc["v"].get_array().value; + + example(arr[0]); + example(arr[1]); + example(arr[2]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp new file mode 100644 index 0000000000..0bed3cb849 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2.0, "three"] +void example(bsoncxx::array::element e) { + if (e.type() == bsoncxx::type::k_int32) { + assert(e.key() == "0"); + + bsoncxx::types::b_int32 v = e.get_int32(); + + assert(v.type_id == bsoncxx::type::k_int32); + assert(v.value == 1); + } else { + assert(e.key() != "0"); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); + const auto arr = doc["v"].get_array().value; + + example(arr[0]); + example(arr[1]); + example(arr[2]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp new file mode 100644 index 0000000000..a4343c047c --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp @@ -0,0 +1,56 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}} +void example(bsoncxx::document::element e) { + bsoncxx::types::b_int32 a{1}; + bsoncxx::types::b_int64 b{2}; + + if (e.get_value() == a) { + assert(e.key() == "a"); + } else if (e.get_value() == b) { + assert(e.key() == "b"); + } + + bsoncxx::types::bson_value::view va{a}; + bsoncxx::types::bson_value::view vb{b}; + + if (e == va) { + assert(e.key() == "a"); + } else if (e == vb) { + assert(e.key() == "b"); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}})"); + + example(doc["a"]); + example(doc["b"]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp new file mode 100644 index 0000000000..ea4ff945e8 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}} +void example(bsoncxx::document::element e) { + std::int32_t a{1}; + std::int64_t b{2}; + + if (e.type() == bsoncxx::type::k_int32) { + assert(e.key() == "a"); + assert(e.get_int32().value == a); + } else if (e.type() == bsoncxx::type::k_int64) { + assert(e.key() == "b"); + assert(e.get_int64().value == b); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}})"); + + example(doc["a"]); + example(doc["b"]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp new file mode 100644 index 0000000000..e2171129e7 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp @@ -0,0 +1,71 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2.0, "c": "three"} +void example(bsoncxx::document::element e) { + switch (e.type()) { + case bsoncxx::type::k_int32: { + assert(e.key() == "a"); + + bsoncxx::types::b_int32 v = e.get_int32(); + + assert(v.type_id == bsoncxx::type::k_int32); + assert(v.value == 1); + + break; + } + case bsoncxx::type::k_double: { + assert(e.key() == "b"); + + bsoncxx::types::b_double v = e.get_double(); + + assert(v.type_id == bsoncxx::type::k_double); + assert(v.value == 2.0); + + break; + } + case bsoncxx::type::k_string: { + assert(e.key() == "c"); + + bsoncxx::types::b_string v = e.get_string(); + + assert(v.type_id == bsoncxx::type::k_string); + assert(v.value == "three"); + + break; + } + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); + + example(doc["a"]); + example(doc["b"]); + example(doc["c"]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp new file mode 100644 index 0000000000..a078b86b5d --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2.0, "c": "three"} +void example(bsoncxx::document::element e) { + if (e.type() == bsoncxx::type::k_int32) { + assert(e.key() == "a"); + + bsoncxx::types::b_int32 v = e.get_int32(); + + assert(v.type_id == bsoncxx::type::k_int32); + assert(v.value == 1); + } else { + assert(e.key() != "a"); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); + + example(doc["a"]); + example(doc["b"]); + example(doc["c"]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp new file mode 100644 index 0000000000..507c33f04a --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp @@ -0,0 +1,105 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::ExtendedJsonMode; + + std::uint8_t data[]{"three"}; // Base64: dGhyZWU= + std::uint32_t data_len{5u}; // Exclude null terminator. + bsoncxx::types::b_binary binary{bsoncxx::binary_sub_type::k_binary, data_len, data}; + + bsoncxx::array::value owner = bsoncxx::builder::basic::make_array( + std::int32_t{1}, // "$numberInt": "1" + std::int64_t{2}, // "$numberLong": "2" + binary // "$binary": { "$base64": "dGhyZWU=", "subType": 00 } + ); + bsoncxx::array::view arr = owner.view(); + + { + // Canonical Extended JSON: + // [ + // { "$numberInt": "1" }, + // { "$numberLong": "2" }, + // { + // "$binary": { + // "base64": "dGhyZWU=", + // "subType": "00" + // } + // } + // ] + std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_canonical); + assert( + json == + R"([ { "$numberInt" : "1" }, { "$numberLong" : "2" }, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); + } + + { + // Relaxed Extended JSON + // [ + // 1, + // 2, + // { + // "$binary": { + // "base64": "dGhyZWU=", + // "subType": "00" + // } + // } + // ] + std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_relaxed); + assert(json == R"([ 1, 2, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); + } + + { + // Legacy Extended JSON + // [ + // 1, + // 2, + // { + // "$binary": "dGhyZWU=", + // "$type": "00" + // } + // ] + std::string json = bsoncxx::to_json(arr); + assert(json == R"([ 1, 2, { "$binary" : "dGhyZWU=", "$type" : "00" } ])"); + } + + { + std::string a = bsoncxx::to_json(arr); + std::string b = bsoncxx::to_json(arr, ExtendedJsonMode::k_legacy); + + assert(a == b); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp new file mode 100644 index 0000000000..94af4ccb2a --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp @@ -0,0 +1,109 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::ExtendedJsonMode; + using bsoncxx::builder::basic::kvp; + + std::uint8_t data[]{"three"}; // Base64: dGhyZWU= + auto data_len = static_cast(sizeof(data) - 1u); // Exclude null terminator. + bsoncxx::types::b_binary binary{bsoncxx::binary_sub_type::k_binary, data_len, data}; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("a", std::int32_t{1}), // "$numberInt": "1" + kvp("b", std::int64_t{2}), // "$numberLong": "2" + kvp("c", binary) // "$binary": { "$base64": "dGhyZWU=", "subType": 00 } + ); + bsoncxx::document::view doc = owner.view(); + + { + // Canonical Extended JSON: + // { + // "a": { "$numberInt": "1" }, + // "b": { "$numberLong": "2" }, + // "c": { + // "$binary": { + // "base64": "dGhyZWU=", + // "subType": "00" + // } + // } + // } + std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_canonical); + assert( + json == + R"({ "a" : { "$numberInt" : "1" }, "b" : { "$numberLong" : "2" }, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); + } + + { + // Relaxed Extended JSON + // { + // "a": 1, + // "b": 2, + // "c": { + // "$binary": { + // "base64": "dGhyZWU=", + // "subType": "00" + // } + // } + // } + std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_relaxed); + assert( + json == + R"({ "a" : 1, "b" : 2, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); + } + + { + // Legacy Extended JSON + // { + // "a": 1, + // "b": 2, + // "c": { + // "$binary": "dGhyZWU=", + // "$type": "00" + // } + // } + std::string json = bsoncxx::to_json(doc); + assert(json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : "dGhyZWU=", "$type" : "00" } })"); + } + + { + std::string a = bsoncxx::to_json(doc); + std::string b = bsoncxx::to_json(doc, ExtendedJsonMode::k_legacy); + + assert(a == b); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp new file mode 100644 index 0000000000..08ecab8a25 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::bson_value::value v = nullptr; + + assert(v.view().type() == bsoncxx::type::k_null); + assert(v.view().get_null() == bsoncxx::types::b_null{}); + + v = bsoncxx::from_json(R"({"v": ["value"]})") // Temporary object. + ["v"] + .get_owning_value(); // Copy: no dangling. + + assert(v.view().type() == bsoncxx::type::k_array); + + v = v.view().get_array().value[0].get_string(); // Copy: no dangling. + + assert(v.view().type() == bsoncxx::type::k_string); + assert(v.view().get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp new file mode 100644 index 0000000000..a1191ee687 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp @@ -0,0 +1,58 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2.0, "three"] +void example(bsoncxx::array::element e) { + bsoncxx::types::bson_value::view v = e.get_value(); + assert(v.type() == e.type()); + + switch (v.type()) { + case bsoncxx::type::k_int32: + assert(e.key() == "0"); + assert(v.get_int32() == e.get_int32()); + break; + case bsoncxx::type::k_double: + assert(e.key() == "1"); + assert(v.get_double() == e.get_double()); + break; + case bsoncxx::type::k_string: + assert(e.key() == "2"); + assert(v.get_string() == e.get_string()); + break; + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); + const auto arr = doc["v"].get_array().value; + + example(arr[0]); + example(arr[1]); + example(arr[2]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp new file mode 100644 index 0000000000..df34192f2d --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 + +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::bson_value::value v = nullptr; + + assert(v.view().type() == bsoncxx::type::k_null); + assert(v.view().get_null() == bsoncxx::types::b_null{}); + + v = bsoncxx::types::b_int32{1}; + assert(v.view().type() == bsoncxx::type::k_int32); + assert(v.view().get_int32().value == 1); + + v = bsoncxx::types::b_double{2.0}; + assert(v.view().type() == bsoncxx::type::k_double); + assert(v.view().get_double().value == 2.0); + + v = bsoncxx::types::b_string{"three"}; + assert(v.view().type() == bsoncxx::type::k_string); + assert(v.view().get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp new file mode 100644 index 0000000000..2c7045a4ba --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 + +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using view_type = bsoncxx::types::bson_value::view; + + bsoncxx::types::b_int32 v0{1}; + bsoncxx::types::b_double v1{2.0}; + bsoncxx::types::b_string v2{"three"}; + + view_type v; + assert(v.type() == bsoncxx::type::k_null); + assert(v.get_null() == bsoncxx::types::b_null{}); + + v = view_type(v0); + assert(v.type() == v0.type_id); + assert(v.get_int32() == v0); + + v = view_type(v1); + assert(v.type() == v1.type_id); + assert(v.get_double() == v1); + + v = view_type(v2); + assert(v.type() == v2.type_id); + assert(v.get_string() == v2); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp new file mode 100644 index 0000000000..704545d0f0 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::bson_value::value v = nullptr; + + assert(v.view().type() == bsoncxx::type::k_null); + assert(v.view().get_null() == bsoncxx::types::b_null{}); + + v = bsoncxx::from_json(R"({"v": {"key": "value"}})") // Temporary object. + ["v"] + .get_owning_value(); // Copy: no dangling. + + assert(v.view().type() == bsoncxx::type::k_document); + + v = v.view().get_document().value["key"].get_string(); // Copy: no dangling. + + assert(v.view().type() == bsoncxx::type::k_string); + assert(v.view().get_string().value == "value"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp new file mode 100644 index 0000000000..45ffdbc462 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2.0, "c": "three"} +void example(bsoncxx::document::element e) { + bsoncxx::types::bson_value::view v = e.get_value(); + assert(v.type() == e.type()); + + switch (v.type()) { + case bsoncxx::type::k_int32: + assert(e.key() == "a"); + assert(v.get_int32() == e.get_int32()); + break; + case bsoncxx::type::k_double: + assert(e.key() == "b"); + assert(v.get_double() == e.get_double()); + break; + case bsoncxx::type::k_string: + assert(e.key() == "c"); + assert(v.get_string() == e.get_string()); + break; + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); + + example(doc["a"]); + example(doc["b"]); + example(doc["c"]); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp new file mode 100644 index 0000000000..cdd0ad9f21 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp @@ -0,0 +1,62 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using value_type = bsoncxx::types::bson_value::value; + using view_type = bsoncxx::types::bson_value::view; + + { + value_type owner = bsoncxx::types::bson_value::make_value(1); + view_type v = owner.view(); + + assert(v.type() == bsoncxx::type::k_int32); + assert(v.get_int32().value == 1); + } + + { + value_type owner = bsoncxx::types::bson_value::make_value(bsoncxx::types::b_int64{2}); + view_type v = owner.view(); + + assert(v.type() == bsoncxx::type::k_int64); + assert(v.get_int64().value == 2); + } + + { + value_type owner = bsoncxx::types::bson_value::make_value(std::string("three")); + view_type v = owner.view(); + + assert(v.type() == bsoncxx::type::k_string); + assert(v.get_string().value == "three"); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp new file mode 100644 index 0000000000..ea93a088ae --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 +#include + +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::bson_value::value v = nullptr; + + assert(v.view().type() == bsoncxx::type::k_null); + assert(v.view().get_null() == bsoncxx::types::b_null{}); + + v = std::int32_t{1}; + + assert(v.view().type() == bsoncxx::type::k_int32); + assert(v.view().get_int32().value == 1); + + v = 2.0; + + assert(v.view().type() == bsoncxx::type::k_double); + assert(v.view().get_double().value == 2.0); + + v = std::string("three"); + + assert(v.view().type() == bsoncxx::type::k_string); + assert(v.view().get_string().value == "three"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index b9f6504ace..cb2ccfb9d5 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -95,15 +95,22 @@ /// @li @subpage topic-bsoncxx-examples-bson-documents /// -/// -/// @page topic-mongocxx-examples Using the mongocxx Library -/// @brief Examples of how to use the bsoncxx library. -/// - /// /// @page topic-bsoncxx-examples-bson-documents BSON Documents /// @brief How to create and use BSON documents. /// @tableofcontents +/// @include{doc} api/bsoncxx/examples/bson_documents/create_doc.md +/// @include{doc} api/bsoncxx/examples/bson_documents/create_array.md +/// @include{doc} api/bsoncxx/examples/bson_documents/access_doc.md +/// @include{doc} api/bsoncxx/examples/bson_documents/access_array.md +/// @include{doc} api/bsoncxx/examples/bson_documents/elements.md +/// @include{doc} api/bsoncxx/examples/bson_documents/values.md +/// @include{doc} api/bsoncxx/examples/bson_documents/json.md +/// + +/// +/// @page topic-mongocxx-examples Using the mongocxx Library +/// @brief Examples of how to use the bsoncxx library. /// /// From 6b2c4f55849c32d51607f13e4fea61d0f01496ca Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:10 -0500 Subject: [PATCH 05/26] Add bsoncxx error handling examples --- docs/api/bsoncxx/examples/bson_errors.md | 115 ++++++++++++++++++ .../bson_errors/access_arr_iter_end.cpp | 53 ++++++++ .../bson_errors/access_arr_iter_invalid.cpp | 49 ++++++++ .../bson_errors/access_arr_key_missing.cpp | 42 +++++++ .../bson_errors/access_doc_iter_end.cpp | 52 ++++++++ .../bson_errors/access_doc_iter_invalid.cpp | 50 ++++++++ .../bson_errors/access_doc_key_missing.cpp | 42 +++++++ .../examples/bson_errors/big_string.hh | 41 +++++++ .../bson_errors/create_arr_append.cpp | 56 +++++++++ .../create_arr_append_sub_array.cpp | 74 +++++++++++ .../create_arr_append_sub_document.cpp | 77 ++++++++++++ .../create_bson_value_invalid_element.cpp | 43 +++++++ .../create_bson_value_invalid_type.cpp | 42 +++++++ .../bson_errors/create_doc_append.cpp | 59 +++++++++ .../create_doc_append_sub_array.cpp | 79 ++++++++++++ .../create_doc_append_sub_document.cpp | 78 ++++++++++++ .../examples/bson_errors/create_json.cpp | 41 +++++++ .../query_bson_value_invalid_type.cpp | 54 ++++++++ .../bson_errors/query_element_arr_invalid.cpp | 63 ++++++++++ .../query_element_arr_invalid_type.cpp | 56 +++++++++ .../bson_errors/query_element_doc_invalid.cpp | 65 ++++++++++ .../query_element_doc_invalid_type.cpp | 58 +++++++++ .../bson_errors/to_json_invalid_arr.cpp | 54 ++++++++ .../bson_errors/to_json_invalid_doc.cpp | 54 ++++++++ src/bsoncxx/include/bsoncxx/doc.hpp | 8 ++ 25 files changed, 1405 insertions(+) create mode 100644 docs/api/bsoncxx/examples/bson_errors.md create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/big_string.hh create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/create_json.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp create mode 100644 examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp diff --git a/docs/api/bsoncxx/examples/bson_errors.md b/docs/api/bsoncxx/examples/bson_errors.md new file mode 100644 index 0000000000..308a9a0013 --- /dev/null +++ b/docs/api/bsoncxx/examples/bson_errors.md @@ -0,0 +1,115 @@ +# Create a BSON Document + +## From an Invalid JSON String + +@snippet api/bsoncxx/examples/bson_errors/create_json.cpp Example + +## Using the Basic Document Builder + +### Basic Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_doc_append.cpp Example + +### Sub-Document Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp Example + +### Sub-Array Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp Example + +## Using the Basic Array Builder + +### Basic Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_arr_append.cpp Example + +### Sub-Document Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp Example + +### Sub-Array Append Failure + +@snippet api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp Example + +# Access a Document Element + +## By Iteration + +### End Iterators + +@snippet api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp Example + +### Invalid BSON Documents + +@snippet api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp Example + +## By Key + +### Missing Element + +@snippet api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp Example + +# Access an Array Element + +## By Iteration + +### End Iterators + +@snippet api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp Example + +### Invalid BSON Arrays + +@snippet api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp Example + +## By Key + +### Missing Element + +@snippet api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp Example + +# Query an Element + +## In a Document + +### Invalid Element + +@snippet api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp Example + +### Invalid Type + +@snippet api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp Example + +## In an Array + +### Invalid Element + +@snippet api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp Example + +### Invalid Type + +@snippet api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp Example + +# Use a BSON Value + +## Query an Invalid Type + +@snippet api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp Example + +## Create an Invalid Value + +@snippet api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp Example + +## From an Invalid Element + +@snippet api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp Example + +# Convert to a JSON String + +## From an Invalid BSON Document + +@snippet api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp Example + +## From an Invalid BSON Array + +@snippet api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp Example diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp new file mode 100644 index 0000000000..8465791c33 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2, 3] +void example(bsoncxx::array::view arr) { + assert(arr.begin() != arr.end()); + + auto iter = arr.begin(); // 1 + + ++iter; // 2 + ++iter; // 3 + ++iter; // End iterator. + + assert(iter == arr.end()); + + ++iter; // DO NOT DO THIS + + assert(iter == arr.end()); // Incrementing an end iterator results in an end iterator. + + bsoncxx::array::element e = *iter; // DO NOT DO THIS + + assert(!e); // An end iterator returns an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2, 3]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp new file mode 100644 index 0000000000..f45b835f6d --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // An invalid BSON array. + + bsoncxx::array::value owner{data, sizeof(data), deleter}; + bsoncxx::array::view arr = owner.view(); + + auto iter = arr.begin(); + + assert(iter == arr.end()); // An invalid BSON document returns an end iterator. + + bsoncxx::array::element e = *iter; // DO NOT DO THIS + + assert(!e); // An end iterator returns an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp new file mode 100644 index 0000000000..98508ad565 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp @@ -0,0 +1,42 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1, 2, 3] +void example(bsoncxx::array::view arr) { + assert(std::distance(arr.begin(), arr.end()) == 3); + + bsoncxx::array::element e = arr[3]; + + assert(!e); // A missing element is represented as an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1, 2, 3]})")["v"].get_array().value); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp new file mode 100644 index 0000000000..53ba5850fe --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp @@ -0,0 +1,52 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2} +void example(bsoncxx::document::view doc) { + assert(doc.begin() != doc.end()); + + auto iter = doc.begin(); // "a": 1 + + ++iter; // "b": 2 + ++iter; // End iterator. + + assert(iter == doc.end()); + + ++iter; // DO NOT DO THIS + + assert(iter == doc.end()); // Incrementing an end iterator results in an end iterator. + + bsoncxx::document::element e = *iter; // DO NOT DO THIS + + assert(!e); // An end iterator returns an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp new file mode 100644 index 0000000000..ffd3a0a735 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // An invalid BSON document. + + bsoncxx::document::value owner{data, sizeof(data), deleter}; + bsoncxx::document::view doc = owner.view(); + + auto iter = doc.begin(); + + assert(iter == doc.end()); // An invalid BSON document returns an end iterator. + + bsoncxx::document::element e = *iter; // DO NOT DO THIS + + assert(!e); // An end iterator returns an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp new file mode 100644 index 0000000000..d885a8e3d0 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp @@ -0,0 +1,42 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"a": 1, "b": 2} +void example(bsoncxx::document::view doc) { + assert(doc["a"]); + assert(doc["b"]); + + bsoncxx::document::element e = doc["c"]; + + assert(!e); // A missing element is represented as an invalid element. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/big_string.hh b/examples/api/bsoncxx/examples/bson_errors/big_string.hh new file mode 100644 index 0000000000..f97e4b6060 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/big_string.hh @@ -0,0 +1,41 @@ +// Copyright 2009-present 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. + +#pragma once + +#include +#include +#include +#include + +#include + +namespace examples { + +// Used to trigger builder append failure. +struct big_string { + // BSON_SIZE_MAX == 0x7FFFFFFF + std::size_t length{static_cast(std::numeric_limits::max())}; + + // Allocate an UNINITIALIZED blob of data that will not be accessed due to length checks. + // Leaving memory unitialized (rather than zero-init) should hopefully avoid slow and expensive + // physical memory allocation at runtime. + std::unique_ptr data{new char[length]}; + + bsoncxx::stdx::string_view view() const { + return bsoncxx::stdx::string_view(data.get(), length); + } +}; + +} // namespace examples diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp new file mode 100644 index 0000000000..620feb3d3e --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp @@ -0,0 +1,56 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + bsoncxx::builder::basic::array builder; + builder.append("element"); + bsoncxx::array::value original{builder.view()}; + + try { + builder.append(big_string); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp new file mode 100644 index 0000000000..d10c24aaef --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp @@ -0,0 +1,74 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + bsoncxx::builder::basic::array builder; + builder.append("element"); + bsoncxx::array::value original{builder.view()}; // Copy of current state. + + try { + builder.append([&](bsoncxx::builder::basic::sub_array arr) { + arr.append(big_string); // Throws. + }); + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + // Builder is in an erroneous state. + try { + builder.view(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + } + + // Reset builder to a usable state. + builder.clear(); + assert(builder.view().empty()); + + // Restore the original state prior to the append failure. + builder.append(bsoncxx::builder::concatenate_array{original.view()}); + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp new file mode 100644 index 0000000000..0b385fac88 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp @@ -0,0 +1,77 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::array builder; + builder.append("element"); + bsoncxx::array::value original{builder.view()}; // Copy of current state. + + try { + builder.append([&](bsoncxx::builder::basic::sub_document doc) { + doc.append(kvp("too big", big_string)); // Throws. + }); + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + // Builder is in an erroneous state. + try { + builder.view(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + } + + // Reset builder to a usable state. + builder.clear(); + assert(builder.view().empty()); + + // Restore the original state prior to the append failure. + builder.append(bsoncxx::builder::concatenate_array{original.view()}); + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp new file mode 100644 index 0000000000..2378213ad9 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp @@ -0,0 +1,43 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + try { + bsoncxx::document::element e; + bsoncxx::types::bson_value::view v = e.get_value(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unset_element); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp new file mode 100644 index 0000000000..4700ed6dd8 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp @@ -0,0 +1,42 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + try { + bsoncxx::types::bson_value::value value{bsoncxx::type::k_null}; // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp new file mode 100644 index 0000000000..8f8c33d21e --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + builder.append(kvp("key", "value")); + bsoncxx::document::value original{builder.view()}; + + try { + builder.append(kvp("too big", big_string)); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp new file mode 100644 index 0000000000..ec4b949c7b --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp @@ -0,0 +1,79 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + builder.append(kvp("key", "value")); + bsoncxx::document::value original{builder.view()}; // Copy of current state. + + try { + builder.append(kvp("bad", [&](bsoncxx::builder::basic::sub_array arr) { + arr.append(big_string); // Throws. + })); + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + // Builder is in an erroneous state. + try { + builder.view(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + } + + // Reset builder to a usable state. + builder.clear(); + assert(builder.view().empty()); + + // Restore the original state prior to the append failure. + builder.append(bsoncxx::builder::concatenate_doc{original.view()}); + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp new file mode 100644 index 0000000000..82898813f2 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp @@ -0,0 +1,78 @@ +// Copyright 2009-present 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 "big_string.hh" + +// + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example(bsoncxx::stdx::string_view big_string) { + using bsoncxx::builder::basic::kvp; + + bsoncxx::builder::basic::document builder; + builder.append(kvp("key", "value")); + bsoncxx::document::value original{builder.view()}; // Copy of current state. + + try { + builder.append(kvp("bad", [&](bsoncxx::builder::basic::sub_document doc) { + doc.append(kvp("too big", big_string)); // Throws. + })); + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + } + + // Builder is in an erroneous state. + try { + builder.view(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + } + + // Reset builder to a usable state. + builder.clear(); + assert(builder.view().empty()); + + // Restore the original state prior to the append failure. + builder.append(bsoncxx::builder::concatenate_doc{original.view()}); + + assert(builder.view() == original.view()); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(examples::big_string().view()); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp new file mode 100644 index 0000000000..8a0658ea92 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp @@ -0,0 +1,41 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + try { + const auto doc = bsoncxx::from_json(R"(invalid json)"); + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_json_parse_failure); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp new file mode 100644 index 0000000000..c8b544290e --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::types::bson_value::view v; + assert(v.type() == bsoncxx::type::k_null); + assert(v.get_null() == bsoncxx::types::b_null{}); + + try { + bsoncxx::types::b_int32 i = v.get_int32(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32); + } + + try { + bsoncxx::types::b_int64 i = v.get_int64(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp new file mode 100644 index 0000000000..1aa2645c56 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp @@ -0,0 +1,63 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // An invalid BSON array. + + bsoncxx::array::value owner{data, sizeof(data), deleter}; + bsoncxx::array::view arr = owner.view(); + + bsoncxx::array::element e = arr[0]; + + assert(!e); // An invalid BSON array returns an invalid element. + + try { + bsoncxx::stdx::string_view key = e.key(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unset_element); + } + + try { + bsoncxx::type type = e.type(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unset_element); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp new file mode 100644 index 0000000000..b5292422b0 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp @@ -0,0 +1,56 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// [1] +void example(bsoncxx::array::element e) { + assert(e.key() == "0"); + assert(e.type() == bsoncxx::type::k_int32); + assert(e.get_int32().value == 1); + + try { + bsoncxx::types::b_double d = e.get_double(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + } + + try { + bsoncxx::types::b_string str = e.get_string(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(bsoncxx::from_json(R"({"v": [1]})")["v"].get_array().value[0]); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp new file mode 100644 index 0000000000..eb13cfe9cf --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp @@ -0,0 +1,65 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // An invalid BSON document. + + bsoncxx::document::value owner{data, sizeof(data), deleter}; + bsoncxx::document::view doc = owner.view(); + + bsoncxx::document::element e = doc["x"]; + + assert(!e); // An invalid BSON document returns an invalid element. + + try { + bsoncxx::stdx::string_view key = e.key(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unset_element); + } + + try { + bsoncxx::type type = e.type(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_unset_element); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp new file mode 100644 index 0000000000..1edcd14dbc --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp @@ -0,0 +1,58 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +// {"x": 1} +void example(bsoncxx::document::element e) { + assert(e.key() == "x"); + assert(e.type() == bsoncxx::type::k_int32); + assert(e.get_int32().value == 1); + + try { + bsoncxx::types::b_double d = e.get_double(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + } + + try { + bsoncxx::types::b_string str = e.get_string(); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + const auto doc = bsoncxx::from_json(R"({"x": 1})"); + + example(doc["x"]); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp new file mode 100644 index 0000000000..01da648afd --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::ExtendedJsonMode; + + bsoncxx::array::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // Invalid BSON array. + + bsoncxx::array::value owner{data, sizeof(data), deleter}; + bsoncxx::array::view doc = owner.view(); + + try { + std::string json = bsoncxx::to_json(doc); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp new file mode 100644 index 0000000000..4157e1189f --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::ExtendedJsonMode; + + bsoncxx::document::value::deleter_type deleter = [](std::uint8_t*) {}; + std::uint8_t data[] = {0u}; // Invalid BSON document. + + bsoncxx::document::value owner{data, sizeof(data), deleter}; + bsoncxx::document::view doc = owner.view(); + + try { + std::string json = bsoncxx::to_json(doc); // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index cb2ccfb9d5..5993d4fb9f 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -93,6 +93,7 @@ /// @page topic-bsoncxx-examples Using the bsoncxx Library /// @brief Examples of how to use the bsoncxx library. /// @li @subpage topic-bsoncxx-examples-bson-documents +/// @li @subpage topic-bsoncxx-examples-bson-errors /// /// @@ -108,6 +109,13 @@ /// @include{doc} api/bsoncxx/examples/bson_documents/json.md /// +/// +/// @page topic-bsoncxx-examples-bson-errors BSON Error Handling +/// @brief How to handle BSON document failures, errors, and exceptions. +/// @tableofcontents +/// @include{doc} api/bsoncxx/examples/bson_errors.md +/// + /// /// @page topic-mongocxx-examples Using the mongocxx Library /// @brief Examples of how to use the bsoncxx library. From 45eb6415b05d66f4a69fcfb115a9730a3738bf99 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:10 -0500 Subject: [PATCH 06/26] Add decimal128 examples --- docs/api/bsoncxx/examples/decimal128.md | 11 ++ .../examples/decimal128/basic_usage.cpp | 105 ++++++++++++++++++ .../bsoncxx/examples/decimal128/errors.cpp | 41 +++++++ .../examples/decimal128/from_bytes.cpp | 86 ++++++++++++++ src/bsoncxx/include/bsoncxx/doc.hpp | 8 ++ 5 files changed, 251 insertions(+) create mode 100644 docs/api/bsoncxx/examples/decimal128.md create mode 100644 examples/api/bsoncxx/examples/decimal128/basic_usage.cpp create mode 100644 examples/api/bsoncxx/examples/decimal128/errors.cpp create mode 100644 examples/api/bsoncxx/examples/decimal128/from_bytes.cpp diff --git a/docs/api/bsoncxx/examples/decimal128.md b/docs/api/bsoncxx/examples/decimal128.md new file mode 100644 index 0000000000..f0e31f8efa --- /dev/null +++ b/docs/api/bsoncxx/examples/decimal128.md @@ -0,0 +1,11 @@ +## Basic Usage + +@snippet api/bsoncxx/examples/decimal128/basic_usage.cpp Example + +## From Bytes + +@snippet api/bsoncxx/examples/decimal128/from_bytes.cpp Example + +## Error Handling + +@snippet api/bsoncxx/examples/decimal128/errors.cpp Example diff --git a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp new file mode 100644 index 0000000000..e3f7e1ae1c --- /dev/null +++ b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp @@ -0,0 +1,105 @@ +// Copyright 2009-present 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 +#include + +#include + +#include + +namespace { + +// [Example] +// See: https://speleotrove.com/decimal/daconvs.html +void example() { + using d128 = bsoncxx::decimal128; // For brevity. + + // Default ctor. + assert(d128{}.to_string() == "0E-6176"); + + assert(d128{"0"}.to_string() == "0"); + assert(d128{"12"}.to_string() == "12"); + assert(d128{"-76"}.to_string() == "-76"); + assert(d128{"12.70"}.to_string() == "12.70"); + assert(d128{"+0.003"}.to_string() == "0.003"); + assert(d128{"017."}.to_string() == "17"); + assert(d128{".5"}.to_string() == "0.5"); + assert(d128{"4E+9"}.to_string() == "4E+9"); + assert(d128{"0.73e-7"}.to_string() == "7.3E-8"); + assert(d128{"Inf"}.to_string() == "Infinity"); + assert(d128{"-infinity"}.to_string() == "-Infinity"); + assert(d128{"NaN"}.to_string() == "NaN"); + // "NaN8275" is not a supported string representation. + + assert(d128{"123"}.to_string() == "123"); + assert(d128{"-123"}.to_string() == "-123"); + assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); + assert(d128{"1.23E+5"}.to_string() == "1.23E+5"); + assert(d128{"12.3"}.to_string() == "12.3"); + assert(d128{"0.00123"}.to_string() == "0.00123"); + assert(d128{"1.23E-8"}.to_string() == "1.23E-8"); + assert(d128{"-1.23E-10"}.to_string() == "-1.23E-10"); + assert(d128{"0"}.to_string() == "0"); + assert(d128{"0.00"}.to_string() == "0.00"); + assert(d128{"0E+2"}.to_string() == "0E+2"); + assert(d128{"-0"}.to_string() == "-0"); + assert(d128{"0.000005"}.to_string() == "0.000005"); + assert(d128{"0.0000050"}.to_string() == "0.0000050"); + assert(d128{"5E-7"}.to_string() == "5E-7"); + assert(d128{"Infinity"}.to_string() == "Infinity"); + assert(d128{"-Infinity"}.to_string() == "-Infinity"); + assert(d128{"NaN"}.to_string() == "NaN"); + // "NaN123" is not a supported string representation. + // "-sNaN" is not a supported string representation. + + assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); + assert(d128{"123E+3"}.to_string() == "1.23E+5"); + assert(d128{"12.3E-9"}.to_string() == "1.23E-8"); + assert(d128{"-123E-12"}.to_string() == "-1.23E-10"); + assert(d128{"700E-9"}.to_string() == "7.00E-7"); + assert(d128{"70"}.to_string() == "70"); + assert(d128{"0.00E+3"}.to_string() == "0E+1"); + + assert(d128{"0"}.to_string() == "0"); + assert(d128{"0.00"}.to_string() == "0.00"); + assert(d128{"123"}.to_string() == "123"); + assert(d128{"-123"}.to_string() == "-123"); + assert(d128{"1.23E3"}.to_string() == "1.23E+3"); + assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); + assert(d128{"12.3E+7"}.to_string() == "1.23E+8"); + assert(d128{"12.0"}.to_string() == "12.0"); + assert(d128{"12.3"}.to_string() == "12.3"); + assert(d128{"0.00123"}.to_string() == "0.00123"); + assert(d128{"-1.23E-12"}.to_string() == "-1.23E-12"); + assert(d128{"1234.5E-4"}.to_string() == "0.12345"); + assert(d128{"-0"}.to_string() == "-0"); + assert(d128{"-0.00"}.to_string() == "-0.00"); + assert(d128{"0E+7"}.to_string() == "0E+7"); + assert(d128{"-0E-7"}.to_string() == "-0E-7"); + assert(d128{"inf"}.to_string() == "Infinity"); + assert(d128{"+inFiniTy"}.to_string() == "Infinity"); + assert(d128{"-Infinity"}.to_string() == "-Infinity"); + assert(d128{"NaN"}.to_string() == "NaN"); + assert(d128{"-NAN"}.to_string() == "NaN"); + // "SNaN" is not a supported string representation. + // "Fred" is not a supported string representation. +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/decimal128/errors.cpp b/examples/api/bsoncxx/examples/decimal128/errors.cpp new file mode 100644 index 0000000000..a2fd321d22 --- /dev/null +++ b/examples/api/bsoncxx/examples/decimal128/errors.cpp @@ -0,0 +1,41 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + try { + bsoncxx::decimal128 d{"invalid"}; // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_invalid_decimal128); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main(void) { + example(); +} diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp new file mode 100644 index 0000000000..66f9033fa9 --- /dev/null +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -0,0 +1,86 @@ +// Copyright 2009-present 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 +#include + +#include + +#include + +namespace { + +// [Example] +// See: https://speleotrove.com/decimal/daconvs.html +void example() { + using d128 = bsoncxx::decimal128; // For brevity. + + // Default ctor. + assert((d128{0u, 0u}) == d128{}); + + // [0,0,0] + assert((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"}); + + // [1,0,0] + assert((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"}); + + // [0,0,1] + assert((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"}); + + // [0,0,-1] + assert((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"}); + + // [0,123,0] + assert((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"}); + + // [1,123,0] + assert((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"}); + + // [0,123,1] + assert((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"}); + + // [1,123,1] + assert((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"}); + + // [0,123,-1] + assert((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); + + // [0,123,-1] + assert((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); + + // [0,inf] + assert((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"}); + + // [1,inf] + assert((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"}); + + // [0,qNaN] + assert((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"}); + + // [1,qNaN]: Negative NaN is string-represented as NaN. + assert((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN"); + + // [0,sNaN]: Signaling NaN is string-represented as NaN. + assert((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); + + // [1,sNaN]: Negative Signaling NaN is string-represented as NaN. + assert((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 5993d4fb9f..04b8cca037 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -94,6 +94,7 @@ /// @brief Examples of how to use the bsoncxx library. /// @li @subpage topic-bsoncxx-examples-bson-documents /// @li @subpage topic-bsoncxx-examples-bson-errors +/// @li @subpage topic-bsoncxx-examples-decimal128 /// /// @@ -116,6 +117,13 @@ /// @include{doc} api/bsoncxx/examples/bson_errors.md /// +/// +/// @page topic-bsoncxx-examples-decimal128 Decimal128 +/// @brief How to use Decimal128 interfaces. +/// @tableofcontents +/// @include{doc} api/bsoncxx/examples/decimal128.md +/// + /// /// @page topic-mongocxx-examples Using the mongocxx Library /// @brief Examples of how to use the bsoncxx library. From dccdfec10388ab6894f94f254a5f12e6cb44bde1 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:11 -0500 Subject: [PATCH 07/26] Add oid examples --- docs/api/bsoncxx/examples/oid.md | 7 ++ .../api/bsoncxx/examples/oid/basic_usage.cpp | 84 +++++++++++++++++++ examples/api/bsoncxx/examples/oid/errors.cpp | 51 +++++++++++ src/bsoncxx/include/bsoncxx/doc.hpp | 8 ++ 4 files changed, 150 insertions(+) create mode 100644 docs/api/bsoncxx/examples/oid.md create mode 100644 examples/api/bsoncxx/examples/oid/basic_usage.cpp create mode 100644 examples/api/bsoncxx/examples/oid/errors.cpp diff --git a/docs/api/bsoncxx/examples/oid.md b/docs/api/bsoncxx/examples/oid.md new file mode 100644 index 0000000000..2746a0f2a7 --- /dev/null +++ b/docs/api/bsoncxx/examples/oid.md @@ -0,0 +1,7 @@ +## Basic Usage + +@snippet api/bsoncxx/examples/oid/basic_usage.cpp Example + +## Error Handling + +@snippet api/bsoncxx/examples/oid/errors.cpp Example diff --git a/examples/api/bsoncxx/examples/oid/basic_usage.cpp b/examples/api/bsoncxx/examples/oid/basic_usage.cpp new file mode 100644 index 0000000000..297121c158 --- /dev/null +++ b/examples/api/bsoncxx/examples/oid/basic_usage.cpp @@ -0,0 +1,84 @@ +// Copyright 2009-present 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 +#include +#include +#include + +#include + +#include + +namespace { + +// [Example] +void example() { + { + bsoncxx::oid a; + bsoncxx::oid b; + + assert(a == b || a != b); // Random. + } + + { + char bytes[bsoncxx::oid::k_oid_length]{}; + + bsoncxx::oid oid{bytes, sizeof(bytes)}; + + { + std::time_t time = oid.get_time_t(); + char str[sizeof("YYYY-MM-DD HH:MM:SS")]; + assert(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + sizeof(str) - 1u); + assert(std::string(str) == "1970-01-01 00:00:00"); + } + + assert(oid.bytes() != bytes); // Seperate storage. + assert(oid.size() == bsoncxx::oid::k_oid_length); + assert(std::memcmp(bytes, oid.bytes(), oid.size()) == 0); + + assert(oid.to_string() == "000000000000000000000000"); + assert(oid == bsoncxx::oid{"000000000000000000000000"}); + } + + { + // Timestamp: 946771199 (0x386e94ff) + // Value: 286462997 (0x11131415) + // Counter: 2171427 (0x212223) + bsoncxx::oid oid{"386e94ff1112131415212223"}; + + { + std::time_t time = oid.get_time_t(); + char str[sizeof("YYYY-MM-DD HH:MM:SS")]; + assert(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + sizeof(str) - 1u); + assert(std::string(str) == "2000-01-01 23:59:59"); + } + + assert(oid < bsoncxx::oid{"389622001112131415212223"}); // Timestamp: 2000-02-01 00:00:00 + assert(oid > bsoncxx::oid{"386d43801112131415212223"}); // Timestamp: 2000-01-01 00:00:00 + assert(oid < bsoncxx::oid{"386e94ffffffffffff212223"}); // Value: 1099511627775 + assert(oid > bsoncxx::oid{"386e94ff0000000000212223"}); // Value: 0 + assert(oid < bsoncxx::oid{"386e94ff1112131415ffffff"}); // Counter: 16777215 + assert(oid > bsoncxx::oid{"386e94ff1112131415000000"}); // Counter: 0 + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/oid/errors.cpp b/examples/api/bsoncxx/examples/oid/errors.cpp new file mode 100644 index 0000000000..fd4a3d35f4 --- /dev/null +++ b/examples/api/bsoncxx/examples/oid/errors.cpp @@ -0,0 +1,51 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + try { + bsoncxx::oid oid{"invalid"}; // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_invalid_oid); + } + + try { + char bytes[1]{}; + + bsoncxx::oid oid{bytes, sizeof(bytes)}; // Throws. + + assert(false && "should not reach this point"); + } catch (const bsoncxx::exception& ex) { + assert(ex.code() == bsoncxx::error_code::k_invalid_oid); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 04b8cca037..5aff50cb6c 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -95,6 +95,7 @@ /// @li @subpage topic-bsoncxx-examples-bson-documents /// @li @subpage topic-bsoncxx-examples-bson-errors /// @li @subpage topic-bsoncxx-examples-decimal128 +/// @li @subpage topic-bsoncxx-examples-oid /// /// @@ -124,6 +125,13 @@ /// @include{doc} api/bsoncxx/examples/decimal128.md /// +/// +/// @page topic-bsoncxx-examples-oid ObjectID +/// @brief How to use ObjectID (OID) interfaces. +/// @tableofcontents +/// @include{doc} api/bsoncxx/examples/oid.md +/// + /// /// @page topic-mongocxx-examples Using the mongocxx Library /// @brief Examples of how to use the bsoncxx library. From 675dcd8d5d7ac9db33b19f777a1d52c98d269676 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:11 -0500 Subject: [PATCH 08/26] Add validation examples --- docs/api/bsoncxx/examples/validation.md | 7 + .../examples/validation/basic_usage.cpp | 75 +++++++++++ .../bsoncxx/examples/validation/validator.cpp | 120 ++++++++++++++++++ src/bsoncxx/include/bsoncxx/doc.hpp | 8 ++ 4 files changed, 210 insertions(+) create mode 100644 docs/api/bsoncxx/examples/validation.md create mode 100644 examples/api/bsoncxx/examples/validation/basic_usage.cpp create mode 100644 examples/api/bsoncxx/examples/validation/validator.cpp diff --git a/docs/api/bsoncxx/examples/validation.md b/docs/api/bsoncxx/examples/validation.md new file mode 100644 index 0000000000..b8fc93c550 --- /dev/null +++ b/docs/api/bsoncxx/examples/validation.md @@ -0,0 +1,7 @@ +## Basic Usage + +@snippet api/bsoncxx/examples/validation/basic_usage.cpp Example + +## With Validator + +@snippet api/bsoncxx/examples/validation/validator.cpp Example diff --git a/examples/api/bsoncxx/examples/validation/basic_usage.cpp b/examples/api/bsoncxx/examples/validation/basic_usage.cpp new file mode 100644 index 0000000000..076464d551 --- /dev/null +++ b/examples/api/bsoncxx/examples/validation/basic_usage.cpp @@ -0,0 +1,75 @@ +// Copyright 2009-present 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 +#include +#include + +#include +#include +#include +#include + +#include + +namespace { + +// [Example] +void example() { + { + std::uint8_t bytes[1]{}; // Invalid. + + assert(!bsoncxx::validate(bytes, sizeof(bytes)).has_value()); + + std::size_t offset; + + assert(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset).has_value()); + + // Set to `0` for an invalid BSON document. + assert(offset == 0u); + } + + bsoncxx::document::value owner = bsoncxx::from_json(R"({"x": 1})"); + const std::uint8_t* data = owner.data(); + const std::size_t length = owner.length(); + + { + auto doc_opt = bsoncxx::validate(data, length); + assert(doc_opt.has_value()); + + bsoncxx::document::view doc = *doc_opt; + + assert(doc.data() == data); + assert(doc.length() == length); + assert(doc == owner.view()); + } + + { + bsoncxx::validator options; + std::size_t offset = 123u; + + assert(bsoncxx::validate(data, length) == + bsoncxx::validate(data, length, options, &offset)); + + // Not set when valid. + assert(offset == 123u); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + example(); +} diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp new file mode 100644 index 0000000000..8f773e427d --- /dev/null +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -0,0 +1,120 @@ +// Copyright 2009-present 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 +#include + +#include + +#include + +namespace { + +// [Example] +// { +// "x": "a\0b", +// "a.b": 1, +// "v": { +// "$numberInt": "123" +// } +// } +void example(const std::uint8_t* bytes, std::size_t length) { + // Default options. + { + bsoncxx::validator options; + std::size_t offset; + + assert(bsoncxx::validate(bytes, length, options, &offset).has_value()); + } + + // Validate UTF-8 strings. + { + bsoncxx::validator options; + std::size_t offset; + + options.check_utf8(true); + + assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + + // Offset of `"x": "\0"` relative to start of the document. + assert(offset == 4u); + + options.check_utf8_allow_null(true); + + assert(bsoncxx::validate(bytes, length, options, &offset).has_value()); + } + + // Validate dot keys. + { + bsoncxx::validator options; + std::size_t offset; + + options.check_dot_keys(true); + + assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + + // Offset of `"a.b": 1` relative to start of the document. + assert(offset == 15u); + } + + // Validate dollar keys. + { + bsoncxx::validator options; + std::size_t offset; + + options.check_dollar_keys(true); + + assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + + // Offset of `"a.b": 1` relative to start of the sub-document. (CDRIVER-5710) + assert(offset == 4u); + } +} +// [Example] + +} // namespace + +int EXAMPLES_CDECL main() { + // clang-format off + std::uint8_t bytes[] = { + // Total Bytes: 53 + 0x35, 0x00, 0x00, 0x00, + // Element: String (Offset: 4) + 0x02, + 0x78, 0x00, // Key: "x" + 0x04, 0x00, 0x00, 0x00, // Length: 4 + 0x61, 0x00, 0x62, 0x00, // Value: "a\0b" + + // Element: Int32 (Offset: 15) + 0x10, + 0x61, 0x2e, 0x62, 0x00, // Key: "a.b" + 0x01, 0x00, 0x00, 0x00, // Value: 1 + + // Element: Document (Offset: 24) + 0x03, + 0x76, 0x00, // Key: "v" + 0x19, 0x00, 0x00, 0x00, // Total Bytes: 25 + // Element: String (Offset: 31) + 0x02, + // Key: "$numberInt" + 0x24, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x49, 0x6e, 0x74, 0x00, + 0x04, 0x00, 0x00, 0x00, // Length: 4 + 0x31, 0x32, 0x33, 0x00, // Value: "123" + 0x00, // End of Document. + 0x00, // End of Document. + }; + // clang-format on + + example(bytes, sizeof(bytes)); +} diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 5aff50cb6c..c6d07dfbfa 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -96,6 +96,7 @@ /// @li @subpage topic-bsoncxx-examples-bson-errors /// @li @subpage topic-bsoncxx-examples-decimal128 /// @li @subpage topic-bsoncxx-examples-oid +/// @li @subpage topic-bsoncxx-examples-validation /// /// @@ -132,6 +133,13 @@ /// @include{doc} api/bsoncxx/examples/oid.md /// +/// +/// @page topic-bsoncxx-examples-validation Validation +/// @brief How to use BSON document validation interfaces. +/// @tableofcontents +/// @include{doc} api/bsoncxx/examples/validation.md +/// + /// /// @page topic-mongocxx-examples Using the mongocxx Library /// @brief Examples of how to use the bsoncxx library. From 54174514ebd9243044449f4374e198af8da94174 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:11 -0500 Subject: [PATCH 09/26] CXX-3101 Workaround missing features of mnmlstc/core polyfills --- .../bson_documents/access_array/algorithms.cpp | 6 +++--- .../examples/bson_documents/access_array/basic.cpp | 8 ++++---- .../examples/bson_documents/access_array/find.cpp | 2 +- .../examples/bson_documents/access_array/iterators.cpp | 6 +++--- .../examples/bson_documents/access_array/subscript.cpp | 2 +- .../examples/bson_documents/access_doc/algorithms.cpp | 6 +++--- .../examples/bson_documents/access_doc/basic.cpp | 8 ++++---- .../examples/bson_documents/access_doc/find.cpp | 2 +- .../examples/bson_documents/access_doc/iterators.cpp | 6 +++--- .../examples/bson_documents/access_doc/subscript.cpp | 2 +- .../bson_documents/create_array/builder_append.cpp | 2 +- .../bson_documents/create_array/builder_basic.cpp | 2 +- .../bson_documents/create_array/builder_bson_type.cpp | 2 +- .../create_array/builder_make_document.cpp | 2 +- .../create_array/builder_sub_document.cpp | 2 +- .../bson_documents/create_array/builder_value_type.cpp | 2 +- .../bson_documents/create_array/json_basic.cpp | 2 +- .../bson_documents/create_array/json_sub_document.cpp | 2 +- .../bson_documents/create_doc/builder_append.cpp | 2 +- .../bson_documents/create_doc/builder_basic.cpp | 2 +- .../create_doc/builder_make_document.cpp | 2 +- .../bson_documents/create_doc/builder_raw_value.cpp | 2 +- .../bson_documents/create_doc/builder_raw_view.cpp | 2 +- .../bson_documents/create_doc/builder_sub_document.cpp | 2 +- .../bson_documents/create_doc/builder_value_type.cpp | 2 +- .../examples/bson_documents/create_doc/json_basic.cpp | 2 +- .../bson_documents/create_doc/json_sub_document.cpp | 2 +- .../examples/bson_documents/elements/arr_multi.cpp | 8 ++++---- .../examples/bson_documents/elements/arr_single.cpp | 4 ++-- .../bson_documents/elements/cmp_bson_value.cpp | 8 ++++---- .../examples/bson_documents/elements/cmp_type.cpp | 4 ++-- .../examples/bson_documents/elements/doc_multi.cpp | 8 ++++---- .../examples/bson_documents/elements/doc_single.cpp | 4 ++-- .../examples/bson_documents/values/arr_value.cpp | 2 +- .../examples/bson_documents/values/arr_view.cpp | 6 +++--- .../examples/bson_documents/values/bson_type_value.cpp | 2 +- .../examples/bson_documents/values/doc_value.cpp | 2 +- .../examples/bson_documents/values/doc_view.cpp | 6 +++--- .../examples/bson_documents/values/make_value.cpp | 2 +- .../examples/bson_documents/values/value_type.cpp | 2 +- .../bson_errors/query_element_arr_invalid_type.cpp | 2 +- .../bson_errors/query_element_doc_invalid_type.cpp | 2 +- .../api/bsoncxx/examples/validation/basic_usage.cpp | 6 +++--- examples/api/bsoncxx/examples/validation/validator.cpp | 10 +++++----- 44 files changed, 80 insertions(+), 80 deletions(-) diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp index 367b064264..4ef4855883 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp @@ -34,12 +34,12 @@ void example(bsoncxx::array::view arr) { std::copy_if( arr.begin(), arr.end(), std::back_inserter(elements), [](const bsoncxx::array::element& e) { - return e.key() == "0" || e.type() == bsoncxx::type::k_string; + return e.key().compare("0") == 0 || e.type() == bsoncxx::type::k_string; }); assert(elements.size() == 2u); - assert(elements[0].key() == "0"); - assert(elements[1].key() == "2"); + assert(elements[0].key().compare("0") == 0); + assert(elements[1].key().compare("2") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp index fea4ee62da..ba78672bdd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp @@ -29,16 +29,16 @@ void example(bsoncxx::array::view arr) { for (bsoncxx::array::element e : arr) { switch (e.type()) { case bsoncxx::type::k_int32: - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); assert(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - assert(e.key() == "1"); + assert(e.key().compare("1") == 0); assert(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - assert(e.key() == "2"); - assert(e.get_string().value == "three"); + assert(e.key().compare("2") == 0); + assert(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp index 90d3d59ab7..74fee6cb2e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp @@ -31,7 +31,7 @@ void example(bsoncxx::array::view arr) { auto iter = arr.find(1); assert(iter != arr.end()); - assert(iter->key() == "1"); + assert(iter->key().compare("1") == 0); assert(iter->get_int32().value == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp index c6f9a25582..40d9af8c8f 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp @@ -34,20 +34,20 @@ void example(bsoncxx::array::view arr) { { bsoncxx::array::element e = *iter; - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); assert(e.get_int32().value == 1); } ++iter; - assert(iter->key() == "1"); + assert(iter->key().compare("1") == 0); assert(iter->get_int32().value == 2); { auto iter_copy = iter++; assert(iter_copy != iter); - assert(iter_copy->key() == "1"); + assert(iter_copy->key().compare("1") == 0); assert(iter_copy->get_int32() == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp index 4c0d1cadd1..3c6a145782 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp @@ -31,7 +31,7 @@ void example(bsoncxx::array::view arr) { { bsoncxx::array::element e = arr[1]; - assert(e.key() == "1"); + assert(e.key().compare("1") == 0); assert(e.get_int32().value == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp index ab1073fb94..80ebb06dd9 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp @@ -37,12 +37,12 @@ void example(bsoncxx::document::view doc) { doc.end(), std::back_inserter(elements), [](const bsoncxx::document::element& e) { - return e.key() == "a" || e.type() == bsoncxx::type::k_string; + return e.key().compare("a") == 0 || e.type() == bsoncxx::type::k_string; }); assert(elements.size() == 2u); - assert(elements[0].key() == "a"); - assert(elements[1].key() == "c"); + assert(elements[0].key().compare("a") == 0); + assert(elements[1].key().compare("c") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp index 8129589a61..c2ab704a6c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp @@ -29,16 +29,16 @@ void example(bsoncxx::document::view doc) { for (bsoncxx::document::element e : doc) { switch (e.type()) { case bsoncxx::type::k_int32: - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); assert(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); assert(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - assert(e.key() == "c"); - assert(e.get_string().value == "three"); + assert(e.key().compare("c") == 0); + assert(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp index 250fb66757..0b7295dead 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp @@ -31,7 +31,7 @@ void example(bsoncxx::document::view doc) { auto iter = doc.find("b"); assert(iter != doc.end()); - assert(iter->key() == "b"); + assert(iter->key().compare("b") == 0); assert(iter->get_int32().value == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp index 3c9db0a4ba..936f801e56 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp @@ -34,20 +34,20 @@ void example(bsoncxx::document::view doc) { { bsoncxx::document::element e = *iter; - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); assert(e.get_int32().value == 1); } ++iter; - assert(iter->key() == "b"); + assert(iter->key().compare("b") == 0); assert(iter->get_int32().value == 2); { auto iter_copy = iter++; assert(iter_copy != iter); - assert(iter_copy->key() == "b"); + assert(iter_copy->key().compare("b") == 0); assert(iter_copy->get_int32() == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp index 9d32daabfa..07b7862ed6 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp @@ -31,7 +31,7 @@ void example(bsoncxx::document::view doc) { { bsoncxx::document::element e = doc["b"]; - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); assert(e.get_int32().value == 2); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp index b84b091da5..6a31774dc7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp @@ -35,7 +35,7 @@ void example() { assert(arr[0].get_int32().value == 1); assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value == "three"); + assert(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp index e58ab188b2..bfc9371d43 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp @@ -33,7 +33,7 @@ void example() { assert(arr[0].get_int32().value == 1); assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value == "three"); + assert(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp index 22271873fc..5c3447a9f0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp @@ -38,7 +38,7 @@ void example() { assert(arr[0].get_int32().value == 1); assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value == "three"); + assert(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp index 8638211c2d..9f764d00b2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp @@ -32,7 +32,7 @@ void example() { assert(arr[0].get_int32().value == 1); assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value == "three"); + assert(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp index 0cbe7119bd..6b76f0263d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -31,7 +31,7 @@ void example() { [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); }); bsoncxx::document::view v = owner.view()[0].get_document().value; - assert(v["key"].get_string().value == "value"); + assert(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp index 4643ea3219..34252c0cfd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp @@ -36,7 +36,7 @@ void example() { assert(arr[0].get_int32().value == 1); assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value == "three"); + assert(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp index c24fbd9b38..7b9c7d0e54 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp @@ -32,7 +32,7 @@ void example() { assert(doc["0"].get_int32().value == 1); assert(doc["1"].get_double().value == 2.0); - assert(doc["2"].get_string().value == "three"); + assert(doc["2"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp index dde4bda28a..ac4d4df72a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp @@ -32,7 +32,7 @@ void example() { )"); bsoncxx::document::view v = owner.view()["0"].get_document().value; - assert(v["key"].get_string().value == "value"); + assert(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp index 161abc2102..b1806e599b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp @@ -38,7 +38,7 @@ void example() { assert(doc["a"].get_int32().value == 1); assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value == "three"); + assert(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp index 7f5c7d72e7..a000be789e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp @@ -36,7 +36,7 @@ void example() { assert(doc["a"].get_int32().value == 1); assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value == "three"); + assert(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp index 124ad3ccd1..3481c35314 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp @@ -35,7 +35,7 @@ void example() { assert(doc["a"].get_int32().value == 1); assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value == "three"); + assert(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp index 2fb68a0be8..dd796dd911 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp @@ -38,7 +38,7 @@ void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::value owner{raw, length, deleter}; bsoncxx::document::view doc = owner.view(); - assert(doc["key"].get_string().value == "value"); + assert(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp index caea6eed52..672b396e62 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp @@ -30,7 +30,7 @@ namespace { void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::view doc{data, length}; - assert(doc["key"].get_string().value == "value"); + assert(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp index b9f2e5fd9c..c80ab459ea 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -32,7 +32,7 @@ void example() { "v", [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); })); bsoncxx::document::view v = owner.view()["v"].get_document().value; - assert(v["key"].get_string().value == "value"); + assert(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp index fe236f70c9..27cfabdcbc 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp @@ -39,7 +39,7 @@ void example() { assert(doc["a"].get_int32().value == 1); assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value == "three"); + assert(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp index a6897486fa..1484404ca9 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp @@ -36,7 +36,7 @@ void example() { assert(doc["a"].get_int32().value == 1); assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value == "three"); + assert(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp index eb04204b6f..5a37ca6fad 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp @@ -32,7 +32,7 @@ void example() { )"); bsoncxx::document::view v = doc["v"].get_document().value; - assert(v["key"].get_string().value == "value"); + assert(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp index e0089cde79..7d292a4127 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp @@ -27,7 +27,7 @@ namespace { void example(bsoncxx::array::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); @@ -37,7 +37,7 @@ void example(bsoncxx::array::element e) { break; } case bsoncxx::type::k_double: { - assert(e.key() == "1"); + assert(e.key().compare("1") == 0); bsoncxx::types::b_double v = e.get_double(); @@ -47,12 +47,12 @@ void example(bsoncxx::array::element e) { break; } case bsoncxx::type::k_string: { - assert(e.key() == "2"); + assert(e.key().compare("2") == 0); bsoncxx::types::b_string v = e.get_string(); assert(v.type_id == bsoncxx::type::k_string); - assert(v.value == "three"); + assert(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp index 0bed3cb849..ab215145d1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp @@ -26,14 +26,14 @@ namespace { // [1, 2.0, "three"] void example(bsoncxx::array::element e) { if (e.type() == bsoncxx::type::k_int32) { - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); assert(v.type_id == bsoncxx::type::k_int32); assert(v.value == 1); } else { - assert(e.key() != "0"); + assert(e.key().compare("0") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp index a4343c047c..8072b0dde2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp @@ -30,18 +30,18 @@ void example(bsoncxx::document::element e) { bsoncxx::types::b_int64 b{2}; if (e.get_value() == a) { - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); } else if (e.get_value() == b) { - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); } bsoncxx::types::bson_value::view va{a}; bsoncxx::types::bson_value::view vb{b}; if (e == va) { - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); } else if (e == vb) { - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp index ea4ff945e8..4d141e958d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp @@ -30,10 +30,10 @@ void example(bsoncxx::document::element e) { std::int64_t b{2}; if (e.type() == bsoncxx::type::k_int32) { - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); assert(e.get_int32().value == a); } else if (e.type() == bsoncxx::type::k_int64) { - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); assert(e.get_int64().value == b); } } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp index e2171129e7..8a21e58ef3 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp @@ -27,7 +27,7 @@ namespace { void example(bsoncxx::document::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); @@ -37,7 +37,7 @@ void example(bsoncxx::document::element e) { break; } case bsoncxx::type::k_double: { - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); bsoncxx::types::b_double v = e.get_double(); @@ -47,12 +47,12 @@ void example(bsoncxx::document::element e) { break; } case bsoncxx::type::k_string: { - assert(e.key() == "c"); + assert(e.key().compare("c") == 0); bsoncxx::types::b_string v = e.get_string(); assert(v.type_id == bsoncxx::type::k_string); - assert(v.value == "three"); + assert(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp index a078b86b5d..ebdf878158 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp @@ -26,14 +26,14 @@ namespace { // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::element e) { if (e.type() == bsoncxx::type::k_int32) { - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); assert(v.type_id == bsoncxx::type::k_int32); assert(v.value == 1); } else { - assert(e.key() != "a"); + assert(e.key().compare("a") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp index 08ecab8a25..bc8720838f 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp @@ -38,7 +38,7 @@ void example() { v = v.view().get_array().value[0].get_string(); // Copy: no dangling. assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value == "value"); + assert(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp index a1191ee687..f7a2a79c62 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp @@ -31,15 +31,15 @@ void example(bsoncxx::array::element e) { switch (v.type()) { case bsoncxx::type::k_int32: - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); assert(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - assert(e.key() == "1"); + assert(e.key().compare("1") == 0); assert(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - assert(e.key() == "2"); + assert(e.key().compare("2") == 0); assert(v.get_string() == e.get_string()); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp index df34192f2d..da47b162d5 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp @@ -38,7 +38,7 @@ void example() { v = bsoncxx::types::b_string{"three"}; assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value == "three"); + assert(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp index 704545d0f0..c231a72183 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp @@ -38,7 +38,7 @@ void example() { v = v.view().get_document().value["key"].get_string(); // Copy: no dangling. assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value == "value"); + assert(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp index 45ffdbc462..d3bd932e39 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp @@ -31,15 +31,15 @@ void example(bsoncxx::document::element e) { switch (v.type()) { case bsoncxx::type::k_int32: - assert(e.key() == "a"); + assert(e.key().compare("a") == 0); assert(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - assert(e.key() == "b"); + assert(e.key().compare("b") == 0); assert(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - assert(e.key() == "c"); + assert(e.key().compare("c") == 0); assert(v.get_string() == e.get_string()); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp index cdd0ad9f21..5045364ed8 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp @@ -50,7 +50,7 @@ void example() { view_type v = owner.view(); assert(v.type() == bsoncxx::type::k_string); - assert(v.get_string().value == "three"); + assert(v.get_string().value.compare("three") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp index ea93a088ae..670886c211 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp @@ -42,7 +42,7 @@ void example() { v = std::string("three"); assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value == "three"); + assert(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp index b5292422b0..830fafd7dc 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp @@ -27,7 +27,7 @@ namespace { // [Example] // [1] void example(bsoncxx::array::element e) { - assert(e.key() == "0"); + assert(e.key().compare("0") == 0); assert(e.type() == bsoncxx::type::k_int32); assert(e.get_int32().value == 1); diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp index 1edcd14dbc..c36e1a5be1 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp @@ -27,7 +27,7 @@ namespace { // [Example] // {"x": 1} void example(bsoncxx::document::element e) { - assert(e.key() == "x"); + assert(e.key().compare("x") == 0); assert(e.type() == bsoncxx::type::k_int32); assert(e.get_int32().value == 1); diff --git a/examples/api/bsoncxx/examples/validation/basic_usage.cpp b/examples/api/bsoncxx/examples/validation/basic_usage.cpp index 076464d551..27f019f937 100644 --- a/examples/api/bsoncxx/examples/validation/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/validation/basic_usage.cpp @@ -30,11 +30,11 @@ void example() { { std::uint8_t bytes[1]{}; // Invalid. - assert(!bsoncxx::validate(bytes, sizeof(bytes)).has_value()); + assert(!bsoncxx::validate(bytes, sizeof(bytes))); std::size_t offset; - assert(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset).has_value()); + assert(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset)); // Set to `0` for an invalid BSON document. assert(offset == 0u); @@ -46,7 +46,7 @@ void example() { { auto doc_opt = bsoncxx::validate(data, length); - assert(doc_opt.has_value()); + assert(doc_opt); bsoncxx::document::view doc = *doc_opt; diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp index 8f773e427d..fdf11d624b 100644 --- a/examples/api/bsoncxx/examples/validation/validator.cpp +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -35,7 +35,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { bsoncxx::validator options; std::size_t offset; - assert(bsoncxx::validate(bytes, length, options, &offset).has_value()); + assert(bsoncxx::validate(bytes, length, options, &offset)); } // Validate UTF-8 strings. @@ -45,14 +45,14 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_utf8(true); - assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + assert(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"x": "\0"` relative to start of the document. assert(offset == 4u); options.check_utf8_allow_null(true); - assert(bsoncxx::validate(bytes, length, options, &offset).has_value()); + assert(bsoncxx::validate(bytes, length, options, &offset)); } // Validate dot keys. @@ -62,7 +62,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dot_keys(true); - assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + assert(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"a.b": 1` relative to start of the document. assert(offset == 15u); @@ -75,7 +75,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dollar_keys(true); - assert(!bsoncxx::validate(bytes, length, options, &offset).has_value()); + assert(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"a.b": 1` relative to start of the sub-document. (CDRIVER-5710) assert(offset == 4u); From 6edb28600a01d4e8506ec0feba8fc18c053fe607 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:12 -0500 Subject: [PATCH 10/26] Refactor API examples to use component registration pattern --- examples/CMakeLists.txt | 47 +++++- .../access_array/algorithms.cpp | 3 +- .../bson_documents/access_array/basic.cpp | 3 +- .../bson_documents/access_array/find.cpp | 3 +- .../bson_documents/access_array/iterators.cpp | 3 +- .../bson_documents/access_array/subscript.cpp | 3 +- .../bson_documents/access_doc/algorithms.cpp | 3 +- .../bson_documents/access_doc/basic.cpp | 3 +- .../bson_documents/access_doc/find.cpp | 3 +- .../bson_documents/access_doc/iterators.cpp | 3 +- .../bson_documents/access_doc/subscript.cpp | 3 +- .../create_array/builder_append.cpp | 3 +- .../create_array/builder_basic.cpp | 3 +- .../create_array/builder_bson_type.cpp | 3 +- .../create_array/builder_bson_value.cpp | 3 +- .../create_array/builder_make_document.cpp | 3 +- .../create_array/builder_raw_value.cpp | 3 +- .../create_array/builder_raw_view.cpp | 3 +- .../create_array/builder_reset.cpp | 3 +- .../create_array/builder_sub_array.cpp | 3 +- .../create_array/builder_sub_document.cpp | 3 +- .../create_array/builder_value_type.cpp | 3 +- .../create_array/json_basic.cpp | 3 +- .../create_array/json_extended.cpp | 3 +- .../create_array/json_sub_array.cpp | 3 +- .../create_array/json_sub_document.cpp | 3 +- .../bson_documents/create_array/json_udl.cpp | 3 +- .../create_doc/builder_append.cpp | 3 +- .../create_doc/builder_basic.cpp | 3 +- .../create_doc/builder_bson_type.cpp | 3 +- .../create_doc/builder_bson_value.cpp | 3 +- .../create_doc/builder_make_document.cpp | 3 +- .../create_doc/builder_raw_value.cpp | 3 +- .../create_doc/builder_raw_view.cpp | 3 +- .../create_doc/builder_reset.cpp | 3 +- .../create_doc/builder_sub_array.cpp | 3 +- .../create_doc/builder_sub_document.cpp | 3 +- .../create_doc/builder_value_type.cpp | 3 +- .../bson_documents/create_doc/json_basic.cpp | 3 +- .../create_doc/json_extended.cpp | 3 +- .../create_doc/json_sub_array.cpp | 3 +- .../create_doc/json_sub_document.cpp | 3 +- .../bson_documents/create_doc/json_udl.cpp | 3 +- .../bson_documents/elements/arr_multi.cpp | 3 +- .../bson_documents/elements/arr_single.cpp | 3 +- .../elements/cmp_bson_value.cpp | 3 +- .../bson_documents/elements/cmp_type.cpp | 3 +- .../bson_documents/elements/doc_multi.cpp | 3 +- .../bson_documents/elements/doc_single.cpp | 3 +- .../examples/bson_documents/json/array.cpp | 3 +- .../examples/bson_documents/json/document.cpp | 3 +- .../bson_documents/values/arr_value.cpp | 3 +- .../bson_documents/values/arr_view.cpp | 3 +- .../bson_documents/values/bson_type_value.cpp | 3 +- .../bson_documents/values/bson_type_view.cpp | 3 +- .../bson_documents/values/doc_value.cpp | 3 +- .../bson_documents/values/doc_view.cpp | 3 +- .../bson_documents/values/make_value.cpp | 3 +- .../bson_documents/values/value_type.cpp | 3 +- .../bson_errors/access_arr_iter_end.cpp | 3 +- .../bson_errors/access_arr_iter_invalid.cpp | 3 +- .../bson_errors/access_arr_key_missing.cpp | 3 +- .../bson_errors/access_doc_iter_end.cpp | 3 +- .../bson_errors/access_doc_iter_invalid.cpp | 3 +- .../bson_errors/access_doc_key_missing.cpp | 3 +- .../bson_errors/create_arr_append.cpp | 3 +- .../create_arr_append_sub_array.cpp | 3 +- .../create_arr_append_sub_document.cpp | 3 +- .../create_bson_value_invalid_element.cpp | 3 +- .../create_bson_value_invalid_type.cpp | 3 +- .../bson_errors/create_doc_append.cpp | 3 +- .../create_doc_append_sub_array.cpp | 3 +- .../create_doc_append_sub_document.cpp | 3 +- .../examples/bson_errors/create_json.cpp | 3 +- .../query_bson_value_invalid_type.cpp | 3 +- .../bson_errors/query_element_arr_invalid.cpp | 3 +- .../query_element_arr_invalid_type.cpp | 3 +- .../bson_errors/query_element_doc_invalid.cpp | 3 +- .../query_element_doc_invalid_type.cpp | 3 +- .../bson_errors/to_json_invalid_arr.cpp | 3 +- .../bson_errors/to_json_invalid_doc.cpp | 3 +- .../examples/decimal128/basic_usage.cpp | 3 +- .../bsoncxx/examples/decimal128/errors.cpp | 3 +- .../examples/decimal128/from_bytes.cpp | 3 +- .../api/bsoncxx/examples/oid/basic_usage.cpp | 3 +- examples/api/bsoncxx/examples/oid/errors.cpp | 3 +- .../examples/validation/basic_usage.cpp | 3 +- .../bsoncxx/examples/validation/validator.cpp | 3 +- examples/api/runner.cpp | 137 ++++++++++++++++++ examples/api/runner.hh | 30 ++++ examples/macros.hh | 3 + 91 files changed, 385 insertions(+), 93 deletions(-) create mode 100644 examples/api/runner.cpp create mode 100644 examples/api/runner.hh diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index aaa51cf09b..53612c38b7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -108,12 +108,6 @@ function(add_examples_executable source) endif () endfunction() -file(GLOB_RECURSE api_examples_sources - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - CONFIGURE_DEPENDS - "api/*.cpp" -) - file(GLOB_RECURSE bsoncxx_examples_sources RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS @@ -169,6 +163,46 @@ foreach(source ${mongocxx_examples_sources}) ) endforeach() +file(GLOB_RECURSE api_examples_sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + CONFIGURE_DEPENDS + "api/*.cpp" +) +list(REMOVE_ITEM api_examples_sources "api/runner.cpp") + +function (add_abi_examples) + # Avoid MAX_PATH errors on Windows due to long target names by using a single `api-runner` . + add_examples_executable(api/runner.cpp LIBRARIES mongocxx_target Threads::Threads) + + # Define a unique entry function name per component. + foreach (source ${api_examples_sources}) + target_sources (api-runner PRIVATE ${source}) + + # path/to/source.cpp -> source + get_filename_component(name ${source} NAME_WE) + + # path/to/source.cpp -> path/to + get_filename_component(subdir ${source} DIRECTORY) + + # path/to/source -> path_to_source + string(REPLACE "/" "_" component_name "${subdir}/${name}") + + # Define a unique component name for each component to be registered with the runner. + set_property (SOURCE ${source} APPEND PROPERTY COMPILE_DEFINITIONS "EXAMPLES_COMPONENT_NAME=${component_name}") + endforeach () + + # Silence warnings for common patterns in API examples. + set_property (TARGET api-runner APPEND PROPERTY COMPILE_OPTIONS + # Allow unused variables. + $<$:-Wno-unused> + $<$:/wd4189> + + # Allow simpler switch statements. + $<$:-Wno-switch> + ) +endfunction () +add_abi_examples () + set_local_dist (examples_DIST_local CMakeLists.txt ${api_examples_sources} @@ -179,6 +213,7 @@ set_local_dist (examples_DIST_local add_subdirectory/.gitignore add_subdirectory/CMakeLists.txt add_subdirectory/hello_mongocxx.cpp + api/runner.cpp projects/bsoncxx/cmake/shared/build.sh projects/bsoncxx/cmake/shared/build/.gitignore projects/bsoncxx/cmake/shared/CMakeLists.txt diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp index 4ef4855883..6653fbaef2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -45,6 +46,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp index ba78672bdd..fdfacbfd07 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -47,7 +48,7 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { // clang-format off +RUNNER_REGISTER_COMPONENT() { // clang-format off example(bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})")["v"].get_array().value); } // clang-format on diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp index 74fee6cb2e..7ec3bd7521 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp index 40d9af8c8f..97e7f9b4cf 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -57,6 +58,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp index 3c6a145782..a2ae35c81a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp index 80ebb06dd9..696b46a699 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -48,6 +49,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})")); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp index c2ab704a6c..382ec8feb0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -47,6 +48,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})")); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp index 0b7295dead..1713c23263 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp index 936f801e56..8a6c8efbc0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -57,6 +58,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp index 07b7862ed6..f25c85267b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp index 6a31774dc7..10f5cbdde2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp index bfc9371d43..cd2a859ff1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -39,6 +40,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp index 5c3447a9f0..c9c3316098 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp index db9fb201b2..9276ce2096 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -52,6 +53,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp index 9f764d00b2..3fe7def209 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp index d0b8e5674a..bedba93d42 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp @@ -23,6 +23,7 @@ #include #include +#include #include namespace { @@ -46,7 +47,7 @@ void example(const std::uint8_t* data, std::size_t length) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { bsoncxx::document::value owner = bsoncxx::from_json(R"({"v": [1, 2]})"); bsoncxx::array::view arr = owner.view()["v"].get_array().value; diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp index 4eca5d7908..05ecb34004 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -38,7 +39,7 @@ void example(const std::uint8_t* data, std::size_t length) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { bsoncxx::document::value owner = bsoncxx::from_json(R"({"v": [1, 2]})"); bsoncxx::array::view arr = owner.view()["v"].get_array().value; diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp index a9b7d966c2..db59df57ee 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp index 677ffb8ab3..52691e861e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp index 6b76f0263d..d1b8f91265 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -37,6 +38,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp index 34252c0cfd..b06f0c9471 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -42,6 +43,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp index 7b9c7d0e54..ade817dd6b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp index d4a05882c6..57c5322148 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -42,6 +43,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp index 7470aee536..3833153d25 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -39,6 +40,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp index ac4d4df72a..308ca6463e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp index 20e1482959..cfbb498b06 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace { @@ -34,6 +35,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp index b1806e599b..239cb2d998 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp index a000be789e..28d19c28d7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -42,6 +43,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp index 86f96eaf29..006f072409 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp index 87254af5eb..989b9be533 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -52,6 +53,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp index 3481c35314..e466f8e524 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -41,6 +42,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp index dd796dd911..a263adb6a0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -44,7 +45,7 @@ void example(const std::uint8_t* data, std::size_t length) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { bsoncxx::document::value owner = bsoncxx::from_json(R"({"key": "value"})"); bsoncxx::document::view doc = owner.view(); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp index 672b396e62..6e8047e181 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -36,7 +37,7 @@ void example(const std::uint8_t* data, std::size_t length) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { bsoncxx::document::value owner = bsoncxx::from_json(R"({"key": "value"})"); bsoncxx::document::view doc = owner.view(); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp index 1056d27163..807d16dc18 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -52,6 +53,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp index e9bc6cdcc2..f1708ef354 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -46,6 +47,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp index c80ab459ea..9bcd6d7dc9 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp index 27cfabdcbc..965d925dbe 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -45,6 +46,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp index 1484404ca9..2ba21dd2e1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -42,6 +43,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp index 39c9a7c11d..a7910b0667 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -42,6 +43,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp index 313e12b1c7..2cbfdf61a7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -39,6 +40,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp index 5a37ca6fad..ff40f69422 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp index cc7d0a58c9..696c842909 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace { @@ -34,6 +35,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp index 7d292a4127..1b966ce44a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -62,7 +63,7 @@ void example(bsoncxx::array::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); const auto arr = doc["v"].get_array().value; diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp index ab215145d1..afb0fb2ff9 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -40,7 +41,7 @@ void example(bsoncxx::array::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); const auto arr = doc["v"].get_array().value; diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp index 8072b0dde2..b6a90cf19c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -48,7 +49,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}})"); example(doc["a"]); diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp index 4d141e958d..46d4abc1ef 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -41,7 +42,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"a": {"$numberInt": "1"}, "b": {"$numberLong": "2"}})"); example(doc["a"]); diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp index 8a21e58ef3..54605aa28a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -62,7 +63,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); example(doc["a"]); diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp index ebdf878158..9c98a804f7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -40,7 +41,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); example(doc["a"]); diff --git a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp index 507c33f04a..c6fc8787cb 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -100,6 +101,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp index 94af4ccb2a..b9387b0e28 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp @@ -23,6 +23,7 @@ #include #include +#include #include namespace { @@ -104,6 +105,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp index bc8720838f..078bd4f28c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp index f7a2a79c62..844f403411 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -48,7 +49,7 @@ void example(bsoncxx::array::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"v": [1, 2.0, "three"]})"); const auto arr = doc["v"].get_array().value; diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp index da47b162d5..607372bffe 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp index 2c7045a4ba..39bea3ccfd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp index c231a72183..89e00c50cd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp index d3bd932e39..4401c404f2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -48,7 +49,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"a": 1, "b": 2.0, "c": "three"})"); example(doc["a"]); diff --git a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp index 5045364ed8..bee1e55bea 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -57,6 +58,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp index 670886c211..66889204d7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -48,6 +49,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp index 8465791c33..fc3854df50 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -48,6 +49,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2, 3]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp index f45b835f6d..137279a464 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -44,6 +45,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp index 98508ad565..0a6836b490 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -37,6 +38,6 @@ void example(bsoncxx::array::view arr) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1, 2, 3]})")["v"].get_array().value); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp index 53ba5850fe..e403c1047e 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -47,6 +48,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp index ffd3a0a735..2f05d8e85a 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -45,6 +46,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp index d885a8e3d0..ce0e0323bb 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -37,6 +38,6 @@ void example(bsoncxx::document::view doc) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"a": 1, "b": 2})")); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp index 620feb3d3e..0dda07aa4e 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp @@ -27,6 +27,7 @@ #include #include +#include #include namespace { @@ -51,6 +52,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp index d10c24aaef..ce0052cd62 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp @@ -27,6 +27,7 @@ #include #include +#include #include namespace { @@ -69,6 +70,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp index 0b385fac88..b222e74751 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp @@ -28,6 +28,7 @@ #include #include +#include #include namespace { @@ -72,6 +73,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp index 2378213ad9..e40f93b134 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -38,6 +39,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp index 4700ed6dd8..061d90f9bc 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -37,6 +38,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp index 8f8c33d21e..199a2acfa8 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp @@ -28,6 +28,7 @@ #include #include +#include #include namespace { @@ -54,6 +55,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp index ec4b949c7b..89e0f5d261 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp @@ -30,6 +30,7 @@ #include #include +#include #include namespace { @@ -74,6 +75,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp index 82898813f2..60cd056683 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp @@ -29,6 +29,7 @@ #include #include +#include #include namespace { @@ -73,6 +74,6 @@ void example(bsoncxx::stdx::string_view big_string) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(examples::big_string().view()); } diff --git a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp index 8a0658ea92..58db0029bc 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -36,6 +37,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp index c8b544290e..2341d8af43 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp @@ -19,6 +19,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp index 1aa2645c56..c898196493 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp @@ -22,6 +22,7 @@ #include #include +#include #include namespace { @@ -58,6 +59,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp index 830fafd7dc..31e6ae1048 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -51,6 +52,6 @@ void example(bsoncxx::array::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(bsoncxx::from_json(R"({"v": [1]})")["v"].get_array().value[0]); } diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp index eb13cfe9cf..59cb680a64 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp @@ -24,6 +24,7 @@ #include #include +#include #include namespace { @@ -60,6 +61,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp index c36e1a5be1..4ba44c42b7 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp @@ -20,6 +20,7 @@ #include #include +#include #include namespace { @@ -51,7 +52,7 @@ void example(bsoncxx::document::element e) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { const auto doc = bsoncxx::from_json(R"({"x": 1})"); example(doc["x"]); diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp index 01da648afd..e0632892d7 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp @@ -23,6 +23,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp index 4157e1189f..36fdee592a 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp @@ -23,6 +23,7 @@ #include #include +#include #include namespace { @@ -49,6 +50,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp index e3f7e1ae1c..67c3222cdd 100644 --- a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp @@ -17,6 +17,7 @@ #include +#include #include namespace { @@ -100,6 +101,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/decimal128/errors.cpp b/examples/api/bsoncxx/examples/decimal128/errors.cpp index a2fd321d22..edc9f806f8 100644 --- a/examples/api/bsoncxx/examples/decimal128/errors.cpp +++ b/examples/api/bsoncxx/examples/decimal128/errors.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -36,6 +37,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main(void) { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp index 66f9033fa9..bee7248e2c 100644 --- a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -17,6 +17,7 @@ #include +#include #include namespace { @@ -81,6 +82,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/oid/basic_usage.cpp b/examples/api/bsoncxx/examples/oid/basic_usage.cpp index 297121c158..542a9dc482 100644 --- a/examples/api/bsoncxx/examples/oid/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/oid/basic_usage.cpp @@ -19,6 +19,7 @@ #include +#include #include namespace { @@ -79,6 +80,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/oid/errors.cpp b/examples/api/bsoncxx/examples/oid/errors.cpp index fd4a3d35f4..950428745d 100644 --- a/examples/api/bsoncxx/examples/oid/errors.cpp +++ b/examples/api/bsoncxx/examples/oid/errors.cpp @@ -18,6 +18,7 @@ #include #include +#include #include namespace { @@ -46,6 +47,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/validation/basic_usage.cpp b/examples/api/bsoncxx/examples/validation/basic_usage.cpp index 27f019f937..dd91fb4cb5 100644 --- a/examples/api/bsoncxx/examples/validation/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/validation/basic_usage.cpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace { @@ -70,6 +71,6 @@ void example() { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { example(); } diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp index fdf11d624b..89d7f711a0 100644 --- a/examples/api/bsoncxx/examples/validation/validator.cpp +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -17,6 +17,7 @@ #include +#include #include namespace { @@ -85,7 +86,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { } // namespace -int EXAMPLES_CDECL main() { +RUNNER_REGISTER_COMPONENT() { // clang-format off std::uint8_t bytes[] = { // Total Bytes: 53 diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp new file mode 100644 index 0000000000..cd2e1a3a05 --- /dev/null +++ b/examples/api/runner.cpp @@ -0,0 +1,137 @@ +// Copyright 2009-present 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. + +// Disable macro definition check: this is not a component. +#define EXAMPLES_COMPONENT_NAME + +#include + +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +class runner_type { + public: + using fn_type = void (*)(); + + private: + std::vector fns; + std::minstd_rand::result_type seed = 0u; + std::minstd_rand gen; + unsigned int jobs = 0; + + public: + void add(fn_type fn) { + fns.push_back(fn); + } + + void set_seed(std::minstd_rand::result_type seed) { + this->seed = seed; + } + + void set_jobs(unsigned int jobs) { + if (jobs <= 0) { + this->jobs = std::thread::hardware_concurrency(); + } else { + this->jobs = jobs; + } + } + + int run() { + assert(jobs > 0); + + std::cout << "seed: " << seed << std::endl; + + gen.seed(seed); + + // Prevent ordering dependencies across examples. + std::shuffle(fns.begin(), fns.end(), gen); + + std::queue threads; + + // Rudimentary job scheduler. + for (auto fn : fns) { + while (threads.size() >= jobs) { + threads.front().join(); + threads.pop(); + } + + threads.emplace(fn); + } + + while (!threads.empty()) { + threads.front().join(); + threads.pop(); + } + + return 0; + } +}; + +runner_type runner; + +} // namespace + +void runner_register_fn(void (*fn)()) { + runner.add(fn); +} + +int EXAMPLES_CDECL main(int argc, char** argv) { + // Permit using a custom seed for reproducibility. + if (argc > 1) { + char* const seed_str = argv[1]; + char* end = nullptr; + + runner.set_seed( + static_cast(std::strtoul(seed_str, &end, 10))); + + if (static_cast(end - argv[1]) != std::strlen(seed_str)) { + std::cerr << "invalid seed string" << std::endl; + return 1; + } + } else { + runner.set_seed(static_cast(std::random_device()())); + } + + // Permit using a custom seed for reproducibility. + if (argc > 2) { + char* const jobs_str = argv[2]; + + char* end = nullptr; + + runner.set_jobs(static_cast(std::strtoul(jobs_str, &end, 10))); + + if (static_cast(end - argv[1]) != std::strlen(jobs_str)) { + std::cerr << "invalid jobs string" << std::endl; + return 1; + } + } else { + runner.set_jobs(0); + } + + return runner.run(); +} diff --git a/examples/api/runner.hh b/examples/api/runner.hh new file mode 100644 index 0000000000..f2b3164c52 --- /dev/null +++ b/examples/api/runner.hh @@ -0,0 +1,30 @@ +// Copyright 2009-present 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. + +#pragma once + +#include + +void runner_register_fn(void (*fn)()); + +#if !defined(EXAMPLES_COMPONENT_NAME) +#error "EXAMPLES_COMPONENT_NAME is not defined!" +#endif // !defined(EXAMPLES_COMPONENT_NAME) + +#define RUNNER_REGISTER_COMPONENT() \ + static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void); \ + static int EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _registerator) = \ + ([] { ::runner_register_fn(&EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)); }(), \ + 0); \ + static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void) diff --git a/examples/macros.hh b/examples/macros.hh index 2b477ea889..df8e7f6788 100644 --- a/examples/macros.hh +++ b/examples/macros.hh @@ -19,3 +19,6 @@ #else #define EXAMPLES_CDECL #endif + +#define EXAMPLES_CONCAT(a, b) EXAMPLES_CONCAT_1(a, b) +#define EXAMPLES_CONCAT_1(a, b) a##b From 685506d2090cc4f8eb86729cabdd5fb23a47d1ba Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 13 Sep 2024 11:01:14 -0500 Subject: [PATCH 11/26] Ensure assertions in examples are always evaluated --- .../access_array/algorithms.cpp | 9 +- .../bson_documents/access_array/basic.cpp | 14 +-- .../bson_documents/access_array/find.cpp | 12 +- .../bson_documents/access_array/iterators.cpp | 22 ++-- .../bson_documents/access_array/subscript.cpp | 10 +- .../bson_documents/access_doc/algorithms.cpp | 9 +- .../bson_documents/access_doc/basic.cpp | 14 +-- .../bson_documents/access_doc/find.cpp | 12 +- .../bson_documents/access_doc/iterators.cpp | 22 ++-- .../bson_documents/access_doc/subscript.cpp | 10 +- .../create_array/builder_append.cpp | 7 +- .../create_array/builder_basic.cpp | 7 +- .../create_array/builder_bson_type.cpp | 14 +-- .../create_array/builder_bson_value.cpp | 13 +- .../create_array/builder_make_document.cpp | 7 +- .../create_array/builder_raw_value.cpp | 5 +- .../create_array/builder_raw_view.cpp | 5 +- .../create_array/builder_reset.cpp | 9 +- .../create_array/builder_sub_array.cpp | 9 +- .../create_array/builder_sub_document.cpp | 4 +- .../create_array/builder_value_type.cpp | 13 +- .../create_array/json_basic.cpp | 8 +- .../create_array/json_extended.cpp | 8 +- .../create_array/json_sub_array.cpp | 6 +- .../create_array/json_sub_document.cpp | 4 +- .../bson_documents/create_array/json_udl.cpp | 4 +- .../create_doc/builder_append.cpp | 7 +- .../create_doc/builder_basic.cpp | 7 +- .../create_doc/builder_bson_type.cpp | 13 +- .../create_doc/builder_bson_value.cpp | 13 +- .../create_doc/builder_make_document.cpp | 7 +- .../create_doc/builder_raw_value.cpp | 3 +- .../create_doc/builder_raw_view.cpp | 3 +- .../create_doc/builder_reset.cpp | 9 +- .../create_doc/builder_sub_array.cpp | 9 +- .../create_doc/builder_sub_document.cpp | 4 +- .../create_doc/builder_value_type.cpp | 13 +- .../bson_documents/create_doc/json_basic.cpp | 8 +- .../create_doc/json_extended.cpp | 8 +- .../create_doc/json_sub_array.cpp | 6 +- .../create_doc/json_sub_document.cpp | 4 +- .../bson_documents/create_doc/json_udl.cpp | 4 +- .../bson_documents/elements/arr_multi.cpp | 20 ++- .../bson_documents/elements/arr_single.cpp | 10 +- .../elements/cmp_bson_value.cpp | 10 +- .../bson_documents/elements/cmp_type.cpp | 9 +- .../bson_documents/elements/doc_multi.cpp | 20 ++- .../bson_documents/elements/doc_single.cpp | 10 +- .../examples/bson_documents/json/array.cpp | 9 +- .../examples/bson_documents/json/document.cpp | 9 +- .../bson_documents/values/arr_value.cpp | 12 +- .../bson_documents/values/arr_view.cpp | 16 ++- .../bson_documents/values/bson_type_value.cpp | 18 ++- .../bson_documents/values/bson_type_view.cpp | 18 ++- .../bson_documents/values/doc_value.cpp | 12 +- .../bson_documents/values/doc_view.cpp | 16 ++- .../bson_documents/values/make_value.cpp | 13 +- .../bson_documents/values/value_type.cpp | 17 ++- .../bson_errors/access_arr_iter_end.cpp | 10 +- .../bson_errors/access_arr_iter_invalid.cpp | 5 +- .../bson_errors/access_arr_key_missing.cpp | 5 +- .../bson_errors/access_doc_iter_end.cpp | 10 +- .../bson_errors/access_doc_iter_invalid.cpp | 5 +- .../bson_errors/access_doc_key_missing.cpp | 8 +- .../bson_errors/create_arr_append.cpp | 7 +- .../create_arr_append_sub_array.cpp | 13 +- .../create_arr_append_sub_document.cpp | 13 +- .../create_bson_value_invalid_element.cpp | 6 +- .../create_bson_value_invalid_type.cpp | 6 +- .../bson_errors/create_doc_append.cpp | 7 +- .../create_doc_append_sub_array.cpp | 13 +- .../create_doc_append_sub_document.cpp | 13 +- .../examples/bson_errors/create_json.cpp | 6 +- .../query_bson_value_invalid_type.cpp | 14 +-- .../bson_errors/query_element_arr_invalid.cpp | 12 +- .../query_element_arr_invalid_type.cpp | 16 ++- .../bson_errors/query_element_doc_invalid.cpp | 11 +- .../query_element_doc_invalid_type.cpp | 16 ++- .../bson_errors/to_json_invalid_arr.cpp | 5 +- .../bson_errors/to_json_invalid_doc.cpp | 5 +- .../examples/decimal128/basic_usage.cpp | 119 +++++++++--------- .../bsoncxx/examples/decimal128/errors.cpp | 6 +- .../examples/decimal128/from_bytes.cpp | 35 +++--- .../api/bsoncxx/examples/oid/basic_usage.cpp | 33 +++-- examples/api/bsoncxx/examples/oid/errors.cpp | 10 +- .../examples/validation/basic_usage.cpp | 19 ++- .../bsoncxx/examples/validation/validator.cpp | 17 ++- examples/api/runner.cpp | 3 +- examples/macros.hh | 13 ++ 89 files changed, 468 insertions(+), 587 deletions(-) diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp index 6653fbaef2..e0897f7151 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include @@ -29,7 +28,7 @@ namespace { // [Example] // [1, 2.0, "three"] void example(bsoncxx::array::view arr) { - assert(std::distance(arr.begin(), arr.end()) == 3); + ASSERT(std::distance(arr.begin(), arr.end()) == 3); std::vector elements; @@ -38,9 +37,9 @@ void example(bsoncxx::array::view arr) { return e.key().compare("0") == 0 || e.type() == bsoncxx::type::k_string; }); - assert(elements.size() == 2u); - assert(elements[0].key().compare("0") == 0); - assert(elements[1].key().compare("2") == 0); + ASSERT(elements.size() == 2u); + ASSERT(elements[0].key().compare("0") == 0); + ASSERT(elements[1].key().compare("2") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp index fdfacbfd07..eb0dad5a65 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -30,16 +28,16 @@ void example(bsoncxx::array::view arr) { for (bsoncxx::array::element e : arr) { switch (e.type()) { case bsoncxx::type::k_int32: - assert(e.key().compare("0") == 0); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("0") == 0); + ASSERT(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - assert(e.key().compare("1") == 0); - assert(e.get_double().value == 2.0); + ASSERT(e.key().compare("1") == 0); + ASSERT(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - assert(e.key().compare("2") == 0); - assert(e.get_string().value.compare("three") == 0); + ASSERT(e.key().compare("2") == 0); + ASSERT(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp index 7ec3bd7521..94fbefcbf9 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -26,17 +24,17 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - assert(arr.find(0) == arr.begin()); + ASSERT(arr.find(0) == arr.begin()); { auto iter = arr.find(1); - assert(iter != arr.end()); - assert(iter->key().compare("1") == 0); - assert(iter->get_int32().value == 2); + ASSERT(iter != arr.end()); + ASSERT(iter->key().compare("1") == 0); + ASSERT(iter->get_int32().value == 2); } - assert(arr.find(2) == arr.end()); + ASSERT(arr.find(2) == arr.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp index 97e7f9b4cf..30c3f9532f 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,32 +25,32 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - assert(arr.begin() != arr.end()); + ASSERT(arr.begin() != arr.end()); auto iter = arr.begin(); - assert(iter == arr.begin()); + ASSERT(iter == arr.begin()); { bsoncxx::array::element e = *iter; - assert(e.key().compare("0") == 0); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("0") == 0); + ASSERT(e.get_int32().value == 1); } ++iter; - assert(iter->key().compare("1") == 0); - assert(iter->get_int32().value == 2); + ASSERT(iter->key().compare("1") == 0); + ASSERT(iter->get_int32().value == 2); { auto iter_copy = iter++; - assert(iter_copy != iter); - assert(iter_copy->key().compare("1") == 0); - assert(iter_copy->get_int32() == 2); + ASSERT(iter_copy != iter); + ASSERT(iter_copy->key().compare("1") == 0); + ASSERT(iter_copy->get_int32() == 2); } - assert(iter == arr.end()); + ASSERT(iter == arr.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp index a2ae35c81a..db1bfdab51 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,16 +25,16 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - assert(arr[0]); + ASSERT(arr[0]); { bsoncxx::array::element e = arr[1]; - assert(e.key().compare("1") == 0); - assert(e.get_int32().value == 2); + ASSERT(e.key().compare("1") == 0); + ASSERT(e.get_int32().value == 2); } - assert(!arr[2]); // Invalid element. + ASSERT(!arr[2]); // Invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp index 696b46a699..f24e42a98d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include @@ -30,7 +29,7 @@ namespace { // [Example] // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::view doc) { - assert(std::distance(doc.begin(), doc.end()) == 3); + ASSERT(std::distance(doc.begin(), doc.end()) == 3); std::vector elements; @@ -41,9 +40,9 @@ void example(bsoncxx::document::view doc) { return e.key().compare("a") == 0 || e.type() == bsoncxx::type::k_string; }); - assert(elements.size() == 2u); - assert(elements[0].key().compare("a") == 0); - assert(elements[1].key().compare("c") == 0); + ASSERT(elements.size() == 2u); + ASSERT(elements[0].key().compare("a") == 0); + ASSERT(elements[1].key().compare("c") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp index 382ec8feb0..95aa123912 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -30,16 +28,16 @@ void example(bsoncxx::document::view doc) { for (bsoncxx::document::element e : doc) { switch (e.type()) { case bsoncxx::type::k_int32: - assert(e.key().compare("a") == 0); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("a") == 0); + ASSERT(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - assert(e.key().compare("b") == 0); - assert(e.get_double().value == 2.0); + ASSERT(e.key().compare("b") == 0); + ASSERT(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - assert(e.key().compare("c") == 0); - assert(e.get_string().value.compare("three") == 0); + ASSERT(e.key().compare("c") == 0); + ASSERT(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp index 1713c23263..03afcb3af7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -26,17 +24,17 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - assert(doc.find("a") == doc.begin()); + ASSERT(doc.find("a") == doc.begin()); { auto iter = doc.find("b"); - assert(iter != doc.end()); - assert(iter->key().compare("b") == 0); - assert(iter->get_int32().value == 2); + ASSERT(iter != doc.end()); + ASSERT(iter->key().compare("b") == 0); + ASSERT(iter->get_int32().value == 2); } - assert(doc.find("x") == doc.end()); + ASSERT(doc.find("x") == doc.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp index 8a6c8efbc0..ff8cbcc5ce 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,32 +25,32 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - assert(doc.begin() != doc.end()); + ASSERT(doc.begin() != doc.end()); auto iter = doc.begin(); - assert(iter == doc.begin()); + ASSERT(iter == doc.begin()); { bsoncxx::document::element e = *iter; - assert(e.key().compare("a") == 0); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("a") == 0); + ASSERT(e.get_int32().value == 1); } ++iter; - assert(iter->key().compare("b") == 0); - assert(iter->get_int32().value == 2); + ASSERT(iter->key().compare("b") == 0); + ASSERT(iter->get_int32().value == 2); { auto iter_copy = iter++; - assert(iter_copy != iter); - assert(iter_copy->key().compare("b") == 0); - assert(iter_copy->get_int32() == 2); + ASSERT(iter_copy != iter); + ASSERT(iter_copy->key().compare("b") == 0); + ASSERT(iter_copy->get_int32() == 2); } - assert(iter == doc.end()); + ASSERT(iter == doc.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp index f25c85267b..398558dd52 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,16 +25,16 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - assert(doc["a"]); + ASSERT(doc["a"]); { bsoncxx::document::element e = doc["b"]; - assert(e.key().compare("b") == 0); - assert(e.get_int32().value == 2); + ASSERT(e.key().compare("b") == 0); + ASSERT(e.get_int32().value == 2); } - assert(!doc["c"]); // Invalid element. + ASSERT(!doc["c"]); // Invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp index 10f5cbdde2..ad56ae3c5a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -34,9 +33,9 @@ void example() { bsoncxx::array::value owner = builder.extract(); bsoncxx::array::view arr = owner.view(); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value.compare("three") == 0); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_double().value == 2.0); + ASSERT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp index cd2a859ff1..53ffd5facf 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -32,9 +31,9 @@ void example() { bsoncxx::array::value owner = builder.extract(); bsoncxx::array::view arr = owner.view(); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value.compare("three") == 0); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_double().value == 2.0); + ASSERT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp index c9c3316098..f15f76e480 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,13 +31,13 @@ void example() { bsoncxx::array::value owner = bsoncxx::builder::basic::make_array(a, b, c); bsoncxx::array::view arr = owner.view(); - assert(arr[0].type() == bsoncxx::type::k_int32); - assert(arr[1].type() == bsoncxx::type::k_double); - assert(arr[2].type() == bsoncxx::type::k_string); + ASSERT(arr[0].type() == bsoncxx::type::k_int32); + ASSERT(arr[1].type() == bsoncxx::type::k_double); + ASSERT(arr[2].type() == bsoncxx::type::k_string); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value.compare("three") == 0); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_double().value == 2.0); + ASSERT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp index 9276ce2096..4a66341d01 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -41,13 +40,13 @@ void example() { bsoncxx::builder::basic::make_array(values[0], values[1], values[2]); bsoncxx::array::view arr = owner.view(); - assert(arr[0].type() == bsoncxx::type::k_int32); - assert(arr[1].type() == bsoncxx::type::k_double); - assert(arr[2].type() == bsoncxx::type::k_string); + ASSERT(arr[0].type() == bsoncxx::type::k_int32); + ASSERT(arr[1].type() == bsoncxx::type::k_double); + ASSERT(arr[2].type() == bsoncxx::type::k_string); - assert(arr[0].get_value() == values[0]); - assert(arr[1].get_value() == values[1]); - assert(arr[2].get_value() == values[2]); + ASSERT(arr[0].get_value() == values[0]); + ASSERT(arr[1].get_value() == values[1]); + ASSERT(arr[2].get_value() == values[2]); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp index 3fe7def209..b36ade8f15 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -31,9 +30,9 @@ void example() { bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); bsoncxx::array::view arr = owner.view(); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value.compare("three") == 0); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_double().value == 2.0); + ASSERT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp index bedba93d42..aa7777425d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include @@ -40,8 +39,8 @@ void example(const std::uint8_t* data, std::size_t length) { bsoncxx::array::value owner{raw, length, deleter}; bsoncxx::array::view arr = owner.view(); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_int32().value == 2); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp index 05ecb34004..4c1c194482 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -32,8 +31,8 @@ namespace { void example(const std::uint8_t* data, std::size_t length) { bsoncxx::array::view arr{data, length}; - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_int32().value == 2); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp index db59df57ee..20938e8b48 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -40,11 +39,11 @@ void example() { bsoncxx::array::view a = a_owner.view(); bsoncxx::array::view b = b_owner.view(); - assert(a[0].type() == bsoncxx::type::k_int32); - assert(b[0].type() == bsoncxx::type::k_int64); + ASSERT(a[0].type() == bsoncxx::type::k_int32); + ASSERT(b[0].type() == bsoncxx::type::k_int64); - assert(a[0].get_int32().value == 1); - assert(b[0].get_int64().value == 2); + ASSERT(a[0].get_int32().value == 1); + ASSERT(b[0].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp index 52691e861e..a60c7d17d4 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -32,11 +31,11 @@ void example() { }); bsoncxx::array::view v = owner.view()[0].get_array().value; - assert(v[0].type() == bsoncxx::type::k_int32); - assert(v[1].type() == bsoncxx::type::k_int64); + ASSERT(v[0].type() == bsoncxx::type::k_int32); + ASSERT(v[1].type() == bsoncxx::type::k_int64); - assert(v[0].get_int32().value == 1); - assert(v[1].get_int64().value == 2); + ASSERT(v[0].get_int32().value == 1); + ASSERT(v[1].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp index d1b8f91265..540da1879c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -32,7 +30,7 @@ void example() { [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); }); bsoncxx::document::view v = owner.view()[0].get_document().value; - assert(v["key"].get_string().value.compare("value") == 0); + ASSERT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp index b06f0c9471..d577099be6 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -31,13 +30,13 @@ void example() { bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); bsoncxx::array::view arr = owner.view(); - assert(arr[0].type() == bsoncxx::type::k_int32); - assert(arr[1].type() == bsoncxx::type::k_double); - assert(arr[2].type() == bsoncxx::type::k_string); + ASSERT(arr[0].type() == bsoncxx::type::k_int32); + ASSERT(arr[1].type() == bsoncxx::type::k_double); + ASSERT(arr[2].type() == bsoncxx::type::k_string); - assert(arr[0].get_int32().value == 1); - assert(arr[1].get_double().value == 2.0); - assert(arr[2].get_string().value.compare("three") == 0); + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_double().value == 2.0); + ASSERT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp index ade817dd6b..b52cdfb24b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -31,9 +29,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - assert(doc["0"].get_int32().value == 1); - assert(doc["1"].get_double().value == 2.0); - assert(doc["2"].get_string().value.compare("three") == 0); + ASSERT(doc["0"].get_int32().value == 1); + ASSERT(doc["1"].get_double().value == 2.0); + ASSERT(doc["2"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp index 57c5322148..1cbb9cddb0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -35,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - assert(doc["0"].get_int32().value == 1); - assert(doc["1"].get_int64().value == 2); - assert(doc["2"].get_double().value == 3.0); + ASSERT(doc["0"].get_int32().value == 1); + ASSERT(doc["1"].get_int64().value == 2); + ASSERT(doc["2"].get_double().value == 3.0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp index 3833153d25..ab5f41743a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,8 +31,8 @@ void example() { )"); bsoncxx::array::view sub = owner.view()["0"].get_array().value; - assert(sub[0].get_int32().value == 1); - assert(sub[1].get_int32().value == 2); + ASSERT(sub[0].get_int32().value == 1); + ASSERT(sub[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp index 308ca6463e..4efcb1c9fd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,7 +31,7 @@ void example() { )"); bsoncxx::document::view v = owner.view()["0"].get_document().value; - assert(v["key"].get_string().value.compare("value") == 0); + ASSERT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp index cfbb498b06..544674d790 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include @@ -29,7 +27,7 @@ void example() { bsoncxx::document::value a = R"([1, 2])"_bson; bsoncxx::document::value b = bsoncxx::from_json(R"([1, 2])"); - assert(a == b); + ASSERT(a == b); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp index 239cb2d998..fc7aea9fc2 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -37,9 +36,9 @@ void example() { bsoncxx::document::value owner = builder.extract(); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value.compare("three") == 0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_double().value == 2.0); + ASSERT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp index 28d19c28d7..c523665e4d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -35,9 +34,9 @@ void example() { bsoncxx::document::value owner = builder.extract(); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value.compare("three") == 0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_double().value == 2.0); + ASSERT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp index 006f072409..3beeaa2eeb 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -38,13 +37,13 @@ void example() { bsoncxx::builder::basic::make_document(kvp("a", a), kvp("b", b), kvp("c", c)); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].type() == bsoncxx::type::k_int32); - assert(doc["b"].type() == bsoncxx::type::k_double); - assert(doc["c"].type() == bsoncxx::type::k_string); + ASSERT(doc["a"].type() == bsoncxx::type::k_int32); + ASSERT(doc["b"].type() == bsoncxx::type::k_double); + ASSERT(doc["c"].type() == bsoncxx::type::k_string); - assert(doc["a"].get_int32() == a); - assert(doc["b"].get_double() == b); - assert(doc["c"].get_string() == c); + ASSERT(doc["a"].get_int32() == a); + ASSERT(doc["b"].get_double() == b); + ASSERT(doc["c"].get_string() == c); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp index 989b9be533..eec1f9a05e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -41,13 +40,13 @@ void example() { kvp("a", values[0]), kvp("b", values[1]), kvp("c", values[2])); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].type() == bsoncxx::type::k_int32); - assert(doc["b"].type() == bsoncxx::type::k_double); - assert(doc["c"].type() == bsoncxx::type::k_string); + ASSERT(doc["a"].type() == bsoncxx::type::k_int32); + ASSERT(doc["b"].type() == bsoncxx::type::k_double); + ASSERT(doc["c"].type() == bsoncxx::type::k_string); - assert(doc["a"].get_value() == values[0]); - assert(doc["b"].get_value() == values[1]); - assert(doc["c"].get_value() == values[2]); + ASSERT(doc["a"].get_value() == values[0]); + ASSERT(doc["b"].get_value() == values[1]); + ASSERT(doc["c"].get_value() == values[2]); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp index e466f8e524..36440ddcba 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -34,9 +33,9 @@ void example() { kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value.compare("three") == 0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_double().value == 2.0); + ASSERT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp index a263adb6a0..90d263daa1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include @@ -39,7 +38,7 @@ void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::value owner{raw, length, deleter}; bsoncxx::document::view doc = owner.view(); - assert(doc["key"].get_string().value.compare("value") == 0); + ASSERT(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp index 6e8047e181..0f45aa72c1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -31,7 +30,7 @@ namespace { void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::view doc{data, length}; - assert(doc["key"].get_string().value.compare("value") == 0); + ASSERT(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp index 807d16dc18..a508694604 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -43,11 +42,11 @@ void example() { bsoncxx::document::view a = a_owner.view(); bsoncxx::document::view b = b_owner.view(); - assert(a["v"].type() == bsoncxx::type::k_int32); - assert(b["v"].type() == bsoncxx::type::k_int64); + ASSERT(a["v"].type() == bsoncxx::type::k_int32); + ASSERT(b["v"].type() == bsoncxx::type::k_int64); - assert(a["v"].get_int32().value == 1); - assert(b["v"].get_int64().value == 2); + ASSERT(a["v"].get_int32().value == 1); + ASSERT(b["v"].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp index f1708ef354..3ba49f8888 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -37,11 +36,11 @@ void example() { })); bsoncxx::array::view v = owner.view()["v"].get_array().value; - assert(v[0].type() == bsoncxx::type::k_int32); - assert(v[1].type() == bsoncxx::type::k_int64); + ASSERT(v[0].type() == bsoncxx::type::k_int32); + ASSERT(v[1].type() == bsoncxx::type::k_int64); - assert(v[0].get_int32().value == 1); - assert(v[1].get_int64().value == 2); + ASSERT(v[0].get_int32().value == 1); + ASSERT(v[1].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp index 9bcd6d7dc9..32aa749da1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,7 +31,7 @@ void example() { "v", [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); })); bsoncxx::document::view v = owner.view()["v"].get_document().value; - assert(v["key"].get_string().value.compare("value") == 0); + ASSERT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp index 965d925dbe..22398aa81a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -34,13 +33,13 @@ void example() { kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].type() == bsoncxx::type::k_int32); - assert(doc["b"].type() == bsoncxx::type::k_double); - assert(doc["c"].type() == bsoncxx::type::k_string); + ASSERT(doc["a"].type() == bsoncxx::type::k_int32); + ASSERT(doc["b"].type() == bsoncxx::type::k_double); + ASSERT(doc["c"].type() == bsoncxx::type::k_string); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value.compare("three") == 0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_double().value == 2.0); + ASSERT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp index 2ba21dd2e1..c9eccc453e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -35,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_double().value == 2.0); - assert(doc["c"].get_string().value.compare("three") == 0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_double().value == 2.0); + ASSERT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp index a7910b0667..0cad361a20 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -35,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - assert(doc["a"].get_int32().value == 1); - assert(doc["b"].get_int64().value == 2); - assert(doc["c"].get_double().value == 3.0); + ASSERT(doc["a"].get_int32().value == 1); + ASSERT(doc["b"].get_int64().value == 2); + ASSERT(doc["c"].get_double().value == 3.0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp index 2cbfdf61a7..07c4e74d24 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,8 +31,8 @@ void example() { )"); bsoncxx::array::view sub = owner.view()["v"].get_array().value; - assert(sub[0].get_int32().value == 1); - assert(sub[1].get_int32().value == 2); + ASSERT(sub[0].get_int32().value == 1); + ASSERT(sub[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp index ff40f69422..3296d7c2c0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -33,7 +31,7 @@ void example() { )"); bsoncxx::document::view v = doc["v"].get_document().value; - assert(v["key"].get_string().value.compare("value") == 0); + ASSERT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp index 696c842909..28753a48fd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include @@ -29,7 +27,7 @@ void example() { bsoncxx::document::value a = R"({"key": "value"})"_bson; bsoncxx::document::value b = bsoncxx::from_json(R"({"key": "value"})"); - assert(a == b); + ASSERT(a == b); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp index 1b966ce44a..1008a846ff 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,32 +26,32 @@ namespace { void example(bsoncxx::array::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - assert(e.key().compare("0") == 0); + ASSERT(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - assert(v.type_id == bsoncxx::type::k_int32); - assert(v.value == 1); + ASSERT(v.type_id == bsoncxx::type::k_int32); + ASSERT(v.value == 1); break; } case bsoncxx::type::k_double: { - assert(e.key().compare("1") == 0); + ASSERT(e.key().compare("1") == 0); bsoncxx::types::b_double v = e.get_double(); - assert(v.type_id == bsoncxx::type::k_double); - assert(v.value == 2.0); + ASSERT(v.type_id == bsoncxx::type::k_double); + ASSERT(v.value == 2.0); break; } case bsoncxx::type::k_string: { - assert(e.key().compare("2") == 0); + ASSERT(e.key().compare("2") == 0); bsoncxx::types::b_string v = e.get_string(); - assert(v.type_id == bsoncxx::type::k_string); - assert(v.value.compare("three") == 0); + ASSERT(v.type_id == bsoncxx::type::k_string); + ASSERT(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp index afb0fb2ff9..fce3eb4b4c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,14 +25,14 @@ namespace { // [1, 2.0, "three"] void example(bsoncxx::array::element e) { if (e.type() == bsoncxx::type::k_int32) { - assert(e.key().compare("0") == 0); + ASSERT(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - assert(v.type_id == bsoncxx::type::k_int32); - assert(v.value == 1); + ASSERT(v.type_id == bsoncxx::type::k_int32); + ASSERT(v.value == 1); } else { - assert(e.key().compare("0") != 0); + ASSERT(e.key().compare("0") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp index b6a90cf19c..44c2d9dba6 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -31,18 +29,18 @@ void example(bsoncxx::document::element e) { bsoncxx::types::b_int64 b{2}; if (e.get_value() == a) { - assert(e.key().compare("a") == 0); + ASSERT(e.key().compare("a") == 0); } else if (e.get_value() == b) { - assert(e.key().compare("b") == 0); + ASSERT(e.key().compare("b") == 0); } bsoncxx::types::bson_value::view va{a}; bsoncxx::types::bson_value::view vb{b}; if (e == va) { - assert(e.key().compare("a") == 0); + ASSERT(e.key().compare("a") == 0); } else if (e == vb) { - assert(e.key().compare("b") == 0); + ASSERT(e.key().compare("b") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp index 46d4abc1ef..29c7a0b1ea 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -31,11 +30,11 @@ void example(bsoncxx::document::element e) { std::int64_t b{2}; if (e.type() == bsoncxx::type::k_int32) { - assert(e.key().compare("a") == 0); - assert(e.get_int32().value == a); + ASSERT(e.key().compare("a") == 0); + ASSERT(e.get_int32().value == a); } else if (e.type() == bsoncxx::type::k_int64) { - assert(e.key().compare("b") == 0); - assert(e.get_int64().value == b); + ASSERT(e.key().compare("b") == 0); + ASSERT(e.get_int64().value == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp index 54605aa28a..fe5c323736 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,32 +26,32 @@ namespace { void example(bsoncxx::document::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - assert(e.key().compare("a") == 0); + ASSERT(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - assert(v.type_id == bsoncxx::type::k_int32); - assert(v.value == 1); + ASSERT(v.type_id == bsoncxx::type::k_int32); + ASSERT(v.value == 1); break; } case bsoncxx::type::k_double: { - assert(e.key().compare("b") == 0); + ASSERT(e.key().compare("b") == 0); bsoncxx::types::b_double v = e.get_double(); - assert(v.type_id == bsoncxx::type::k_double); - assert(v.value == 2.0); + ASSERT(v.type_id == bsoncxx::type::k_double); + ASSERT(v.value == 2.0); break; } case bsoncxx::type::k_string: { - assert(e.key().compare("c") == 0); + ASSERT(e.key().compare("c") == 0); bsoncxx::types::b_string v = e.get_string(); - assert(v.type_id == bsoncxx::type::k_string); - assert(v.value.compare("three") == 0); + ASSERT(v.type_id == bsoncxx::type::k_string); + ASSERT(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp index 9c98a804f7..c8bf3ac940 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,14 +25,14 @@ namespace { // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::element e) { if (e.type() == bsoncxx::type::k_int32) { - assert(e.key().compare("a") == 0); + ASSERT(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - assert(v.type_id == bsoncxx::type::k_int32); - assert(v.value == 1); + ASSERT(v.type_id == bsoncxx::type::k_int32); + ASSERT(v.value == 1); } else { - assert(e.key().compare("a") != 0); + ASSERT(e.key().compare("a") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp index c6fc8787cb..f74104ecb4 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -55,7 +54,7 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_canonical); - assert( + ASSERT( json == R"([ { "$numberInt" : "1" }, { "$numberLong" : "2" }, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); } @@ -73,7 +72,7 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_relaxed); - assert(json == R"([ 1, 2, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); + ASSERT(json == R"([ 1, 2, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); } { @@ -87,14 +86,14 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr); - assert(json == R"([ 1, 2, { "$binary" : "dGhyZWU=", "$type" : "00" } ])"); + ASSERT(json == R"([ 1, 2, { "$binary" : "dGhyZWU=", "$type" : "00" } ])"); } { std::string a = bsoncxx::to_json(arr); std::string b = bsoncxx::to_json(arr, ExtendedJsonMode::k_legacy); - assert(a == b); + ASSERT(a == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp index b9387b0e28..d7055e6a98 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -57,7 +56,7 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_canonical); - assert( + ASSERT( json == R"({ "a" : { "$numberInt" : "1" }, "b" : { "$numberLong" : "2" }, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); } @@ -75,7 +74,7 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_relaxed); - assert( + ASSERT( json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); } @@ -91,14 +90,14 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc); - assert(json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : "dGhyZWU=", "$type" : "00" } })"); + ASSERT(json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : "dGhyZWU=", "$type" : "00" } })"); } { std::string a = bsoncxx::to_json(doc); std::string b = bsoncxx::to_json(doc, ExtendedJsonMode::k_legacy); - assert(a == b); + ASSERT(a == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp index 078bd4f28c..979bb557a0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,19 +25,19 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - assert(v.view().type() == bsoncxx::type::k_null); - assert(v.view().get_null() == bsoncxx::types::b_null{}); + ASSERT(v.view().type() == bsoncxx::type::k_null); + ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::from_json(R"({"v": ["value"]})") // Temporary object. ["v"] .get_owning_value(); // Copy: no dangling. - assert(v.view().type() == bsoncxx::type::k_array); + ASSERT(v.view().type() == bsoncxx::type::k_array); v = v.view().get_array().value[0].get_string(); // Copy: no dangling. - assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value.compare("value") == 0); + ASSERT(v.view().type() == bsoncxx::type::k_string); + ASSERT(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp index 844f403411..5af43844fc 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,20 +26,20 @@ namespace { // [1, 2.0, "three"] void example(bsoncxx::array::element e) { bsoncxx::types::bson_value::view v = e.get_value(); - assert(v.type() == e.type()); + ASSERT(v.type() == e.type()); switch (v.type()) { case bsoncxx::type::k_int32: - assert(e.key().compare("0") == 0); - assert(v.get_int32() == e.get_int32()); + ASSERT(e.key().compare("0") == 0); + ASSERT(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - assert(e.key().compare("1") == 0); - assert(v.get_double() == e.get_double()); + ASSERT(e.key().compare("1") == 0); + ASSERT(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - assert(e.key().compare("2") == 0); - assert(v.get_string() == e.get_string()); + ASSERT(e.key().compare("2") == 0); + ASSERT(v.get_string() == e.get_string()); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp index 607372bffe..51ce072c61 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include @@ -26,20 +24,20 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - assert(v.view().type() == bsoncxx::type::k_null); - assert(v.view().get_null() == bsoncxx::types::b_null{}); + ASSERT(v.view().type() == bsoncxx::type::k_null); + ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::types::b_int32{1}; - assert(v.view().type() == bsoncxx::type::k_int32); - assert(v.view().get_int32().value == 1); + ASSERT(v.view().type() == bsoncxx::type::k_int32); + ASSERT(v.view().get_int32().value == 1); v = bsoncxx::types::b_double{2.0}; - assert(v.view().type() == bsoncxx::type::k_double); - assert(v.view().get_double().value == 2.0); + ASSERT(v.view().type() == bsoncxx::type::k_double); + ASSERT(v.view().get_double().value == 2.0); v = bsoncxx::types::b_string{"three"}; - assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value.compare("three") == 0); + ASSERT(v.view().type() == bsoncxx::type::k_string); + ASSERT(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp index 39bea3ccfd..1d66ac44ee 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include @@ -31,20 +29,20 @@ void example() { bsoncxx::types::b_string v2{"three"}; view_type v; - assert(v.type() == bsoncxx::type::k_null); - assert(v.get_null() == bsoncxx::types::b_null{}); + ASSERT(v.type() == bsoncxx::type::k_null); + ASSERT(v.get_null() == bsoncxx::types::b_null{}); v = view_type(v0); - assert(v.type() == v0.type_id); - assert(v.get_int32() == v0); + ASSERT(v.type() == v0.type_id); + ASSERT(v.get_int32() == v0); v = view_type(v1); - assert(v.type() == v1.type_id); - assert(v.get_double() == v1); + ASSERT(v.type() == v1.type_id); + ASSERT(v.get_double() == v1); v = view_type(v2); - assert(v.type() == v2.type_id); - assert(v.get_string() == v2); + ASSERT(v.type() == v2.type_id); + ASSERT(v.get_string() == v2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp index 89e00c50cd..ead44f8128 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,19 +25,19 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - assert(v.view().type() == bsoncxx::type::k_null); - assert(v.view().get_null() == bsoncxx::types::b_null{}); + ASSERT(v.view().type() == bsoncxx::type::k_null); + ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::from_json(R"({"v": {"key": "value"}})") // Temporary object. ["v"] .get_owning_value(); // Copy: no dangling. - assert(v.view().type() == bsoncxx::type::k_document); + ASSERT(v.view().type() == bsoncxx::type::k_document); v = v.view().get_document().value["key"].get_string(); // Copy: no dangling. - assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value.compare("value") == 0); + ASSERT(v.view().type() == bsoncxx::type::k_string); + ASSERT(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp index 4401c404f2..362c233f38 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,20 +26,20 @@ namespace { // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::element e) { bsoncxx::types::bson_value::view v = e.get_value(); - assert(v.type() == e.type()); + ASSERT(v.type() == e.type()); switch (v.type()) { case bsoncxx::type::k_int32: - assert(e.key().compare("a") == 0); - assert(v.get_int32() == e.get_int32()); + ASSERT(e.key().compare("a") == 0); + ASSERT(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - assert(e.key().compare("b") == 0); - assert(v.get_double() == e.get_double()); + ASSERT(e.key().compare("b") == 0); + ASSERT(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - assert(e.key().compare("c") == 0); - assert(v.get_string() == e.get_string()); + ASSERT(e.key().compare("c") == 0); + ASSERT(v.get_string() == e.get_string()); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp index bee1e55bea..f2837d5e42 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -34,24 +33,24 @@ void example() { value_type owner = bsoncxx::types::bson_value::make_value(1); view_type v = owner.view(); - assert(v.type() == bsoncxx::type::k_int32); - assert(v.get_int32().value == 1); + ASSERT(v.type() == bsoncxx::type::k_int32); + ASSERT(v.get_int32().value == 1); } { value_type owner = bsoncxx::types::bson_value::make_value(bsoncxx::types::b_int64{2}); view_type v = owner.view(); - assert(v.type() == bsoncxx::type::k_int64); - assert(v.get_int64().value == 2); + ASSERT(v.type() == bsoncxx::type::k_int64); + ASSERT(v.get_int64().value == 2); } { value_type owner = bsoncxx::types::bson_value::make_value(std::string("three")); view_type v = owner.view(); - assert(v.type() == bsoncxx::type::k_string); - assert(v.get_string().value.compare("three") == 0); + ASSERT(v.type() == bsoncxx::type::k_string); + ASSERT(v.get_string().value.compare("three") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp index 66889204d7..34f17aaa18 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -27,23 +26,23 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - assert(v.view().type() == bsoncxx::type::k_null); - assert(v.view().get_null() == bsoncxx::types::b_null{}); + ASSERT(v.view().type() == bsoncxx::type::k_null); + ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); v = std::int32_t{1}; - assert(v.view().type() == bsoncxx::type::k_int32); - assert(v.view().get_int32().value == 1); + ASSERT(v.view().type() == bsoncxx::type::k_int32); + ASSERT(v.view().get_int32().value == 1); v = 2.0; - assert(v.view().type() == bsoncxx::type::k_double); - assert(v.view().get_double().value == 2.0); + ASSERT(v.view().type() == bsoncxx::type::k_double); + ASSERT(v.view().get_double().value == 2.0); v = std::string("three"); - assert(v.view().type() == bsoncxx::type::k_string); - assert(v.view().get_string().value.compare("three") == 0); + ASSERT(v.view().type() == bsoncxx::type::k_string); + ASSERT(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp index fc3854df50..a6d37f260e 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,7 +25,7 @@ namespace { // [Example] // [1, 2, 3] void example(bsoncxx::array::view arr) { - assert(arr.begin() != arr.end()); + ASSERT(arr.begin() != arr.end()); auto iter = arr.begin(); // 1 @@ -35,15 +33,15 @@ void example(bsoncxx::array::view arr) { ++iter; // 3 ++iter; // End iterator. - assert(iter == arr.end()); + ASSERT(iter == arr.end()); ++iter; // DO NOT DO THIS - assert(iter == arr.end()); // Incrementing an end iterator results in an end iterator. + ASSERT(iter == arr.end()); // Incrementing an end iterator results in an end iterator. bsoncxx::array::element e = *iter; // DO NOT DO THIS - assert(!e); // An end iterator returns an invalid element. + ASSERT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp index 137279a464..92d5ef7f24 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -35,11 +34,11 @@ void example() { auto iter = arr.begin(); - assert(iter == arr.end()); // An invalid BSON document returns an end iterator. + ASSERT(iter == arr.end()); // An invalid BSON document returns an end iterator. bsoncxx::array::element e = *iter; // DO NOT DO THIS - assert(!e); // An end iterator returns an invalid element. + ASSERT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp index 0a6836b490..99f8749e35 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include @@ -28,11 +27,11 @@ namespace { // [Example] // [1, 2, 3] void example(bsoncxx::array::view arr) { - assert(std::distance(arr.begin(), arr.end()) == 3); + ASSERT(std::distance(arr.begin(), arr.end()) == 3); bsoncxx::array::element e = arr[3]; - assert(!e); // A missing element is represented as an invalid element. + ASSERT(!e); // A missing element is represented as an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp index e403c1047e..27f235a654 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,22 +25,22 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - assert(doc.begin() != doc.end()); + ASSERT(doc.begin() != doc.end()); auto iter = doc.begin(); // "a": 1 ++iter; // "b": 2 ++iter; // End iterator. - assert(iter == doc.end()); + ASSERT(iter == doc.end()); ++iter; // DO NOT DO THIS - assert(iter == doc.end()); // Incrementing an end iterator results in an end iterator. + ASSERT(iter == doc.end()); // Incrementing an end iterator results in an end iterator. bsoncxx::document::element e = *iter; // DO NOT DO THIS - assert(!e); // An end iterator returns an invalid element. + ASSERT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp index 2f05d8e85a..5079e48ee1 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -36,11 +35,11 @@ void example() { auto iter = doc.begin(); - assert(iter == doc.end()); // An invalid BSON document returns an end iterator. + ASSERT(iter == doc.end()); // An invalid BSON document returns an end iterator. bsoncxx::document::element e = *iter; // DO NOT DO THIS - assert(!e); // An end iterator returns an invalid element. + ASSERT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp index ce0e0323bb..e7a3db6268 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,12 +25,12 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - assert(doc["a"]); - assert(doc["b"]); + ASSERT(doc["a"]); + ASSERT(doc["b"]); bsoncxx::document::element e = doc["c"]; - assert(!e); // A missing element is represented as an invalid element. + ASSERT(!e); // A missing element is represented as an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp index 0dda07aa4e..48e2be60f6 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -41,12 +40,12 @@ void example(bsoncxx::stdx::string_view big_string) { try { builder.append(big_string); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp index ce0052cd62..7f8c019919 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -43,28 +42,28 @@ void example(bsoncxx::stdx::string_view big_string) { arr.append(big_string); // Throws. }); - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - assert(builder.view().empty()); + ASSERT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_array{original.view()}); - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp index b222e74751..acc2ddb3d1 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -46,28 +45,28 @@ void example(bsoncxx::stdx::string_view big_string) { doc.append(kvp("too big", big_string)); // Throws. }); - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - assert(builder.view().empty()); + ASSERT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_array{original.view()}); - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp index e40f93b134..b47eac9c3a 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -30,9 +28,9 @@ void example() { bsoncxx::document::element e; bsoncxx::types::bson_value::view v = e.get_value(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unset_element); + ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp index 061d90f9bc..c02b75331d 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -29,9 +27,9 @@ void example() { try { bsoncxx::types::bson_value::value value{bsoncxx::type::k_null}; // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id); + ASSERT(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp index 199a2acfa8..cd97ea327b 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -44,12 +43,12 @@ void example(bsoncxx::stdx::string_view big_string) { try { builder.append(kvp("too big", big_string)); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp index 89e0f5d261..8e5ea16e21 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -48,28 +47,28 @@ void example(bsoncxx::stdx::string_view big_string) { arr.append(big_string); // Throws. })); - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - assert(builder.view().empty()); + ASSERT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_doc{original.view()}); - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp index 60cd056683..e45ebb5cd0 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp @@ -16,7 +16,6 @@ // -#include #include #include #include @@ -47,28 +46,28 @@ void example(bsoncxx::stdx::string_view big_string) { doc.append(kvp("too big", big_string)); // Throws. })); - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_cannot_append_string); + ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - assert(builder.view().empty()); + ASSERT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_doc{original.view()}); - assert(builder.view() == original.view()); + ASSERT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp index 58db0029bc..3621351f5e 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,9 +26,9 @@ void example() { try { const auto doc = bsoncxx::from_json(R"(invalid json)"); - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_json_parse_failure); + ASSERT(ex.code() == bsoncxx::error_code::k_json_parse_failure); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp index 2341d8af43..b89f67d7ab 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -27,23 +25,23 @@ namespace { // [Example] void example() { bsoncxx::types::bson_value::view v; - assert(v.type() == bsoncxx::type::k_null); - assert(v.get_null() == bsoncxx::types::b_null{}); + ASSERT(v.type() == bsoncxx::type::k_null); + ASSERT(v.get_null() == bsoncxx::types::b_null{}); try { bsoncxx::types::b_int32 i = v.get_int32(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32); } try { bsoncxx::types::b_int64 i = v.get_int64(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp index c898196493..a1dd6a6af0 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -37,22 +35,22 @@ void example() { bsoncxx::array::element e = arr[0]; - assert(!e); // An invalid BSON array returns an invalid element. + ASSERT(!e); // An invalid BSON array returns an invalid element. try { bsoncxx::stdx::string_view key = e.key(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unset_element); + ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); } try { bsoncxx::type type = e.type(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unset_element); + ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp index 31e6ae1048..3ccb657a74 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,24 +26,24 @@ namespace { // [Example] // [1] void example(bsoncxx::array::element e) { - assert(e.key().compare("0") == 0); - assert(e.type() == bsoncxx::type::k_int32); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("0") == 0); + ASSERT(e.type() == bsoncxx::type::k_int32); + ASSERT(e.get_int32().value == 1); try { bsoncxx::types::b_double d = e.get_double(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); } try { bsoncxx::types::b_string str = e.get_string(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp index 59cb680a64..987e607833 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -39,22 +38,22 @@ void example() { bsoncxx::document::element e = doc["x"]; - assert(!e); // An invalid BSON document returns an invalid element. + ASSERT(!e); // An invalid BSON document returns an invalid element. try { bsoncxx::stdx::string_view key = e.key(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unset_element); + ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); } try { bsoncxx::type type = e.type(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_unset_element); + ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp index 4ba44c42b7..58b8abd72e 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,24 +26,24 @@ namespace { // [Example] // {"x": 1} void example(bsoncxx::document::element e) { - assert(e.key().compare("x") == 0); - assert(e.type() == bsoncxx::type::k_int32); - assert(e.get_int32().value == 1); + ASSERT(e.key().compare("x") == 0); + ASSERT(e.type() == bsoncxx::type::k_int32); + ASSERT(e.get_int32().value == 1); try { bsoncxx::types::b_double d = e.get_double(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); } try { bsoncxx::types::b_string str = e.get_string(); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp index e0632892d7..afc36e1fd4 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -41,9 +40,9 @@ void example() { try { std::string json = bsoncxx::to_json(doc); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + ASSERT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp index 36fdee592a..85eee021a0 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -41,9 +40,9 @@ void example() { try { std::string json = bsoncxx::to_json(doc); // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + ASSERT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); } } // [Example] diff --git a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp index 67c3222cdd..e5aae1cd3c 100644 --- a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -28,72 +27,72 @@ void example() { using d128 = bsoncxx::decimal128; // For brevity. // Default ctor. - assert(d128{}.to_string() == "0E-6176"); + ASSERT(d128{}.to_string() == "0E-6176"); - assert(d128{"0"}.to_string() == "0"); - assert(d128{"12"}.to_string() == "12"); - assert(d128{"-76"}.to_string() == "-76"); - assert(d128{"12.70"}.to_string() == "12.70"); - assert(d128{"+0.003"}.to_string() == "0.003"); - assert(d128{"017."}.to_string() == "17"); - assert(d128{".5"}.to_string() == "0.5"); - assert(d128{"4E+9"}.to_string() == "4E+9"); - assert(d128{"0.73e-7"}.to_string() == "7.3E-8"); - assert(d128{"Inf"}.to_string() == "Infinity"); - assert(d128{"-infinity"}.to_string() == "-Infinity"); - assert(d128{"NaN"}.to_string() == "NaN"); + ASSERT(d128{"0"}.to_string() == "0"); + ASSERT(d128{"12"}.to_string() == "12"); + ASSERT(d128{"-76"}.to_string() == "-76"); + ASSERT(d128{"12.70"}.to_string() == "12.70"); + ASSERT(d128{"+0.003"}.to_string() == "0.003"); + ASSERT(d128{"017."}.to_string() == "17"); + ASSERT(d128{".5"}.to_string() == "0.5"); + ASSERT(d128{"4E+9"}.to_string() == "4E+9"); + ASSERT(d128{"0.73e-7"}.to_string() == "7.3E-8"); + ASSERT(d128{"Inf"}.to_string() == "Infinity"); + ASSERT(d128{"-infinity"}.to_string() == "-Infinity"); + ASSERT(d128{"NaN"}.to_string() == "NaN"); // "NaN8275" is not a supported string representation. - assert(d128{"123"}.to_string() == "123"); - assert(d128{"-123"}.to_string() == "-123"); - assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); - assert(d128{"1.23E+5"}.to_string() == "1.23E+5"); - assert(d128{"12.3"}.to_string() == "12.3"); - assert(d128{"0.00123"}.to_string() == "0.00123"); - assert(d128{"1.23E-8"}.to_string() == "1.23E-8"); - assert(d128{"-1.23E-10"}.to_string() == "-1.23E-10"); - assert(d128{"0"}.to_string() == "0"); - assert(d128{"0.00"}.to_string() == "0.00"); - assert(d128{"0E+2"}.to_string() == "0E+2"); - assert(d128{"-0"}.to_string() == "-0"); - assert(d128{"0.000005"}.to_string() == "0.000005"); - assert(d128{"0.0000050"}.to_string() == "0.0000050"); - assert(d128{"5E-7"}.to_string() == "5E-7"); - assert(d128{"Infinity"}.to_string() == "Infinity"); - assert(d128{"-Infinity"}.to_string() == "-Infinity"); - assert(d128{"NaN"}.to_string() == "NaN"); + ASSERT(d128{"123"}.to_string() == "123"); + ASSERT(d128{"-123"}.to_string() == "-123"); + ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + ASSERT(d128{"1.23E+5"}.to_string() == "1.23E+5"); + ASSERT(d128{"12.3"}.to_string() == "12.3"); + ASSERT(d128{"0.00123"}.to_string() == "0.00123"); + ASSERT(d128{"1.23E-8"}.to_string() == "1.23E-8"); + ASSERT(d128{"-1.23E-10"}.to_string() == "-1.23E-10"); + ASSERT(d128{"0"}.to_string() == "0"); + ASSERT(d128{"0.00"}.to_string() == "0.00"); + ASSERT(d128{"0E+2"}.to_string() == "0E+2"); + ASSERT(d128{"-0"}.to_string() == "-0"); + ASSERT(d128{"0.000005"}.to_string() == "0.000005"); + ASSERT(d128{"0.0000050"}.to_string() == "0.0000050"); + ASSERT(d128{"5E-7"}.to_string() == "5E-7"); + ASSERT(d128{"Infinity"}.to_string() == "Infinity"); + ASSERT(d128{"-Infinity"}.to_string() == "-Infinity"); + ASSERT(d128{"NaN"}.to_string() == "NaN"); // "NaN123" is not a supported string representation. // "-sNaN" is not a supported string representation. - assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); - assert(d128{"123E+3"}.to_string() == "1.23E+5"); - assert(d128{"12.3E-9"}.to_string() == "1.23E-8"); - assert(d128{"-123E-12"}.to_string() == "-1.23E-10"); - assert(d128{"700E-9"}.to_string() == "7.00E-7"); - assert(d128{"70"}.to_string() == "70"); - assert(d128{"0.00E+3"}.to_string() == "0E+1"); + ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + ASSERT(d128{"123E+3"}.to_string() == "1.23E+5"); + ASSERT(d128{"12.3E-9"}.to_string() == "1.23E-8"); + ASSERT(d128{"-123E-12"}.to_string() == "-1.23E-10"); + ASSERT(d128{"700E-9"}.to_string() == "7.00E-7"); + ASSERT(d128{"70"}.to_string() == "70"); + ASSERT(d128{"0.00E+3"}.to_string() == "0E+1"); - assert(d128{"0"}.to_string() == "0"); - assert(d128{"0.00"}.to_string() == "0.00"); - assert(d128{"123"}.to_string() == "123"); - assert(d128{"-123"}.to_string() == "-123"); - assert(d128{"1.23E3"}.to_string() == "1.23E+3"); - assert(d128{"1.23E+3"}.to_string() == "1.23E+3"); - assert(d128{"12.3E+7"}.to_string() == "1.23E+8"); - assert(d128{"12.0"}.to_string() == "12.0"); - assert(d128{"12.3"}.to_string() == "12.3"); - assert(d128{"0.00123"}.to_string() == "0.00123"); - assert(d128{"-1.23E-12"}.to_string() == "-1.23E-12"); - assert(d128{"1234.5E-4"}.to_string() == "0.12345"); - assert(d128{"-0"}.to_string() == "-0"); - assert(d128{"-0.00"}.to_string() == "-0.00"); - assert(d128{"0E+7"}.to_string() == "0E+7"); - assert(d128{"-0E-7"}.to_string() == "-0E-7"); - assert(d128{"inf"}.to_string() == "Infinity"); - assert(d128{"+inFiniTy"}.to_string() == "Infinity"); - assert(d128{"-Infinity"}.to_string() == "-Infinity"); - assert(d128{"NaN"}.to_string() == "NaN"); - assert(d128{"-NAN"}.to_string() == "NaN"); + ASSERT(d128{"0"}.to_string() == "0"); + ASSERT(d128{"0.00"}.to_string() == "0.00"); + ASSERT(d128{"123"}.to_string() == "123"); + ASSERT(d128{"-123"}.to_string() == "-123"); + ASSERT(d128{"1.23E3"}.to_string() == "1.23E+3"); + ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + ASSERT(d128{"12.3E+7"}.to_string() == "1.23E+8"); + ASSERT(d128{"12.0"}.to_string() == "12.0"); + ASSERT(d128{"12.3"}.to_string() == "12.3"); + ASSERT(d128{"0.00123"}.to_string() == "0.00123"); + ASSERT(d128{"-1.23E-12"}.to_string() == "-1.23E-12"); + ASSERT(d128{"1234.5E-4"}.to_string() == "0.12345"); + ASSERT(d128{"-0"}.to_string() == "-0"); + ASSERT(d128{"-0.00"}.to_string() == "-0.00"); + ASSERT(d128{"0E+7"}.to_string() == "0E+7"); + ASSERT(d128{"-0E-7"}.to_string() == "-0E-7"); + ASSERT(d128{"inf"}.to_string() == "Infinity"); + ASSERT(d128{"+inFiniTy"}.to_string() == "Infinity"); + ASSERT(d128{"-Infinity"}.to_string() == "-Infinity"); + ASSERT(d128{"NaN"}.to_string() == "NaN"); + ASSERT(d128{"-NAN"}.to_string() == "NaN"); // "SNaN" is not a supported string representation. // "Fred" is not a supported string representation. } diff --git a/examples/api/bsoncxx/examples/decimal128/errors.cpp b/examples/api/bsoncxx/examples/decimal128/errors.cpp index edc9f806f8..da6c1f24e0 100644 --- a/examples/api/bsoncxx/examples/decimal128/errors.cpp +++ b/examples/api/bsoncxx/examples/decimal128/errors.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,9 +26,9 @@ void example() { try { bsoncxx::decimal128 d{"invalid"}; // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_invalid_decimal128); + ASSERT(ex.code() == bsoncxx::error_code::k_invalid_decimal128); } } // [Example] diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp index bee7248e2c..1c27746898 100644 --- a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -28,55 +27,55 @@ void example() { using d128 = bsoncxx::decimal128; // For brevity. // Default ctor. - assert((d128{0u, 0u}) == d128{}); + ASSERT((d128{0u, 0u}) == d128{}); // [0,0,0] - assert((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"}); + ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"}); // [1,0,0] - assert((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"}); + ASSERT((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"}); // [0,0,1] - assert((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"}); + ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"}); // [0,0,-1] - assert((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"}); + ASSERT((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"}); // [0,123,0] - assert((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"}); + ASSERT((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"}); // [1,123,0] - assert((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"}); + ASSERT((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"}); // [0,123,1] - assert((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"}); + ASSERT((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"}); // [1,123,1] - assert((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"}); + ASSERT((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"}); // [0,123,-1] - assert((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); + ASSERT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); // [0,123,-1] - assert((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); + ASSERT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); // [0,inf] - assert((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"}); + ASSERT((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"}); // [1,inf] - assert((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"}); + ASSERT((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"}); // [0,qNaN] - assert((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"}); + ASSERT((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"}); // [1,qNaN]: Negative NaN is string-represented as NaN. - assert((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN"); + ASSERT((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN"); // [0,sNaN]: Signaling NaN is string-represented as NaN. - assert((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); + ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); // [1,sNaN]: Negative Signaling NaN is string-represented as NaN. - assert((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); + ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); } // [Example] diff --git a/examples/api/bsoncxx/examples/oid/basic_usage.cpp b/examples/api/bsoncxx/examples/oid/basic_usage.cpp index 542a9dc482..ff99429960 100644 --- a/examples/api/bsoncxx/examples/oid/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/oid/basic_usage.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include #include @@ -30,7 +29,7 @@ void example() { bsoncxx::oid a; bsoncxx::oid b; - assert(a == b || a != b); // Random. + ASSERT(a == b || a != b); // Random. } { @@ -41,17 +40,17 @@ void example() { { std::time_t time = oid.get_time_t(); char str[sizeof("YYYY-MM-DD HH:MM:SS")]; - assert(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + ASSERT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == sizeof(str) - 1u); - assert(std::string(str) == "1970-01-01 00:00:00"); + ASSERT(std::string(str) == "1970-01-01 00:00:00"); } - assert(oid.bytes() != bytes); // Seperate storage. - assert(oid.size() == bsoncxx::oid::k_oid_length); - assert(std::memcmp(bytes, oid.bytes(), oid.size()) == 0); + ASSERT(oid.bytes() != bytes); // Seperate storage. + ASSERT(oid.size() == bsoncxx::oid::k_oid_length); + ASSERT(std::memcmp(bytes, oid.bytes(), oid.size()) == 0); - assert(oid.to_string() == "000000000000000000000000"); - assert(oid == bsoncxx::oid{"000000000000000000000000"}); + ASSERT(oid.to_string() == "000000000000000000000000"); + ASSERT(oid == bsoncxx::oid{"000000000000000000000000"}); } { @@ -63,17 +62,17 @@ void example() { { std::time_t time = oid.get_time_t(); char str[sizeof("YYYY-MM-DD HH:MM:SS")]; - assert(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + ASSERT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == sizeof(str) - 1u); - assert(std::string(str) == "2000-01-01 23:59:59"); + ASSERT(std::string(str) == "2000-01-01 23:59:59"); } - assert(oid < bsoncxx::oid{"389622001112131415212223"}); // Timestamp: 2000-02-01 00:00:00 - assert(oid > bsoncxx::oid{"386d43801112131415212223"}); // Timestamp: 2000-01-01 00:00:00 - assert(oid < bsoncxx::oid{"386e94ffffffffffff212223"}); // Value: 1099511627775 - assert(oid > bsoncxx::oid{"386e94ff0000000000212223"}); // Value: 0 - assert(oid < bsoncxx::oid{"386e94ff1112131415ffffff"}); // Counter: 16777215 - assert(oid > bsoncxx::oid{"386e94ff1112131415000000"}); // Counter: 0 + ASSERT(oid < bsoncxx::oid{"389622001112131415212223"}); // Timestamp: 2000-02-01 00:00:00 + ASSERT(oid > bsoncxx::oid{"386d43801112131415212223"}); // Timestamp: 2000-01-01 00:00:00 + ASSERT(oid < bsoncxx::oid{"386e94ffffffffffff212223"}); // Value: 1099511627775 + ASSERT(oid > bsoncxx::oid{"386e94ff0000000000212223"}); // Value: 0 + ASSERT(oid < bsoncxx::oid{"386e94ff1112131415ffffff"}); // Counter: 16777215 + ASSERT(oid > bsoncxx::oid{"386e94ff1112131415000000"}); // Counter: 0 } } // [Example] diff --git a/examples/api/bsoncxx/examples/oid/errors.cpp b/examples/api/bsoncxx/examples/oid/errors.cpp index 950428745d..856adcb182 100644 --- a/examples/api/bsoncxx/examples/oid/errors.cpp +++ b/examples/api/bsoncxx/examples/oid/errors.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -28,9 +26,9 @@ void example() { try { bsoncxx::oid oid{"invalid"}; // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_invalid_oid); + ASSERT(ex.code() == bsoncxx::error_code::k_invalid_oid); } try { @@ -38,9 +36,9 @@ void example() { bsoncxx::oid oid{bytes, sizeof(bytes)}; // Throws. - assert(false && "should not reach this point"); + ASSERT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - assert(ex.code() == bsoncxx::error_code::k_invalid_oid); + ASSERT(ex.code() == bsoncxx::error_code::k_invalid_oid); } } // [Example] diff --git a/examples/api/bsoncxx/examples/validation/basic_usage.cpp b/examples/api/bsoncxx/examples/validation/basic_usage.cpp index dd91fb4cb5..41f3198cdd 100644 --- a/examples/api/bsoncxx/examples/validation/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/validation/basic_usage.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -31,14 +30,14 @@ void example() { { std::uint8_t bytes[1]{}; // Invalid. - assert(!bsoncxx::validate(bytes, sizeof(bytes))); + ASSERT(!bsoncxx::validate(bytes, sizeof(bytes))); std::size_t offset; - assert(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset)); + ASSERT(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset)); // Set to `0` for an invalid BSON document. - assert(offset == 0u); + ASSERT(offset == 0u); } bsoncxx::document::value owner = bsoncxx::from_json(R"({"x": 1})"); @@ -47,24 +46,24 @@ void example() { { auto doc_opt = bsoncxx::validate(data, length); - assert(doc_opt); + ASSERT(doc_opt); bsoncxx::document::view doc = *doc_opt; - assert(doc.data() == data); - assert(doc.length() == length); - assert(doc == owner.view()); + ASSERT(doc.data() == data); + ASSERT(doc.length() == length); + ASSERT(doc == owner.view()); } { bsoncxx::validator options; std::size_t offset = 123u; - assert(bsoncxx::validate(data, length) == + ASSERT(bsoncxx::validate(data, length) == bsoncxx::validate(data, length, options, &offset)); // Not set when valid. - assert(offset == 123u); + ASSERT(offset == 123u); } } // [Example] diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp index 89d7f711a0..be04730cdc 100644 --- a/examples/api/bsoncxx/examples/validation/validator.cpp +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include #include @@ -36,7 +35,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { bsoncxx::validator options; std::size_t offset; - assert(bsoncxx::validate(bytes, length, options, &offset)); + ASSERT(bsoncxx::validate(bytes, length, options, &offset)); } // Validate UTF-8 strings. @@ -46,14 +45,14 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_utf8(true); - assert(!bsoncxx::validate(bytes, length, options, &offset)); + ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"x": "\0"` relative to start of the document. - assert(offset == 4u); + ASSERT(offset == 4u); options.check_utf8_allow_null(true); - assert(bsoncxx::validate(bytes, length, options, &offset)); + ASSERT(bsoncxx::validate(bytes, length, options, &offset)); } // Validate dot keys. @@ -63,10 +62,10 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dot_keys(true); - assert(!bsoncxx::validate(bytes, length, options, &offset)); + ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"a.b": 1` relative to start of the document. - assert(offset == 15u); + ASSERT(offset == 15u); } // Validate dollar keys. @@ -76,10 +75,10 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dollar_keys(true); - assert(!bsoncxx::validate(bytes, length, options, &offset)); + ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"a.b": 1` relative to start of the sub-document. (CDRIVER-5710) - assert(offset == 4u); + ASSERT(offset == 4u); } } // [Example] diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index cd2e1a3a05..447ad1d8ee 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -20,7 +20,6 @@ // #include -#include #include #include #include @@ -62,7 +61,7 @@ class runner_type { } int run() { - assert(jobs > 0); + ASSERT(jobs > 0); std::cout << "seed: " << seed << std::endl; diff --git a/examples/macros.hh b/examples/macros.hh index df8e7f6788..f11b9e1f47 100644 --- a/examples/macros.hh +++ b/examples/macros.hh @@ -14,6 +14,9 @@ #pragma once +#include +#include + #if defined(_MSC_VER) #define EXAMPLES_CDECL __cdecl #else @@ -22,3 +25,13 @@ #define EXAMPLES_CONCAT(a, b) EXAMPLES_CONCAT_1(a, b) #define EXAMPLES_CONCAT_1(a, b) a##b + +// Unconditionally define `assert()` for examples. +#define ASSERT(...) \ + if (!static_cast(__VA_ARGS__)) { \ + std::printf( \ + "%s:%d: %s: assertion failed: %s\n", __FILE__, __LINE__, __func__, #__VA_ARGS__); \ + std::fflush(stdout); \ + std::abort(); \ + } else \ + ((void)0) From a99e9e6529fb4249fa8fe72060aa7df3bec3972a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 11:00:41 -0500 Subject: [PATCH 12/26] Fix section heading levels --- docs/api/bsoncxx/examples/decimal128.md | 6 +++--- docs/api/bsoncxx/examples/oid.md | 4 ++-- docs/api/bsoncxx/examples/validation.md | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/api/bsoncxx/examples/decimal128.md b/docs/api/bsoncxx/examples/decimal128.md index f0e31f8efa..65bb07d99a 100644 --- a/docs/api/bsoncxx/examples/decimal128.md +++ b/docs/api/bsoncxx/examples/decimal128.md @@ -1,11 +1,11 @@ -## Basic Usage +# Basic Usage @snippet api/bsoncxx/examples/decimal128/basic_usage.cpp Example -## From Bytes +# From Bytes @snippet api/bsoncxx/examples/decimal128/from_bytes.cpp Example -## Error Handling +# Error Handling @snippet api/bsoncxx/examples/decimal128/errors.cpp Example diff --git a/docs/api/bsoncxx/examples/oid.md b/docs/api/bsoncxx/examples/oid.md index 2746a0f2a7..95705d7fd8 100644 --- a/docs/api/bsoncxx/examples/oid.md +++ b/docs/api/bsoncxx/examples/oid.md @@ -1,7 +1,7 @@ -## Basic Usage +# Basic Usage @snippet api/bsoncxx/examples/oid/basic_usage.cpp Example -## Error Handling +# Error Handling @snippet api/bsoncxx/examples/oid/errors.cpp Example diff --git a/docs/api/bsoncxx/examples/validation.md b/docs/api/bsoncxx/examples/validation.md index b8fc93c550..be4e549617 100644 --- a/docs/api/bsoncxx/examples/validation.md +++ b/docs/api/bsoncxx/examples/validation.md @@ -1,7 +1,7 @@ -## Basic Usage +# Basic Usage @snippet api/bsoncxx/examples/validation/basic_usage.cpp Example -## With Validator +# With Validator @snippet api/bsoncxx/examples/validation/validator.cpp Example From aa26db14cee12b534b6e2db33dcb90c51cbd3a2a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 11:00:43 -0500 Subject: [PATCH 13/26] Simplify runner registration --- examples/api/runner.hh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/api/runner.hh b/examples/api/runner.hh index f2b3164c52..0e35e830af 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -22,9 +22,8 @@ void runner_register_fn(void (*fn)()); #error "EXAMPLES_COMPONENT_NAME is not defined!" #endif // !defined(EXAMPLES_COMPONENT_NAME) -#define RUNNER_REGISTER_COMPONENT() \ - static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void); \ - static int EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _registerator) = \ - ([] { ::runner_register_fn(&EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)); }(), \ - 0); \ +#define RUNNER_REGISTER_COMPONENT() \ + static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void); \ + static int EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _registerator) = \ + (::runner_register_fn(&EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)), 0); \ static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void) From e4ecf4a0035b12fab129982aa14676f7450522f3 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 11:00:44 -0500 Subject: [PATCH 14/26] Note where EXAMPLES_COMPONENT_NAME is defined --- examples/api/runner.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/api/runner.hh b/examples/api/runner.hh index 0e35e830af..0d4a5d6811 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -18,6 +18,7 @@ void runner_register_fn(void (*fn)()); +// Defined by examples/CMakeLists.txt. #if !defined(EXAMPLES_COMPONENT_NAME) #error "EXAMPLES_COMPONENT_NAME is not defined!" #endif // !defined(EXAMPLES_COMPONENT_NAME) From 724d7f69bbae834bb6a737829ee884c710b6b826 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 11:00:44 -0500 Subject: [PATCH 15/26] Fix comment documenting argv[2] --- examples/api/runner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 447ad1d8ee..45db3b3ac4 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -116,7 +116,7 @@ int EXAMPLES_CDECL main(int argc, char** argv) { runner.set_seed(static_cast(std::random_device()())); } - // Permit using a custom seed for reproducibility. + // Permit using a custom job count. if (argc > 2) { char* const jobs_str = argv[2]; From 2e70e4e760db8175302655db9aa8c5534083dc73 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 13:41:06 -0500 Subject: [PATCH 16/26] Group topic pages by library + reword titles --- src/bsoncxx/include/bsoncxx/doc.hpp | 21 +++++++++------------ src/mongocxx/include/mongocxx/doc.hpp | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index c6d07dfbfa..1b389189d5 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -90,7 +90,14 @@ /// /// -/// @page topic-bsoncxx-examples Using the bsoncxx Library +/// @page topic-bsoncxx The bsoncxx Library +/// @brief Topics relating to the bsoncxx library. +/// @li @subpage topic-bsoncxx-examples +/// @li @subpage topic-bsoncxx-about +/// + +/// +/// @page topic-bsoncxx-examples How-To Guides /// @brief Examples of how to use the bsoncxx library. /// @li @subpage topic-bsoncxx-examples-bson-documents /// @li @subpage topic-bsoncxx-examples-bson-errors @@ -141,16 +148,6 @@ /// /// -/// @page topic-mongocxx-examples Using the mongocxx Library -/// @brief Examples of how to use the bsoncxx library. -/// - -/// -/// @page topic-bsoncxx-about About the bsoncxx Library +/// @page topic-bsoncxx-about Explanation /// @brief Design decisions, history, and other technical details about the bsoncxx library. /// - -/// -/// @page topic-mongocxx-about About the mongocxx Library -/// @brief Design decisions, history, and other technical details about the mongocxx library. -/// diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index bf16832ead..5ea25b9f2d 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -84,3 +84,20 @@ /// @warning These redeclarations are for internal use only! Users should reference entities /// declared in @ref bsoncxx directly, not through @ref mongocxx. /// + +/// +/// @page topic-mongocxx The mongocxx Library +/// @brief Topics relating to the mongocxx library. +/// @li @subpage topic-mongocxx-examples +/// @li @subpage topic-mongocxx-about +/// + +/// +/// @page topic-mongocxx-examples How-To Guides +/// @brief Examples of how to use the mongocxx library. +/// + +/// +/// @page topic-mongocxx-about Explanation +/// @brief Design decisions, history, and other technical details about the mongocxx library. +/// From 3c17f5af081e58c8077bd45bc1c14a001fae2112 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 16 Sep 2024 16:15:19 -0500 Subject: [PATCH 17/26] Remove redundant call to add_dependencies --- examples/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 53612c38b7..162db903d6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -101,10 +101,6 @@ function(add_examples_executable source) if (PARSED_ADD_TO_RUN_EXAMPLES) add_dependencies(run-${type}-examples run-examples-${target_name}) - - if (${subdir} MATCHES "^api/") - add_dependencies(run-${type}-examples run-examples-${target_name}) - endif () endif () endfunction() From dbe38bd705fb8b1202e27ae77af478e5b43bb2e0 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 18 Sep 2024 09:57:11 -0500 Subject: [PATCH 18/26] Move references to external resources into page description --- examples/api/bsoncxx/examples/decimal128/basic_usage.cpp | 1 - examples/api/bsoncxx/examples/decimal128/from_bytes.cpp | 1 - src/bsoncxx/include/bsoncxx/doc.hpp | 4 ++++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp index e5aae1cd3c..db2ae6c899 100644 --- a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp @@ -22,7 +22,6 @@ namespace { // [Example] -// See: https://speleotrove.com/decimal/daconvs.html void example() { using d128 = bsoncxx::decimal128; // For brevity. diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp index 1c27746898..125b725e6b 100644 --- a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -22,7 +22,6 @@ namespace { // [Example] -// See: https://speleotrove.com/decimal/daconvs.html void example() { using d128 = bsoncxx::decimal128; // For brevity. diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 1b389189d5..5ecdbd7f69 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -130,6 +130,10 @@ /// @page topic-bsoncxx-examples-decimal128 Decimal128 /// @brief How to use Decimal128 interfaces. /// @tableofcontents +/// @see @parblock +/// @li [BSON Decimal128 Type Handling in Drivers (MongoDB Specifications)](https://specifications.readthedocs.io/en/latest/bson-decimal128/decimal128/) +/// @li [Conversions (General Decimal Arithmetic Specification)](https://speleotrove.com/decimal/daconvs.html) +/// @endparblock /// @include{doc} api/bsoncxx/examples/decimal128.md /// From d6bc01af2840d32d31c5bcd8b786275b6cf19e61 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 18 Sep 2024 15:41:14 -0500 Subject: [PATCH 19/26] Formatting --- src/bsoncxx/include/bsoncxx/doc.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bsoncxx/include/bsoncxx/doc.hpp b/src/bsoncxx/include/bsoncxx/doc.hpp index 5ecdbd7f69..d091ac0f79 100644 --- a/src/bsoncxx/include/bsoncxx/doc.hpp +++ b/src/bsoncxx/include/bsoncxx/doc.hpp @@ -131,8 +131,10 @@ /// @brief How to use Decimal128 interfaces. /// @tableofcontents /// @see @parblock -/// @li [BSON Decimal128 Type Handling in Drivers (MongoDB Specifications)](https://specifications.readthedocs.io/en/latest/bson-decimal128/decimal128/) -/// @li [Conversions (General Decimal Arithmetic Specification)](https://speleotrove.com/decimal/daconvs.html) +/// @li [BSON Decimal128 Type Handling in Drivers (MongoDB +/// Specifications)](https://specifications.readthedocs.io/en/latest/bson-decimal128/decimal128/) +/// @li [Conversions (General Decimal Arithmetic +/// Specification)](https://speleotrove.com/decimal/daconvs.html) /// @endparblock /// @include{doc} api/bsoncxx/examples/decimal128.md /// From b9ab81cd8fc8a086bbd05d67e5f0f1af550ce693 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 24 Sep 2024 10:29:40 -0500 Subject: [PATCH 20/26] Fix reference to element in validator example --- examples/api/bsoncxx/examples/validation/validator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp index be04730cdc..04ea221a0b 100644 --- a/examples/api/bsoncxx/examples/validation/validator.cpp +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -77,7 +77,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); - // Offset of `"a.b": 1` relative to start of the sub-document. (CDRIVER-5710) + // Offset of `"$numberInt": "123"` relative to start of the sub-document. (CDRIVER-5710) ASSERT(offset == 4u); } } From 5a9b6dccdd8c9611e389046edf27b0053c7546f7 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 24 Sep 2024 10:29:41 -0500 Subject: [PATCH 21/26] Fix sign bit in decimal128/from_bytes.cpp description --- examples/api/bsoncxx/examples/decimal128/from_bytes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp index 125b725e6b..86f1ec0999 100644 --- a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -55,7 +55,7 @@ void example() { // [0,123,-1] ASSERT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); - // [0,123,-1] + // [1,123,-1] ASSERT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); // [0,inf] From 68346a14e71382ece1416cf60d0fdbe427649842 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 25 Sep 2024 11:08:54 -0500 Subject: [PATCH 22/26] Use `--flag arg` syntax for CLI arguments --- examples/api/runner.cpp | 70 ++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 45db3b3ac4..66c3fe3bcf 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -100,37 +100,63 @@ void runner_register_fn(void (*fn)()) { } int EXAMPLES_CDECL main(int argc, char** argv) { - // Permit using a custom seed for reproducibility. - if (argc > 1) { - char* const seed_str = argv[1]; - char* end = nullptr; + bool set_seed = false; + bool set_jobs = false; + + // Simple command-line argument parser. + for (int i = 1; i < argc; ++i) { + // Permit using a custom seed for reproducibility. + if (strcmp(argv[i], "--seed") == 0) { + if (i + 1 >= argc) { + std::cerr << "missing argument to --seed" << std::endl; + return 1; + } + + char* const seed_str = argv[++i]; // Next argument. + char* end = nullptr; - runner.set_seed( - static_cast(std::strtoul(seed_str, &end, 10))); + const auto seed = + static_cast(std::strtoul(seed_str, &end, 10)); + + if (static_cast(end - seed_str) != std::strlen(seed_str)) { + std::cerr << "invalid seed string" << std::endl; + return 1; + } - if (static_cast(end - argv[1]) != std::strlen(seed_str)) { - std::cerr << "invalid seed string" << std::endl; - return 1; + runner.set_seed(seed); + set_seed = true; } - } else { - runner.set_seed(static_cast(std::random_device()())); - } - // Permit using a custom job count. - if (argc > 2) { - char* const jobs_str = argv[2]; + if (strcmp(argv[i], "--jobs") == 0) { + if (i + 1 >= argc) { + std::cerr << "missing argument to --jobs" << std::endl; + return 1; + } + + char* const jobs_str = argv[++i]; // Next argument. + char* end = nullptr; - char* end = nullptr; + const auto jobs = static_cast(std::strtoul(jobs_str, &end, 10)); - runner.set_jobs(static_cast(std::strtoul(jobs_str, &end, 10))); + if (static_cast(end - jobs_str) != std::strlen(jobs_str)) { + std::cerr << "invalid jobs string" << std::endl; + return 1; + } - if (static_cast(end - argv[1]) != std::strlen(jobs_str)) { - std::cerr << "invalid jobs string" << std::endl; - return 1; + runner.set_jobs(jobs); + set_jobs = true; } - } else { + } + + // Default: use a random seed. + if (!set_seed) { + runner.set_seed(static_cast(std::random_device()())); + } + + // Default: request maximum job count. + if (!set_jobs) { runner.set_jobs(0); } - return runner.run(); + return runner.run(); // Return directly from forked processes. } From 33f23ad0c91f8e2d9edb7f2512e03486e82db586 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 25 Sep 2024 11:08:54 -0500 Subject: [PATCH 23/26] Better integer type consistency for jobs value --- examples/api/runner.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 66c3fe3bcf..b2c7989b82 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -20,6 +20,7 @@ // #include +#include #include #include #include @@ -53,7 +54,7 @@ class runner_type { } void set_jobs(unsigned int jobs) { - if (jobs <= 0) { + if (jobs == 0u) { this->jobs = std::thread::hardware_concurrency(); } else { this->jobs = jobs; @@ -61,7 +62,7 @@ class runner_type { } int run() { - ASSERT(jobs > 0); + ASSERT(jobs > 0u); std::cout << "seed: " << seed << std::endl; @@ -136,14 +137,18 @@ int EXAMPLES_CDECL main(int argc, char** argv) { char* const jobs_str = argv[++i]; // Next argument. char* end = nullptr; - const auto jobs = static_cast(std::strtoul(jobs_str, &end, 10)); + const auto jobs = std::strtoul(jobs_str, &end, 10); if (static_cast(end - jobs_str) != std::strlen(jobs_str)) { std::cerr << "invalid jobs string" << std::endl; return 1; } - runner.set_jobs(jobs); + if (jobs >= UINT_MAX) { + std::cerr << "invalid jobs string (too large): " << jobs_str << std::endl; + } + + runner.set_jobs(static_cast(jobs)); set_jobs = true; } } From 1742faa189f508b3cffca08f8f1f6f47b654768b Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 25 Sep 2024 11:08:54 -0500 Subject: [PATCH 24/26] Print invalid argument on error --- examples/api/runner.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index b2c7989b82..abf80fa6c0 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -120,7 +120,7 @@ int EXAMPLES_CDECL main(int argc, char** argv) { static_cast(std::strtoul(seed_str, &end, 10)); if (static_cast(end - seed_str) != std::strlen(seed_str)) { - std::cerr << "invalid seed string" << std::endl; + std::cerr << "invalid seed string: " << seed_str << std::endl; return 1; } @@ -140,7 +140,7 @@ int EXAMPLES_CDECL main(int argc, char** argv) { const auto jobs = std::strtoul(jobs_str, &end, 10); if (static_cast(end - jobs_str) != std::strlen(jobs_str)) { - std::cerr << "invalid jobs string" << std::endl; + std::cerr << "invalid jobs string: " << jobs_str << std::endl; return 1; } From 01661e3f31486f2695b0993865bf6632fa3fc79d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 25 Sep 2024 11:08:55 -0500 Subject: [PATCH 25/26] Avoid implying equal OID counter value results --- examples/api/bsoncxx/examples/oid/basic_usage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/bsoncxx/examples/oid/basic_usage.cpp b/examples/api/bsoncxx/examples/oid/basic_usage.cpp index ff99429960..6599104d4c 100644 --- a/examples/api/bsoncxx/examples/oid/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/oid/basic_usage.cpp @@ -29,7 +29,7 @@ void example() { bsoncxx::oid a; bsoncxx::oid b; - ASSERT(a == b || a != b); // Random. + ASSERT(a != b); // Random and unique per process. } { From 64fee4a525775b9572279f09d55f2c305daacb00 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 25 Sep 2024 11:08:55 -0500 Subject: [PATCH 26/26] Split sub-doc and sub-arr examples into make_* and sub_* --- .../examples/bson_documents/create_array.md | 8 +++ .../examples/bson_documents/create_doc.md | 8 +++ .../create_array/builder_sub_array.cpp | 7 ++- .../create_array/builder_sub_array_append.cpp | 48 +++++++++++++++++ .../create_array/builder_sub_document.cpp | 7 +-- .../builder_sub_document_append.cpp | 53 +++++++++++++++++++ .../create_doc/builder_sub_array.cpp | 8 +-- .../create_doc/builder_sub_array_append.cpp | 53 +++++++++++++++++++ .../create_doc/builder_sub_document.cpp | 4 +- .../builder_sub_document_append.cpp | 53 +++++++++++++++++++ 10 files changed, 236 insertions(+), 13 deletions(-) create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp create mode 100644 examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp diff --git a/docs/api/bsoncxx/examples/bson_documents/create_array.md b/docs/api/bsoncxx/examples/bson_documents/create_array.md index 0b188a4139..0a08e21243 100644 --- a/docs/api/bsoncxx/examples/bson_documents/create_array.md +++ b/docs/api/bsoncxx/examples/bson_documents/create_array.md @@ -56,10 +56,18 @@ @snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp Example +### With Multiple Sub-Document Appends + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp Example + ### With a Sub-Array @snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp Example +### With Multiple Sub-Array Appends + +@snippet api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp Example + ## From Raw Bytes ### As a View diff --git a/docs/api/bsoncxx/examples/bson_documents/create_doc.md b/docs/api/bsoncxx/examples/bson_documents/create_doc.md index 1f4cafabc3..34ef4194c5 100644 --- a/docs/api/bsoncxx/examples/bson_documents/create_doc.md +++ b/docs/api/bsoncxx/examples/bson_documents/create_doc.md @@ -56,10 +56,18 @@ @snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp Example +### With Multiple Sub-Document Appends + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp Example + ### With a Sub-Array @snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp Example +### With Multiple Sub-Array Appends + +@snippet api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp Example + ## From Raw Bytes ### As a View diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp index a60c7d17d4..84698f7cdd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp @@ -25,10 +25,9 @@ namespace { // [Example] void example() { - bsoncxx::array::value owner = - bsoncxx::builder::basic::make_array([](bsoncxx::builder::basic::sub_array arr) { - arr.append(std::int32_t{1}, std::int64_t{2}); - }); + using bsoncxx::builder::basic::make_array; + + bsoncxx::array::value owner = make_array(make_array(std::int32_t{1}, std::int64_t{2})); bsoncxx::array::view v = owner.view()[0].get_array().value; ASSERT(v[0].type() == bsoncxx::type::k_int32); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp new file mode 100644 index 0000000000..aea40ad1ea --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp @@ -0,0 +1,48 @@ +// Copyright 2009-present 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 + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + std::int32_t values[] = {1, 2, 3}; + + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array([&](bsoncxx::builder::basic::sub_array arr) { + for (int i = 0; i < 3; ++i) { + arr.append(values[i]); + } + }); + bsoncxx::array::view v = owner.view()[0].get_array().value; + + ASSERT(v[0].get_int32().value == 1); + ASSERT(v[1].get_int32().value == 2); + ASSERT(v[2].get_int32().value == 3); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp index 540da1879c..412eab43dd 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -13,8 +13,8 @@ // limitations under the License. #include +#include #include -#include #include #include @@ -25,9 +25,10 @@ namespace { // [Example] void example() { using bsoncxx::builder::basic::kvp; + using bsoncxx::builder::basic::make_array; + using bsoncxx::builder::basic::make_document; - bsoncxx::array::value owner = bsoncxx::builder::basic::make_array( - [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); }); + bsoncxx::array::value owner = make_array(make_document(kvp("key", "value"))); bsoncxx::document::view v = owner.view()[0].get_document().value; ASSERT(v["key"].get_string().value.compare("value") == 0); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp new file mode 100644 index 0000000000..77f158152f --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + std::string keys[] = {"a", "b", "c"}; + std::int32_t values[] = {1, 2, 3}; + + bsoncxx::array::value owner = + bsoncxx::builder::basic::make_array([&](bsoncxx::builder::basic::sub_document doc) { + for (int i = 0; i < 3; ++i) { + doc.append(kvp(keys[i], values[i])); + } + }); + bsoncxx::document::view v = owner.view()[0].get_document().value; + + ASSERT(v["a"].get_int32().value == 1); + ASSERT(v["b"].get_int32().value == 2); + ASSERT(v["c"].get_int32().value == 3); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp index 3ba49f8888..94f2ac8639 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp @@ -15,9 +15,9 @@ #include #include +#include #include #include -#include #include #include @@ -29,11 +29,11 @@ namespace { // [Example] void example() { using bsoncxx::builder::basic::kvp; + using bsoncxx::builder::basic::make_array; + using bsoncxx::builder::basic::make_document; bsoncxx::document::value owner = - bsoncxx::builder::basic::make_document(kvp("v", [](bsoncxx::builder::basic::sub_array arr) { - arr.append(std::int32_t{1}, std::int64_t{2}); - })); + make_document(kvp("v", make_array(std::int32_t{1}, std::int64_t{2}))); bsoncxx::array::view v = owner.view()["v"].get_array().value; ASSERT(v[0].type() == bsoncxx::type::k_int32); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp new file mode 100644 index 0000000000..f5c62e1967 --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + std::int32_t values[] = {1, 2, 3}; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("v", [&](bsoncxx::builder::basic::sub_array arr) { + for (int i = 0; i < 3; ++i) { + arr.append(values[i]); + } + })); + bsoncxx::array::view v = owner.view()["v"].get_array().value; + + ASSERT(v[0].get_int32().value == 1); + ASSERT(v[1].get_int32().value == 2); + ASSERT(v[2].get_int32().value == 3); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT() { + example(); +} diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp index 32aa749da1..c5b2da0cec 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -26,9 +26,9 @@ namespace { // [Example] void example() { using bsoncxx::builder::basic::kvp; + using bsoncxx::builder::basic::make_document; - bsoncxx::document::value owner = bsoncxx::builder::basic::make_document(kvp( - "v", [](bsoncxx::builder::basic::sub_document doc) { doc.append(kvp("key", "value")); })); + bsoncxx::document::value owner = make_document(kvp("v", make_document(kvp("key", "value")))); bsoncxx::document::view v = owner.view()["v"].get_document().value; ASSERT(v["key"].get_string().value.compare("value") == 0); diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp new file mode 100644 index 0000000000..6537488a0b --- /dev/null +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present 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 + +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + using bsoncxx::builder::basic::kvp; + + std::string keys[] = {"a", "b", "c"}; + std::int32_t values[] = {1, 2, 3}; + + bsoncxx::document::value owner = bsoncxx::builder::basic::make_document( + kvp("v", [&](bsoncxx::builder::basic::sub_document doc) { + for (int i = 0; i < 3; ++i) { + doc.append(kvp(keys[i], values[i])); + } + })); + bsoncxx::document::view v = owner.view()["v"].get_document().value; + + ASSERT(v["a"].get_int32().value == 1); + ASSERT(v["b"].get_int32().value == 2); + ASSERT(v["c"].get_int32().value == 3); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT() { + example(); +}