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)
{}
// string must be null-terminated
String::String(const char* string)
:String()
{
auto size = std::strlen(string) + 1; // make space for null-terminator
auto data = new char[size];
@ -19,7 +20,7 @@ String::String(const char* string)
_str = new StringValue(data); // initialize new StringValue with data
}
String::String(const String& other)
String::String(const String& other) noexcept
{
_str = other._str;
_str->operator++();
@ -32,7 +33,7 @@ String& String::operator=(String other) // take parameter by value: can bind
return *this;
}
String::~String()
String::~String() noexcept
{
if (_str)
_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);
}
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); }
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);
}
size_t String::size() const
size_t String::size() const noexcept
{
if (_str)
return _str->size();

View File

@ -18,12 +18,12 @@ private:
public:
String();
String() noexcept;
String(const char*);
String(const String&);
String(const String&) noexcept;
String& operator=(String);
String(String&&);
~String();
String(String&&) noexcept;
~String() noexcept;
friend void swap(String&, String&) noexcept;
String operator+(const String&) const;
@ -34,7 +34,7 @@ public:
const char& operator[](size_t) const;
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;
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)
{}
void StringValue::operator++()
void StringValue::operator++() noexcept
{ _refs++; }
void StringValue::operator--()
void StringValue::operator--() noexcept
{
_refs--;
if (_refs == 0)
@ -33,10 +33,10 @@ const char& StringValue::operator[](size_t index) const
char& StringValue::operator[](size_t 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; }
size_t StringValue::size() const
size_t StringValue::size() const noexcept
{ return _size - 1; }
size_t StringValue::index_of(char* c) const

View File

@ -21,12 +21,12 @@ private:
public:
StringValue(char* data);
void operator++();
void operator--();
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;
size_t size() const;
operator const char*() const noexcept;
size_t size() const noexcept;
size_t index_of(char* c) const;
};