Skip to content
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

FieldLocation shared ptr #89

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test(field_location): fix make_shared
  • Loading branch information
mcakircali committed Feb 19, 2025
commit 24abfca5d788fe7e55f80dcda44ea08addb9108b
15 changes: 8 additions & 7 deletions tests/fdb/database/test_fieldlocation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#include "fdb5/database/FieldLocation.h"
#include "fdb5/toc/TocFieldLocation.h"

#include "eckit/filesystem/URI.h"
#include "eckit/testing/Test.h"
Expand All @@ -27,32 +28,32 @@ CASE("FieldLocation - shared_ptr") {

// GOOD (not best)
{
auto location = std::shared_ptr<fdb5::FieldLocation>(fdb5::FieldLocationFactory::instance().build("file", uri));
auto location = fdb5::FieldLocationFactory::instance().build("file", uri);

auto loc1 = location->sharedPtr();
auto loc1 = location->shared_from_this();
EXPECT_EQUAL(loc1.use_count(), 2);

auto loc2 = location->sharedPtr();
auto loc2 = location->shared_from_this();
EXPECT_EQUAL(loc2.use_count(), 3);

// check that the shared pointers are the same
EXPECT_EQUAL(loc2, loc1);

{
auto loc3 = location->sharedPtr();
auto loc3 = location->shared_from_this();
EXPECT_EQUAL(loc3.use_count(), 4);
EXPECT_EQUAL(loc3, loc1);
}

auto loc4 = location->sharedPtr();
auto loc4 = location->shared_from_this();
EXPECT_EQUAL(loc4.use_count(), 4);
EXPECT_EQUAL(loc4, loc1);
}

// BAD: this is a how NOT to use shared_ptr on FieldLocation
{
auto location = std::unique_ptr<fdb5::FieldLocation>(fdb5::FieldLocationFactory::instance().build("file", uri));
EXPECT_THROWS_AS(location->sharedPtr(), std::bad_weak_ptr);
auto location = new fdb5::TocFieldLocation("dummy_path", 0, 0, fdb5::Key{});
EXPECT_THROWS_AS(location->shared_from_this(), std::bad_weak_ptr);
}
}

Expand Down