Removed StringData

This commit is contained in:
Phil Nash 2017-08-14 09:14:49 +01:00
parent 3772f69f0f
commit 94d347b059
5 changed files with 8 additions and 116 deletions

View File

@ -170,7 +170,6 @@ set(INTERNAL_HEADERS
${HEADER_DIR}/internal/catch_startup_exception_registry.h
${HEADER_DIR}/internal/catch_stream.h
${HEADER_DIR}/internal/catch_streambuf.h
${HEADER_DIR}/internal/catch_stringdata.h
${HEADER_DIR}/internal/catch_stringref.h
${HEADER_DIR}/internal/catch_string_manip.h
${HEADER_DIR}/internal/catch_suppress_warnings.h
@ -222,7 +221,6 @@ set(IMPL_SOURCES
${HEADER_DIR}/internal/catch_section_info.cpp
${HEADER_DIR}/internal/catch_startup_exception_registry.cpp
${HEADER_DIR}/internal/catch_stream.cpp
${HEADER_DIR}/internal/catch_stringdata.cpp
${HEADER_DIR}/internal/catch_stringref.cpp
${HEADER_DIR}/internal/catch_string_manip.cpp
${HEADER_DIR}/internal/catch_tag_alias.cpp

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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();