made some final changes to the comments

This commit is contained in:
Kjistóf 2016-10-29 20:08:55 +02:00
parent f284a9a471
commit bdfc14357a
4 changed files with 8 additions and 4 deletions

View File

@ -151,8 +151,9 @@ std::istream& operator>>(std::istream& is, String& str)
{ {
constexpr size_t BUFSIZE = 50; // set some reasonable buffer size constexpr size_t BUFSIZE = 50; // set some reasonable buffer size
char buf[BUFSIZE]; char buf[BUFSIZE];
std::unique_ptr<char[]> data(new char[1]); // istream data - I use unique_ptr for exception safety std::unique_ptr<char[]> data(new char[1]); // istream data - unique_ptr for exception safety
data.get()[0] = '\0'; // init it with a null terminator data.get()[0] = '\0'; // init it with a null terminator
while (is.get(buf, BUFSIZE)) // while there is still some data in the stream while (is.get(buf, BUFSIZE)) // while there is still some data in the stream
{ {
std::unique_ptr<char[]> newdata(new char[strlen(data.get()) + BUFSIZE]); // make some space std::unique_ptr<char[]> newdata(new char[strlen(data.get()) + BUFSIZE]); // make some space
@ -160,6 +161,7 @@ std::istream& operator>>(std::istream& is, String& str)
data.swap(newdata); // make the old data go out of scope instead of newdata data.swap(newdata); // make the old data go out of scope instead of newdata
std::strcat(data.get(), buf); // append new input std::strcat(data.get(), buf); // append new input
} }
std::unique_ptr<char[]> newdata(new char[strlen(data.get()) + 1]); // shrink the array std::unique_ptr<char[]> newdata(new char[strlen(data.get()) + 1]); // shrink the array
std::strcpy(newdata.get(), data.get()); // to fit the data std::strcpy(newdata.get(), data.get()); // to fit the data
data.swap(newdata); data.swap(newdata);

View File

@ -28,7 +28,7 @@ const char& StringValue::operator[](size_t index) const
return _data[index]; return _data[index];
} }
// reuse const operator[], just as Scott Meyers would // reuse const operator[]
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)); }

View File

@ -7,7 +7,8 @@
/* This is a demonstration, that the String class and it's components work /* This is a demonstration, that the String class and it's components work
* correctly, basically a non-unit test. Perfect for usage with valgrind */ * correctly, basically a non-unit test. Perfect for usage with valgrind.
* Note that these tests are only used in case GTest is not present on the system. */
int main() int main()
{ {
if (std::is_default_constructible<String>::value) if (std::is_default_constructible<String>::value)

View File

@ -10,7 +10,8 @@
/* These unit tests were written using the Google Test framework, /* These unit tests were written using the Google Test framework,
* and check everything stated in the specification. */ * and check everything stated in the specification. Note that
* these tests are only used in case GTest is present on the system.*/
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
/// Testing helper classes /// /// Testing helper classes ///
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////