From fa89e9825c0ff84c97267aa39c5644b14ca4bf81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sat, 7 Jul 2018 12:37:07 +0300 Subject: [PATCH] libcore: constexpr constructor for Vector; other minor improvements --- doomsday/libs/core/include/de/core/app.h | 5 +++++ doomsday/libs/core/include/de/core/vector.h | 6 +++--- doomsday/libs/core/include/de/data/bitarray.h | 6 +++++- doomsday/libs/core/include/de/data/hash.h | 1 + doomsday/libs/core/include/de/data/map.h | 1 + doomsday/libs/core/include/de/data/string.h | 2 +- doomsday/libs/core/include/de/data/time.h | 2 +- doomsday/libs/core/include/de/data/variable.h | 6 ++++-- doomsday/libs/core/include/de/libcore.h | 6 ++++++ doomsday/libs/core/src/core/app.cpp | 6 ++++++ doomsday/libs/core/src/data/bitarray.cpp | 11 +++++++++++ doomsday/libs/core/src/data/string.cpp | 4 ++-- doomsday/libs/core/src/data/variable.cpp | 5 +++++ 13 files changed, 51 insertions(+), 10 deletions(-) diff --git a/doomsday/libs/core/include/de/core/app.h b/doomsday/libs/core/include/de/core/app.h index 0c415f4d24..1d10627f08 100644 --- a/doomsday/libs/core/include/de/core/app.h +++ b/doomsday/libs/core/include/de/core/app.h @@ -283,6 +283,11 @@ class DE_PUBLIC App : DE_OBSERVES(Clock, TimeChange) */ static NativePath tempPath(); + /** + * Returns a native directory for caching non-user-specific (native) files. + */ + static NativePath cachePath(); + /** * Changes the application's current native working directory. * diff --git a/doomsday/libs/core/include/de/core/vector.h b/doomsday/libs/core/include/de/core/vector.h index abc48b3f8f..d8eb45e7db 100644 --- a/doomsday/libs/core/include/de/core/vector.h +++ b/doomsday/libs/core/include/de/core/vector.h @@ -107,7 +107,7 @@ class Vector2 typedef Type value_type; public: - Vector2(Type a = Type(0), Type b = Type(0)) : x(a), y(b) {} + constexpr Vector2(Type a = Type(0), Type b = Type(0)) : x(a), y(b) {} Vector2(Type const *ab) : x(ab[0]), y(ab[1]) {} Vector2(Value const &value) { *this = vectorFromValue>(value); } Vector2(Vector2 const &other) = default; @@ -323,7 +323,7 @@ template class Vector3 : public Vector2 { public: - Vector3(Type a = 0, Type b = 0, Type c = 0) : Vector2(a, b), z(c) {} + constexpr Vector3(Type a = 0, Type b = 0, Type c = 0) : Vector2(a, b), z(c) {} Vector3(Vector2 const &v2, Type c = 0) : Vector2(v2), z(c) {} Vector3(Type const *abc) : Vector2(abc), z(abc[2]) {} Vector3(Value const &value) { *this = vectorFromValue< Vector3 >(value); } @@ -552,7 +552,7 @@ template class Vector4 : public Vector3 { public: - Vector4(Type a = 0, Type b = 0, Type c = 0, Type d = 0) : Vector3(a, b, c), w(d) {} + constexpr Vector4(Type a = 0, Type b = 0, Type c = 0, Type d = 0) : Vector3(a, b, c), w(d) {} Vector4(Vector3 const &v3, Type d = 0) : Vector3(v3), w(d) {} Vector4(Vector2 const &a, Vector2 const &b) : Vector3(a, b.x), w(b.y) {} Vector4(Type const *abcd) : Vector3(abcd), w(abcd[3]) {} diff --git a/doomsday/libs/core/include/de/data/bitarray.h b/doomsday/libs/core/include/de/data/bitarray.h index b8209b054e..9676114a6a 100644 --- a/doomsday/libs/core/include/de/data/bitarray.h +++ b/doomsday/libs/core/include/de/data/bitarray.h @@ -39,12 +39,16 @@ class DE_PUBLIC BitArray dsize size() const; dsize count(bool bit) const; + inline int sizei() const { return int(_bits.size()); } + void clear(); void resize(dsize count); void fill(bool bit); - void setBit(dsize pos, bool bit); + void setBit(dsize pos, bool bit = true); BitArray &operator=(const BitArray &); + BitArray &operator<<(bool bit); + private: std::vector _bits; }; diff --git a/doomsday/libs/core/include/de/data/hash.h b/doomsday/libs/core/include/de/data/hash.h index 87e1e3fbad..63890fe899 100644 --- a/doomsday/libs/core/include/de/data/hash.h +++ b/doomsday/libs/core/include/de/data/hash.h @@ -54,6 +54,7 @@ class Hash : public std::unordered_map using Base::find; bool isEmpty() const { return empty(); } + inline int sizei() const { return int(Base::size()); } iterator insert(const Key &key, const Value &value) { return Base::insert(std::make_pair(key, value)).first; } void remove(const Key &key) { Base::erase(key); } diff --git a/doomsday/libs/core/include/de/data/map.h b/doomsday/libs/core/include/de/data/map.h index 5c86844be1..945c4c31e3 100644 --- a/doomsday/libs/core/include/de/data/map.h +++ b/doomsday/libs/core/include/de/data/map.h @@ -44,6 +44,7 @@ class Map : public std::map using const_reverse_iterator = typename Base::const_reverse_iterator; inline bool isEmpty() const { return Base::empty(); } + inline int sizei() const { return int(Base::size()); } iterator insert(const Key &key, const Value &value) { diff --git a/doomsday/libs/core/include/de/data/string.h b/doomsday/libs/core/include/de/data/string.h index 31fe634896..274120b7af 100644 --- a/doomsday/libs/core/include/de/data/string.h +++ b/doomsday/libs/core/include/de/data/string.h @@ -318,7 +318,7 @@ class DE_PUBLIC String : public IByteArray bool contains(char c) const; bool contains(Char c) const; - bool contains(const char *cStr) const; + bool contains(const char *cStr, Sensitivity cs = CaseSensitive) const; int count(char ch) const; bool beginsWith(const String &s, Sensitivity cs = CaseSensitive) const diff --git a/doomsday/libs/core/include/de/data/time.h b/doomsday/libs/core/include/de/data/time.h index 7b3b3d803f..9e5f7f70f0 100644 --- a/doomsday/libs/core/include/de/data/time.h +++ b/doomsday/libs/core/include/de/data/time.h @@ -62,7 +62,7 @@ class DE_PUBLIC Time : public ISerializable * * @param seconds Length of the time span. */ - Span(ddouble seconds = 0.0) + constexpr Span(ddouble seconds = 0.0) : _seconds(seconds) {} diff --git a/doomsday/libs/core/include/de/data/variable.h b/doomsday/libs/core/include/de/data/variable.h index 1f0261a331..75d17023cd 100644 --- a/doomsday/libs/core/include/de/data/variable.h +++ b/doomsday/libs/core/include/de/data/variable.h @@ -143,14 +143,14 @@ class DE_PUBLIC Variable : public Deletable, public ISerializable * * @param v New value. Variable gets ownership. Cannot be NULL. */ - Variable &operator = (Value *v); + Variable &operator=(Value *v); /** * Sets the value of the variable. * * @param textValue Text string. A new TextValue is created. */ - Variable &operator = (String const &textValue); + Variable &operator=(String const &textValue); /** * Sets the value of the variable. @@ -172,6 +172,8 @@ class DE_PUBLIC Variable : public Deletable, public ISerializable Value *valuePtr(); Value const *valuePtr() const; + bool operator==(const String &text) const; + /** * Returns the value of the variable. */ diff --git a/doomsday/libs/core/include/de/libcore.h b/doomsday/libs/core/include/de/libcore.h index 0308d5d2dd..e8f474de9f 100644 --- a/doomsday/libs/core/include/de/libcore.h +++ b/doomsday/libs/core/include/de/libcore.h @@ -143,6 +143,12 @@ # undef DE_UNUSED #endif +#if defined (__clang__) +# define DE_FALLTHROUGH [[clang::fallthrough]] +#else +# define DE_FALLTHROUGH +#endif + #ifndef NDEBUG # define DE_DEBUG DE_EXTERN_C DE_PUBLIC void LogBuffer_Flush(void); diff --git a/doomsday/libs/core/src/core/app.cpp b/doomsday/libs/core/src/core/app.cpp index 3381df6f11..ed2ed5e28e 100644 --- a/doomsday/libs/core/src/core/app.cpp +++ b/doomsday/libs/core/src/core/app.cpp @@ -682,6 +682,12 @@ NativePath App::tempPath() #endif } +NativePath App::cachePath() +{ + DE_ASSERT_FAIL("App::cachePath() not implemented"); + return NativePath(); +} + bool App::setCurrentWorkPath(NativePath const &cwd) { return NativePath::setWorkPath(cwd); diff --git a/doomsday/libs/core/src/data/bitarray.cpp b/doomsday/libs/core/src/data/bitarray.cpp index eb38888129..f4d6a3d4af 100644 --- a/doomsday/libs/core/src/data/bitarray.cpp +++ b/doomsday/libs/core/src/data/bitarray.cpp @@ -51,6 +51,11 @@ dsize BitArray::count(bool bit) const return num; } +void BitArray::clear() +{ + _bits.clear(); +} + void BitArray::resize(dsize count) { return _bits.resize(count); @@ -76,4 +81,10 @@ BitArray &BitArray::operator=(const BitArray &other) return *this; } +BitArray &BitArray::operator<<(bool bit) +{ + _bits.push_back(bit ? 1 : 0); + return *this; +} + } // namespace de diff --git a/doomsday/libs/core/src/data/string.cpp b/doomsday/libs/core/src/data/string.cpp index c96e1b8fb7..0aa78944d2 100644 --- a/doomsday/libs/core/src/data/string.cpp +++ b/doomsday/libs/core/src/data/string.cpp @@ -192,9 +192,9 @@ bool String::contains(Char c) const return contains(mb.bytes); } -bool String::contains(const char *cStr) const +bool String::contains(const char *cStr, Sensitivity cs) const { - return indexOfCStr_String(&_str, cStr) != iInvalidPos; + return indexOfCStrSc_String(&_str, cStr, cs) != iInvalidPos; } int String::count(char ch) const diff --git a/doomsday/libs/core/src/data/variable.cpp b/doomsday/libs/core/src/data/variable.cpp index 3a9d978c2e..263dc1775f 100644 --- a/doomsday/libs/core/src/data/variable.cpp +++ b/doomsday/libs/core/src/data/variable.cpp @@ -178,6 +178,11 @@ Value const *Variable::valuePtr() const return d->value; } +bool Variable::operator==(const String &text) const +{ + return d->value && d->value->compare(TextValue(text)) == 0; +} + Record &Variable::valueAsRecord() { return value().dereference();