mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 15:26:11 +01:00
Removed StringData
This commit is contained in:
parent
3772f69f0f
commit
94d347b059
@ -170,7 +170,6 @@ set(INTERNAL_HEADERS
|
|||||||
${HEADER_DIR}/internal/catch_startup_exception_registry.h
|
${HEADER_DIR}/internal/catch_startup_exception_registry.h
|
||||||
${HEADER_DIR}/internal/catch_stream.h
|
${HEADER_DIR}/internal/catch_stream.h
|
||||||
${HEADER_DIR}/internal/catch_streambuf.h
|
${HEADER_DIR}/internal/catch_streambuf.h
|
||||||
${HEADER_DIR}/internal/catch_stringdata.h
|
|
||||||
${HEADER_DIR}/internal/catch_stringref.h
|
${HEADER_DIR}/internal/catch_stringref.h
|
||||||
${HEADER_DIR}/internal/catch_string_manip.h
|
${HEADER_DIR}/internal/catch_string_manip.h
|
||||||
${HEADER_DIR}/internal/catch_suppress_warnings.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_section_info.cpp
|
||||||
${HEADER_DIR}/internal/catch_startup_exception_registry.cpp
|
${HEADER_DIR}/internal/catch_startup_exception_registry.cpp
|
||||||
${HEADER_DIR}/internal/catch_stream.cpp
|
${HEADER_DIR}/internal/catch_stream.cpp
|
||||||
${HEADER_DIR}/internal/catch_stringdata.cpp
|
|
||||||
${HEADER_DIR}/internal/catch_stringref.cpp
|
${HEADER_DIR}/internal/catch_stringref.cpp
|
||||||
${HEADER_DIR}/internal/catch_string_manip.cpp
|
${HEADER_DIR}/internal/catch_string_manip.cpp
|
||||||
${HEADER_DIR}/internal/catch_tag_alias.cpp
|
${HEADER_DIR}/internal/catch_tag_alias.cpp
|
||||||
|
@ -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_stringref.h"
|
||||||
#include "catch_stringdata.h"
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
@ -25,12 +24,8 @@ namespace Catch {
|
|||||||
|
|
||||||
StringRef::StringRef( StringRef const& other ) noexcept
|
StringRef::StringRef( StringRef const& other ) noexcept
|
||||||
: m_start( other.m_start ),
|
: m_start( other.m_start ),
|
||||||
m_size( other.m_size ),
|
m_size( other.m_size )
|
||||||
m_data( other.m_data )
|
{}
|
||||||
{
|
|
||||||
if( m_data )
|
|
||||||
m_data->addRef();
|
|
||||||
}
|
|
||||||
|
|
||||||
StringRef::StringRef( StringRef&& other ) noexcept
|
StringRef::StringRef( StringRef&& other ) noexcept
|
||||||
: m_start( other.m_start ),
|
: m_start( other.m_start ),
|
||||||
@ -62,8 +57,7 @@ namespace Catch {
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
StringRef::~StringRef() noexcept {
|
StringRef::~StringRef() noexcept {
|
||||||
if( isOwned() )
|
delete[] m_data;
|
||||||
m_data->release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& {
|
auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& {
|
||||||
@ -98,8 +92,10 @@ namespace Catch {
|
|||||||
|
|
||||||
void StringRef::takeOwnership() {
|
void StringRef::takeOwnership() {
|
||||||
if( !isOwned() ) {
|
if( !isOwned() ) {
|
||||||
m_data = StringData::create( *this );
|
m_data = new char[m_size+1];
|
||||||
m_start = m_data->chars;
|
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 {
|
auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef {
|
||||||
|
@ -24,14 +24,13 @@ namespace Catch {
|
|||||||
/// threads.
|
/// threads.
|
||||||
class StringRef {
|
class StringRef {
|
||||||
friend struct StringRefTestAccess;
|
friend struct StringRefTestAccess;
|
||||||
friend class StringData;
|
|
||||||
|
|
||||||
using size_type = size_t;
|
using size_type = size_t;
|
||||||
|
|
||||||
char const* m_start;
|
char const* m_start;
|
||||||
size_type m_size;
|
size_type m_size;
|
||||||
|
|
||||||
StringData const* m_data = nullptr;
|
char* m_data = nullptr;
|
||||||
|
|
||||||
void takeOwnership();
|
void takeOwnership();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user