diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 5bd0ac2b..f30a3481 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -24,6 +24,7 @@ install( strops.hpp std.hpp types.hpp + typetraits.hpp DESTINATION include/ox/std ) diff --git a/deps/ox/src/ox/std/math.hpp b/deps/ox/src/ox/std/math.hpp index 6cb6c7f6..71fa2f65 100644 --- a/deps/ox/src/ox/std/math.hpp +++ b/deps/ox/src/ox/std/math.hpp @@ -8,6 +8,8 @@ #pragma once +#include "typetraits.hpp" + namespace ox { template @@ -20,4 +22,13 @@ inline const T &max(const T &a, const T &b) { return a > b ? a : b; } +template +inline I pow(I v, int e) { + I out = 1; + for (I i = 0; i < e; i++) { + out *= v; + } + return out; +} + } diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 9613abab..32164470 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -16,4 +16,5 @@ #include "strops.hpp" #include "string.hpp" #include "types.hpp" +#include "typetraits.hpp" #include "vector.hpp" diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 2677d46c..a7e7d289 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -29,6 +29,12 @@ class BString { const BString &operator=(char *str); + const BString &operator=(int64_t i); + + const BString &operator+=(const char *str); + + const BString &operator+=(char *str); + bool operator==(const BString &other); char *data(); @@ -59,6 +65,13 @@ BString::BString(const char *str) { *this = str; } +template +const BString &BString::operator=(int64_t i) { + char str[65]; + ox_itoa(i, str); + return this->operator=(str); +} + template const BString &BString::operator=(const char *str) { size_t strLen = ox_strlen(str) + 1; @@ -76,6 +89,24 @@ const BString &BString::operator=(char *str) { return *this = (const char*) str; } +template +const BString &BString::operator+=(const char *str) { + size_t strLen = ox_strlen(str) + 1; + auto currentSize = size(); + if (cap() < currentSize + strLen) { + strLen = cap() - currentSize; + } + ox_memcpy(m_buff + currentSize, str, strLen); + // make sure last element is a null terminator + m_buff[cap() - 1] = 0; + return *this; +} + +template +const BString &BString::operator+=(char *str) { + return *this = (const char*) str; +} + template bool BString::operator==(const BString &other) { bool retval = true; diff --git a/deps/ox/src/ox/std/strops.cpp b/deps/ox/src/ox/std/strops.cpp index 2c0eccf7..4eeb8b4e 100644 --- a/deps/ox/src/ox/std/strops.cpp +++ b/deps/ox/src/ox/std/strops.cpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "math.hpp" + #include "strops.hpp" int ox_strcmp(const char *str1, const char *str2) { @@ -89,3 +91,30 @@ int ox_atoi(const char *str) { return total; } + +char *ox_itoa(int64_t v, char *str) { + auto mod = 1000000000000000000; + constexpr auto base = 10; + auto it = 0; + if (v < 0) { + str[it] = '-'; + it++; + } + while (mod) { + auto digit = v / mod; + v %= mod; + mod /= base; + if (it or digit) { + int start; + if (digit < 10) { + start = '0'; + } else { + start = 'a'; + digit -= 10; + } + str[it] = start + digit; + it++; + } + } + return str; +} diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index df01a507..58fcfd15 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -9,6 +9,7 @@ #pragma once #include "types.hpp" +#include "typetraits.hpp" int ox_strcmp(const char *str1, const char *str2); @@ -25,3 +26,5 @@ int ox_lastIndexOf(const char *str, int character, int maxLen = 0xFFFFFFFF); int ox_lastIndexOf(char *str, int character, int maxLen = 0xFFFFFFFF); int ox_atoi(const char *str); + +char *ox_itoa(int64_t v, char *str); diff --git a/deps/ox/src/ox/std/typetraits.hpp b/deps/ox/src/ox/std/typetraits.hpp new file mode 100644 index 00000000..414df464 --- /dev/null +++ b/deps/ox/src/ox/std/typetraits.hpp @@ -0,0 +1,63 @@ +/* + * Copyright 2015 - 2018 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "types.hpp" + +namespace ox { + +template +struct integral_constant { + + using value_type = T; + using type = integral_constant; + + static constexpr T value = v; + + constexpr operator value_type() const noexcept { + return value; + } + + constexpr value_type operator()() const noexcept { + return value; + } + +}; + +using false_type = ox::integral_constant; +using true_type = ox::integral_constant; + + +// is_integral ///////////////////////////////////////////////////////////////// + +template struct is_integral: ox::false_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral: ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral: ox::true_type {}; +template<> struct is_integral : ox::true_type {}; +template<> struct is_integral: ox::true_type {}; + + +// enable_if /////////////////////////////////////////////////////////////////// + +template +struct enable_if { +}; + +template +struct enable_if { + using type = T; +}; + +}; diff --git a/deps/ox/src/ox/trace/trace.cpp b/deps/ox/src/ox/trace/trace.cpp index 789349fe..2bd43239 100644 --- a/deps/ox/src/ox/trace/trace.cpp +++ b/deps/ox/src/ox/trace/trace.cpp @@ -6,24 +6,17 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include -#include - #include #include "trace.hpp" namespace ox { -struct TraceMsg { - const char *file; - int line; - uint64_t time; - const char *ch; - const char *msg; -}; - -void trace(const char *file, int line, const char *ch, const char *msg) { +OutStream::OutStream(const char *file, int line, const char *ch, const char *msg) { + m_msg.file = file; + m_msg.line = line; + m_msg.ch = ch; + m_msg.msg = msg; } } diff --git a/deps/ox/src/ox/trace/trace.hpp b/deps/ox/src/ox/trace/trace.hpp index ce5ac8f2..73b652cb 100644 --- a/deps/ox/src/ox/trace/trace.hpp +++ b/deps/ox/src/ox/trace/trace.hpp @@ -8,10 +8,37 @@ #pragma once +#include + namespace ox { -void trace(const char *file, int line, const char *ch, const char *msg); +struct TraceMsg { + const char *file; + int line; + uint64_t time; + const char *ch; + ox::BString<100> msg; +}; + +class OutStream { + + private: + TraceMsg m_msg; + + public: + OutStream() = default; + + OutStream(const char *file, int line, const char *ch, const char *msg = ""); + + template + OutStream &operator<<(T v) { + m_msg.msg += " "; + m_msg.msg += v; + return *this; + } + +}; } -#define ox_trace(ch, msg) ox::trace(__FILE__, __LINE__, ch, msg) +#define oxTrace(ch, msg) ox::OutStream(__FILE__, __LINE__, ch, msg)