aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/util')
-rw-r--r--src/crepe/util/Log.cpp9
-rw-r--r--src/crepe/util/Log.h27
-rw-r--r--src/crepe/util/Log.hpp3
-rw-r--r--src/crepe/util/LogColor.cpp65
-rw-r--r--src/crepe/util/LogColor.h24
-rw-r--r--src/crepe/util/OptionalRef.h47
-rw-r--r--src/crepe/util/OptionalRef.hpp45
-rw-r--r--src/crepe/util/Proxy.h6
-rw-r--r--src/crepe/util/Proxy.hpp6
9 files changed, 115 insertions, 117 deletions
diff --git a/src/crepe/util/Log.cpp b/src/crepe/util/Log.cpp
index e583734..84d80a8 100644
--- a/src/crepe/util/Log.cpp
+++ b/src/crepe/util/Log.cpp
@@ -1,9 +1,8 @@
-#include <cstdarg>
-#include <cstdio>
-#include <cstdlib>
+#include <iostream>
#include <string>
#include "../api/Config.h"
+
#include "Log.h"
using namespace crepe;
@@ -33,6 +32,6 @@ void Log::log(const Level & level, const string & msg) {
if (!out.ends_with("\n")) out += "\n";
// TODO: also log to file or smth
- fwrite(out.c_str(), 1, out.size(), stdout);
- fflush(stdout);
+ cout.write(out.data(), out.size());
+ cout.flush();
}
diff --git a/src/crepe/util/Log.h b/src/crepe/util/Log.h
index 01452b2..fc0bb3a 100644
--- a/src/crepe/util/Log.h
+++ b/src/crepe/util/Log.h
@@ -9,16 +9,14 @@
// utility macros
#define _crepe_logf_here(level, fmt, ...) \
- crepe::Log::logf( \
- level, "{}" fmt, \
- crepe::LogColor().fg_white(false).str(std::format( \
- "{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), \
- __VA_ARGS__)
+ crepe::Log::logf(level, "{}" fmt, \
+ crepe::LogColor().fg_white(false).str(std::format( \
+ "{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), \
+ __VA_ARGS__)
// very illegal global function-style macros
// NOLINTBEGIN
-#define dbg_logf(fmt, ...) \
- _crepe_logf_here(crepe::Log::Level::DEBUG, ": " fmt, __VA_ARGS__)
+#define dbg_logf(fmt, ...) _crepe_logf_here(crepe::Log::Level::DEBUG, ": " fmt, __VA_ARGS__)
#define dbg_log(str) _crepe_logf_here(crepe::Log::Level::DEBUG, ": {}", str)
#define dbg_trace() _crepe_logf_here(crepe::Log::Level::TRACE, "", "")
// NOLINTEND
@@ -36,11 +34,11 @@ class Log {
public:
//! Log message severity
enum Level {
- TRACE, //< Include (internal) function calls
- DEBUG, //< Include dbg_logf output
- INFO, //< General-purpose messages
- WARNING, //< Non-fatal errors
- ERROR, //< Fatal errors
+ TRACE, //!< Include (internal) function calls
+ DEBUG, //!< Include dbg_logf output
+ INFO, //!< General-purpose messages
+ WARNING, //!< Non-fatal errors
+ ERROR, //!< Fatal errors
};
/**
@@ -59,8 +57,7 @@ public:
* \param args Format arguments
*/
template <class... Args>
- static void logf(const Level & level, std::format_string<Args...> fmt,
- Args &&... args);
+ static void logf(const Level & level, std::format_string<Args...> fmt, Args &&... args);
/**
* \brief Format a message and log it (with default severity \c INFO)
@@ -76,6 +73,8 @@ private:
* \brief Output a message prefix depending on the log level
*
* \param level Message severity
+ *
+ * \return Colored message severity prefix string
*/
static std::string prefix(const Level & level);
};
diff --git a/src/crepe/util/Log.hpp b/src/crepe/util/Log.hpp
index 651f076..c2156cd 100644
--- a/src/crepe/util/Log.hpp
+++ b/src/crepe/util/Log.hpp
@@ -10,8 +10,7 @@ void Log::logf(std::format_string<Args...> fmt, Args &&... args) {
}
template <class... Args>
-void Log::logf(const Level & level, std::format_string<Args...> fmt,
- Args &&... args) {
+void Log::logf(const Level & level, std::format_string<Args...> fmt, Args &&... args) {
Log::log(level, std::format(fmt, std::forward<Args>(args)...));
}
diff --git a/src/crepe/util/LogColor.cpp b/src/crepe/util/LogColor.cpp
index ae44d72..5411898 100644
--- a/src/crepe/util/LogColor.cpp
+++ b/src/crepe/util/LogColor.cpp
@@ -1,6 +1,7 @@
#include <cstdarg>
#include "../api/Config.h"
+
#include "LogColor.h"
using namespace crepe;
@@ -27,51 +28,19 @@ LogColor & LogColor::reset() {
return *this;
}
-LogColor & LogColor::fg_black(bool bright) {
- return this->add_code(bright ? 90 : 30);
-}
-LogColor & LogColor::fg_red(bool bright) {
- return this->add_code(bright ? 91 : 31);
-}
-LogColor & LogColor::fg_green(bool bright) {
- return this->add_code(bright ? 92 : 32);
-}
-LogColor & LogColor::fg_yellow(bool bright) {
- return this->add_code(bright ? 93 : 33);
-}
-LogColor & LogColor::fg_blue(bool bright) {
- return this->add_code(bright ? 94 : 34);
-}
-LogColor & LogColor::fg_magenta(bool bright) {
- return this->add_code(bright ? 95 : 35);
-}
-LogColor & LogColor::fg_cyan(bool bright) {
- return this->add_code(bright ? 96 : 36);
-}
-LogColor & LogColor::fg_white(bool bright) {
- return this->add_code(bright ? 97 : 37);
-}
-LogColor & LogColor::bg_black(bool bright) {
- return this->add_code(bright ? 100 : 40);
-}
-LogColor & LogColor::bg_red(bool bright) {
- return this->add_code(bright ? 101 : 41);
-}
-LogColor & LogColor::bg_green(bool bright) {
- return this->add_code(bright ? 102 : 42);
-}
-LogColor & LogColor::bg_yellow(bool bright) {
- return this->add_code(bright ? 103 : 43);
-}
-LogColor & LogColor::bg_blue(bool bright) {
- return this->add_code(bright ? 104 : 44);
-}
-LogColor & LogColor::bg_magenta(bool bright) {
- return this->add_code(bright ? 105 : 45);
-}
-LogColor & LogColor::bg_cyan(bool bright) {
- return this->add_code(bright ? 106 : 46);
-}
-LogColor & LogColor::bg_white(bool bright) {
- return this->add_code(bright ? 107 : 47);
-}
+LogColor & LogColor::fg_black(bool bright) { return this->add_code(bright ? 90 : 30); }
+LogColor & LogColor::fg_red(bool bright) { return this->add_code(bright ? 91 : 31); }
+LogColor & LogColor::fg_green(bool bright) { return this->add_code(bright ? 92 : 32); }
+LogColor & LogColor::fg_yellow(bool bright) { return this->add_code(bright ? 93 : 33); }
+LogColor & LogColor::fg_blue(bool bright) { return this->add_code(bright ? 94 : 34); }
+LogColor & LogColor::fg_magenta(bool bright) { return this->add_code(bright ? 95 : 35); }
+LogColor & LogColor::fg_cyan(bool bright) { return this->add_code(bright ? 96 : 36); }
+LogColor & LogColor::fg_white(bool bright) { return this->add_code(bright ? 97 : 37); }
+LogColor & LogColor::bg_black(bool bright) { return this->add_code(bright ? 100 : 40); }
+LogColor & LogColor::bg_red(bool bright) { return this->add_code(bright ? 101 : 41); }
+LogColor & LogColor::bg_green(bool bright) { return this->add_code(bright ? 102 : 42); }
+LogColor & LogColor::bg_yellow(bool bright) { return this->add_code(bright ? 103 : 43); }
+LogColor & LogColor::bg_blue(bool bright) { return this->add_code(bright ? 104 : 44); }
+LogColor & LogColor::bg_magenta(bool bright) { return this->add_code(bright ? 105 : 45); }
+LogColor & LogColor::bg_cyan(bool bright) { return this->add_code(bright ? 106 : 46); }
+LogColor & LogColor::bg_white(bool bright) { return this->add_code(bright ? 107 : 47); }
diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h
index 4b65127..132fb94 100644
--- a/src/crepe/util/LogColor.h
+++ b/src/crepe/util/LogColor.h
@@ -12,7 +12,13 @@ namespace crepe {
*/
class LogColor {
public:
- //! Get color code as stl string (or color content string)
+ /**
+ * \brief Get color code as STL string
+ *
+ * \param content If given, color this string and append a color reset escape sequence.
+ *
+ * \returns Color escape sequence
+ */
const std::string str(const std::string & content = "") const;
public:
@@ -20,6 +26,13 @@ public:
LogColor & reset();
public:
+ /**
+ * \name Foreground colors
+ *
+ * These functions set the foreground (text) color. The \c bright parameter
+ * makes the color brighter, or bold on some terminals.
+ * \{
+ */
LogColor & fg_black(bool bright = false);
LogColor & fg_red(bool bright = false);
LogColor & fg_green(bool bright = false);
@@ -28,8 +41,16 @@ public:
LogColor & fg_magenta(bool bright = false);
LogColor & fg_cyan(bool bright = false);
LogColor & fg_white(bool bright = false);
+ /// \}
public:
+ /**
+ * \name Background colors
+ *
+ * These functions set the background color. The \c bright parameter makes
+ * the color brighter.
+ * \{
+ */
LogColor & bg_black(bool bright = false);
LogColor & bg_red(bool bright = false);
LogColor & bg_green(bool bright = false);
@@ -38,6 +59,7 @@ public:
LogColor & bg_magenta(bool bright = false);
LogColor & bg_cyan(bool bright = false);
LogColor & bg_white(bool bright = false);
+ /// \}
private:
/**
diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h
index 1ad3a6d..1b2cb3f 100644
--- a/src/crepe/util/OptionalRef.h
+++ b/src/crepe/util/OptionalRef.h
@@ -12,20 +12,46 @@ namespace crepe {
template <typename T>
class OptionalRef {
public:
+ //! Initialize empty (nonexistant) reference
OptionalRef() = default;
- OptionalRef(T &);
- OptionalRef<T> & operator=(T &);
+ //! Initialize reference with value
+ OptionalRef(T & ref);
+ /**
+ * \brief Assign new reference
+ *
+ * \param ref Reference to assign
+ *
+ * \return Reference to this (required for operator)
+ */
+ OptionalRef<T> & operator=(T & ref);
+ /**
+ * \brief Retrieve this reference (cast)
+ *
+ * \returns Internal reference if it is set
+ *
+ * \throws std::runtime_error if this function is called while the reference it not set
+ */
+ operator T &() const;
+ /**
+ * \brief Retrieve this reference (member access)
+ *
+ * \returns Internal reference if it is set
+ *
+ * \throws std::runtime_error if this function is called while the reference it not set
+ */
+ T * operator->() const;
+ /**
+ * \brief Check if this reference is not empty
+ *
+ * \returns `true` if reference is set, or `false` if it is not
+ */
explicit operator bool() const noexcept;
- void set(T &) noexcept;
- T & get() const;
+ /**
+ * \brief Make this reference empty
+ */
void clear() noexcept;
- OptionalRef(const OptionalRef<T> &);
- OptionalRef(OptionalRef<T> &&);
- OptionalRef<T> & operator=(const OptionalRef<T> &);
- OptionalRef<T> & operator=(OptionalRef<T> &&);
-
private:
/**
* \brief Reference to the value of type \c T
@@ -35,7 +61,6 @@ private:
T * ref = nullptr;
};
-}
+} // namespace crepe
#include "OptionalRef.hpp"
-
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp
index 7b201b0..5e36b3a 100644
--- a/src/crepe/util/OptionalRef.hpp
+++ b/src/crepe/util/OptionalRef.hpp
@@ -8,53 +8,26 @@ namespace crepe {
template <typename T>
OptionalRef<T>::OptionalRef(T & ref) {
- this->set(ref);
-}
-
-template <typename T>
-OptionalRef<T>::OptionalRef(const OptionalRef<T> & other) {
- this->ref = other.ref;
-}
-
-template <typename T>
-OptionalRef<T>::OptionalRef(OptionalRef<T> && other) {
- this->ref = other.ref;
- other.clear();
-}
-
-template <typename T>
-OptionalRef<T> & OptionalRef<T>::operator=(const OptionalRef<T> & other) {
- this->ref = other.ref;
- return *this;
-}
-
-template <typename T>
-OptionalRef<T> & OptionalRef<T>::operator=(OptionalRef<T> && other) {
- this->ref = other.ref;
- other.clear();
- return *this;
+ this->ref = &ref;
}
template <typename T>
-T & OptionalRef<T>::get() const {
+OptionalRef<T>::operator T &() const {
if (this->ref == nullptr)
throw std::runtime_error("OptionalRef: attempt to dereference nullptr");
return *this->ref;
}
template <typename T>
-void OptionalRef<T>::set(T & ref) noexcept {
- this->ref = &ref;
-}
-
-template <typename T>
-void OptionalRef<T>::clear() noexcept {
- this->ref = nullptr;
+T * OptionalRef<T>::operator->() const {
+ if (this->ref == nullptr)
+ throw std::runtime_error("OptionalRef: attempt to dereference nullptr");
+ return this->ref;
}
template <typename T>
OptionalRef<T> & OptionalRef<T>::operator=(T & ref) {
- this->set(ref);
+ this->ref = &ref;
return *this;
}
@@ -63,5 +36,9 @@ OptionalRef<T>::operator bool() const noexcept {
return this->ref != nullptr;
}
+template <typename T>
+void OptionalRef<T>::clear() noexcept {
+ this->ref = nullptr;
}
+} // namespace crepe
diff --git a/src/crepe/util/Proxy.h b/src/crepe/util/Proxy.h
index f84e462..789144a 100644
--- a/src/crepe/util/Proxy.h
+++ b/src/crepe/util/Proxy.h
@@ -7,8 +7,8 @@ namespace crepe {
/**
* \brief Utility wrapper for \c ValueBroker
*
- * This class can be used to to wrap a ValueBroker instance so it behaves like
- * a regular variable.
+ * This class can be used to to wrap a ValueBroker instance so it behaves like a regular
+ * variable.
*
* \tparam T Type of the underlying variable
*/
@@ -16,6 +16,8 @@ template <typename T>
class Proxy {
public:
//! Set operator
+ Proxy & operator=(Proxy &);
+ //! Set operator
Proxy & operator=(const T &);
//! Get operator
operator const T &();
diff --git a/src/crepe/util/Proxy.hpp b/src/crepe/util/Proxy.hpp
index b9923db..ef2b69f 100644
--- a/src/crepe/util/Proxy.hpp
+++ b/src/crepe/util/Proxy.hpp
@@ -14,6 +14,12 @@ Proxy<T> & Proxy<T>::operator=(const T & val) {
}
template <typename T>
+Proxy<T> & Proxy<T>::operator=(Proxy & proxy) {
+ this->broker.set(T(proxy));
+ return *this;
+}
+
+template <typename T>
Proxy<T>::operator const T &() {
return this->broker.get();
}