Skip to content

Commit

Permalink
More lazy initialization of global static objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Aug 7, 2023
1 parent d560047 commit 515826f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 61 deletions.
4 changes: 2 additions & 2 deletions examples/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct termios Global::original_term_io_settings{};


//----------------------------------------------------------------------
void onExit()
static void onExit()
{
// Restore terminal control
tcsetattr (STDIN_FILENO, TCSAFLUSH, &Global::original_term_io_settings);
Expand All @@ -66,7 +66,7 @@ auto main() -> int
tcgetattr (STDIN_FILENO, &Global::original_term_io_settings);
atexit (onExit);
struct termios new_term_io_settings{Global::original_term_io_settings};
new_term_io_settings.c_lflag &= ~(ECHO | ICANON);
new_term_io_settings.c_lflag &= uInt(~(ECHO | ICANON));
tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_term_io_settings);

// Set file descriptor of stdin to non-blocking mode
Expand Down
2 changes: 1 addition & 1 deletion final/eventloop/kqueue_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class KqueueHandler final
{
return item.timer_id == ident;
};
auto& timer_nodes = getTimerNodes();
static auto& timer_nodes = getTimerNodes();
const auto iter = std::find_if( timer_nodes.begin()
, timer_nodes.end()
, is_timer_id );
Expand Down
4 changes: 2 additions & 2 deletions final/eventloop/posix_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ PosixTimer::~PosixTimer() noexcept // destructor
if ( timer_id == timer_t{} )
return;

auto& timer_nodes{getTimerNodes()};
auto iter{timer_nodes.begin()};
static auto& timer_nodes = getTimerNodes();
auto iter = timer_nodes.begin();

