catch2/include/internal/catch_stringdata.h

54 lines
1.5 KiB
C
Raw Normal View History

2017-06-29 12:18:14 +02:00
/*
* 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>
2017-08-05 23:53:21 +02:00
#include <cstddef>
2017-06-29 12:18:14 +02:00
namespace Catch {
class StringRef;
class StringData {
mutable std::atomic<unsigned int> m_refs;
public:
2017-08-05 23:53:21 +02:00
size_t size;
2017-06-29 12:18:14 +02:00
union {
char chars[1];
};
auto isUniquelyOwned() const noexcept -> bool {
return m_refs == 1;
}
static auto getEmpty() -> StringData*;
static auto create( StringRef const& stringRef ) -> StringData*;
2017-08-05 23:53:21 +02:00
static auto create( StringRef const& stringRef, size_t capacity ) -> StringData*;
2017-06-29 12:18:14 +02:00
void addRef() const noexcept {
if( m_refs > 0 )
2017-07-25 15:45:50 +02:00
++m_refs;
2017-06-29 12:18:14 +02:00
}
void release() const noexcept {
unsigned int refs = m_refs;
if( refs > 1 )
2017-07-25 15:45:50 +02:00
--m_refs;
2017-06-29 12:18:14 +02:00
else if( refs == 1 )
delete[] reinterpret_cast<char const*>( this );
}
private:
StringData( unsigned int initialRef = 1 );
2017-08-05 23:53:21 +02:00
StringData( StringRef const& stringRef, size_t capacity );
2017-06-29 12:18:14 +02:00
StringData( StringData const& ) = delete;
StringData& operator=( StringData const& ) = delete;
};
} // namespace Catch
#endif // CATCH_STRINGDATA_H_INCLUDED