mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	Removed StringData
This commit is contained in:
		| @@ -1,48 +0,0 @@ | ||||
| /* | ||||
|  *  Copyright 2016 Two Blue Cubes Ltd. All rights reserved. | ||||
|  * | ||||
|  *  Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||||
|  */ | ||||
|  | ||||
| #include "catch_stringdata.h" | ||||
| #include "catch_stringref.h" | ||||
|  | ||||
| #include <new> | ||||
| #include <cstring> | ||||
| #include <cassert> | ||||
|  | ||||
| namespace Catch { | ||||
|      | ||||
|     auto StringData::getEmpty() -> StringData* { | ||||
|         static StringData s_empty( 0 ); | ||||
|         return &s_empty; | ||||
|     } | ||||
|     auto StringData::create( StringRef const& stringRef ) -> StringData* { | ||||
|         return create( stringRef, stringRef.size() ); | ||||
|     } | ||||
|     auto StringData::create( StringRef const& stringRef, size_t capacity ) -> StringData* { | ||||
|         if( capacity == 0 ) { | ||||
|             return getEmpty(); | ||||
|         } | ||||
|         else { | ||||
|             assert( stringRef.size() <= capacity ); | ||||
|             auto bufferLen = sizeof(StringData)+capacity; | ||||
|             void* buffer = new char[bufferLen]; | ||||
|              | ||||
|             return new(buffer) StringData( stringRef, capacity ); | ||||
|         } | ||||
|     } | ||||
|     StringData::StringData( unsigned int initialRef ) | ||||
|     :   m_refs( initialRef ), | ||||
|         size( 0 ) | ||||
|     {} | ||||
|     StringData::StringData( StringRef const& stringRef, size_t capacity ) | ||||
|     :   m_refs( 1 ), | ||||
|         size( capacity) | ||||
|     { | ||||
|         std::memcpy( chars, stringRef.data(), stringRef.size() ); | ||||
|         chars[stringRef.size() ] = 0; | ||||
|     } | ||||
|      | ||||
| } // namespace Catch | ||||
| @@ -1,53 +0,0 @@ | ||||
| /* | ||||
|  *  Copyright 2016 Two Blue Cubes Ltd. All rights reserved. | ||||
|  * | ||||
|  *  Distributed under the Boost Software License, Version 1.0. (See accompanying | ||||
|  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||||
|  */ | ||||
| #ifndef CATCH_STRINGDATA_H_INCLUDED | ||||
| #define CATCH_STRINGDATA_H_INCLUDED | ||||
|  | ||||
| #include <atomic> | ||||
| #include <cstddef> | ||||
|  | ||||
| namespace Catch { | ||||
|      | ||||
|     class StringRef; | ||||
|  | ||||
|     class StringData { | ||||
|         mutable std::atomic<unsigned int> m_refs; | ||||
|     public: | ||||
|         size_t size; | ||||
|         union { | ||||
|             char chars[1]; | ||||
|         }; | ||||
|  | ||||
|         auto isUniquelyOwned() const noexcept -> bool { | ||||
|             return m_refs == 1; | ||||
|         } | ||||
|         static auto getEmpty() -> StringData*; | ||||
|         static auto create( StringRef const& stringRef ) -> StringData*; | ||||
|         static auto create( StringRef const& stringRef, size_t capacity ) -> StringData*; | ||||
|  | ||||
|         void addRef() const noexcept { | ||||
|             if( m_refs > 0 ) | ||||
|                 ++m_refs; | ||||
|         } | ||||
|         void release() const noexcept { | ||||
|             unsigned int refs = m_refs; | ||||
|             if( refs > 1 ) | ||||
|                 --m_refs; | ||||
|             else if( refs == 1 ) | ||||
|                 delete[] reinterpret_cast<char const*>( this ); | ||||
|         } | ||||
|     private: | ||||
|         StringData( unsigned int initialRef = 1 ); | ||||
|         StringData( StringRef const& stringRef, size_t capacity ); | ||||
|          | ||||
|         StringData( StringData const& ) = delete; | ||||
|         StringData& operator=( StringData const& ) = delete; | ||||
|     }; | ||||
|  | ||||
| } // namespace Catch | ||||
|  | ||||
| #endif // CATCH_STRINGDATA_H_INCLUDED | ||||
| @@ -6,7 +6,6 @@ | ||||
|  */ | ||||
|  | ||||
| #include "catch_stringref.h" | ||||
| #include "catch_stringdata.h" | ||||
|  | ||||
| #include <cstring> | ||||
| #include <ostream> | ||||
| @@ -25,12 +24,8 @@ namespace Catch { | ||||
|      | ||||
|     StringRef::StringRef( StringRef const& other ) noexcept | ||||
|     :   m_start( other.m_start ), | ||||
|         m_size( other.m_size ), | ||||
|         m_data( other.m_data ) | ||||
|     { | ||||
|         if( m_data ) | ||||
|             m_data->addRef(); | ||||
|     } | ||||
|         m_size( other.m_size ) | ||||
|     {} | ||||
|      | ||||
|     StringRef::StringRef( StringRef&& other ) noexcept | ||||
|     :   m_start( other.m_start ), | ||||
| @@ -62,8 +57,7 @@ namespace Catch { | ||||
|     {} | ||||
|  | ||||
|     StringRef::~StringRef() noexcept { | ||||
|         if( isOwned() ) | ||||
|             m_data->release(); | ||||
|         delete[] m_data; | ||||
|     } | ||||
|      | ||||
|     auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& { | ||||
| @@ -98,8 +92,10 @@ namespace Catch { | ||||
|      | ||||
|     void StringRef::takeOwnership() { | ||||
|         if( !isOwned() ) { | ||||
|             m_data = StringData::create( *this ); | ||||
|             m_start = m_data->chars; | ||||
|             m_data = new char[m_size+1]; | ||||
|             strncpy( m_data, m_start, m_size ); | ||||
|             m_data[m_size] = '\0'; | ||||
|             m_start = m_data; | ||||
|         } | ||||
|     } | ||||
|     auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef { | ||||
|   | ||||
| @@ -24,14 +24,13 @@ namespace Catch { | ||||
|     /// threads. | ||||
|     class StringRef { | ||||
|         friend struct StringRefTestAccess; | ||||
|         friend class StringData; | ||||
|  | ||||
|         using size_type = size_t; | ||||
|          | ||||
|         char const* m_start; | ||||
|         size_type m_size; | ||||
|          | ||||
|         StringData const* m_data = nullptr; | ||||
|         char* m_data = nullptr; | ||||
|          | ||||
|         void takeOwnership(); | ||||
|          | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash