Skip to content

Commit

Permalink
SmartPtr: Support shared ptr and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 3, 2024
1 parent 1656391 commit 6f1c749
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
72 changes: 72 additions & 0 deletions trunk/src/core/srs_core_autofree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,76 @@ class impl_SrsAutoFree
}
};

// Shared ptr smart pointer, see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107
// Usage:
// SrsSharedPtr<MyClass> ptr(new MyClass());
// ptr->do_something();
//
// SrsSharedPtr<MyClass> cp = ptr;
// cp->do_something();
template<class T>
class SrsSharedPtr
{
private:
// The pointer to the object.
T* ptr_;
// The reference count of the object.
uint32_t* ref_count_;
public:
// Create a shared ptr with the object.
SrsSharedPtr(T* ptr) {
ptr_ = ptr;
ref_count_ = new uint32_t(1);
}
// Copy the shared ptr.
SrsSharedPtr(const SrsSharedPtr<T>& cp) {
ptr_ = cp.ptr_;
ref_count_ = cp.ref_count_;
if (ref_count_) (*ref_count_)++;
}
// Dispose and delete the shared ptr.
~SrsSharedPtr() {
reset();
}
private:
// Reset the shared ptr.
void reset() {
if (!ref_count_) return;

(*ref_count_)--;
if (*ref_count_ == 0) {
delete ptr_;
delete ref_count_;
}

ptr_ = NULL;
ref_count_ = NULL;
}
public:
// Get the object.
T* get() {
return ptr_;
}
// Overload the -> operator.
T* operator->() {
return ptr_;
}
private:
// Overload the * operator.
T& operator*() {
return *ptr_;
}
// Overload the bool operator.
operator bool() const {
return ptr_ != NULL;
}
private:
// Disable the assign operator.
SrsSharedPtr<T>& operator=(const SrsSharedPtr<T>&);
// Disable the move constructor.
SrsSharedPtr(SrsSharedPtr<T>&&);
// Disable the move assign operator.
SrsSharedPtr<T>& operator=(SrsSharedPtr<T>&&);
};

#endif
70 changes: 70 additions & 0 deletions trunk/src/utest/srs_utest_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,73 @@ VOID TEST(CoreLogger, CheckVsnprintf)
}
}

VOID TEST(CoreLogger, SharedPtrTypical)
{
if (true) {
SrsSharedPtr<int> p(new int(100));
EXPECT_TRUE(p);
EXPECT_EQ(100, *p);
}

if (true) {
SrsSharedPtr<int> p = SrsSharedPtr<int>(new int(100));
EXPECT_TRUE(p);
EXPECT_EQ(100, *p);
}

if (true) {
SrsSharedPtr<int> p(new int(100));
SrsSharedPtr<int> q = p;
EXPECT_EQ(p.get(), q.get());
}

if (true) {
SrsSharedPtr<int> p(new int(100));
SrsSharedPtr<int> q = p;
EXPECT_TRUE(p);
EXPECT_TRUE(q);
EXPECT_EQ(100, *p);
EXPECT_EQ(100, *q);
}
}

VOID TEST(CoreLogger, SharedPtrReset)
{
if (true) {
SrsSharedPtr<int> p(new int(100));
SrsSharedPtr<int> q = p;
p.reset();
EXPECT_FALSE(p);
EXPECT_TRUE(q);
EXPECT_EQ(100, *q);
}

if (true) {
SrsSharedPtr<int> p(new int(100));
SrsSharedPtr<int> q = p;
q.reset();
EXPECT_TRUE(p);
EXPECT_FALSE(q);
EXPECT_EQ(100, *p);
}
}

VOID TEST(CoreLogger, SharedPtrObject)
{
SrsSharedPtr<MyNormalObject> p(new MyNormalObject(100));
EXPECT_TRUE(p);
EXPECT_EQ(100, p->id());
}

VOID TEST(CoreLogger, SharedPtrNullptr)
{
SrsSharedPtr<int> p(NULL);
EXPECT_FALSE(p);

p.reset();
EXPECT_FALSE(p);

SrsSharedPtr<int> q = p;
EXPECT_FALSE(q);
}

13 changes: 13 additions & 0 deletions trunk/src/utest/srs_utest_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,18 @@

#include <string>

class MyNormalObject
{
private:
int id_;
public:
MyNormalObject(int id) {
id_ = id;
}
int id() {
return id_;
}
};

#endif

0 comments on commit 6f1c749

Please sign in to comment.