ref
This commit is contained in:
@@ -10,9 +10,12 @@ namespace TinySTL{
|
|||||||
std::atomic<size_t> ncount_;
|
std::atomic<size_t> ncount_;
|
||||||
T *data_;
|
T *data_;
|
||||||
|
|
||||||
explicit ref_t(size_t n = 0, T *p = nullptr) :ncount_(n), data_(p){}
|
explicit ref_t(T *p = nullptr): ncount_(0), data_(p){
|
||||||
ref_t(const ref_t&) = default;
|
if (data_)
|
||||||
ref_t& operator = (const ref_t&) = default;
|
ncount_ = 1;
|
||||||
|
}
|
||||||
|
ref_t(const ref_t&) = delete;
|
||||||
|
ref_t& operator = (const ref_t&) = delete;
|
||||||
|
|
||||||
size_t count()const{ return ncount_.load(); }
|
size_t count()const{ return ncount_.load(); }
|
||||||
T *get_data()const{ return data_; }
|
T *get_data()const{ return data_; }
|
||||||
@@ -36,6 +39,14 @@ namespace TinySTL{
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
template<class T>
|
||||||
|
bool operator ==(const ref_t<T>& lhs, const ref_t<T>& rhs){
|
||||||
|
return lhs.get_data() == rhs.get_data();
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
bool operator !=(const ref_t<T>& lhs, const ref_t<T>& rhs){
|
||||||
|
return !(lhs == rhs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
26
TinySTL/Test/RefTest.cpp
Normal file
26
TinySTL/Test/RefTest.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include "RefTest.h"
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace RefTest{
|
||||||
|
void testCaseRef(){
|
||||||
|
ref_t<int> r1;
|
||||||
|
assert(r1.count() == 0);
|
||||||
|
assert(r1.get_data() == nullptr);
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
ref_t<int> r2(&n);
|
||||||
|
assert(r2.count() == 1);
|
||||||
|
assert(r2.get_data() != nullptr);
|
||||||
|
|
||||||
|
++r2;
|
||||||
|
assert(r2.count() == 2);
|
||||||
|
|
||||||
|
--r2;
|
||||||
|
assert(r2.count() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testAllCases(){
|
||||||
|
testCaseRef();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
TinySTL/Test/RefTest.h
Normal file
18
TinySTL/Test/RefTest.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef _REF_TEST_H_
|
||||||
|
#define _REF_TEST_H_
|
||||||
|
|
||||||
|
#include "../Detail/Ref.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace RefTest{
|
||||||
|
template<class T>
|
||||||
|
using ref_t = TinySTL::Detail::ref_t < T > ;
|
||||||
|
|
||||||
|
void testCaseRef();
|
||||||
|
void testAllCases();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -96,6 +96,7 @@
|
|||||||
<ClCompile Include="Test\PairTest.cpp" />
|
<ClCompile Include="Test\PairTest.cpp" />
|
||||||
<ClCompile Include="Test\PriorityQueueTest.cpp" />
|
<ClCompile Include="Test\PriorityQueueTest.cpp" />
|
||||||
<ClCompile Include="Test\QueueTest.cpp" />
|
<ClCompile Include="Test\QueueTest.cpp" />
|
||||||
|
<ClCompile Include="Test\RefTest.cpp" />
|
||||||
<ClCompile Include="Test\StackTest.cpp" />
|
<ClCompile Include="Test\StackTest.cpp" />
|
||||||
<ClCompile Include="Test\StringTest.cpp" />
|
<ClCompile Include="Test\StringTest.cpp" />
|
||||||
<ClCompile Include="Test\SuffixArrayTest.cpp" />
|
<ClCompile Include="Test\SuffixArrayTest.cpp" />
|
||||||
@@ -146,6 +147,7 @@
|
|||||||
<ClInclude Include="Test\PairTest.h" />
|
<ClInclude Include="Test\PairTest.h" />
|
||||||
<ClInclude Include="Test\PriorityQueueTest.h" />
|
<ClInclude Include="Test\PriorityQueueTest.h" />
|
||||||
<ClInclude Include="Test\QueueTest.h" />
|
<ClInclude Include="Test\QueueTest.h" />
|
||||||
|
<ClInclude Include="Test\RefTest.h" />
|
||||||
<ClInclude Include="Test\StackTest.h" />
|
<ClInclude Include="Test\StackTest.h" />
|
||||||
<ClInclude Include="Test\StringTest.h" />
|
<ClInclude Include="Test\StringTest.h" />
|
||||||
<ClInclude Include="Test\SuffixArrayTest.h" />
|
<ClInclude Include="Test\SuffixArrayTest.h" />
|
||||||
|
|||||||
@@ -99,6 +99,9 @@
|
|||||||
<ClCompile Include="Test\UniquePtrTest.cpp">
|
<ClCompile Include="Test\UniquePtrTest.cpp">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Test\RefTest.cpp">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="TypeTraits.h">
|
<ClInclude Include="TypeTraits.h">
|
||||||
@@ -266,6 +269,9 @@
|
|||||||
<ClInclude Include="Detail\Ref.h">
|
<ClInclude Include="Detail\Ref.h">
|
||||||
<Filter>Detail</Filter>
|
<Filter>Detail</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Test\RefTest.h">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\README.md" />
|
<None Include="..\README.md" />
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "Test\PairTest.h"
|
#include "Test\PairTest.h"
|
||||||
#include "Test\PriorityQueueTest.h"
|
#include "Test\PriorityQueueTest.h"
|
||||||
#include "Test\QueueTest.h"
|
#include "Test\QueueTest.h"
|
||||||
|
#include "Test\RefTest.h"
|
||||||
#include "Test\StackTest.h"
|
#include "Test\StackTest.h"
|
||||||
#include "Test\StringTest.h"
|
#include "Test\StringTest.h"
|
||||||
#include "Test\SuffixArrayTest.h"
|
#include "Test\SuffixArrayTest.h"
|
||||||
@@ -36,6 +37,7 @@ int main(){
|
|||||||
TinySTL::PairTest::testAllCases();
|
TinySTL::PairTest::testAllCases();
|
||||||
TinySTL::PriorityQueueTest::testAllCases();
|
TinySTL::PriorityQueueTest::testAllCases();
|
||||||
TinySTL::QueueTest::testAllCases();
|
TinySTL::QueueTest::testAllCases();
|
||||||
|
TinySTL::RefTest::testAllCases();
|
||||||
TinySTL::StackTest::testAllCases();
|
TinySTL::StackTest::testAllCases();
|
||||||
TinySTL::StringTest::testAllCases();
|
TinySTL::StringTest::testAllCases();
|
||||||
TinySTL::SuffixArrayTest::testAllCases();
|
TinySTL::SuffixArrayTest::testAllCases();
|
||||||
|
|||||||
Reference in New Issue
Block a user