added header files for String and StringValue

This commit is contained in:
Kjistóf 2016-10-23 03:45:46 +02:00
parent 7ee0ebcfac
commit 9311aa6938
2 changed files with 72 additions and 0 deletions

41
String.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include <iostream>
#include "StringValue.h"
/* Design decisions:
* - Everything related to ref-counting is the responsibility of the StringValue class
* - Anything dynamically allocated is owned (and thus once deleted) by the StringValue class
* (including itself -- this is possible because of the reference counting mechanism) */
class String
{
private:
StringValue* _str;
public:
String();
String(const char*);
String(const String&);
String& operator=(String);
String(String&&);
~String();
friend void swap(String&, String&) noexcept;
String operator+(const String&) const;
String& operator+=(const String&);
String operator+(char) const;
String& operator+=(char);
bool operator==(const String&) const;
const char& operator[](size_t) const;
char& operator[](size_t);
size_t size() const; // does not include null-terminator
const char* c_str() const;
friend std::istream& operator>>(std::istream&, String&);
};
std::ostream& operator<<(std::ostream&, const String&);

31
StringValue.h Normal file
View File

@ -0,0 +1,31 @@
#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;
};