refcountedstring/StringValue.h

33 lines
915 B
C++

#pragma once
#include <stdexcept>
#include <cstring>
/* This class claims ownership of:
*
* - The char* passed to it in its ctor
* - Itself (sounds funny) -> When no longer referenced the object deletes it's own memory,
* therefore objects from this class should only be constructed
* via operator new. This might seem like an odd design choice,
* but */
class StringValue
{
private:
char* _data;
size_t _size; // with null-terminator
size_t _refs;
public:
StringValue(char* data) noexcept;
void operator++() noexcept;
void operator--() noexcept;
const char& operator[](size_t index) const;
char& operator[](size_t index);
operator const char*() const noexcept;
size_t size() const noexcept;
size_t index_of(char* c) const;
};