Compare commits

...

4 Commits

Author SHA1 Message Date
2c00b9b2e0 Add very basic Logger implementation 2021-03-16 15:09:30 +01:00
081063be69 Add *.os rule to gitignore 2021-03-16 15:09:15 +01:00
06d11d2c0d Add custom String class
from ACIC
2021-03-16 15:05:39 +01:00
bb85fab65f Restructure to engine library with test executables 2021-03-16 14:55:54 +01:00
10 changed files with 193 additions and 4 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@
**/.sconsign.dblite **/.sconsign.dblite
compile_commands.json compile_commands.json
.cache/ .cache/
*.os

View File

@ -5,6 +5,11 @@ env = DefaultEnvironment(tools=['default', 'compilation_db'])
env.CompilationDatabase() env.CompilationDatabase()
env.Append(CCFLAGS=["-Wall", "-Wextra", "-Werror", "-pedantic"]) env.Append(CCFLAGS=["-Wall", "-Wextra", "-Werror", "-pedantic"])
env.Append(CPPPATH=['.', 'include'])
# Build the output program # Build the test programs
Program('vector.out', Glob('*.cpp')) catch_cpp = "test/catch_amalgamated.cpp"
Program('vector-test.out', [catch_cpp, 'test/vector-test.cpp'])
# Build the library
SharedLibrary('ged', Glob('cpp/*.cpp'))

6
cpp/Logger.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "Logger.h"
#include <iostream>
void Logger::debug(const String &text) {
std::cout << "Debug: " << text << std::endl;
}

119
cpp/String.cpp Normal file
View File

@ -0,0 +1,119 @@
#include "String.h"
String::String() : String("") {
}
String::String(const char *stringText)
: length(strlen(stringText)), text(new char[length + sizeof(char)]) {
memcpy(text, stringText, length + sizeof(char));
}
String::String(const String &other)
: length(other.getLength()), text(new char[length + sizeof(char)]) {
memcpy(text, other.c_str(), length + sizeof(char));
}
String &String::operator=(const String &other) {
// TODO: We could also check whether the other string has the same capacity
// as this one, and if so, reuse the
// current char array. However, this would introduce another check which
// might cost more performance than it gains (since the length will rarely
// be the same)
if (this != &other) {
length = other.length;
// Delete old text and create new
delete[] text;
text = new char[length + sizeof(char)];
// Copy text
memcpy(text, other.c_str(), length + sizeof(char));
}
return *this;
}
String::String(String &&other) : length(other.length), text(other.text) {
other.text = nullptr;
other.length = 0;
}
String &String::operator=(String &&other) {
if (this != &other) {
delete[] text;
text = other.text;
length = other.length;
other.text = nullptr;
other.length = 0;
}
return *this;
}
String &String::operator+=(const String &other) {
concatenate(other);
return *this;
}
String &String::operator+=(const char *other) {
concatenate(other);
return *this;
}
String operator+(String left, const String &right) {
left += right;
return left;
}
String operator+(String left, const char *right) {
left += right;
return left;
}
// TODO: Should cases like `"Hello" + String("World")` be handled?
// I think not; it would be up to `const char *` to handle that, since getting
// a String after adding something to a `const char *` is strange. It could
// look like this however:
// String operator+(const char *left, const String &right) {
// String left_string = String(left);
// left_string += right;
// return left_string;
//}
String::operator const char *() const {
return this->c_str();
}
String::~String() {
delete[] text;
}
void String::concatenate(const String &other) {
concatenate(other.c_str());
}
void String::concatenate(const char *other) {
// Allocate the new text
size_t newStringLength = strlen(other);
char *newString = new char[length + newStringLength + sizeof(char)];
// Copy current string, and other string after it
memcpy(newString, text, length);
memcpy(newString + length, other, newStringLength + sizeof(char));
delete[] text;
text = newString;
length += newStringLength;
}
const char *String::c_str() const {
return text;
}
size_t String::getLength() const {
return length;
}

5
include/Logger.h Normal file
View File

@ -0,0 +1,5 @@
#include "String.h"
class Logger {
static void debug(const String &text);
};

53
include/String.h Normal file
View File

@ -0,0 +1,53 @@
#include <cstring>
class String {
public:
/// Create a new empty String
String();
/// Create a new String out of a char array.
/// The given character array must be \0-terminated.
explicit String(const char *stringText);
/// Copy constructor
String(const String &other);
/// Copy assignment operator
String &operator=(const String &other);
/// Move constructor
String(String &&other);
/// Move assignment operator
String &operator=(String &&other);
/// += operators for concatenate
/// Used in the + operator as well (defined outside the class)
String &operator+=(const String &other);
String &operator+=(const char *other);
/// Conversion to char pointer
operator const char *() const;
~String();
/// Append a String at the end of this String.
void concatenate(const String &other);
/// Append characters at the end of this String.
/// The given character array must be \0-terminated.
void concatenate(const char *other);
/// Return the internal char array representation of this String.
/// Note that calling concatenate() on the String invalidates this returned
/// pointer.
const char *c_str() const;
/// Return the length of the String (without the terminator).
size_t getLength() const;
private:
size_t length;
char *text;
};