diff --git a/TinySTL/Detail/COWPtr.impl.h b/TinySTL/Detail/COWPtr.impl.h new file mode 100644 index 0000000000000000000000000000000000000000..69711f5a8599fd65b9f40e008b9b1747773dd313 --- /dev/null +++ b/TinySTL/Detail/COWPtr.impl.h @@ -0,0 +1,32 @@ +#ifndef _COWPTR_IMPL_H_ +#define _COWPTR_IMPL_H_ + +namespace TinySTL{ + template + cow_ptr::cow_ptr(T *p = nullptr) :ptr_(p){} + template + template + cow_ptr::cow_ptr(T *p, D d) : ptr_(p, d){} + template + cow_ptr::cow_ptr(const cow_ptr& cp){ + ptr_ = cp.ptr_; + } + template + cow_ptr& cow_ptr::operator = (const cow_ptr& cp){ + if (this != &cp){ + ptr_.decrease_ref(); + ptr_ = cp.ptr_; + } + return *this; + } + template + typename cow_ptr::element_type cow_ptr::operator *()const{ + return *ptr_; + } + template + typename cow_ptr::element_type *cow_ptr::operator ->()const{ + return ptr_.operator->(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/Test/COWPtrTest.cpp b/TinySTL/Test/COWPtrTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5cf34783eb1644606165940ed4aae137bcdec42 --- /dev/null +++ b/TinySTL/Test/COWPtrTest.cpp @@ -0,0 +1,24 @@ +#include "COWPtrTest.h" + +#include "../String.h" + +namespace TinySTL{ + namespace COWPtrTest{ + void testCase1(){ + cow_ptr cp1(new string("hello")); + assert(*cp1 == "hello"); + + cp1->append(" world"); + auto cp2 = cp1; + assert(*cp2 == "hello world"); + + cow_ptr cp3; + cp3 = cp1; + assert(*cp3 == "hello world"); + } + + void testAllCases(){ + testCase1(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/COWPtrTest.h b/TinySTL/Test/COWPtrTest.h new file mode 100644 index 0000000000000000000000000000000000000000..bb7499d9becb2cd93c536d2278dc2d61f21434be --- /dev/null +++ b/TinySTL/Test/COWPtrTest.h @@ -0,0 +1,16 @@ +#ifndef _COWPTR_TEST_H_ +#define _COWPTR_TEST_H_ + +#include "../COWPtr.h" + +#include + +namespace TinySTL{ + namespace COWPtrTest{ + void testCase1(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 222161f3ff328d6a73bb70325ad5920b92e6cb93..89016799a8a1bde8f59e173b79352565d656e54e 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -82,7 +82,7 @@ - + @@ -90,6 +90,7 @@ + @@ -115,11 +116,13 @@ + + @@ -142,6 +145,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index b8069281aaf2901fe091e549902a4f6f20ac2c6a..166fc9779f5f07a5580e64af142d2c63590fa4b5 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -90,9 +90,6 @@ Test - - Detail - Test @@ -105,6 +102,12 @@ Test + + Detail + + + Test + @@ -278,6 +281,15 @@ Test + + 头文件 + + + Detail + + + Test + diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index c6a1757a1963a20cf3992a69bbbd718020bb9fb5..4c8bd6045237ef05c2b9ea16f33388ee273b8868 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -8,6 +8,7 @@ #include "Test\BitmapTest.h" #include "Test\BinarySearchTreeTest.h" #include "Test\CircularBufferTest.h" +#include "Test\COWPtrTest.h" #include "Test\DequeTest.h" #include "Test\GraphTest.h" #include "Test\ListTest.h" @@ -32,6 +33,7 @@ int main(){ TinySTL::BitmapTest::testAllCases(); TinySTL::BinarySearchTreeTest::testAllCases(); TinySTL::CircularBufferTest::testAllCases(); + TinySTL::COWPtrTest::testAllCases(); TinySTL::DequeTest::testAllCases(); TinySTL::ListTest::testAllCases(); TinySTL::GraphTest::testAllCases();