while ( iter != timer_nodes.end() )
{
Expand Down
4 changes: 2 additions & 2 deletions final/eventloop/signal_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void SignalMonitor::init (int sn, handler_t hdl, void* uc)
throw monitor_error{"This instance has already been initialised."};

signal_number = sn;
auto& signal_monitors = getSignalMonitorMap();
static auto& signal_monitors = getSignalMonitorMap();
setEvents (POLLIN);
setHandler (std::move(hdl));
setUserContext (uc);
Expand Down Expand Up @@ -169,7 +169,7 @@ void SignalMonitor::init (int sn, handler_t hdl, void* uc)
void SignalMonitor::onSignal (int signal_number)
{
// Determine the signal monitor instance
auto& signal_monitors = getSignalMonitorMap();
static auto& signal_monitors = getSignalMonitorMap();
const auto iter = signal_monitors.find(signal_number);

if ( iter == signal_monitors.end() )
Expand Down
88 changes: 54 additions & 34 deletions final/output/tty/foptiattr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,46 +939,14 @@ auto FOptiAttr::setTermDefaultColor (FChar& term) -> bool
//----------------------------------------------------------------------
void FOptiAttr::setAttributesOn (FChar& term)
{
static const AttributeHandlers attribute_on_handlers
{{
{ {{0x00, 0x08, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermAltCharset(fchar); } },
{ {{0x00, 0x10, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermPCcharset(fchar); } },
{ {{0x01, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermBold(fchar); } },
{ {{0x02, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermDim(fchar); } },
{ {{0x04, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermItalic(fchar); } },
{ {{0x08, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermUnderline(fchar); } },
{ {{0x10, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermBlink(fchar); } },
{ {{0x20, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermReverse(fchar); } },
{ {{0x40, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermStandout(fchar); } },
{ {{0x80, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermInvisible(fchar); } },
{ {{0x00, 0x01, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermProtected(fchar); } },
{ {{0x00, 0x02, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermCrossedOut(fchar); } },
{ {{0x00, 0x04, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermDoubleUnderline(fchar); } }
}};

static const auto& attribute_on_handlers = getAttributeOnHandlers();
setAttributes (on.attr, attribute_on_handlers, term);
}

//----------------------------------------------------------------------
void FOptiAttr::setAttributesOff (FChar& term)
{
static const AttributeHandlers attribute_off_handlers
{{
{ {{0x00, 0x10, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermPCcharset(fchar); } },
{ {{0x00, 0x08, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermAltCharset(fchar); } },
{ {{0x01, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermBold(fchar); } },
{ {{0x02, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermDim(fchar); } },
{ {{0x04, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermItalic(fchar); } },
{ {{0x08, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermUnderline(fchar); } },
{ {{0x10, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermBlink(fchar); } },
{ {{0x20, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermReverse(fchar); } },
{ {{0x40, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermStandout(fchar); } },
{ {{0x80, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermInvisible(fchar); } },
{ {{0x00, 0x01, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermProtected(fchar); } },
{ {{0x00, 0x02, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermCrossedOut(fchar); } },
{ {{0x00, 0x04, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermDoubleUnderline(fchar); } }
}};

static const auto& attribute_off_handlers = getAttributeOffHandlers();
setAttributes (off.attr, attribute_off_handlers, term);
}

Expand Down Expand Up @@ -1392,6 +1360,58 @@ inline auto FOptiAttr::hasCharsetEquivalence() const -> bool
return false;
}

//----------------------------------------------------------------------
auto FOptiAttr::getAttributeOnHandlers() -> const AttributeHandlers&
{
static const auto& attribute_on_handlers = std::make_unique<AttributeHandlers>
(
AttributeHandlers
{{
{ {{0x00, 0x08, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermAltCharset(fchar); } },
{ {{0x00, 0x10, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermPCcharset(fchar); } },
{ {{0x01, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermBold(fchar); } },
{ {{0x02, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermDim(fchar); } },
{ {{0x04, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermItalic(fchar); } },
{ {{0x08, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermUnderline(fchar); } },
{ {{0x10, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermBlink(fchar); } },
{ {{0x20, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermReverse(fchar); } },
{ {{0x40, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermStandout(fchar); } },
{ {{0x80, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermInvisible(fchar); } },
{ {{0x00, 0x01, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermProtected(fchar); } },
{ {{0x00, 0x02, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermCrossedOut(fchar); } },
{ {{0x00, 0x04, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->setTermDoubleUnderline(fchar); } }
}}
);

return *attribute_on_handlers;
}

//----------------------------------------------------------------------
auto FOptiAttr::getAttributeOffHandlers() -> const AttributeHandlers&
{
static const auto& attribute_off_handlers = std::make_unique<AttributeHandlers>
(
AttributeHandlers
{{
{ {{0x00, 0x10, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermPCcharset(fchar); } },
{ {{0x00, 0x08, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermAltCharset(fchar); } },
{ {{0x01, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermBold(fchar); } },
{ {{0x02, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermDim(fchar); } },
{ {{0x04, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermItalic(fchar); } },
{ {{0x08, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermUnderline(fchar); } },
{ {{0x10, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermBlink(fchar); } },
{ {{0x20, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermReverse(fchar); } },
{ {{0x40, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermStandout(fchar); } },
{ {{0x80, 0x00, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermInvisible(fchar); } },
{ {{0x00, 0x01, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermProtected(fchar); } },
{ {{0x00, 0x02, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermCrossedOut(fchar); } },
{ {{0x00, 0x04, 0x00, 0x00}}, [] (FOptiAttr* obj, FChar& fchar) { return obj->unsetTermDoubleUnderline(fchar); } }
}}
);

return *attribute_off_handlers;
}

//----------------------------------------------------------------------
auto FOptiAttr::getByte0ReverseMask() -> uInt8
{
Expand Down
2 changes: 2 additions & 0 deletions final/output/tty/foptiattr.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ class FOptiAttr final
void reset (FChar&) const;
auto caused_reset_attributes (const char[], uChar = all_tests) const -> bool;
auto hasCharsetEquivalence() const -> bool;
static auto getAttributeOnHandlers() -> const AttributeHandlers&;
static auto getAttributeOffHandlers() -> const AttributeHandlers&;
static auto getByte0ReverseMask() -> uInt8;
static auto getByte1Mask() -> uInt8;
static auto getByte1ResetMask() -> uInt8;
Expand Down
56 changes: 36 additions & 20 deletions final/vterm/fvtermattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ namespace finalcut
// static class attributes
FChar FVTermAttribute::next_attribute{};

// Using-declaration
using map_type = std::pair<const Style, std::function<void(bool)>>;
using AttributeLookupMap = std::unordered_map<const Style, std::function<void(bool)>>;

//----------------------------------------------------------------------
static auto getAttributeLookupMap() -> AttributeLookupMap&
{
// Encapsulate global unordered_map object
static const auto& attribute_lookup = std::make_unique<AttributeLookupMap>
(
std::initializer_list<map_type>
({
{ Style::Bold, &FVTermAttribute::setBold },
{ Style::Dim, &FVTermAttribute::setDim },
{ Style::Italic, &FVTermAttribute::setItalic },
{ Style::Underline, &FVTermAttribute::setUnderline },
{ Style::Blink, &FVTermAttribute::setBlink },
{ Style::Reverse, &FVTermAttribute::setReverse },
{ Style::Standout, &FVTermAttribute::setStandout },
{ Style::Invisible, &FVTermAttribute::setInvisible },
{ Style::Protected, &FVTermAttribute::setProtected },
{ Style::CrossedOut, &FVTermAttribute::setCrossedOut },
{ Style::DoubleUnderline, &FVTermAttribute::setDoubleUnderline },
{ Style::Transparent, &FVTermAttribute::setTransparent },
{ Style::ColorOverlay, &FVTermAttribute::setColorOverlay },
{ Style::InheritBackground, &FVTermAttribute::setInheritBackground }
})
);

return *attribute_lookup;
}


//----------------------------------------------------------------------
// class FVTermAttribute
//----------------------------------------------------------------------
Expand Down Expand Up @@ -64,25 +97,8 @@ void FVTermAttribute::initAttribute()
//----------------------------------------------------------------------
void FVTermAttribute::print (const FStyle& style)
{
static const std::unordered_map<Style, std::function<void(bool)>> attributeLookup
{
{ Style::Bold, &FVTermAttribute::setBold },
{ Style::Dim, &FVTermAttribute::setDim },
{ Style::Italic, &FVTermAttribute::setItalic },
{ Style::Underline, &FVTermAttribute::setUnderline },
{ Style::Blink, &FVTermAttribute::setBlink },
{ Style::Reverse, &FVTermAttribute::setReverse },
{ Style::Standout, &FVTermAttribute::setStandout },
{ Style::Invisible, &FVTermAttribute::setInvisible },
{ Style::Protected, &FVTermAttribute::setProtected },
{ Style::CrossedOut, &FVTermAttribute::setCrossedOut },
{ Style::DoubleUnderline, &FVTermAttribute::setDoubleUnderline },
{ Style::Transparent, &FVTermAttribute::setTransparent },
{ Style::ColorOverlay, &FVTermAttribute::setColorOverlay },
{ Style::InheritBackground, &FVTermAttribute::setInheritBackground }
};

Style attr = style.getStyle();
static const auto& attribute_lookup = getAttributeLookupMap();

if ( attr == Style::None )
{
Expand All @@ -93,9 +109,9 @@ void FVTermAttribute::print (const FStyle& style)
while ( attr != Style::None )
{
const auto style_name = Style(attr & -attr); // Find rightmost set bit
const auto iter = attributeLookup.find(style_name);
const auto iter = attribute_lookup.find(style_name);

if ( iter != attributeLookup.end() )
if ( iter != attribute_lookup.end() )
iter->second(true); // Sets the found style

attr ^= style_name; // Clear the rightmost set bit
Expand Down

0 comments on commit 515826f

Please sign in to comment.