Dropping smartstreams (not fully cleared) and added get_pid()

This commit is contained in:
NaifBanana 2026-01-18 18:24:26 -06:00
parent c4ee778040
commit 847bf3a078
9 changed files with 114 additions and 296 deletions

View File

@ -2,6 +2,7 @@ include_directories(./.)
add_library(NBCore
./src/Errors.cpp
./src/Processes.cpp
)
if (NB_BUILD_TESTS)

View File

@ -8,8 +8,10 @@
#include <thread>
#include <unordered_map>
#include "Processes.hpp"
#include "ThreadSafeQueue.hpp"
namespace nb {
typedef std::chrono::time_point<
@ -21,6 +23,8 @@ 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&);
@ -46,7 +50,13 @@ public:
_queue.empty();
}
void msg(const std::string& _msg, uint8_t lvl=0) {
_queue.push(LogEvent{std::chrono::system_clock::now(), lvl, _msg});
_queue.push(LogEvent{
std::chrono::system_clock::now(),
lvl,
_msg,
std::this_thread::get_id(),
get_pid()
});
}
void warn(const std::string& _msg, uint8_t lvl=1) {
msg(_msg, lvl);
@ -64,30 +74,15 @@ public:
protected:
virtual void process(const LogEvent& event) {
uint8_t lvl = event.lvl;
try {
*_ostream << _function_map.at(lvl)(event.time, event.msg).c_str();
} catch (std::out_of_range e) {
*_ostream << _function_map.at(0)(event.time, event.msg).c_str();
//*_ostream << event.msg;
}
*_ostream << "\n";
}
static const LogProcessFunctionMap _function_map;
ThreadsafeQueue<LogEvent> _queue;
StreamType* const _ostream;
std::atomic<bool> _running;
std::shared_ptr<std::thread> _runningThread = nullptr;
};
template <typename ST>
const LogProcessFunctionMap Logger<ST>::_function_map = {
{0, [](const LoggerTimePoint& tp, const std::string& msg){
return msg;
}}
};
template <typename ST>
bool Logger<ST>::isRunning() const {
return _running && bool(_runningThread);

View File

@ -0,0 +1,14 @@
#pragma once
#ifndef _NB_PROCESSES
#define _NB_PROCESSES
#include <cstdint>
namespace nb {
uint64_t get_pid();
} // namespace nb
#endif // _NB_PROCESSES

View File

@ -16,12 +16,12 @@ namespace nb {
template <typename StreamType, typename... Mixins>
class SmartStream;
template<typename StreamFormat, typename U, typename Mix>
template<typename Wrapper, typename U, typename Mix>
struct MixinHasProcess {
protected:
template <typename M>
static auto test(int)->decltype(
M::process(std::declval<StreamFormat&>(), std::declval<const U&>()),
std::declval<M>().template process<Wrapper>(std::declval<const U&>()),
std::true_type{}
);
@ -32,12 +32,12 @@ public:
static const bool value = decltype(test<Mix>(0))::value;
};
template<typename StreamFormat, typename U, typename... Mixins>
template<typename Wrapper, typename U, typename... Mixins>
struct PackHasProcess;
template<typename StreamFormat, typename U, typename Mix>
struct PackHasProcess<StreamFormat, U, Mix> {
static const bool value = MixinHasProcess<StreamFormat, U, Mix>::value;
template<typename Wrapper, typename U, typename Mix>
struct PackHasProcess<Wrapper, U, Mix> {
static const bool value = MixinHasProcess<Wrapper, U, Mix>::value;
using type = typename std::conditional<
value,
Mix,
@ -45,53 +45,59 @@ struct PackHasProcess<StreamFormat, U, Mix> {
>::type;
};
template<typename StreamFormat, typename U, typename Mix1, typename... Mixins>
struct PackHasProcess<StreamFormat, U, Mix1, Mixins...> {
template<typename Wrapper, typename U, typename Mix1, typename... Mixins>
struct PackHasProcess<Wrapper, U, Mix1, Mixins...> {
protected:
static const bool base_value = MixinHasProcess<StreamFormat, U, Mix1>::value;
static const bool base_value = MixinHasProcess<Wrapper, U, Mix1>::value;
public:
static const bool value = std::conditional<
base_value,
std::true_type,
typename PackHasProcess<StreamFormat, U, Mixins...>::value
typename PackHasProcess<Wrapper, U, Mixins...>::value
>::type::value;
using type = typename std::conditional<
base_value,
Mix1,
typename PackHasProcess<StreamFormat, U, Mixins...>::type
typename PackHasProcess<Wrapper, U, Mixins...>::type
>::type;
};
/* template <typename T, typename=void>
struct HasStreamType : std::false_type {};
template <typename T>
struct HasStreamType<T, > : std::false_type {}; */
template<typename ST, typename... Mixins>
class SmartStream<ST, Mixins...> : public virtual Mixins... {
class SmartStream : public Mixins... {
using StreamType = typename std::conditional<
std::is_base_of<std>::value
decltype(std::declval<ST::StreamType>(), std::true_type{}),
ST::StreamType,
ST
>::type;
protected:
StreamType* const _stream;
public:
SmartStream(StreamType& stream) : _stream(&stream) {}
StreamType* getStream() const { return _stream; }
SmartStream(StreamType& stream) : _stream(&stream), Mixins(&stream)... {}
using Mixins::process...;
template <typename U, typename StreamFmt=SmartStream<StreamType, Mixins...>>
static typename std::enable_if<PackHasProcess<StreamFmt, U, Mixins...>::value, void>::type
toStream(SmartStream& stream, const U& val, const StreamFmt* fmtas=nullptr) {
PackHasProcess<StreamFmt, U, Mixins...>::type::process(stream, val);
typename std::enable_if<PackHasProcess<SmartStream, U, Mixins...>::value, void>::type
toStream(const U& val, const StreamFmt* fmtas=nullptr) {
PackHasProcess<SmartStream, U, Mixins...>::type::template process<SmartStream>(val);
}
template <typename U, typename StreamFmt=SmartStream<StreamType, Mixins...>>
static typename std::enable_if<!PackHasProcess<StreamFmt, U, Mixins...>::value, void>::type
toStream(SmartStream& stream, const U& val, const StreamFmt* fmtas=nullptr) {
*(stream._stream) << val;
typename std::enable_if<!PackHasProcess<SmartStream, U, Mixins...>::value, void>::type
toStream(const U& val, const StreamFmt* fmtas=nullptr) {
*_stream << val;
}
template <typename ST, typename U, typename... Mixes>
friend SmartStream<ST, Mixes...>& operator<<(SmartStream<ST, Mixes...>& stream, const U& val) {
SmartStream<ST, Mixes...>::toStream(stream, val);
template <typename U>
friend SmartStream& operator<<(SmartStream& stream, const U& val) {
stream.toStream(val);
return stream;
}
};
@ -100,231 +106,19 @@ struct SmartText {
int x;
};
struct SmartStreamFormatter {
template <typename StreamType, typename... M>
static void process(SmartStream<StreamType, M...>& stream, const SmartText& val, StreamType* fmtas = nullptr) {
*(stream.getStream()) << val.x;
template <typename StreamType>
struct ANSIFormatter {
template <typename Wrapper>
void process(const SmartText& val, StreamType* fmtas = nullptr) {
static_cast<Wrapper*>(this)->toStream(val.x);
}
protected:
ANSIFormatter(StreamType* stream) : _stream(stream) {}
StreamType* const _stream;
};
}
/*namespace nb {
using RGB = std::array<unsigned char, 3>;
class TerminalBufferController {
template <typename T>
friend void switchCoutBuf(T);
private:
static auto terminal_buffer = std::cout.rdbuf();
TerminalBufferController();
};
std::string toANSI(const RGB& color) {
return "\x1b[38;2;"+std::to_string(color[0])+";"+std::to_string(color[1])+
";"+std::to_string(color[2])+"m";
}
std::string toANSI(const unsigned int& color) {
return "\x1b[38;5;"+std::to_string(color)+"m";
}
template <typename T>
void switchCoutBuf(T val) {
}
/* class TextStream {
virtual void write(std::)
};
struct SmartTextBase {
virtual std::string toString(std::ostream&) const = 0;
virtual ~SmartTextBase() = default;
};
template <typename U, typename Derived=void>
struct SmartText;
template <typename U, typename Derived>
struct SmartText<U, Derived> : SmartTextBase {
explicit SmartText(const U& val) : msg(val) {}
SmartText(const SmartText&) = default;
template<typename C, typename T>
std::string toString(std::basic_ostream<C,T>& stream) const {
return static_cast<const Derived&>(*this).toString(stream);
}
std::string toString(std::ostream& stream) const override {
return static_cast<const Derived&>(*this).toString(stream);
}
U msg;
};
template <typename U>
struct SmartText<U, void> : SmartTextBase {
explicit SmartText(const U& val) : msg(val) {}
SmartText(const SmartText&) = default;
template<typename C, typename T>
std::string toString(std::basic_ostream<C,T>& stream) const {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
stream << msg;
stream.rdbuf(stream_buf);
return sstr.str();
}
std::string toString(std::ostream& stream) const override {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
stream << msg;
stream.rdbuf(stream_buf);
return sstr.str();
}
U msg;
};
template <typename Derived>
struct SmartText<std::string, Derived> : SmartTextBase {
template <std::size_t N>
SmartText(char const(&val) [N]) : msg(val) {}
SmartText(const std::string& val) : msg(val) {}
SmartText(const char* val) : msg(val) {}
SmartText(const SmartText&) = default;
template<typename C, typename T>
std::string toString(std::basic_ostream<C,T>& stream) const {
return static_cast<const Derived&>(*this).toString(stream);
}
std::string toString(std::ostream& stream) const override {
return static_cast<const Derived&>(*this).toString(stream);
}
std::string msg;
};
template <>
struct SmartText<std::string, void> : SmartTextBase {
template <std::size_t N>
SmartText(char const(&val) [N]) : msg(val) {}
SmartText(const std::string& val) : msg(val) {}
SmartText(const char* val) : msg(val) {}
SmartText(const SmartText&) = default;
template<typename C, typename T>
std::string toString(std::basic_ostream<C,T>& stream) const {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
stream << msg;
stream.rdbuf(stream_buf);
return sstr.str();
}
std::string toString(std::ostream& stream) const override {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
stream << msg;
stream.rdbuf(stream_buf);
return sstr.str();
}
std::string msg;
};
template<typename C, typename T>
std::basic_ostream<C, T>& operator<<(std::basic_ostream<C, T>& stream, const SmartTextBase& smt) {
stream << smt.toString(stream);
return stream;
}
template <typename U>
struct TerminalColor : public SmartText<U, TerminalColor<U>> {
typedef SmartText<U, TerminalColor<U>> Base;
using Base::msg;
explicit TerminalColor(unsigned int color, const U& val)
: Base(val), colorSequence(toANSI(color)) { }
explicit TerminalColor(const RGB& color, const U& val)
: Base(val), colorSequence(toANSI(color)) { }
TerminalColor(const TerminalColor&) = default;
const std::string colorSequence;
template <typename C, typename T>
std::string toString(std::basic_ostream<C, T>& stream) const {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
if (&stream == &std::cout) {
stream << colorSequence << msg << "\x1b[0m";
} else {
stream << msg;
}
stream.rdbuf(stream_buf);
return sstr.str();
}
};
template <typename T, typename U>
std::vector<std::shared_ptr<T>> variadicToBaseVec(std::vector<std::shared_ptr<T>> vec, const U& val) {
static_assert(std::is_base_of<T, U>::value, "Push value type must be base of vector base type.");
vec.push(static_cast<std::shared_ptr<T>>(std::make_shared(val)));
return vec;
}
template <typename T, typename... U>
std::vector<std::shared_ptr<T>> variadicToBaseVec(std::vector<std::shared_ptr<T>> vec, const U&... val) {
vec.push(static_cast<std::shared_ptr<T>>(std::make_shared(val...)));
return variadicToBaseVec<T, U...>(vec, val...);
}
struct SmartGroup : public SmartText<std::vector<std::shared_ptr<SmartTextBase>>, SmartGroup> {
using SmartVec = std::vector<std::shared_ptr<SmartTextBase>>;
typedef SmartText<SmartVec, SmartGroup> Base;
using Base::msg;
SmartGroup(const SmartGroup&) = default;
template<typename... Types>
explicit SmartGroup(const Types&... vals)
: Base(variadicToBaseVec<SmartTextBase, Types...>({}, vals...)) {}
template <typename T>
void push(const T& val) {
msg = variadicToBaseVec(msg, val);
}
template <typename T>
SmartGroup operator+(const T& val) {
SmartGroup ret(*this);
ret.push(val);
return ret;
}
template <typename C, typename T>
std::string toString(std::basic_ostream<C, T>& stream) const {
std::stringstream sstr;
auto stream_buf = stream.rdbuf(sstr.rdbuf());
for (const std::shared_ptr<SmartTextBase>& st : msg) {
stream << st;
}
stream.rdbuf(stream_buf);
return sstr.str();
}
};
template <typename lU, typename lD, typename rU, typename rD>
SmartGroup operator+(const SmartText<lU, lD>& lhs, const SmartText<rU, rD>& rhs) {
return SmartGroup(lhs, rhs);
}
} */
#endif // _NB_SMARTSTREAM

View File

@ -0,0 +1,25 @@
#ifdef _TARGET_WINDOWS
#include <Windows.h>
#endif // _TARGET_WINDOWS
#ifdef _TARGET_LINUX
#include <unistd.h>
#endif // _TARGET_LINUX
#include "Processes.hpp"
namespace nb {
#ifdef _TARGET_WINDOWS
uint64_t get_pid() {
const auto pid_w = GetCurrentProcessId();
uint64_t ret;
memcpy(&ret, &pid_w, sizeof(pid_w));
return ret;
}
#endif // _TARGET_WINDOWS
#ifdef _TARGET_LINUX
#endif // _TARGET_LINUX
} // namespace nb

View File

@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.26.0)
project(gtest_Graphics VERSION 0.1.0 LANGUAGES C CXX)
add_subdirectory(./ErrorTest)
add_subdirectory(./SmartStreamTest)
add_subdirectory(./ProcessTest)

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.26.0)
project(gtest_Graphics VERSION 0.1.0 LANGUAGES C CXX)
add_executable(SmartStreamTest main.cpp)
target_link_libraries(SmartStreamTest NBCore)
add_executable(ProcessTest main.cpp)
target_link_libraries(ProcessTest NBCore)

View File

@ -0,0 +1,17 @@
#define CODE_ERROR_LOCATIONS
#include <iostream>
#include "Processes.hpp"
#include <Windows.h>
int main() {
std::cout << "nb::get_pid() -> " << nb::get_pid() << std::endl;
#ifdef _TARGET_WINDOWS
std::cout << "GetCurrentProcessId() -> " << GetCurrentProcessId() << std::endl;
#endif // _TARGET_WINDOWS
return 0;
}

View File

@ -1,28 +0,0 @@
#include <exception>
#include <iostream>
#include <fstream>
#include "SmartStreams.hpp"
int main() {
nb::SmartStream<decltype(std::cout), nb::SmartStreamFormatter> stout(std::cout);
stout << nb::SmartText{5} << "\ngoop\n";
/* nb::SmartText test(100);
std::cout << test;
std::ofstream file;
file.open("test.txt");
if (file.is_open()) {
file << test;
}
file.close(); */
return 0;
}