refcountedstring/StringValue.h

32 lines
850 B
C++

#pragma once
#include <stddef.h>
/* 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);
void operator++();
void operator--();
const char& operator[](size_t index) const;
char& operator[](size_t index);
operator const char*() const;
size_t size() const;
size_t index_of(char* c) const;
};