add more operations
This commit is contained in:
@@ -16,11 +16,31 @@ namespace TinySTL{
|
||||
cow_ptr(const cow_ptr& cp);
|
||||
cow_ptr& operator = (const cow_ptr& cp);
|
||||
|
||||
element_type operator *()const;
|
||||
element_type *operator ->()const;
|
||||
const element_type& operator *()const;
|
||||
const element_type *operator ->()const;
|
||||
//ע<><D7A2> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB>ı<EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ҫcow<6F><77><EFBFBD><EFBFBD>
|
||||
//element_type& operator *();
|
||||
//element_type *operator ->();
|
||||
|
||||
element_type *get();
|
||||
const element_type *get()const;
|
||||
|
||||
operator bool() const;
|
||||
private:
|
||||
shared_ptr<element_type> ptr_;
|
||||
|
||||
public:
|
||||
template<class T1, class T2>
|
||||
friend bool operator == (const cow_ptr<T1>& cp1, const cow_ptr<T2>& cp2);
|
||||
template<class T>
|
||||
friend bool operator == (const cow_ptr<T>& cp, nullptr_t p);
|
||||
template<class T>
|
||||
friend bool operator == (nullptr_t p, const cow_ptr<T>& cp);
|
||||
template<class T1, class T2>
|
||||
friend bool operator != (const cow_ptr<T1>& cp1, const cow_ptr<T2>& cp2);
|
||||
template<class T>
|
||||
friend bool operator != (const cow_ptr<T>& cp, nullptr_t p);
|
||||
template<class T>
|
||||
friend bool operator != (nullptr_t p, const cow_ptr<T>& cp);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -20,13 +20,55 @@ namespace TinySTL{
|
||||
return *this;
|
||||
}
|
||||
template<class T>
|
||||
typename cow_ptr<T>::element_type cow_ptr<T>::operator *()const{
|
||||
const typename cow_ptr<T>::element_type& cow_ptr<T>::operator *()const{
|
||||
return *ptr_;
|
||||
}
|
||||
template<class T>
|
||||
typename cow_ptr<T>::element_type *cow_ptr<T>::operator ->()const{
|
||||
const typename cow_ptr<T>::element_type *cow_ptr<T>::operator ->()const{
|
||||
return ptr_.operator->();
|
||||
}
|
||||
//ע<><D7A2> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB>ı<EFBFBD>ָ<EFBFBD><D6B8>ָ<EFBFBD><D6B8><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ҫcow<6F><77><EFBFBD><EFBFBD>
|
||||
//template<class T>
|
||||
//typename cow_ptr<T>::element_type& cow_ptr<T>::operator *();
|
||||
//template<class T>
|
||||
//typename cow_ptr<T>::element_type *cow_ptr<T>::operator ->();
|
||||
template<class T>
|
||||
typename cow_ptr<T>::element_type *cow_ptr<T>::get(){
|
||||
return ptr_.get();
|
||||
}
|
||||
template<class T>
|
||||
const typename cow_ptr<T>::element_type *cow_ptr<T>::get()const{
|
||||
return ptr_.get();
|
||||
}
|
||||
template<class T>
|
||||
cow_ptr<T>::operator bool()const{
|
||||
return ptr_ != nullptr;
|
||||
}
|
||||
|
||||
template<class T1, class T2>
|
||||
bool operator == (const cow_ptr<T1>& cp1, const cow_ptr<T2>& cp2){
|
||||
return cp1.ptr_ == cp2.ptr_;
|
||||
}
|
||||
template<class T>
|
||||
bool operator == (const cow_ptr<T>& cp, nullptr_t p){
|
||||
return cp.ptr_ == p;
|
||||
}
|
||||
template<class T>
|
||||
bool operator == (nullptr_t p, const cow_ptr<T>& cp){
|
||||
return cp == p;
|
||||
}
|
||||
template<class T1, class T2>
|
||||
bool operator != (const cow_ptr<T1>& cp1, const cow_ptr<T2>& cp2){
|
||||
return !(cp1 == cp2);
|
||||
}
|
||||
template<class T>
|
||||
bool operator != (const cow_ptr<T>& cp, nullptr_t p){
|
||||
return !(cp == p);
|
||||
}
|
||||
template<class T>
|
||||
bool operator != (nullptr_t p, const cow_ptr<T>& cp){
|
||||
return !(cp == p);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,14 +7,21 @@ namespace TinySTL{
|
||||
void testCase1(){
|
||||
cow_ptr<string> cp1(new string("hello"));
|
||||
assert(*cp1 == "hello");
|
||||
assert(cp1);
|
||||
|
||||
cp1->append(" world");
|
||||
auto cp2 = cp1;
|
||||
assert(*cp2 == "hello world");
|
||||
assert(*cp2 == "hello");
|
||||
|
||||
cow_ptr<string> cp3;
|
||||
cp3 = cp1;
|
||||
assert(*cp3 == "hello world");
|
||||
assert(*cp3 == "hello");
|
||||
|
||||
assert(cp1.get() == cp2.get() && cp2.get() == cp3.get());
|
||||
|
||||
assert(cp1 == cp2 && !(cp2 != cp3));
|
||||
|
||||
cow_ptr<string> cp4;
|
||||
assert(cp4 == nullptr);
|
||||
}
|
||||
|
||||
void testAllCases(){
|
||||
|
||||
Reference in New Issue
Block a user