Added type traits and string utils
This commit is contained in:
parent
fb695798e7
commit
40cf079e9d
@ -2,6 +2,7 @@ toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/Errors.cpp
|
||||
# ./src/Logger.cpp
|
||||
./src/Processes.cpp
|
||||
./src/StringUtils.cpp
|
||||
./src/Utils.cpp
|
||||
)
|
||||
|
||||
@ -11,6 +12,7 @@ toAbsolutePath(NB_CORE_INCLUDE
|
||||
./Errors.hpp
|
||||
./Logger.hpp
|
||||
./Processes.hpp
|
||||
./StringUtils.hpp
|
||||
./ThreadsafeQueue.hpp
|
||||
./Types.hpp
|
||||
./TypeTraits.hpp
|
||||
|
||||
@ -77,7 +77,7 @@ public:
|
||||
bool isRunning() const noexcept override {
|
||||
return this->_running && (_runningThread!=nullptr);
|
||||
}
|
||||
bool run() {
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
|
||||
@ -9,23 +9,7 @@
|
||||
#include <type_traits>
|
||||
#include "TypeTraits.hpp"
|
||||
#include <unordered_map>
|
||||
#include "Utils.hpp"
|
||||
|
||||
/* #ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
||||
#endif // CODE_ERROR_LOCATIONS
|
||||
#endif // THROW_WITH_INFO
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef LOGGING
|
||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW */
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
@ -83,9 +67,9 @@ public:
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
virtual inline std::string str() const noexcept override {
|
||||
std::string ret = msg;
|
||||
std::string ret = msg + nb::NEWLINE;
|
||||
if (trace) {
|
||||
const std::string replace = nb::NEWLINE+" ";
|
||||
const std::string tabover = " ";
|
||||
std::string trace_msg;
|
||||
if (traceIsNBError) {
|
||||
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
|
||||
@ -95,13 +79,9 @@ public:
|
||||
} else {
|
||||
trace_msg = std::string(trace->what());
|
||||
}
|
||||
ret += replace + "Trace: " + find_and_replace<std::string>(
|
||||
trace_msg,
|
||||
nb::NEWLINE,
|
||||
replace
|
||||
);
|
||||
ret += indent_strblock(trace_msg, tabover, tabover+"Trace: ")+nb::NEWLINE;
|
||||
}
|
||||
return ret + nb::NEWLINE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
@ -226,6 +206,7 @@ public:
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
using Base::type;
|
||||
using Base::ErrorMessages;
|
||||
|
||||
@ -245,10 +226,11 @@ public:
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
|
||||
using Base::traceIsNBError;
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
@ -2,15 +2,28 @@
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
||||
#endif // CODE_ERROR_LOCATIONS
|
||||
#endif // THROW_WITH_INFO
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef LOGGING
|
||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
@ -23,14 +36,6 @@ typedef std::chrono::time_point<
|
||||
std::chrono::nanoseconds
|
||||
> LoggerTimePoint;
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
};
|
||||
|
||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||
|
||||
@ -41,7 +46,7 @@ class LoggerBase
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
public:
|
||||
bool run() {
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
@ -62,6 +67,14 @@ private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
|
||||
using StreamType = std::vector<std::ostream*>;
|
||||
@ -90,7 +103,7 @@ public:
|
||||
}
|
||||
|
||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(err.what(), lvl);
|
||||
type_ptr->log(prepend_strblock(err.what(), " "), lvl);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
@ -104,7 +117,6 @@ protected:
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
|
||||
142
engine/NBCore/StringUtils.hpp
Normal file
142
engine/NBCore/StringUtils.hpp
Normal file
@ -0,0 +1,142 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible = ValidConversion<std::string, T...>;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible = ValidConversion<std::wstring, T...>;
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool StringConvertible_v = StringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool WStringConvertible_v = WStringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_to = typename StringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_to = typename WStringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_from = typename StringConvertible<T...>::from;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_from = typename WStringConvertible<T...>::from;
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<StringConvertible_v<T>, std::wstring> str_to_wstr(T in) {
|
||||
std::string str(in);
|
||||
std::wstring ret(str.begin(), str.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<WStringConvertible_v<T>, std::string> wstr_to_str(T in) {
|
||||
std::wstring wstr(in);
|
||||
std::size_t wstrlen = wstr.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, wstr.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = std::string, typename A, typename B, typename C>
|
||||
ValidConversion_to<T, A, B, C> find_and_replace(
|
||||
A original,
|
||||
B find,
|
||||
C replace
|
||||
) {
|
||||
const T& find_t = T(find);
|
||||
const T& replace_t = T(replace);
|
||||
|
||||
T ret(original);
|
||||
|
||||
std::size_t find_len = find_t.length();
|
||||
std::size_t replace_len = replace_t.length();
|
||||
std::size_t currpos = 0;
|
||||
while(true) {
|
||||
currpos = ret.find(find_t, currpos);
|
||||
if (currpos == T::npos) {
|
||||
break;
|
||||
}
|
||||
ret = ret.erase(currpos, find_len);
|
||||
ret = ret.insert(currpos, replace_t);
|
||||
currpos += replace_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename A, typename B, typename C>
|
||||
StringConvertible_to<A, B, C> indent_strblock(
|
||||
A block,
|
||||
B prepend,
|
||||
C topIndent
|
||||
) {
|
||||
return std::string(topIndent) + find_and_replace<std::string>(
|
||||
std::string(block),
|
||||
nb::NEWLINE,
|
||||
nb::NEWLINE + std::string(prepend)
|
||||
);
|
||||
}
|
||||
|
||||
template <typename A, typename B>
|
||||
StringConvertible_to<A, B> indent_strblock(
|
||||
A block,
|
||||
B prepend
|
||||
) {
|
||||
return indent_strblock<A, B, B>(block, prepend, prepend);
|
||||
}
|
||||
|
||||
/* template<typename T>
|
||||
T swap_endian(const T& val) {
|
||||
T ret;
|
||||
const int size = sizeof(T);
|
||||
auto retLoc = static_cast<void*>(&ret);
|
||||
auto valLoc = static_cast<const void*>(&val);
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
||||
}
|
||||
return ret;
|
||||
} */
|
||||
|
||||
|
||||
// using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
@ -2,7 +2,9 @@
|
||||
#ifndef _NB_TYPE_TRAITS
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace nb {
|
||||
|
||||
@ -37,11 +39,41 @@ template <
|
||||
>
|
||||
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
|
||||
|
||||
/*template<std::size_t N=0, typename Func, typename... Pack>
|
||||
template <typename... Types>
|
||||
struct ValidConversion;
|
||||
|
||||
template <typename To, typename A, typename B, typename... Rest>
|
||||
struct ValidConversion<To, A, B, Rest...> {
|
||||
typedef std::conjunction<
|
||||
std::is_constructible<To, A>, std::is_constructible<To, A>, std::conjunction<std::is_constructible<To, Rest>...>
|
||||
> value_type;
|
||||
static constexpr bool value = value_type::value;
|
||||
typedef std::enable_if_t<value, To> to;
|
||||
|
||||
};
|
||||
|
||||
template <typename To, typename From>
|
||||
struct ValidConversion<To, From> {
|
||||
typedef std::is_constructible<To, From> value_type;
|
||||
static constexpr bool value = value_type::value;
|
||||
typedef std::enable_if_t<value, To> to;
|
||||
typedef std::enable_if_t<value, From> from;
|
||||
};
|
||||
|
||||
template<typename... Types>
|
||||
using ValidConversion_v = typename ValidConversion<Types...>::value;
|
||||
|
||||
template<typename... Types>
|
||||
using ValidConversion_to = typename ValidConversion<Types...>::to;
|
||||
|
||||
template<typename To, typename From>
|
||||
using ValidConversion_from = typename ValidConversion<To, From>::from;
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
|
||||
template<std::size_t N=0, typename Func, typename... Pack>
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>& tup, Func f) {
|
||||
f(N, std::get<N>(tup));
|
||||
@ -99,7 +131,7 @@ struct PackIsSameType<T, PackType> {
|
||||
template <typename T, typename FirstPack, typename... Pack>
|
||||
struct PackIsSameType<T, FirstPack, Pack...> {
|
||||
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
||||
};*/
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
@ -2,80 +2,8 @@
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\n";
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_convertible_v<T, std::string>, std::wstring> str_to_wstr(T in) {
|
||||
std::string str(in);
|
||||
std::wstring ret(str.begin(), str.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<std::is_convertible_v<T, std::wstring>, std::string> wstr_to_str(T in) {
|
||||
std::wstring wstr(in);
|
||||
std::size_t wstrlen = wstr.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, wstr.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = std::string, typename A, typename B, typename C>
|
||||
T find_and_replace(
|
||||
A original,
|
||||
B find,
|
||||
C replace
|
||||
) {
|
||||
const T& original_t = T(original);
|
||||
const T& find_t = T(find);
|
||||
const T& replace_t = T(replace);
|
||||
|
||||
T ret = original_t;
|
||||
|
||||
std::size_t find_len = find_t.length();
|
||||
std::size_t replace_len = replace_t.length();
|
||||
std::size_t currpos = 0;
|
||||
while(true) {
|
||||
currpos = ret.find(find_t, currpos);
|
||||
if (currpos == T::npos) {
|
||||
break;
|
||||
}
|
||||
ret = ret.erase(currpos, find_len);
|
||||
ret = ret.insert(currpos, replace_t);
|
||||
currpos += replace_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* template<typename T>
|
||||
T swap_endian(const T& val) {
|
||||
T ret;
|
||||
|
||||
@ -10,6 +10,8 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||
return log.run();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
|
||||
} // namespace nb
|
||||
1
engine/NBCore/src/StringUtils.cpp
Normal file
1
engine/NBCore/src/StringUtils.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "StringUtils.hpp"
|
||||
@ -1,8 +1 @@
|
||||
#include <string>
|
||||
#include "Utils.hpp"
|
||||
|
||||
template<>
|
||||
std::wstring nb::str_to_wstr(const std::string& in) {
|
||||
std::wstring ret(in.begin(), in.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <string>
|
||||
#include "Utils.hpp"
|
||||
|
||||
using namespace nb;
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
#include <string>
|
||||
#include "TypeTraits.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user