Skip to content

Reduce Warnings - Address warnings in examples and redundant recompilations #1201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/bsoncxx/decimal128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
decimal128 d128;
try {
d128 = decimal128{"1.234E+3456"};
} catch (const bsoncxx::exception& e) {
} catch (const bsoncxx::exception&) {
// The example won't fail, but in general, arbitrary strings
// might not convert properly.
return EXIT_FAILURE;
Expand Down
36 changes: 28 additions & 8 deletions examples/bsoncxx/view_and_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ int main() {
// note that all of the interesting methods for reading BSON are defined on the view type.

// iterate over the elements in a bson document
for (document::element ele : view) {
for (document::element doc_ele : view) {
// element is non owning view of a key-value pair within a document.

// we can use the key() method to get a string_view of the key.
stdx::string_view field_key{ele.key()};
stdx::string_view field_key{doc_ele.key()};

std::cout << "Got key, key = " << field_key << std::endl;

// we can use type() to get the type of the value.
switch (ele.type()) {
switch (doc_ele.type()) {
case type::k_string:
std::cout << "Got String!" << std::endl;
break;
Expand All @@ -77,20 +77,40 @@ int main() {
case type::k_array: {
std::cout << "Got Array!" << std::endl;
// if we have a subarray, we can access it by getting a view of it.
array::view subarr{ele.get_array().value};
for (array::element ele : subarr) {
array::view subarr{doc_ele.get_array().value};
for (array::element arr_ele : subarr) {
std::cout << "array element: "
<< bsoncxx::string::to_string(ele.get_string().value) << std::endl;
<< bsoncxx::string::to_string(arr_ele.get_string().value)
<< std::endl;
}
break;
}
default:

case type::k_double:
case type::k_document:
case type::k_binary:
case type::k_undefined:
case type::k_bool:
case type::k_date:
case type::k_null:
case type::k_regex:
case type::k_dbpointer:
case type::k_code:
case type::k_symbol:
case type::k_codewscope:
case type::k_int32:
case type::k_timestamp:
case type::k_int64:
case type::k_decimal128:
case type::k_maxkey:
case type::k_minkey:
std::cout << "We messed up!" << std::endl;
break;
}

// usually we don't need to actually use a switch statement, because we can also
// get a variant 'value' that can hold any BSON type.
types::bson_value::view ele_val{ele.get_value()};
types::bson_value::view doc_ele_val{doc_ele.get_value()};
}

// If we want to search for an element we can use operator[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
Expand Down Expand Up @@ -45,6 +46,8 @@ using bsoncxx::types::bson_value::make_value;

using namespace mongocxx;

namespace {

const int kKeyLength = 96;

using ns_pair = std::pair<std::string, std::string>;
Expand Down Expand Up @@ -92,15 +95,19 @@ bsoncxx::document::value doc_from_file(std::string path) {
return bsoncxx::from_json(file_contents);
}

} // namespace

int main() {
instance inst{};

// This must be the same master key that was used to create
// the encryption key; here, we use a random key as a placeholder.
char key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, std::rand);
std::uint8_t key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, []() {
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
});
bsoncxx::types::b_binary local_master_key{
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};

auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
<< close_document << finalize;
Expand Down
2 changes: 1 addition & 1 deletion examples/mongocxx/change_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace {

// watch_forever iterates the change stream until an error occurs.
void watch_forever(mongocxx::collection& collection) {
[[noreturn]] void watch_forever(mongocxx::collection& collection) {
mongocxx::options::change_stream options;
// Wait up to 1 second before polling again.
const std::chrono::milliseconds await_time{1000};
Expand Down
9 changes: 6 additions & 3 deletions examples/mongocxx/explicit_encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <random>

Expand Down Expand Up @@ -49,10 +50,12 @@ int main() {

// This must be the same master key that was used to create
// the encryption key; here, we use a random key as a placeholder.
char key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, std::rand);
std::uint8_t key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, []() {
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
});
bsoncxx::types::b_binary local_master_key{
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};

auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
<< close_document << finalize;
Expand Down
9 changes: 6 additions & 3 deletions examples/mongocxx/explicit_encryption_auto_decryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <random>

Expand Down Expand Up @@ -49,10 +50,12 @@ int main() {

// This must be the same master key that was used to create
// the encryption key; here, we use a random key as a placeholder.
char key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, std::rand);
std::uint8_t key_storage[kKeyLength];
std::generate_n(key_storage, kKeyLength, []() {
return static_cast<std::uint8_t>(std::rand() % UINT8_MAX);
});
bsoncxx::types::b_binary local_master_key{
bsoncxx::binary_sub_type::k_binary, kKeyLength, (const uint8_t*)&key_storage};
bsoncxx::binary_sub_type::k_binary, kKeyLength, key_storage};

auto kms_providers = document{} << "local" << open_document << "key" << local_master_key
<< close_document << finalize;
Expand Down
10 changes: 7 additions & 3 deletions examples/mongocxx/get_values_from_documents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;

namespace {

// Document model, showing array with nested documents:
//
// {
Expand Down Expand Up @@ -61,11 +63,11 @@ void iterate_messagelist(const bsoncxx::document::element& ele) {
// Check validity and type before trying to iterate.
if (ele.type() == type::k_array) {
bsoncxx::array::view subarray{ele.get_array().value};
for (const bsoncxx::array::element& msg : subarray) {
for (const bsoncxx::array::element& message : subarray) {
// Check correct type before trying to access elements.
// Only print out fields if they exist; don't report missing fields.
if (msg.type() == type::k_document) {
bsoncxx::document::view subdoc = msg.get_document().value;
if (message.type() == type::k_document) {
bsoncxx::document::view subdoc = message.get_document().value;
bsoncxx::document::element uid = subdoc["uid"];
bsoncxx::document::element status = subdoc["status"];
bsoncxx::document::element msg = subdoc["msg"];
Expand Down Expand Up @@ -118,6 +120,8 @@ void iterate_documents(mongocxx::collection& coll) {
}
}

} // namespace

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
Expand Down
14 changes: 5 additions & 9 deletions examples/mongocxx/instance_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,22 @@ class mongo_access {
return _pool->acquire();
}

bsoncxx::stdx::optional<connection> try_get_connection() {
return _pool->try_acquire();
}

private:
mongo_access() = default;

std::unique_ptr<mongocxx::instance> _instance = nullptr;
std::unique_ptr<mongocxx::pool> _pool = nullptr;
};

} // namespace

// The 'configure' and 'do_work' functions use the same mongocxx::instance and mongocxx::pool
// objects by way of the mongo_access singleton.

void configure(mongocxx::uri uri) {
class noop_logger : public mongocxx::logger {
public:
virtual void operator()(mongocxx::log_level,
bsoncxx::stdx::string_view,
bsoncxx::stdx::string_view) noexcept {}
void operator()(mongocxx::log_level,
bsoncxx::stdx::string_view,
bsoncxx::stdx::string_view) noexcept override {}
};

// Use `std::make_unique` with C++14 and newer.
Expand All @@ -97,6 +91,8 @@ bool do_work() {
return true;
}

} // namespace

int main(int argc, char* argv[]) {
auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri};
configure(std::move(uri));
Expand Down
7 changes: 4 additions & 3 deletions examples/mongocxx/mongodb.com/aggregation_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@
#include <mongocxx/exception/logic_error.hpp>
#include <mongocxx/instance.hpp>

// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.

using namespace mongocxx;

namespace {

void aggregation_examples(const mongocxx::database& db) {
{
// Start Aggregation Example 1
Expand Down Expand Up @@ -143,6 +142,8 @@ void aggregation_examples(const mongocxx::database& db) {
}
}

} // namespace

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
Expand Down
19 changes: 10 additions & 9 deletions examples/mongocxx/mongodb.com/documentation_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
#include <mongocxx/options/server_api.hpp>
#include <mongocxx/uri.hpp>

// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
// corresponding page on mongodb.com/docs. See CXX-1249 and DRIVERS-356 for more info.
namespace {

template <typename T>
void check_field(const T& document,
Expand Down Expand Up @@ -141,7 +140,7 @@ std::string getenv_or_fail(const char* s) {
// Returns a document with credentials for KMS providers.
// If include_external is true, all KMS providers are set.
// If include_external is false, only the local provider is set.
bsoncxx::document::value _make_kms_doc(bool include_external = true) {
bsoncxx::document::value make_kms_doc(bool include_external = true) {
using bsoncxx::builder::basic::sub_document;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
Expand Down Expand Up @@ -1442,19 +1441,19 @@ static bool is_snapshot_ready(mongocxx::client& client, mongocxx::collection& co
opts.snapshot(true);

auto session = client.start_session(opts);

try {
auto maybe_value = collection.find_one(session, {});
if (maybe_value) {
if (collection.find_one(session, {})) {
return true;
}
return false;
} catch (const mongocxx::operation_exception& e) {
if (e.code().value() == 246) { // snapshot unavailable
return false;
}
throw;
}
return true;

return false;
}

// Seed the pets database and wait for the snapshot to become available.
Expand Down Expand Up @@ -1612,7 +1611,7 @@ static void queryable_encryption_api(mongocxx::client& client) {
options::client_encryption ce_opts;
ce_opts.key_vault_client(&key_vault_client);
ce_opts.key_vault_namespace({"keyvault", "datakeys"});
ce_opts.kms_providers(_make_kms_doc(false));
ce_opts.kms_providers(make_kms_doc(false));
client_encryption client_encryption(std::move(ce_opts));

auto key1_id = client_encryption.create_data_key("local");
Expand All @@ -1634,7 +1633,7 @@ static void queryable_encryption_api(mongocxx::client& client) {
// Create an Queryable Encryption collection.
options::auto_encryption auto_encrypt_opts{};
auto_encrypt_opts.key_vault_namespace({"keyvault", "datakeys"});
auto_encrypt_opts.kms_providers(_make_kms_doc(false));
auto_encrypt_opts.kms_providers(make_kms_doc(false));
auto_encrypt_opts.encrypted_fields_map(encrypted_fields_map.view());

// Optional, If mongocryptd is not in PATH, then find the binary at MONGOCRYPTD_PATH.
Expand Down Expand Up @@ -1699,6 +1698,8 @@ static void queryable_encryption_api(mongocxx::client& client) {
// End Queryable Encryption Example
}

} // namespace

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
Expand Down
5 changes: 3 additions & 2 deletions examples/mongocxx/mongodb.com/index_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.
namespace {

void index_examples(const mongocxx::database& db) {
{
Expand All @@ -39,6 +38,8 @@ void index_examples(const mongocxx::database& db) {
}
}

} // namespace

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
Expand Down
7 changes: 4 additions & 3 deletions examples/mongocxx/mongodb.com/runcommand_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

// NOTE: Any time this file is modified, a DOCS ticket should be opened to sync the changes with the
// corresponding page on mongodb.com/docs. See CXX-1514, CXX-1249, and DRIVERS-356 for more info.
namespace {

void runcommand_examples(mongocxx::database& db) {
{
Expand All @@ -29,12 +28,14 @@ void runcommand_examples(mongocxx::database& db) {
auto buildInfo = db.run_command(make_document(kvp("buildInfo", 1)));
// End runCommand Example 1

if (buildInfo.view()["ok"].get_double() != double{1}) {
if (buildInfo.view()["ok"].get_double() != 1.0) {
throw std::logic_error("buildInfo command failed in runCommand example 1");
}
}
}

} // namespace

int main() {
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
Expand Down
Loading