Skip to content

Commit

Permalink
SafePtr: by AI
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed May 17, 2024
1 parent b8e776c commit a92ba19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// . safe lifecycle: by shared_ptr (auto mem-mgmt, no use-after-free)
// . safe ptr array: no need since std::array
// . safe del : not support self-deletor that maybe unsafe
// . loop-ref : ???
// - DUTY-BOUND:
// . ensure ptr address is safe: legal created, not freed, not wild, etc
// . ensure ptr type is valid: origin*, or base*, or void*
// . not SafePtr but T to ensure T's inner safety (eg no exception within T's constructor)
// . loop-ref: not SafePtr but user to avoid this
// . hope cooperate with tool to ensure/track SafePtr, all T, all code's mem safe
//
// - VALUE:
Expand Down
16 changes: 8 additions & 8 deletions ut/safe_mem/SafePtrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ TEST(SafePtrTest, GOLD_safeCreate_normal)
EXPECT_EQ(43, *i.get()) << "REQ: outside reset not impact SafePtr";
EXPECT_EQ(1, i.use_count()) << "REQ: compatible shared_ptr";
}
TEST(SafePtrTest, GOLD_unsafeCreate_forbid)
{
static_assert(! is_convertible_v<bool*, SafePtr<bool>>, "REQ: forbid SafePtr<T>(new T)");
static_assert(! is_convertible_v<shared_ptr<char>, SafePtr<char>>, "REQ: forbid SafePtr<T>(shared_ptr<T>)");
}
TEST(SafePtrTest, safeCreate_default)
{
SafePtr v;
Expand All @@ -44,7 +49,7 @@ TEST(SafePtrTest, safeCreate_default)
EXPECT_EQ(nullptr, i.get()) << "req: create default is empty";
EXPECT_EQ(type_index(typeid(shared_ptr<int>)), type_index(typeid(i.get()))) << "REQ: specify template";
}
TEST(SafePtrTest, safeCreate_null)
TEST(SafePtrTest, safeCreate_null) // convenient usage (convert nullptr to SafePtr)
{
const SafePtr v(nullptr);
EXPECT_EQ(nullptr, v.get()) << "REQ: explicit create null to compatible with shared_ptr";
Expand All @@ -59,16 +64,11 @@ TEST(SafePtrTest, safeCreate_noexcept_constexpr)
static_assert(is_nothrow_constructible_v<SafePtr<>>, "REQ: noexcept; optional: constexpr");
static_assert(is_nothrow_constructible_v<SafePtr<int>, nullptr_t>, "REQ: noexcept; optional: constexpr");
}
TEST(SafePtrTest, unsafeCreate_forbid)
{
static_assert(! is_convertible_v<bool*, SafePtr<bool>>, "REQ: forbid SafePtr<T>(new T(..)");
static_assert(! is_convertible_v<shared_ptr<char>, SafePtr<char>>, "REQ: forbid SafePtr<T>(make_shared<T>(..))");
}

#define CAST
// ***********************************************************************************************
struct Base { virtual int value() const { return 0; } };
struct Derive : public Base { int value() const override { return 1; } };
struct Base { virtual int value() const { return 0; } };
struct Derive : public Base { int value() const override { return 1; } };
TEST(SafePtrTest, GOLD_safeCast_self_base_void_back)
{
auto d = make_safe<Derive>();
Expand Down

0 comments on commit a92ba19

Please sign in to comment.