From 5092244b81bb2b0b9c301cde1888ad378ceacb43 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Fri, 18 Sep 2020 17:13:52 +0200 Subject: [PATCH] FDataPtr was replaced by the template class FData --- ChangeLog | 4 + doc/first-steps.md | 65 ++++++------ examples/background-color.cpp | 8 +- examples/listbox.cpp | 17 ++-- examples/windows.cpp | 8 +- fonts/bdf2code.sh | 2 +- fonts/newfont_8x16.h | 2 +- fonts/newfont_9x16.h | 2 +- fonts/unicodemap.h | 4 +- fonts/vgafont.h | 2 +- src/fapplication.cpp | 12 +++ src/fcharmap.cpp | 8 +- src/fevent.cpp | 13 +-- src/fkey_map.cpp | 4 +- src/flistbox.cpp | 13 +-- src/flistview.cpp | 42 -------- src/fmenuitem.cpp | 69 ++++++------- src/fstring.cpp | 14 +-- src/fswitch.cpp | 12 +-- src/ftermlinux.cpp | 8 +- src/fvterm.cpp | 17 ++-- src/fwidget.cpp | 4 + src/include/final/fapplication.h | 2 + src/include/final/fcallback.h | 132 ++++++++++++------------ src/include/final/fcharmap.h | 6 +- src/include/final/fcombobox.h | 32 +++--- src/include/final/fdata.h | 170 ++++++++++++++++++++++++++----- src/include/final/fevent.h | 46 ++++++++- src/include/final/fkey_map.h | 6 +- src/include/final/flistbox.h | 125 ++++++++++++++++------- src/include/final/flistview.h | 135 +++++++++++++++++------- src/include/final/fmouse.h | 4 +- src/include/final/fscrollbar.h | 5 +- src/include/final/fscrollview.h | 4 +- src/include/final/fstring.h | 4 +- src/include/final/fterm.h | 8 +- src/include/final/ftermbuffer.h | 8 +- src/include/final/ftermcap.h | 26 ++--- src/include/final/ftermlinux.h | 2 +- src/include/final/ftextview.h | 8 +- src/include/final/ftypes.h | 12 ++- src/include/final/fvterm.h | 4 +- src/include/final/fwidget.h | 12 +-- test/fcallback-test.cpp | 4 +- test/fobject-test.cpp | 10 +- 45 files changed, 679 insertions(+), 416 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4b93c1e1..b41173c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2020-09-18 Markus Gans + * The generic data type FDataPtr is now deprecated and was + completely replaced by the template class FData + 2020-09-11 Markus Gans * Fixes a problem with mouse input in Cygwin in non-blocking read mode diff --git a/doc/first-steps.md b/doc/first-steps.md index 67c99515..6b5b158f 100644 --- a/doc/first-steps.md +++ b/doc/first-steps.md @@ -431,7 +431,7 @@ class extendedApplication : public FApplication || last_avg[2] != load_avg[2] ) { FUserEvent user_event(fc::User_Event, 0); - user_event.setData (FDataPtr(&load_avg)); + user_event.setData (load_avg); FApplication::sendEvent (getMainWidget(), &user_event); } @@ -458,8 +458,7 @@ class dialogWidget final : public FDialog private: void onUserEvent (FUserEvent* ev) override { - FDataPtr dataPtr = ev->getData(); - auto& lavg = *(reinterpret_cast(dataPtr)); + const auto& lavg = ev->getData(); std::setlocale(LC_NUMERIC, "C"); loadavg_label.clear(); loadavg_label << "Load average: " << lavg[0] << ", " @@ -530,9 +529,9 @@ to other widget objects. 1. For calling functions or static methods via a pointer: ```cpp -template::type = nullptr - , typename... Args> +template< typename Function + , typename FunctionPointer::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) @@ -542,9 +541,9 @@ void FWidget::addCallback ( const FString& cb_signal 2. For calling functions or static methods via a reference: ```cpp -template::type = nullptr - , typename... Args> +template< typename Function + , typename FunctionReference::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) @@ -554,11 +553,11 @@ void FWidget::addCallback ( const FString& cb_signal 3. For calling a member method of a specific instance: ```cpp -template::type = nullptr - , typename MemberFunctionPointer::type = nullptr - , typename... Args> +template< typename Object + , typename Function + , typename ObjectPointer::type = nullptr + , typename MemberFunctionPointer::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_member @@ -568,9 +567,9 @@ void FWidget::addCallback ( const FString& cb_signal 4. For calling a std::bind call wrapper or a lambda expression: ```cpp -template::type = nullptr - , typename... Args> +template< typename Function + , typename ClassObject::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) @@ -580,11 +579,11 @@ void FWidget::addCallback ( const FString& cb_signal 5. For calling a std::bind call wrapper to a specific instance: ```cpp -template::type = nullptr - , typename ClassObject::type = nullptr - , typename... Args> +template< typename Object + , typename Function + , typename ObjectPointer::type = nullptr + , typename ClassObject::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_function @@ -596,9 +595,9 @@ void FWidget::addCallback ( const FString& cb_signal with the keyword auto: ```cpp -template::type = nullptr - , typename... Args> +template< typename Function + , typename ClassObject::type = nullptr + , typename... Args > void FWidget::addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) @@ -612,8 +611,8 @@ remove all existing callbacks from an object. 1. To delete functions or static methods callbacks via a pointer: ```cpp -template::type = nullptr> +template< typename FunctionPtr + , typename FunctionPointer::type = nullptr > void FWidget::delCallback (FunctionPtr&& cb_func_ptr) {...} ``` @@ -621,8 +620,8 @@ void FWidget::delCallback (FunctionPtr&& cb_func_ptr) 2. To delete functions or static methods callbacks via a reference: ```cpp -template::type = nullptr> +template< typename Function + , typename FunctionReference::type = nullptr > void FWidget::delCallback (Function& cb_function) {...} ``` @@ -630,8 +629,8 @@ void FWidget::delCallback (Function& cb_function) 3. To delete all callbacks from a specific instance: ```cpp -template::type = nullptr> +template< typename Object + , typename ObjectPointer::type = nullptr > void FWidget::delCallback (Object&& cb_instance) {...} ``` @@ -646,8 +645,8 @@ void delCallback (const FString& cb_signal) 5. To delete all callbacks of a signal and specific instance: ```cpp -template::type = nullptr> +template< typename Object + , typename ObjectPointer::type = nullptr > void delCallback (const FString& cb_signal, Object&& cb_instance) {...} ``` diff --git a/examples/background-color.cpp b/examples/background-color.cpp index 0894b8ea..186bdd20 100644 --- a/examples/background-color.cpp +++ b/examples/background-color.cpp @@ -108,8 +108,7 @@ Background::Background (finalcut::FWidget* parent) for (auto& c : color_list) { - FDataPtr data_ptr = reinterpret_cast(&c.second); - finalcut::FListBoxItem item (c.first, data_ptr); + finalcut::FListBoxItem item (c.first, c.second); color_choice.insert(item); } @@ -199,9 +198,8 @@ void Background::cb_choice() uChar r{}; uChar g{}; uChar b{}; - const FDataPtr data_ptr = color_choice.getItemData(); - const RGB* rgb = reinterpret_cast(data_ptr); - std::tie(r, g, b) = *rgb; + const RGB rgb = color_choice.getItemData(); + std::tie(r, g, b) = rgb; red.setValue(r); green.setValue(g); blue.setValue(b); diff --git a/examples/listbox.cpp b/examples/listbox.cpp index da66a48b..210315cd 100644 --- a/examples/listbox.cpp +++ b/examples/listbox.cpp @@ -38,8 +38,8 @@ static std::weak_ptr temp_str; // Function prototypes void doubleToItem ( FListBoxItem& - , FDataPtr container - , int index); + , FDataAccess* container + , std::size_t index); FString& doubleToString (std::list::const_iterator iter); FString& mapToString ( std::map::const_iterator iter ); @@ -47,14 +47,15 @@ FString& mapToString ( std::map* double_list_ptr; - double_list_ptr dbllist = static_cast(container); - std::list::iterator iter = dbllist->begin(); + typedef std::list DblList; + DblList& dbl_list = FListBoxHelper::getContainer(container); + std::list::iterator iter = dbl_list.begin(); std::advance (iter, index); item.setText (FString() << *iter); - item.setData (FDataPtr(&(*iter))); + item.setData (*iter); } // Insert converter functions @@ -129,7 +130,7 @@ Listbox::Listbox (FWidget* parent) // // Insert via lazy conversion on print // - list2.insert (&double_list, doubleToItem); + list2.insert (double_list, doubleToItem); // (container, converter) // // Direct insert of the complete list diff --git a/examples/windows.cpp b/examples/windows.cpp index e7785f23..1283e6f2 100644 --- a/examples/windows.cpp +++ b/examples/windows.cpp @@ -199,7 +199,9 @@ class Window final : public finalcut::FDialog void configureDialogButtons(); void activateWindow (finalcut::FDialog*) const; void adjustSize() override; - template + template void addClickedCallback ( finalcut::FWidget* , InstanceT&&, CallbackT&&, Args&&... ); template @@ -378,7 +380,9 @@ void Window::adjustSize() } //---------------------------------------------------------------------- -template +template void Window::addClickedCallback ( finalcut::FWidget* widget , InstanceT&& instance , CallbackT&& callback diff --git a/fonts/bdf2code.sh b/fonts/bdf2code.sh index 9c842194..1a2922f3 100755 --- a/fonts/bdf2code.sh +++ b/fonts/bdf2code.sh @@ -10,7 +10,7 @@ function create_code_file () echo -e "#define ${INCLUDE_GUARD}\\n" echo -e "namespace finalcut\\n{\\n" echo -e "namespace fc\\n{\\n" - echo -e "static unsigned char ${NAME}[] =\\n{" + echo -e "constexpr unsigned char ${NAME}[] =\\n{" grep -A"${HEIGHT}" "^BITMAP" "$NEWFONTFILE" \ | tr '\n' ',' \ diff --git a/fonts/newfont_8x16.h b/fonts/newfont_8x16.h index bc24802b..e9f377bc 100644 --- a/fonts/newfont_8x16.h +++ b/fonts/newfont_8x16.h @@ -9,7 +9,7 @@ namespace finalcut namespace fc { -static unsigned char __8x16graph[] = +constexpr unsigned char __8x16graph[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0 */ 0x00, 0x00, 0x7e, 0x81, 0xa5, 0x81, 0x81, 0xbd, 0x99, 0x81, 0x81, 0x7e, 0x00, 0x00, 0x00, 0x00, /* 1 */ diff --git a/fonts/newfont_9x16.h b/fonts/newfont_9x16.h index cfec8120..78f6a8aa 100644 --- a/fonts/newfont_9x16.h +++ b/fonts/newfont_9x16.h @@ -9,7 +9,7 @@ namespace finalcut namespace fc { -static unsigned char __9x16graph[] = +constexpr unsigned char __9x16graph[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0 */ 0x00, 0x00, 0x7e, 0x81, 0xa5, 0x81, 0x81, 0xbd, 0x99, 0x81, 0x81, 0x7e, 0x00, 0x00, 0x00, 0x00, /* 1 */ diff --git a/fonts/unicodemap.h b/fonts/unicodemap.h index 8b01d63d..09f9703d 100644 --- a/fonts/unicodemap.h +++ b/fonts/unicodemap.h @@ -17,7 +17,7 @@ namespace finalcut namespace fc { -static struct unipair unicode_cp437_pairs[] = +constexpr struct unipair unicode_cp437_pairs[] = { // .----------- unicode // | .---- fontpos @@ -306,7 +306,7 @@ static struct unipair unicode_cp437_pairs[] = {0x266c, 0x0e} }; -static struct unipair unicode_newfont_pairs[] = +constexpr struct unipair unicode_newfont_pairs[] = { // .----------- unicode // | .---- fontpos diff --git a/fonts/vgafont.h b/fonts/vgafont.h index bd0852ec..c4532aad 100644 --- a/fonts/vgafont.h +++ b/fonts/vgafont.h @@ -9,7 +9,7 @@ namespace finalcut namespace fc { -static unsigned char __8x16std[] = +constexpr unsigned char __8x16std[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0 */ 0x00, 0x00, 0x7e, 0x81, 0xa5, 0x81, 0x81, 0xbd, 0x99, 0x81, 0x81, 0x7e, 0x00, 0x00, 0x00, 0x00, /* 1 */ diff --git a/src/fapplication.cpp b/src/fapplication.cpp index ec2b6680..70e5b008 100644 --- a/src/fapplication.cpp +++ b/src/fapplication.cpp @@ -121,6 +121,12 @@ FApplication* FApplication::getApplicationObject() return app_object; } +//---------------------------------------------------------------------- +FWidget* FApplication::getKeyboardWidget() +{ + return keyboard_widget; +} + //---------------------------------------------------------------------- FApplication::FLogPtr& FApplication::getLog() { @@ -306,6 +312,12 @@ void FApplication::setDarkTheme() setColorTheme(); } +//---------------------------------------------------------------------- +void FApplication::setKeyboardWidget (FWidget* widget) +{ + keyboard_widget = widget; +} + //---------------------------------------------------------------------- void FApplication::closeConfirmationDialog (FWidget* w, FCloseEvent* ev) { diff --git a/src/fcharmap.cpp b/src/fcharmap.cpp index 23bb8753..ad782f45 100644 --- a/src/fcharmap.cpp +++ b/src/fcharmap.cpp @@ -3,7 +3,7 @@ * * * This file is part of the FINAL CUT widget toolkit * * * -* Copyright 2015-2019 Markus Gans * +* Copyright 2015-2020 Markus Gans * * * * FINAL CUT is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -163,7 +163,7 @@ const std::size_t lastCharItem = \ std::size_t((sizeof(character) / sizeof(character[0])) - 1); -int vt100_key_to_utf8[][2] = +constexpr int vt100_key_to_utf8[][2] = { {fc::vt100_key_rarrow , fc::BlackRightPointingPointer}, // ► {fc::vt100_key_larrow , fc::BlackLeftPointingPointer}, // ◄ @@ -210,7 +210,7 @@ const std::size_t lastKeyItem = \ std::size_t((sizeof(vt100_key_to_utf8) / sizeof(vt100_key_to_utf8[0])) - 1); -wchar_t cp437_ucs[][2] = +constexpr wchar_t cp437_ucs[][2] = { {0x00, 0x0000}, // null {0x01, 0x263a}, // white smiling face @@ -474,7 +474,7 @@ const std::size_t lastCP437Item = \ std::size_t((sizeof(cp437_ucs) / sizeof(cp437_ucs[0])) - 1); // Based on http://www.unicode.org/charts/PDF/UFF00.pdf -const wchar_t halfWidth_fullWidth[][2] = +constexpr wchar_t halfWidth_fullWidth[][2] = { // Fullwidth ASCII variants {0x0020, 0x3000}, // ' ' -> ' ' diff --git a/src/fevent.cpp b/src/fevent.cpp index d9d1468f..588a9e83 100644 --- a/src/fevent.cpp +++ b/src/fevent.cpp @@ -366,18 +366,13 @@ FUserEvent::FUserEvent (fc::events ev_type, int user_event_id) // constructor //---------------------------------------------------------------------- FUserEvent::~FUserEvent() // destructor -{ } +{ + if ( ! external_data_pointer && data_pointer ) + delete data_pointer; +} //---------------------------------------------------------------------- int FUserEvent::getUserId() const { return uid; } -//---------------------------------------------------------------------- -FDataPtr FUserEvent::getData() const -{ return data_pointer; } - -//---------------------------------------------------------------------- -void FUserEvent::setData (FDataPtr data) -{ data_pointer = data; } - } // namespace finalcut diff --git a/src/fkey_map.cpp b/src/fkey_map.cpp index 95a291f0..ea1e0089 100644 --- a/src/fkey_map.cpp +++ b/src/fkey_map.cpp @@ -214,7 +214,7 @@ FKeyMap fkey[] = { 0 , nullptr, "\0" } }; -FMetakeyMap fmetakey[] = +constexpr FMetakeyMap fmetakey[] = { { fc::Fmkey_ic , "\033[2;3~" }, // M-insert { fc::Fmkey_ic , "\033\033[2~" }, // M-insert @@ -447,7 +447,7 @@ FMetakeyMap fmetakey[] = { 0 , "\0" } }; -FKeyName fkeyname[] = +constexpr FKeyName fkeyname[] = { { fc::Fckey_a , "Ctrl+A" }, { fc::Fckey_b , "Ctrl+B" }, diff --git a/src/flistbox.cpp b/src/flistbox.cpp index 163f03ba..4cdf4130 100644 --- a/src/flistbox.cpp +++ b/src/flistbox.cpp @@ -51,12 +51,6 @@ FListBoxItem::FListBoxItem (const FListBoxItem& item) , selected{item.selected} { } -//---------------------------------------------------------------------- -FListBoxItem::FListBoxItem (const FString& txt, FDataPtr data) - : text{txt} - , data_pointer{data} -{ } - //---------------------------------------------------------------------- FListBoxItem::~FListBoxItem() // destructor { } @@ -95,6 +89,9 @@ FListBox::FListBox (FWidget* parent) //---------------------------------------------------------------------- FListBox::~FListBox() // destructor { + if ( source_container ) + delete source_container; // for lazy conversion + delOwnTimers(); } @@ -1729,12 +1726,12 @@ void FListBox::changeOnResize() const } //---------------------------------------------------------------------- -void FListBox::lazyConvert(listBoxItems::iterator iter, int y) +void FListBox::lazyConvert(listBoxItems::iterator iter, std::size_t y) { if ( conv_type != lazy_convert || ! iter->getText().isNull() ) return; - lazy_inserter (*iter, source_container, y + yoffset); + lazy_inserter (*iter, source_container, y + std::size_t(yoffset)); const auto column_width = getColumnWidth(iter->text); recalculateHorizontalBar (column_width, hasBrackets(iter)); diff --git a/src/flistview.cpp b/src/flistview.cpp index 77fb518d..7bcb30e2 100644 --- a/src/flistview.cpp +++ b/src/flistview.cpp @@ -198,21 +198,6 @@ FListViewItem::FListViewItem (iterator parent_iter) insert (this, parent_iter); } -//---------------------------------------------------------------------- -FListViewItem::FListViewItem ( const FStringList& cols - , FDataPtr data - , iterator parent_iter ) - : FObject{nullptr} - , column_list{cols} - , data_pointer{data} -{ - if ( cols.empty() ) - return; - - replaceControlCodes(); - insert (this, parent_iter); -} - //---------------------------------------------------------------------- FListViewItem::~FListViewItem() // destructor { @@ -895,33 +880,6 @@ FObject::iterator FListView::insert ( FListViewItem* item return item_iter; } -//---------------------------------------------------------------------- -FObject::iterator FListView::insert ( const FStringList& cols - , FDataPtr d - , iterator parent_iter ) -{ - FListViewItem* item; - - if ( cols.empty() || parent_iter == getNullIterator() ) - return getNullIterator(); - - if ( ! *parent_iter ) - parent_iter = root; - - try - { - item = new FListViewItem (cols, d, getNullIterator()); - } - catch (const std::bad_alloc&) - { - badAllocOutput ("FListViewItem"); - return getNullIterator(); - } - - item->replaceControlCodes(); - return insert(item, parent_iter); -} - //---------------------------------------------------------------------- void FListView::remove (FListViewItem* item) { diff --git a/src/fmenuitem.cpp b/src/fmenuitem.cpp index 1ebc09c4..2dee3015 100644 --- a/src/fmenuitem.cpp +++ b/src/fmenuitem.cpp @@ -605,47 +605,42 @@ void FMenuItem::createDialogList (FMenu* winmenu) const while ( iter != getDialogList()->end() && *iter ) { auto win = static_cast(*iter); + FMenuItem* win_item{}; + const uInt32 n = uInt32(std::distance(first, iter)); + // get the dialog title + const auto& name = win->getText(); - if ( win ) + try { - FMenuItem* win_item{}; - const uInt32 n = uInt32(std::distance(first, iter)); - // get the dialog title - const auto& name = win->getText(); - - try - { - // create a new dialog list item - win_item = new FMenuItem (name, winmenu); - } - catch (const std::bad_alloc&) - { - badAllocOutput ("FMenuItem"); - return; - } - - if ( n < 9 ) - win_item->addAccelerator (fc::Fmkey_1 + n); // Meta + 1..9 - - win_item->addCallback - ( - "clicked", - static_cast::type>(win_item), - &FMenuItem::cb_switchToDialog, - win - ); - - win->addCallback - ( - "destroy", - static_cast::type>(win_item), - &FMenuItem::cb_destroyDialog, - win - ); - - win_item->associated_window = win; + // create a new dialog list item + win_item = new FMenuItem (name, winmenu); + } + catch (const std::bad_alloc&) + { + badAllocOutput ("FMenuItem"); + return; } + if ( n < 9 ) + win_item->addAccelerator (fc::Fmkey_1 + n); // Meta + 1..9 + + win_item->addCallback + ( + "clicked", + static_cast::type>(win_item), + &FMenuItem::cb_switchToDialog, + win + ); + + win->addCallback + ( + "destroy", + static_cast::type>(win_item), + &FMenuItem::cb_destroyDialog, + win + ); + + win_item->associated_window = win; ++iter; } } diff --git a/src/fstring.cpp b/src/fstring.cpp index 0caa249d..b5be91ad 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -1212,7 +1212,7 @@ inline void FString::_initLength (std::size_t len) try { - string = new wchar_t[bufsize](); + string = new wchar_t[bufsize]; std::wmemset (string, L'\0', bufsize); } catch (const std::bad_alloc&) @@ -1244,7 +1244,7 @@ void FString::_assign (const wchar_t s[]) try { - string = new wchar_t[bufsize](); + string = new wchar_t[bufsize]; } catch (const std::bad_alloc&) { @@ -1272,7 +1272,7 @@ void FString::_insert (std::size_t len, const wchar_t s[]) try { - string = new wchar_t[bufsize](); + string = new wchar_t[bufsize]; } catch (const std::bad_alloc&) { @@ -1319,7 +1319,7 @@ void FString::_insert ( std::size_t pos try { - sptr = new wchar_t[bufsize](); // generate new string + sptr = new wchar_t[bufsize]; // generate new string } catch (const std::bad_alloc&) { @@ -1363,7 +1363,7 @@ void FString::_remove (std::size_t pos, std::size_t len) try { - sptr = new wchar_t[bufsize](); // generate new string + sptr = new wchar_t[bufsize]; // generate new string } catch (const std::bad_alloc&) { @@ -1417,7 +1417,7 @@ inline const char* FString::_to_cstring (const wchar_t s[]) const try { - c_string = new char[size](); + c_string = new char[size]; // pre-initialiaze the whole string with '\0' std::memset (c_string, '\0', size); @@ -1467,7 +1467,7 @@ inline const wchar_t* FString::_to_wcstring (const char s[]) const try { - dest = new wchar_t[size](); + dest = new wchar_t[size]; // pre-initialiaze the whole string with '\0' std::wmemset (dest, L'\0', size); } diff --git a/src/fswitch.cpp b/src/fswitch.cpp index 58274d2d..b3d4618f 100644 --- a/src/fswitch.cpp +++ b/src/fswitch.cpp @@ -141,15 +141,15 @@ void FSwitch::drawCheckButton() //---------------------------------------------------------------------- inline void FSwitch::drawChecked() { - wchar_t on[6]{L" On "}; - const wchar_t off[6]{L" Off "}; + FString on{L" On "}; + const FString off{L" Off "}; const auto& wc = getColorTheme(); if ( hasFocus() && ! button_pressed ) { if ( FTerm::isMonochron() ) { - std::wcsncpy (on, L" ", 6); + on.setString(L" "); setBold(true); } else if ( FTerm::getMaxColor() < 16 ) @@ -191,8 +191,8 @@ inline void FSwitch::drawChecked() //---------------------------------------------------------------------- inline void FSwitch::drawUnchecked() { - const wchar_t on[6]{L" On "}; - wchar_t off[6]{L" Off "}; + const FString on{L" On "}; + FString off{L" Off "}; const auto& wc = getColorTheme(); setColor (wc->button_inactive_fg, wc->button_inactive_bg); @@ -206,7 +206,7 @@ inline void FSwitch::drawUnchecked() { if ( FTerm::isMonochron() ) { - std::wcsncpy (off, L"", 6); + off.setString(L""); setBold(true); } else if ( FTerm::getMaxColor() < 16 ) diff --git a/src/ftermlinux.cpp b/src/ftermlinux.cpp index 8023a248..046de52b 100644 --- a/src/ftermlinux.cpp +++ b/src/ftermlinux.cpp @@ -278,7 +278,7 @@ bool FTermLinux::loadVGAFont() struct unimapdesc unimap; unimap.entry_ct = uInt16 ( sizeof(fc::unicode_cp437_pairs) / sizeof(unipair) ); - unimap.entries = &fc::unicode_cp437_pairs[0]; + unimap.entries = const_cast(&fc::unicode_cp437_pairs[0]); setUnicodeMap(&unimap); } else @@ -327,7 +327,7 @@ bool FTermLinux::loadNewFont() struct unimapdesc unimap; unimap.entry_ct = uInt16 ( sizeof(fc::unicode_newfont_pairs) / sizeof(unipair) ); - unimap.entries = &fc::unicode_newfont_pairs[0]; + unimap.entries = const_cast(&fc::unicode_newfont_pairs[0]); setUnicodeMap(&unimap); } else @@ -660,7 +660,7 @@ FTermLinux::modifier_key& FTermLinux::getModifierKey() } //---------------------------------------------------------------------- -int FTermLinux::setScreenFont ( uChar fontdata[], uInt count +int FTermLinux::setScreenFont ( const uChar fontdata[], uInt count , uInt fontwidth, uInt fontheight , bool direct) { @@ -681,7 +681,7 @@ int FTermLinux::setScreenFont ( uChar fontdata[], uInt count font.charcount = count; if ( direct ) - font.data = fontdata; + font.data = const_cast(fontdata); else { const std::size_t bytes_per_line = font.width / 8; diff --git a/src/fvterm.cpp b/src/fvterm.cpp index 0b94b853..a9611754 100644 --- a/src/fvterm.cpp +++ b/src/fvterm.cpp @@ -1471,20 +1471,17 @@ FVTerm::covered_state FVTerm::isCovered ( const FPoint& pos if ( FWidget::getWindowList() && ! FWidget::getWindowList()->empty() ) { - bool found( area == vdesktop ); + bool found{ area == vdesktop }; for (auto& win_obj : *FWidget::getWindowList()) { const auto& win = win_obj->getVWin(); - if ( ! win ) + if ( ! (win && win->visible) ) continue; - if ( ! win->visible ) - continue; - - const int win_x = win->offset_left; - const int win_y = win->offset_top; + const int& win_x = win->offset_left; + const int& win_y = win->offset_top; const FRect geometry { win_x , win_y , std::size_t(win->width) + std::size_t(win->right_shadow) , std::size_t(win->height) + std::size_t(win->bottom_shadow) }; @@ -1492,9 +1489,9 @@ FVTerm::covered_state FVTerm::isCovered ( const FPoint& pos if ( found && geometry.contains(pos) ) { const int width = win->width + win->right_shadow; - const int x = pos.getX(); - const int y = pos.getY(); - auto tmp = &win->data[(y - win_y) * width + (x - win_x)]; + const int& x = pos.getX(); + const int& y = pos.getY(); + const auto& tmp = &win->data[(y - win_y) * width + (x - win_x)]; if ( tmp->attr.bit.color_overlay ) { diff --git a/src/fwidget.cpp b/src/fwidget.cpp index d3afc904..e5442eb9 100644 --- a/src/fwidget.cpp +++ b/src/fwidget.cpp @@ -109,6 +109,10 @@ FWidget::~FWidget() // destructor if ( this == getClickedWidget() ) setClickedWidget(nullptr); + // unset keyboard widget + if ( this == FApplication::getKeyboardWidget() ) + FApplication::setKeyboardWidget(nullptr); + // unset the local window widget focus if ( flags.focus ) { diff --git a/src/include/final/fapplication.h b/src/include/final/fapplication.h index 40a49f8b..d67f2e71 100644 --- a/src/include/final/fapplication.h +++ b/src/include/final/fapplication.h @@ -114,6 +114,7 @@ class FApplication : public FWidget int getArgc() const; char** getArgv() const; static FApplication* getApplicationObject(); + static FWidget* getKeyboardWidget(); static FLogPtr& getLog(); // Mutator @@ -136,6 +137,7 @@ class FApplication : public FWidget void initTerminal() override; static void setDefaultTheme(); static void setDarkTheme(); + static void setKeyboardWidget (FWidget*); static void closeConfirmationDialog (FWidget*, FCloseEvent*); // Callback method diff --git a/src/include/final/fcallback.h b/src/include/final/fcallback.h index 765c8b38..12829d09 100644 --- a/src/include/final/fcallback.h +++ b/src/include/final/fcallback.h @@ -58,7 +58,7 @@ struct FCallbackData FCallbackData() { } - template + template FCallbackData (const FString& s, FWidget* i, FuncPtr m, const FCall& c) : cb_signal(s) , cb_instance(i) @@ -88,7 +88,7 @@ class FCallback { public: // Using-declaration - template + template using ObjectPointer = typename std::enable_if< ! std::is_member_function_pointer::value && ! std::is_function::type>::value @@ -97,7 +97,7 @@ class FCallback && std::is_object::value && ! std::is_class::value , std::nullptr_t >; - template + template using ClassObject = typename std::enable_if< ! std::is_member_function_pointer::value && ! std::is_function::type>::value @@ -106,7 +106,7 @@ class FCallback && std::is_object::value && std::is_class::value , std::nullptr_t >; - template + template using MemberFunctionPointer = typename std::enable_if< std::is_member_function_pointer::value && ! std::is_function::type>::value @@ -115,7 +115,7 @@ class FCallback && std::is_object::value && ! std::is_class::value , std::nullptr_t >; - template + template using FunctionPointer = typename std::enable_if< ! std::is_member_function_pointer::value && std::is_function::type>::value @@ -124,7 +124,7 @@ class FCallback && std::is_object::value && ! std::is_class::value , std::nullptr_t >; - template + template using FunctionReference = typename std::enable_if< ! std::is_member_function_pointer::value && std::is_function::type>::value @@ -151,61 +151,61 @@ class FCallback std::size_t getCallbackCount() const; // Methods - template::type = nullptr - , typename MemberFunctionPointer::type = nullptr - , typename... Args> + template ::type = nullptr + , typename MemberFunctionPointer::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_member , Args&&... args) noexcept; - template::type = nullptr - , typename ClassObject::type = nullptr - , typename... Args> + template ::type = nullptr + , typename ClassObject::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_function , Args&&... args) noexcept; - template::type = nullptr - , typename... Args> + template < typename Function + , typename ClassObject::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) noexcept; - template::type = nullptr - , typename... Args> + template ::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) noexcept; - template::type = nullptr - , typename... Args> + template ::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) noexcept; - template::type = nullptr - , typename... Args> + template ::type = nullptr + , typename... Args> void addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) noexcept; - template::type = nullptr> + template ::type = nullptr> void delCallback (Object&& cb_instance) noexcept; void delCallback (const FString& cb_signal); - template::type = nullptr> + template ::type = nullptr> void delCallback ( const FString& cb_signal , Object&& cb_instance ) noexcept; - template::type = nullptr> + template ::type = nullptr> void delCallback (FunctionPtr&& cb_func_ptr) noexcept; - template::type = nullptr> + template ::type = nullptr> void delCallback (const Function& cb_function); void delCallback(); void emitCallback (const FString& emit_signal) const; @@ -228,11 +228,11 @@ inline std::size_t FCallback::getCallbackCount() const { return callback_objects.size(); } //---------------------------------------------------------------------- -template::type - , typename FCallback::MemberFunctionPointer::type - , typename... Args> +template ::type + , typename FCallback::MemberFunctionPointer::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_member @@ -249,11 +249,11 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type - , typename FCallback::ClassObject::type - , typename... Args> +template ::type + , typename FCallback::ClassObject::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Object&& cb_instance , Function&& cb_function @@ -267,9 +267,9 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type - , typename... Args> +template ::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) noexcept @@ -283,9 +283,9 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type - , typename... Args> +template ::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) noexcept @@ -298,9 +298,9 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type - , typename... Args> +template ::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Function& cb_function , Args&&... args) noexcept @@ -314,9 +314,9 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type - , typename... Args> +template ::type + , typename... Args> inline void FCallback::addCallback ( const FString& cb_signal , Function&& cb_function , Args&&... args) noexcept @@ -331,8 +331,8 @@ inline void FCallback::addCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type> +template ::type> inline void FCallback::delCallback (Object&& cb_instance) noexcept { // Deletes entries with the given instance from the callback list @@ -352,8 +352,8 @@ inline void FCallback::delCallback (Object&& cb_instance) noexcept } //---------------------------------------------------------------------- -template::type> +template ::type> inline void FCallback::delCallback ( const FString& cb_signal , Object&& cb_instance ) noexcept { @@ -376,8 +376,8 @@ inline void FCallback::delCallback ( const FString& cb_signal } //---------------------------------------------------------------------- -template::type> +template ::type> inline void FCallback::delCallback (FunctionPtr&& cb_func_ptr) noexcept { // Deletes entries with the given function pointer @@ -399,8 +399,8 @@ inline void FCallback::delCallback (FunctionPtr&& cb_func_ptr) noexcept } //---------------------------------------------------------------------- -template::type> +template ::type> inline void FCallback::delCallback (const Function& cb_function) { // Deletes entries with the given function reference diff --git a/src/include/final/fcharmap.h b/src/include/final/fcharmap.h index 009c4a76..02caf1c2 100644 --- a/src/include/final/fcharmap.h +++ b/src/include/final/fcharmap.h @@ -3,7 +3,7 @@ * * * This file is part of the FINAL CUT widget toolkit * * * -* Copyright 2015-2019 Markus Gans * +* Copyright 2015-2020 Markus Gans * * * * FINAL CUT is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -39,10 +39,10 @@ namespace fc extern uInt character[][fc::NUM_OF_ENCODINGS]; extern const std::size_t lastCharItem; -extern int vt100_key_to_utf8[][2]; +extern const int vt100_key_to_utf8[][2]; extern const std::size_t lastKeyItem; -extern wchar_t cp437_ucs[][2]; +extern const wchar_t cp437_ucs[][2]; extern const std::size_t lastCP437Item; extern const wchar_t halfWidth_fullWidth[][2]; diff --git a/src/include/final/fcombobox.h b/src/include/final/fcombobox.h index 8078a85b..18411d0f 100644 --- a/src/include/final/fcombobox.h +++ b/src/include/final/fcombobox.h @@ -150,7 +150,8 @@ class FComboBox : public FWidget const FString getClassName() const override; std::size_t getCount() const; FString getText() const; - FDataPtr getItemData(); + template + clean_fdata_t
& getItemData(); FLineEdit::label_o getLabelOrientation() const; // Mutators @@ -182,11 +183,13 @@ class FComboBox : public FWidget // Methods void insert (const FListBoxItem&); - template + template void insert ( const std::initializer_list& list - , FDataPtr = nullptr ); - template - void insert (const ItemT&, FDataPtr = nullptr); + , DT&& = DT() ); + template + void insert (const ItemT&, DT&& = DT()); void remove (std::size_t); void reserve (std::size_t); void clear(); @@ -247,10 +250,11 @@ inline FString FComboBox::getText() const { return input_field.getText(); } //---------------------------------------------------------------------- -inline FDataPtr FComboBox::getItemData() +template +inline clean_fdata_t
& FComboBox::getItemData() { const std::size_t index = list_window.list.currentItem(); - return list_window.list.getItem(index).getData(); + return list_window.list.getItem(index).getData
(); } //---------------------------------------------------------------------- @@ -298,21 +302,23 @@ inline bool FComboBox::hasShadow() const { return getFlags().shadow; } //---------------------------------------------------------------------- -template -void FComboBox::insert (const std::initializer_list& list, FDataPtr d) +template +void FComboBox::insert (const std::initializer_list& list, DT&& d) { for (auto& item : list) { - FListBoxItem listItem (FString() << item, d); + FListBoxItem listItem (FString() << item, std::forward
(d)); insert (listItem); } } //---------------------------------------------------------------------- -template -void FComboBox::insert (const ItemT& item, FDataPtr d) +template +void FComboBox::insert (const ItemT& item, DT&& d) { - FListBoxItem listItem (FString() << item, d); + FListBoxItem listItem (FString() << item, std::forward
(d)); insert (listItem); } diff --git a/src/include/final/fdata.h b/src/include/final/fdata.h index 02e974eb..9272b07a 100644 --- a/src/include/final/fdata.h +++ b/src/include/final/fdata.h @@ -20,6 +20,19 @@ * . * ***********************************************************************/ +/* Inheritance diagram + * ═══════════════════ + * + * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▏ + * ▕ FDataAccess ▏ + * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▏ + * ▲ + * │ + * ▕▔▔▔▔▔▔▔▏ + * ▕ FData ▏ + * ▕▁▁▁▁▁▁▁▏ + */ + #ifndef FDATA_H #define FDATA_H @@ -29,11 +42,55 @@ #include -template -struct FData +namespace finalcut { - explicit FData (T v) // constructor + + +//---------------------------------------------------------------------- +// struct FDataAccess +//---------------------------------------------------------------------- + +template +struct FData; // Class forward declaration + +struct FDataAccess +{ + public: + virtual ~FDataAccess(); + + template + const T& get() const + { + return static_cast&>(*this).get(); + } + + template + void set (const V& v) + { + static_cast&>(*this).set(v); + } +}; + +//---------------------------------------------------------------------- +inline FDataAccess::~FDataAccess() +{ } + + +//---------------------------------------------------------------------- +// struct FData +//---------------------------------------------------------------------- + +template +struct FData : public FDataAccess +{ + explicit FData (T& v) // constructor + : value_ref{v} + { } + + explicit FData (T&& v) // constructor : value{v} + , value_ref{value} { } ~FData() // destructor @@ -41,64 +98,131 @@ struct FData FData (const FData& d) // Copy constructor : value{d.value} + , value_ref{d.value_ref} { } FData& operator = (const FData& d) // Copy assignment operator (=) { value = d.value; - return *this; - } - - FData (FData&& d) noexcept // Move constructor - : value{std::move(d.value)} - { } - - FData& operator = (FData&& d) noexcept // Move assignment operator (=) - { - value = std::move(d.value); + value_ref = d.value_ref; return *this; } T operator () () const { - return value; + return value_ref; } - template + template T operator () (Args... args) const { - return value(args...); + return value_ref(args...); } explicit operator T () const { - return value; + return value_ref; } T& get() { - return value; + return value_ref; } void set (const T& v) { - value = v; + value_ref = v; + } + + bool isInitializedCopy() + { + return bool(value); + } + + bool isInitializedReference() + { + return ! isInitializedCopy(); } FData& operator << (const T& v) { - value = v; + value_ref = v; return *this; } friend std::ostream& operator << (std::ostream &os, const FData& data) { - os << data.value; + os << data.value_ref; return os; } - T value; + private: + T value{}; + + public: + T& value_ref; }; -#endif // FDATA_H +// non-member functions +//---------------------------------------------------------------------- +namespace internal +{ + +template ::value + , bool isFunction = std::is_function::value> +struct cleanCondition; + +//---------------------------------------------------------------------- +template +struct cleanCondition +{ + // Leave the type untouched + typedef T type; +}; + +//---------------------------------------------------------------------- +template +struct cleanCondition +{ + // Array to pointer + typedef typename std::remove_extent::type* type; +}; + +//---------------------------------------------------------------------- +template +struct cleanCondition +{ + // Add pointer to function + typedef typename std::add_pointer::type type; +}; + +} // namespace internal + +//---------------------------------------------------------------------- +template +class cleanFData +{ + private: + typedef typename std::remove_reference::type remove_ref; + + public: + // Similar to std::decay, but keeps const and volatile + typedef typename internal::cleanCondition::type type; +}; + +//---------------------------------------------------------------------- +template +using clean_fdata_t = typename cleanFData::type; + +//---------------------------------------------------------------------- +template +constexpr FData>* makeFData (T&& data) +{ + return new FData>(std::forward(data)); +} + +} // namespace finalcut + +#endif // FDATA_H diff --git a/src/include/final/fevent.h b/src/include/final/fevent.h index b4c8873b..e22d098f 100644 --- a/src/include/final/fevent.h +++ b/src/include/final/fevent.h @@ -342,15 +342,51 @@ class FUserEvent : public FEvent // user event // Disable copy assignment operator (=) FUserEvent& operator = (const FUserEvent&) = delete; - int getUserId() const; - FDataPtr getData() const; - void setData (FDataPtr); + int getUserId() const; + template + FData&& getFDataObject() const; + template + clean_fdata_t& getData() const; + template + void setFDataObject (T&&); + template + void setData (T&&); private: - int uid{0}; - FDataPtr data_pointer{nullptr}; + int uid{0}; + FDataAccess* data_pointer{nullptr}; + bool external_data_pointer{false}; }; +//---------------------------------------------------------------------- +template +inline FData&& FUserEvent::getFDataObject() const +{ + return static_cast&&>(*data_pointer); +} + +//---------------------------------------------------------------------- +template +inline clean_fdata_t& FUserEvent::getData() const +{ + return static_cast>&>(*data_pointer).get(); +} + +//---------------------------------------------------------------------- +template +inline void FUserEvent::setFDataObject (T&& fdata) +{ + external_data_pointer = true; + data_pointer = &(std::forward(fdata)); +} +//---------------------------------------------------------------------- +template +inline void FUserEvent::setData (T&& data) +{ + external_data_pointer = false; + data_pointer = makeFData(std::forward(data)); +} + } // namespace finalcut #endif // FEVENT_H diff --git a/src/include/final/fkey_map.h b/src/include/final/fkey_map.h index 5a623e42..12dfaa57 100644 --- a/src/include/final/fkey_map.h +++ b/src/include/final/fkey_map.h @@ -3,7 +3,7 @@ * * * This file is part of the FINAL CUT widget toolkit * * * -* Copyright 2015-2019 Markus Gans * +* Copyright 2015-2020 Markus Gans * * * * FINAL CUT is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -34,8 +34,8 @@ namespace fc { extern FKeyMap fkey[]; -extern FMetakeyMap fmetakey[]; -extern FKeyName fkeyname[]; +extern const FMetakeyMap fmetakey[]; +extern const FKeyName fkeyname[]; } // namespace fc diff --git a/src/include/final/flistbox.h b/src/include/final/flistbox.h index e5100a8c..58c2aaea 100644 --- a/src/include/final/flistbox.h +++ b/src/include/final/flistbox.h @@ -72,7 +72,8 @@ class FListBoxItem // Constructors FListBoxItem (); FListBoxItem (const FListBoxItem&); // copy constructor - explicit FListBoxItem (const FString&, FDataPtr = nullptr); + template + explicit FListBoxItem (const FString&, DT&& = DT() ); // Destructor virtual ~FListBoxItem(); @@ -83,11 +84,13 @@ class FListBoxItem // Accessors virtual const FString getClassName() const; virtual FString getText() const; - virtual FDataPtr getData() const; + template + clean_fdata_t
& getData() const; // Mutators void setText (const FString&); - void setData (FDataPtr); + template + void setData (DT&&); // Methods void clear(); @@ -95,7 +98,7 @@ class FListBoxItem private: // Data members FString text{}; - FDataPtr data_pointer{nullptr}; + FDataAccess* data_pointer{nullptr}; fc::brackets_type brackets{fc::NoBrackets}; bool selected{false}; @@ -105,6 +108,13 @@ class FListBoxItem // FListBoxItem inline functions +//---------------------------------------------------------------------- +template +inline FListBoxItem::FListBoxItem (const FString& txt, DT&& data) + : text{txt} + , data_pointer{makeFData(std::forward
(data))} +{ } + //---------------------------------------------------------------------- inline const FString FListBoxItem::getClassName() const { return "FListBoxItem"; } @@ -114,16 +124,22 @@ inline FString FListBoxItem::getText() const { return text; } //---------------------------------------------------------------------- -inline FDataPtr FListBoxItem::getData() const -{ return data_pointer; } +template +inline clean_fdata_t
& FListBoxItem::getData() const +{ + return static_cast>&>(*data_pointer).get(); +} //---------------------------------------------------------------------- inline void FListBoxItem::setText (const FString& txt) { text.setString(txt); } //---------------------------------------------------------------------- -inline void FListBoxItem::setData (FDataPtr data) -{ data_pointer = data; } +template +inline void FListBoxItem::setData (DT&& data) +{ + data_pointer = makeFData(std::forward
(data)); +} //---------------------------------------------------------------------- inline void FListBoxItem::clear() @@ -145,9 +161,11 @@ class FListBox : public FWidget // Constructor explicit FListBox (FWidget* = nullptr); - template + template FListBox (Iterator, Iterator, InsertConverter, FWidget* = nullptr); - template + template FListBox (Container, LazyConverter, FWidget* = nullptr); // Disable copy constructor @@ -197,21 +215,30 @@ class FListBox : public FWidget // Methods void hide() override; - template - void insert (Iterator, Iterator, InsertConverter); - template - void insert (Container, LazyConverter); + template + void insert ( Iterator, Iterator + , const InsertConverter& ); + template + void insert ( const Container& + , const LazyConverter& ); + template + void insert (Container*, const LazyConverter&); void insert (const FListBoxItem&); - template + template void insert ( const std::initializer_list& list , fc::brackets_type = fc::NoBrackets , bool = false - , FDataPtr = nullptr ); - template + , DT&& = DT() ); + template void insert ( const ItemT& , fc::brackets_type = fc::NoBrackets , bool = false - , FDataPtr = nullptr ); + , DT&& = DT() ); void remove (std::size_t); void reserve (std::size_t); void clear(); @@ -236,7 +263,7 @@ class FListBox : public FWidget // Typedefs typedef std::unordered_map> keyMap; typedef std::unordered_map> keyMapResult; - typedef std::function lazyInsert; + typedef std::function lazyInsert; // Enumeration enum convert_type @@ -305,7 +332,7 @@ class FListBox : public FWidget void processSelect() const; void processChanged() const; void changeOnResize() const; - void lazyConvert (listBoxItems::iterator, int); + void lazyConvert (listBoxItems::iterator, std::size_t); listBoxItems::iterator index2iterator (std::size_t); listBoxItems::const_iterator index2iterator (std::size_t index) const; // Callback methods @@ -317,7 +344,7 @@ class FListBox : public FWidget // Data members listBoxItems itemlist{}; - FDataPtr source_container{nullptr}; + FDataAccess* source_container{nullptr}; FScrollbarPtr vbar{nullptr}; FScrollbarPtr hbar{nullptr}; FString text{}; @@ -342,10 +369,23 @@ class FListBox : public FWidget bool click_on_list{false}; }; +// non-member function +//---------------------------------------------------------------------- +namespace FListBoxHelper +{ + +template +constexpr clean_fdata_t& getContainer(FDataAccess* container) +{ + return static_cast>&>(*container).get(); +} + +} // namespace FListBoxHelper // FListBox inline functions //---------------------------------------------------------------------- -template +template inline FListBox::FListBox ( Iterator first , Iterator last , InsertConverter convert @@ -362,7 +402,8 @@ inline FListBox::FListBox ( Iterator first } //---------------------------------------------------------------------- -template +template inline FListBox::FListBox ( Container container , LazyConverter convert , FWidget* parent ) @@ -475,10 +516,11 @@ inline void FListBox::reserve (std::size_t new_cap) { itemlist.reserve(new_cap); } //---------------------------------------------------------------------- -template +template inline void FListBox::insert ( Iterator first , Iterator last - , InsertConverter convert ) + , const InsertConverter& convert ) { conv_type = direct_convert; @@ -490,13 +532,14 @@ inline void FListBox::insert ( Iterator first } //---------------------------------------------------------------------- -template -void FListBox::insert (Container container, LazyConverter convert) +template +void FListBox::insert (const Container& container, const LazyConverter& converter) { conv_type = lazy_convert; - source_container = container; - lazy_inserter = convert; - const std::size_t size = container->size(); + source_container = makeFData(container); + lazy_inserter = converter; + const std::size_t size = container.size(); if ( size > 0 ) itemlist.resize(size); @@ -505,15 +548,24 @@ void FListBox::insert (Container container, LazyConverter convert) } //---------------------------------------------------------------------- -template +template +void FListBox::insert (Container* container, const LazyConverter& converter) +{ + insert (*container, converter); +} + +//---------------------------------------------------------------------- +template void FListBox::insert ( const std::initializer_list& list , fc::brackets_type b , bool s - , FDataPtr d ) + , DT&& d ) { for (auto& item : list) { - FListBoxItem listItem (FString() << item, d); + FListBoxItem listItem (FString() << item, std::forward
(d)); listItem.brackets = b; listItem.selected = s; insert (listItem); @@ -521,13 +573,14 @@ void FListBox::insert ( const std::initializer_list& list } //---------------------------------------------------------------------- -template +template void FListBox::insert ( const ItemT& item , fc::brackets_type b , bool s - , FDataPtr d ) + , DT&& d ) { - FListBoxItem listItem (FString() << item, d); + FListBoxItem listItem (FString() << item, std::forward
(d)); listItem.brackets = b; listItem.selected = s; insert (listItem); diff --git a/src/include/final/flistview.h b/src/include/final/flistview.h index 6bb84a90..dad678a6 100644 --- a/src/include/final/flistview.h +++ b/src/include/final/flistview.h @@ -54,6 +54,7 @@ #include "final/fscrollbar.h" #include "final/ftermbuffer.h" +#include "final/ftypes.h" #include "final/fwidget.h" namespace finalcut @@ -74,9 +75,8 @@ class FListViewItem : public FObject // Constructor FListViewItem (const FListViewItem&); // copy constructor explicit FListViewItem (iterator); - FListViewItem ( const FStringList& - , FDataPtr - , iterator ); + template + FListViewItem (const FStringList&, DT&&, iterator); // Destructor ~FListViewItem() override; @@ -89,12 +89,14 @@ class FListViewItem : public FObject uInt getColumnCount() const; int getSortColumn() const; FString getText (int) const; - FDataPtr getData() const; + template + clean_fdata_t
& getData() const; uInt getDepth() const; // Mutators void setText (int, const FString&); - void setData (FDataPtr); + template + void setData (DT&&); void setCheckable (bool); void setChecked (bool); @@ -124,7 +126,7 @@ class FListViewItem : public FObject // Data members FStringList column_list{}; - FDataPtr data_pointer{nullptr}; + FDataAccess* data_pointer{nullptr}; iterator root{}; std::size_t visible_lines{1}; bool expandable{false}; @@ -139,6 +141,22 @@ class FListViewItem : public FObject // FListViewItem inline functions +//---------------------------------------------------------------------- +template +inline FListViewItem::FListViewItem ( const FStringList& cols + , DT&& data + , iterator parent_iter ) + : FObject{nullptr} + , column_list{cols} + , data_pointer{makeFData(std::forward
(data))} +{ + if ( cols.empty() ) + return; + + replaceControlCodes(); + insert (this, parent_iter); +} + //---------------------------------------------------------------------- inline const FString FListViewItem::getClassName() const { return "FListViewItem"; } @@ -148,12 +166,18 @@ inline uInt FListViewItem::getColumnCount() const { return uInt(column_list.size()); } //---------------------------------------------------------------------- -inline FDataPtr FListViewItem::getData() const -{ return data_pointer; } +template +inline clean_fdata_t
& FListViewItem::getData() const +{ + return static_cast>&>(*data_pointer).get(); +} //---------------------------------------------------------------------- -inline void FListViewItem::setData (FDataPtr data) -{ data_pointer = data; } +template +inline void FListViewItem::setData (DT&& data) +{ + data_pointer = makeFData(std::forward
(data)); +} //---------------------------------------------------------------------- inline void FListViewItem::setChecked (bool checked) @@ -312,32 +336,38 @@ class FListView : public FWidget void hide() override; iterator insert (FListViewItem*); iterator insert (FListViewItem*, iterator); + template iterator insert ( const FStringList& - , FDataPtr = nullptr ); + , DT&& = DT() ); iterator insert ( const FStringList& , iterator ); + template iterator insert ( const FStringList& - , FDataPtr + , DT&& , iterator ); - template + template iterator insert ( const std::initializer_list& - , FDataPtr = nullptr ); + , DT&& = DT() ); template iterator insert ( const std::initializer_list& , iterator ); - template + template iterator insert ( const std::initializer_list& - , FDataPtr + , DT&& , iterator ); - template + template iterator insert ( const std::vector& - , FDataPtr = nullptr ); + , DT&& = DT() ); template iterator insert ( const std::vector& , iterator ); - template + template iterator insert ( const std::vector& - , FDataPtr + , DT&& , iterator ); void remove (FListViewItem*); void clear(); @@ -560,9 +590,10 @@ inline FObject::iterator FListView::insert (FListViewItem* item) { return insert (item, root); } //---------------------------------------------------------------------- +template inline FObject::iterator - FListView::insert (const FStringList& cols, FDataPtr d) -{ return insert (cols, d, root); } + FListView::insert (const FStringList& cols, DT&& d) +{ return insert (cols, std::forward
(d), root); } //---------------------------------------------------------------------- inline FObject::iterator @@ -571,23 +602,53 @@ inline FObject::iterator { return insert (cols, nullptr, parent_iter); } //---------------------------------------------------------------------- -template -inline FObject::iterator - FListView::insert (const std::initializer_list& list, FDataPtr d) -{ return insert (list, d, root); } +template +inline FObject::iterator FListView::insert ( const FStringList& cols + , DT&& d + , iterator parent_iter ) +{ + FListViewItem* item; + + if ( cols.empty() || parent_iter == getNullIterator() ) + return getNullIterator(); + + if ( ! *parent_iter ) + parent_iter = root; + + try + { + item = new FListViewItem (cols, std::forward
(d), getNullIterator()); + } + catch (const std::bad_alloc&) + { + badAllocOutput ("FListViewItem"); + return getNullIterator(); + } + + item->replaceControlCodes(); + return insert(item, parent_iter); +} //---------------------------------------------------------------------- -template +template +inline FObject::iterator + FListView::insert (const std::initializer_list& list, DT&& d) +{ return insert (list, std::forward
(d), root); } + +//---------------------------------------------------------------------- +template inline FObject::iterator FListView::insert ( const std::initializer_list& list , iterator parent_iter ) { return insert (list, 0, parent_iter); } //---------------------------------------------------------------------- -template +template FObject::iterator FListView::insert ( const std::initializer_list& list - , FDataPtr d + , DT&& d , iterator parent_iter ) { FStringList str_cols; @@ -602,15 +663,16 @@ FObject::iterator } ); - auto item_iter = insert (str_cols, d, parent_iter); + auto item_iter = insert (str_cols, std::forward
(d), parent_iter); return item_iter; } //---------------------------------------------------------------------- -template +template inline FObject::iterator - FListView::insert (const std::vector& cols, FDataPtr d) -{ return insert (cols, d, root); } + FListView::insert (const std::vector& cols, DT&& d) +{ return insert (cols, std::forward
(d), root); } //---------------------------------------------------------------------- template @@ -620,10 +682,11 @@ inline FObject::iterator { return insert (cols, 0, parent_iter); } //---------------------------------------------------------------------- -template +template FObject::iterator FListView::insert ( const std::vector& cols - , FDataPtr d + , DT&& d , iterator parent_iter ) { FStringList str_cols; @@ -638,7 +701,7 @@ FObject::iterator } ); - auto item_iter = insert (str_cols, d, parent_iter); + auto item_iter = insert (str_cols, std::forward
(d), parent_iter); return item_iter; } diff --git a/src/include/final/fmouse.h b/src/include/final/fmouse.h index e182c9b7..7d912232 100644 --- a/src/include/final/fmouse.h +++ b/src/include/final/fmouse.h @@ -130,7 +130,7 @@ class FMouse bool isInputDataPending() const; // Methods - template + template static FMouse* createMouseObject (); void clearButtonState(); virtual void setRawData (FKeyboard::keybuffer&) = 0; @@ -193,7 +193,7 @@ class FMouse }; //---------------------------------------------------------------------- -template +template inline FMouse* FMouse::createMouseObject() { return new ClassT; diff --git a/src/include/final/fscrollbar.h b/src/include/final/fscrollbar.h index 5433ff4d..856cc7c5 100644 --- a/src/include/final/fscrollbar.h +++ b/src/include/final/fscrollbar.h @@ -170,9 +170,10 @@ class FScrollbar : public FWidget }; -// non-member function forward declarations +// non-member function //---------------------------------------------------------------------- -template +template void initScrollbar ( FScrollbarPtr& bar , fc::orientation o , Instance cb_instance diff --git a/src/include/final/fscrollview.h b/src/include/final/fscrollview.h index ad90716f..fa3195b8 100644 --- a/src/include/final/fscrollview.h +++ b/src/include/final/fscrollview.h @@ -163,7 +163,7 @@ class FScrollView : public FWidget void init(); void mapKeyFunctions(); void calculateScrollbarPos() const; - template + template void initScrollbar ( FScrollbarPtr& , fc::orientation , Callback ); @@ -268,7 +268,7 @@ inline void FScrollView::print (const FPoint& pos) } //---------------------------------------------------------------------- -template +template inline void FScrollView::initScrollbar ( FScrollbarPtr& bar , fc::orientation o , Callback cb_handler ) diff --git a/src/include/final/fstring.h b/src/include/final/fstring.h index 5a89b97b..78379176 100644 --- a/src/include/final/fstring.h +++ b/src/include/final/fstring.h @@ -177,7 +177,7 @@ class FString wchar_t front() const; wchar_t back() const; - template + template FString& sprintf (const FString&, Args&&...); FString clear(); @@ -416,7 +416,7 @@ inline wchar_t FString::back() const } //---------------------------------------------------------------------- -template +template inline FString& FString::sprintf (const FString& format, Args&&... args) { static constexpr int BUFSIZE = 4096; diff --git a/src/include/final/fterm.h b/src/include/final/fterm.h index 3a04d361..d11fa5bd 100644 --- a/src/include/final/fterm.h +++ b/src/include/final/fterm.h @@ -278,7 +278,7 @@ class FTerm final static void saveColorMap(); static void resetColorMap(); static void setPalette (FColor, int, int, int); - template + template static void setColorPaletteTheme (const FSetPalette& = &FTerm::setPalette); static void setBeep (int, int); static void resetBeep(); @@ -295,7 +295,7 @@ class FTerm final static bool scrollTermReverse(); static defaultPutChar& putchar(); // function pointer - template + template static void putstringf (const char[], Args&&...); static void putstring (const char[], int = 1); static int putchar_ASCII (int); @@ -443,7 +443,7 @@ inline bool FTerm::unsetUTF8() { return setUTF8(false); } //---------------------------------------------------------------------- -template +template inline void FTerm::setColorPaletteTheme (const FSetPalette& f) { getColorPaletteTheme() = std::make_shared(f); @@ -451,7 +451,7 @@ inline void FTerm::setColorPaletteTheme (const FSetPalette& f) } //---------------------------------------------------------------------- -template +template inline void FTerm::putstringf (const char format[], Args&&... args) { const int size = std::snprintf (nullptr, 0, format, args...) + 1; diff --git a/src/include/final/ftermbuffer.h b/src/include/final/ftermbuffer.h index 5d53d733..f3976b9e 100644 --- a/src/include/final/ftermbuffer.h +++ b/src/include/final/ftermbuffer.h @@ -63,7 +63,7 @@ class FTermBuffer // Constructor FTermBuffer() = default; - template + template FTermBuffer (Iterator, Iterator); // Destructor @@ -95,7 +95,7 @@ class FTermBuffer FChar back() const; const FString toString() const; void clear(); - template + template int writef (const FString&, Args&&...); int write (const FString&); int write (wchar_t); @@ -114,7 +114,7 @@ class FTermBuffer // FTermBuffer inline functions //---------------------------------------------------------------------- -template +template inline FTermBuffer::FTermBuffer(Iterator first, Iterator last) { data.assign(first, last); @@ -216,7 +216,7 @@ inline void FTermBuffer::clear() } //---------------------------------------------------------------------- -template +template inline int FTermBuffer::writef (const FString& format, Args&&... args) { FString str{}; diff --git a/src/include/final/ftermcap.h b/src/include/final/ftermcap.h index e5b1e9b7..852ad862 100644 --- a/src/include/final/ftermcap.h +++ b/src/include/final/ftermcap.h @@ -96,17 +96,18 @@ class FTermcap final // Accessors const FString getClassName() const; - template + template static bool getFlag (const CharT&); - template + template static int getNumber (const CharT&); - template + template static char* getString (const CharT&); - template + template static char* encodeMotionParameter (const CharT&, int, int); - template + template static char* encodeParameter (const CharT&, Args&&...); - template + template static int paddingPrint (const CharT&, int, fn_putc); // Inquiry @@ -159,42 +160,43 @@ inline const FString FTermcap::getClassName() const { return "FTermcap"; } //---------------------------------------------------------------------- -template +template bool FTermcap::getFlag (const CharT& cap) { return ::tgetflag(C_STR(cap)); } //---------------------------------------------------------------------- -template +template int FTermcap::getNumber (const CharT& cap) { return ::tgetnum(C_STR(cap)); } //---------------------------------------------------------------------- -template +template char* FTermcap::getString (const CharT& cap) { return ::tgetstr(C_STR(cap), reinterpret_cast(&string_buf)); } //---------------------------------------------------------------------- -template +template char* FTermcap::encodeMotionParameter (const CharT& cap, int col, int row) { return ::tgoto(C_STR(cap), col, row); } //---------------------------------------------------------------------- -template +template inline char* FTermcap::encodeParameter (const CharT& cap, Args&&... args) { return ::tparm (C_STR(cap), std::forward(args)...); } //---------------------------------------------------------------------- -template +template int FTermcap::paddingPrint (const CharT& str, int affcnt, fn_putc putc) { return _tputs (C_STR(str), affcnt, putc); diff --git a/src/include/final/ftermlinux.h b/src/include/final/ftermlinux.h index 52e043ee..4cfc6219 100644 --- a/src/include/final/ftermlinux.h +++ b/src/include/final/ftermlinux.h @@ -152,7 +152,7 @@ class FTermLinux final modifier_key& getModifierKey(); // Mutators - int setScreenFont ( uChar[], uInt, uInt, uInt + int setScreenFont ( const uChar[], uInt, uInt, uInt , bool = false ); int setUnicodeMap (struct unimapdesc*); void setLinuxCursorStyle (fc::linuxConsoleCursorStyle) const; diff --git a/src/include/final/ftextview.h b/src/include/final/ftextview.h index 7d687cbb..090280a7 100644 --- a/src/include/final/ftextview.h +++ b/src/include/final/ftextview.h @@ -112,10 +112,10 @@ class FTextView : public FWidget // Methods void hide() override; - template + template void append (const std::initializer_list&); void append (const FString&); - template + template void insert (const std::initializer_list&, int); void insert (const FString&, int); void replaceRange (const FString&, int, int); @@ -232,7 +232,7 @@ inline void FTextView::scrollTo (const FPoint& pos) { scrollTo(pos.getX(), pos.getY()); } //---------------------------------------------------------------------- -template +template void FTextView::append (const std::initializer_list& list) { for (auto& str : list) @@ -240,7 +240,7 @@ void FTextView::append (const std::initializer_list& list) } //---------------------------------------------------------------------- -template +template void FTextView::insert (const std::initializer_list& list, int pos) { for (auto& str : list) diff --git a/src/include/final/ftypes.h b/src/include/final/ftypes.h index cecfaa8b..5312905e 100644 --- a/src/include/final/ftypes.h +++ b/src/include/final/ftypes.h @@ -75,7 +75,11 @@ typedef std::function FCall; namespace finalcut { -template +namespace internal +{ + +template struct is_negative { inline bool operator () (const T& x) const @@ -93,10 +97,12 @@ struct is_negative } }; +} // namespace internal + template -inline bool isNegative (const T& x) +constexpr bool isNegative (const T& x) { - return is_negative::is_signed>()(x); + return internal::is_negative::is_signed>()(x); } template diff --git a/src/include/final/fvterm.h b/src/include/final/fvterm.h index b6d39abf..c62004af 100644 --- a/src/include/final/fvterm.h +++ b/src/include/final/fvterm.h @@ -259,7 +259,7 @@ class FVTerm virtual void addPreprocessingHandler ( const FVTerm* , const FPreprocessingFunction& ); virtual void delPreprocessingHandler (const FVTerm*); - template + template int printf (const FString&, Args&&...); int print (const FString&); int print (FTermArea*, const FString&); @@ -926,7 +926,7 @@ inline bool FVTerm::isInheritBackground() { return next_attribute.attr.bit.inherit_background; } //---------------------------------------------------------------------- -template +template inline int FVTerm::printf (const FString& format, Args&&... args) { FString str{}; diff --git a/src/include/final/fwidget.h b/src/include/final/fwidget.h index dcb36328..61cdc3f9 100644 --- a/src/include/final/fwidget.h +++ b/src/include/final/fwidget.h @@ -240,7 +240,7 @@ class FWidget : public FVTerm, public FObject static void setMoveSizeWidget (FWidget*); static void setActiveWindow (FWidget*); static void setOpenMenu (FWidget*); - template + template static void setColorTheme(); FAcceleratorList& setAcceleratorList(); virtual void setStatusbarMessage (const FString&); @@ -317,9 +317,9 @@ class FWidget : public FVTerm, public FObject int numOfFocusableChildren(); virtual bool close(); void clearStatusbarMessage(); - template + template void addCallback (const FString&, Args&&...) noexcept; - template + template void delCallback (Args&&...) noexcept; void emitCallback (const FString&) const; void addAccelerator (FKey); @@ -770,7 +770,7 @@ inline void FWidget::setOpenMenu (FWidget* obj) { open_menu = obj; } //---------------------------------------------------------------------- -template +template inline void FWidget::setColorTheme() { getColorTheme() = std::make_shared(); @@ -984,14 +984,14 @@ inline void FWidget::clearStatusbarMessage() { statusbar_message.clear(); } //---------------------------------------------------------------------- -template +template inline void FWidget::addCallback (const FString& cb_signal, Args&&... args) noexcept { callback_impl.addCallback (cb_signal, std::forward(args)...); } //---------------------------------------------------------------------- -template +template inline void FWidget::delCallback (Args&&... args) noexcept { callback_impl.delCallback(std::forward(args)...); diff --git a/test/fcallback-test.cpp b/test/fcallback-test.cpp index 6f21c34e..666b6399 100644 --- a/test/fcallback-test.cpp +++ b/test/fcallback-test.cpp @@ -39,13 +39,13 @@ class Widget { public: - template + template void addCallback (const finalcut::FString& cb_signal, Args&&... args) { cb.addCallback (cb_signal, std::forward(args)...); } - template + template void delCallback (Args&&... args) { cb.delCallback (std::forward(args)...); diff --git a/test/fobject-test.cpp b/test/fobject-test.cpp index eb356b10..28d74480 100644 --- a/test/fobject-test.cpp +++ b/test/fobject-test.cpp @@ -122,7 +122,12 @@ class FObject_userEvent : public finalcut::FObject virtual void onUserEvent (finalcut::FUserEvent* ev) { if ( ev->getUserId() == 42 ) - value = *(static_cast(ev->getData())); + { + value = ev->getData(); + + if ( ev->getFDataObject().isInitializedReference() ) + ev->getData()++; // this has external effects + } } private: @@ -625,9 +630,10 @@ void FObjectTest::userEventTest() int n = 9; finalcut::FUserEvent user_ev (finalcut::fc::User_Event, 42); - user_ev.setData( (void*)(&n) ); + user_ev.setData(n); finalcut::FApplication::sendEvent (&user, &user_ev); CPPUNIT_ASSERT ( user.getValue() == 9 ); + CPPUNIT_ASSERT ( n == 10 ); } // Put the test suite in the registry