Skip to content

Commit

Permalink
Made initialization lazy
Browse files Browse the repository at this point in the history
Before, initialization was done before the call to main(), in the
constructor of InitializationDummy. This initialization was even
repeated for every translation unit. Now, initialization is done
on-demand, via static initialization inside inline functions. This
removes the overhead at the start of the program.

Also hid a few more functions in the case that the OS is not supported.
  • Loading branch information
avdstaaij committed Nov 25, 2021
1 parent 8e6de6e commit c61e4eb
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions include/aecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,10 @@ void checkIfOutputStreamsAreTerminals();

namespace aecImplementation {

static std::atomic_bool aecSupportedStatic(false);

inline std::atomic_bool& coutIsTTY() {
static std::atomic_bool coutIsTTY(false);
return coutIsTTY;
}

inline std::atomic_bool& cerrIsTTY() {
static std::atomic_bool cerrIsTTY(false);
return cerrIsTTY;
}

inline std::atomic<Mode>& mode() noexcept {
static std::atomic<Mode> mode(Mode::Auto);
return mode;
}

struct InitializationDummy {
InitializationDummy() noexcept {
#ifdef AECPP_OS_SUPPORTED

inline bool aecSupportedStatic() noexcept {
static bool supported = [](){
const char *terms[] = {
"ansi",
"color",
Expand All @@ -168,16 +152,24 @@ struct InitializationDummy {
"xterm"
};
auto envTerm = std::getenv("TERM");
aecSupportedStatic = std::any_of(std::begin(terms), std::end(terms),
return std::any_of(std::begin(terms), std::end(terms),
[&](const char* term){
return std::strstr(envTerm, term) != nullptr;
}
);
checkIfOutputStreamsAreTerminals();
#endif
}
};
static InitializationDummy initializationDummy;
}();
return supported;
}

inline std::atomic_bool& coutIsTTY() {
static std::atomic_bool coutIsTTY(isatty(fileno(stdout)));
return coutIsTTY;
}

inline std::atomic_bool& cerrIsTTY() {
static std::atomic_bool cerrIsTTY(isatty(fileno(stderr)));
return cerrIsTTY;
}

inline bool isTTY(const std::ostream& os) noexcept {
if (os.rdbuf() == std::cout.rdbuf()) {
Expand All @@ -189,7 +181,16 @@ inline bool isTTY(const std::ostream& os) noexcept {
return false;
}

#endif // #ifdef AEC_OS_SUPPORTED

inline std::atomic<Mode>& mode() noexcept {
static std::atomic<Mode> mode(Mode::Auto);
return mode;
}

inline bool aecEnabled(const std::ostream& os) noexcept {
// Should we enable aec if the mode is set to Always even if the OS is not
// supported? Maybe.
#ifdef AECPP_OS_SUPPORTED
switch (mode()) {
case Mode::Auto:
Expand Down Expand Up @@ -234,8 +235,10 @@ inline Mode getMode() {
//============================================================================//

inline void checkIfOutputStreamsAreTerminals() {
#ifdef AECPP_OS_SUPPORTED
aecImplementation::coutIsTTY() = isatty(fileno(stdout));
aecImplementation::cerrIsTTY() = isatty(fileno(stderr));
#endif
}

//============================================================================//
Expand All @@ -246,6 +249,7 @@ class Style {
public:
// Using these constructors explicitly is not recommended.
// Instead, rely on the automatic conversion rules of c++.

Style() {}
Style(Reset) : reset(true) {}
Style(Effect effect) : effect(effect) {}
Expand Down

0 comments on commit c61e4eb

Please sign in to comment.