Compare commits
5 Commits
44aa8d060a
...
69a5169841
Author | SHA1 | Date |
---|---|---|
Johannes Kapfhammer | 69a5169841 | |
Johannes Kapfhammer | d594be1ac0 | |
Johannes Kapfhammer | 475a7f7910 | |
Johannes Kapfhammer | 3d1c648b4f | |
Johannes Kapfhammer | d8a3ccc553 |
16
README.md
16
README.md
|
@ -2,21 +2,25 @@
|
|||
|
||||
Include soi and you're ready to go!
|
||||
|
||||
## dbg(...)
|
||||
## Features
|
||||
|
||||
You can debug any expressions using the macro dbg().
|
||||
- includes all headers (a `bits/stdc++.h` that works on all platforms)
|
||||
- has a powerful `dbg()` macro
|
||||
- disables output buffering
|
||||
- checks whether all input has been read
|
||||
- removes evil functions from the standard library such as new and printf.
|
||||
|
||||
## Configuration
|
||||
|
||||
You can set the following environment variables to customize the behaviour.
|
||||
|
||||
`SOI_H_COLOR`: By default, colored output is shown in case a terminal is connected.
|
||||
You can force colors with `SOI_H_COLOR=1` and shut them off with `SOI_H_COLOR=0`.
|
||||
`SOI_COLOR`: By default, colored output is shown in case a terminal is connected.
|
||||
You can force colors with `SOI_COLOR=1` and shut them off with `SOI_COLOR=0`.
|
||||
|
||||
`SOI_H_EOFCHECK`: By default, it is *not* checked, whether you have read all the
|
||||
`SOI_EOFCHECK`: By default, it is *not* checked, whether you have read all the
|
||||
input. In case you pipe the input from a file or want to issue a proper EOF
|
||||
character (Control D under linux or Control Z under Windows), you can enable
|
||||
this check using `SOI_H_COLOR=1`.
|
||||
this check using `SOI_EOFCHECK=1`.
|
||||
|
||||
## Template
|
||||
Using this header is as simple as:
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <deque>
|
||||
#include <forward_list>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <valarray>
|
||||
#include <vector>
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
#include <any>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
#endif
|
|
@ -7,8 +7,8 @@
|
|||
//
|
||||
// The global operator<< overload hs been removed
|
||||
|
||||
#ifndef SOI_H_PRETTY_PRINT
|
||||
#define SOI_H_PRETTY_PRINT
|
||||
#ifndef SOI_PRETTY_PRINT
|
||||
#define SOI_PRETTY_PRINT
|
||||
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
|
@ -88,7 +88,7 @@ namespace pretty_print
|
|||
struct delimiters
|
||||
{
|
||||
using type = delimiters_values<TChar>;
|
||||
static const type values;
|
||||
static const type values;
|
||||
};
|
||||
|
||||
|
||||
|
@ -441,4 +441,4 @@ namespace std
|
|||
}
|
||||
*/
|
||||
|
||||
#endif // SOI_H_PRETTY_PRINT
|
||||
#endif // SOI_PRETTY_PRINT
|
|
@ -7,13 +7,15 @@
|
|||
// initialize with dbg_init() to enable colorized output
|
||||
//
|
||||
|
||||
#ifndef SOI_H_DBG
|
||||
#define SOI_H_DBG
|
||||
#ifndef SOI_DBG
|
||||
#define SOI_DBG
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
||||
#include <unistd.h>
|
||||
|
@ -88,16 +90,6 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
struct static_string_no_std {
|
||||
bool is_debug;
|
||||
bool is_std;
|
||||
static_string type;
|
||||
CONSTEXPR11_TN static_string_no_std(bool is_debug,
|
||||
bool is_std,
|
||||
static_string type)
|
||||
: is_debug(is_debug), is_std(is_std), type(type) {}
|
||||
};
|
||||
|
||||
inline
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, static_string const& s)
|
||||
|
@ -105,20 +97,6 @@ operator<<(std::ostream& os, static_string const& s)
|
|||
return os.write(s.data(), s.size());
|
||||
}
|
||||
|
||||
inline
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, static_string_no_std const& s) {
|
||||
if (s.is_debug) {
|
||||
const int k = sizeof("std::__debug::") - 1;
|
||||
return os.write(s.type.data()+k, s.type.size()-k);
|
||||
} else if (s.is_std) {
|
||||
const int k = sizeof("std::") - 1;
|
||||
return os.write(s.type.data()+k, s.type.size()-k);
|
||||
} else {
|
||||
return os.write(s.type.data(), s.type.size());
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
CONSTEXPR14_TN
|
||||
static_string
|
||||
|
@ -141,31 +119,27 @@ type_name()
|
|||
#endif
|
||||
}
|
||||
|
||||
constexpr bool is_prefix_of(char const *suffix, char const *s) {
|
||||
return suffix[0]=='\0' || (suffix[0] == s[0] && is_prefix_of(suffix + 1, s + 1));
|
||||
}
|
||||
|
||||
static char const* const type_string = "string";
|
||||
|
||||
template <class T>
|
||||
CONSTEXPR14_TN
|
||||
static_string_no_std
|
||||
static_string
|
||||
sanitized_type_name() {
|
||||
CONSTEXPR14_TN static_string t = type_name<T>();
|
||||
CONSTEXPR14_TN bool is_std =
|
||||
t.size() >= sizeof("std::") &&
|
||||
t[ 0] == 's' &&
|
||||
t[ 1] == 't' &&
|
||||
t[ 2] == 'd' &&
|
||||
t[ 3] == ':' &&
|
||||
t[ 4] == ':';
|
||||
CONSTEXPR14_TN bool is_debug =
|
||||
is_std &&
|
||||
t.size() >= sizeof("std::__debug::") &&
|
||||
t[ 5] == '_' &&
|
||||
t[ 6] == '_' &&
|
||||
t[ 7] == 'd' &&
|
||||
t[ 8] == 'e' &&
|
||||
t[ 9] == 'b' &&
|
||||
t[10] == 'u' &&
|
||||
t[11] == 'g';
|
||||
return static_string_no_std(is_debug, is_std, t);
|
||||
CONSTEXPR14_TN std::size_t offset =
|
||||
is_prefix_of("std::__debug::", t.data()) ? sizeof("std::__debug::")-1 :
|
||||
is_prefix_of("std::", t.data()) ? sizeof("std::")-1 :
|
||||
0;
|
||||
return
|
||||
is_prefix_of("std::__cxx11::basic_string<char>", t.data()) ?
|
||||
static_string(type_string, sizeof(type_string)-2) :
|
||||
static_string(t.data()+offset, t.size()-offset);
|
||||
}
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// colorized output
|
||||
|
||||
|
@ -178,7 +152,7 @@ bool tty_supports_colors() {
|
|||
}
|
||||
|
||||
int has_environment_color_overwrite() {
|
||||
if (const char* color_enabled = std::getenv("SOI_H_COLOR")) {
|
||||
if (const char* color_enabled = std::getenv("SOI_COLOR")) {
|
||||
if (!std::strcmp(color_enabled, "1"))
|
||||
return 1;
|
||||
if (!std::strcmp(color_enabled, "0"))
|
||||
|
@ -208,11 +182,11 @@ static const char* ANSI_RESET = "";
|
|||
void dbg_init(bool with_colors) {
|
||||
if (with_colors) {
|
||||
bool colors_enabled = true;
|
||||
ANSI_DEBUG = "\x1b[02m";
|
||||
ANSI_DEBUG = "\x1b[37m";
|
||||
ANSI_EXPRESSION = "\x1b[36m";
|
||||
ANSI_VALUE = "\x1b[01m";
|
||||
ANSI_TYPE = "\x1b[32m";
|
||||
ANSI_MESSAGE = "\x1b[31m\x1b[01m";
|
||||
ANSI_MESSAGE = "\x1b[31;01m";
|
||||
ANSI_RESET = "\x1b[0m";
|
||||
} else {
|
||||
bool colors_enabled = false;
|
||||
|
@ -234,7 +208,7 @@ void dbg_init() {
|
|||
|
||||
template <typename T>
|
||||
T&& dbg_print(T&& value,
|
||||
static_string_no_std const& type,
|
||||
static_string const& type,
|
||||
char const* file,
|
||||
int line,
|
||||
char const* function_name,
|
||||
|
@ -257,11 +231,11 @@ T&& dbg_print(T&& value,
|
|||
|
||||
template<unsigned int N>
|
||||
auto dbg_print(const char (&msg)[N],
|
||||
static_string_no_std const&,
|
||||
static_string const&,
|
||||
char const* file,
|
||||
int line,
|
||||
char const* function_name,
|
||||
char const* expression) -> decltype(msg) {
|
||||
char const* expression) -> decltype(msg) {
|
||||
std::cerr << ANSI_DEBUG
|
||||
<< "[" << file << ":" << line
|
||||
<< " (" << function_name << ")] " << ANSI_RESET
|
||||
|
@ -279,7 +253,7 @@ T&& identity(T&& t) {
|
|||
|
||||
} // end namespace dbg_macro
|
||||
|
||||
#ifdef SOI_H_RELEASE
|
||||
#ifdef SOI_RELEASE
|
||||
#define dbg(...) dbg_macro::identity(__VA_ARGS__)
|
||||
#else
|
||||
#define dbg(...) \
|
||||
|
@ -291,4 +265,4 @@ T&& identity(T&& t) {
|
|||
#__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#endif // SOI_H_DBG
|
||||
#endif // SOI_DBG
|
|
@ -6,8 +6,8 @@
|
|||
// pretty printing with c++
|
||||
//
|
||||
|
||||
#ifndef SOI_H_PRETTY
|
||||
#define SOI_H_PRETTY
|
||||
#ifndef SOI_PRETTY
|
||||
#define SOI_PRETTY
|
||||
|
||||
#include "prettyprint.hpp"
|
||||
|
||||
|
@ -73,4 +73,4 @@ pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, const T& x) {
|
|||
|
||||
}
|
||||
|
||||
#endif // SOI_H_PRETTY
|
||||
#endif // SOI_PRETTY
|
41
include/soi
41
include/soi
|
@ -5,33 +5,11 @@
|
|||
have to understand every concept all at once.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <valarray>
|
||||
#include <vector>
|
||||
|
||||
#include "soi-dbg.hpp"
|
||||
#include "bits/soi-dbg.hpp"
|
||||
|
||||
namespace soi_h {
|
||||
|
||||
|
||||
void check_for_eof() {
|
||||
if (!(std::cin >> std::ws).eof())
|
||||
std::cerr << "WARNING: didn't read the whole input\n";
|
||||
|
@ -56,7 +34,7 @@ void noninteractive_check_eof() {
|
|||
}
|
||||
|
||||
bool should_check_for_eof() {
|
||||
if (const char* eofcheck_enabled = std::getenv("SOI_H_EOFCHECK")) {
|
||||
if (const char* eofcheck_enabled = std::getenv("SOI_EOFCHECK")) {
|
||||
if (!std::strcmp(eofcheck_enabled, "1"))
|
||||
return true;
|
||||
if (!std::strcmp(eofcheck_enabled, "0"))
|
||||
|
@ -69,14 +47,14 @@ void initialize_debug() {
|
|||
std::cout << std::unitbuf; // enable automatic flushing
|
||||
std::cin.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
std::ios::sync_with_stdio(false);
|
||||
|
||||
|
||||
if (should_check_for_eof() &&
|
||||
std::atexit(noninteractive_check_eof) != 0) {
|
||||
std::cerr << "WARNING: soi.h -- registration of sanity check at exit failed\n";
|
||||
std::cerr << "WARNING: soi.h -- registration of sanity check at exit failed\n";
|
||||
}
|
||||
soi_h::detail::dbg_init();
|
||||
}
|
||||
|
||||
|
||||
struct soi_h_initializer {
|
||||
soi_h_initializer(bool release) {
|
||||
if (release) {
|
||||
|
@ -93,11 +71,12 @@ soi_h_initializer soi_h_initializer_{true};
|
|||
#else
|
||||
soi_h_initializer soi_h_initializer_{false};
|
||||
#endif
|
||||
|
||||
|
||||
} // end namespace soi_h
|
||||
|
||||
#include "soi-deprecate.hpp"
|
||||
#include "bits/soi-deprecate.hpp"
|
||||
|
||||
#include "bits/include-all.hpp"
|
||||
|
||||
#define int int64_t
|
||||
using namespace std;
|
||||
|
||||
|
|
Loading…
Reference in New Issue