added noexcept qualifiers where needed

This commit is contained in:
Kjistóf 2016-10-25 14:22:50 +02:00
parent 8ef6635b9d
commit b74a35d1ec
4 changed files with 21 additions and 20 deletions

View File

@ -5,12 +5,13 @@
String::String() String::String() noexcept
:_str(nullptr) :_str(nullptr)
{} {}
// string must be null-terminated // string must be null-terminated
String::String(const char* string) String::String(const char* string)
:String()
{ {
auto size = std::strlen(string) + 1; // make space for null-terminator auto size = std::strlen(string) + 1; // make space for null-terminator
auto data = new char[size]; auto data = new char[size];
@ -19,7 +20,7 @@ String::String(const char* string)
_str = new StringValue(data); // initialize new StringValue with data _str = new StringValue(data); // initialize new StringValue with data
} }
String::String(const String& other) String::String(const String& other) noexcept
{ {
_str = other._str; _str = other._str;
_str->operator++(); _str->operator++();
@ -32,7 +33,7 @@ String& String::operator=(String other) // take parameter by value: can bind
return *this; return *this;
} }
String::~String() String::~String() noexcept
{ {
if (_str) if (_str)
_str->operator--(); // the ref-counting is done by StringValue, no need to do that here _str->operator--(); // the ref-counting is done by StringValue, no need to do that here
@ -45,7 +46,7 @@ void swap(String& s1, String& s2) noexcept
swap(s1._str, s2._str); swap(s1._str, s2._str);
} }
String::String(String&& other) : String() // init via default ctor so _str is a nullptr String::String(String&& other) noexcept : String() // init via default ctor so _str is a nullptr
{ swap(*this, other); } { swap(*this, other); }
String String::operator+(const String& rhs) const String String::operator+(const String& rhs) const
@ -120,7 +121,7 @@ Char String::operator[](size_t index)
return Char(_str->index_of(&const_cast<char&>(const_cast<const String*>(this)->operator[](index))), *this); return Char(_str->index_of(&const_cast<char&>(const_cast<const String*>(this)->operator[](index))), *this);
} }
size_t String::size() const size_t String::size() const noexcept
{ {
if (_str) if (_str)
return _str->size(); return _str->size();

View File

@ -18,12 +18,12 @@ private:
public: public:
String(); String() noexcept;
String(const char*); String(const char*);
String(const String&); String(const String&) noexcept;
String& operator=(String); String& operator=(String);
String(String&&); String(String&&) noexcept;
~String(); ~String() noexcept;
friend void swap(String&, String&) noexcept; friend void swap(String&, String&) noexcept;
String operator+(const String&) const; String operator+(const String&) const;
@ -34,7 +34,7 @@ public:
const char& operator[](size_t) const; const char& operator[](size_t) const;
Char operator[](size_t); Char operator[](size_t);
size_t size() const; // does not include null-terminator size_t size() const noexcept; // does not include null-terminator
const char* c_str() const; const char* c_str() const;
friend std::istream& operator>>(std::istream&, String&); friend std::istream& operator>>(std::istream&, String&);

View File

@ -4,14 +4,14 @@
StringValue::StringValue(char* data) StringValue::StringValue(char* data) noexcept
:_data(data), _size(std::strlen(data) + 1), _refs(1) :_data(data), _size(std::strlen(data) + 1), _refs(1)
{} {}
void StringValue::operator++() void StringValue::operator++() noexcept
{ _refs++; } { _refs++; }
void StringValue::operator--() void StringValue::operator--() noexcept
{ {
_refs--; _refs--;
if (_refs == 0) if (_refs == 0)
@ -33,10 +33,10 @@ const char& StringValue::operator[](size_t index) const
char& StringValue::operator[](size_t index) char& StringValue::operator[](size_t index)
{ return const_cast<char&>(const_cast<const StringValue*>(this)->operator[](index)); } { return const_cast<char&>(const_cast<const StringValue*>(this)->operator[](index)); }
StringValue::operator const char*() const StringValue::operator const char*() const noexcept
{ return _data; } { return _data; }
size_t StringValue::size() const size_t StringValue::size() const noexcept
{ return _size - 1; } { return _size - 1; }
size_t StringValue::index_of(char* c) const size_t StringValue::index_of(char* c) const

View File

@ -21,12 +21,12 @@ private:
public: public:
StringValue(char* data); StringValue(char* data) noexcept;
void operator++(); void operator++() noexcept;
void operator--(); void operator--() noexcept;
const char& operator[](size_t index) const; const char& operator[](size_t index) const;
char& operator[](size_t index); char& operator[](size_t index);
operator const char*() const; operator const char*() const noexcept;
size_t size() const; size_t size() const noexcept;
size_t index_of(char* c) const; size_t index_of(char* c) const;
}; };