From 2c9778698e30c5ee1de51618ffb1a448a5a3a0f2 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Wed, 14 Oct 2020 17:31:52 +0200 Subject: [PATCH] Small improvements --- examples/string-operations.cpp | 6 ++--- examples/ui.cpp | 4 +-- src/fapplication.cpp | 10 +++---- src/flistview.cpp | 5 +--- src/fscrollview.cpp | 6 ++--- src/fstring.cpp | 45 +++++++++++++++---------------- src/ftermdetection.cpp | 8 +++--- src/fwidget.cpp | 6 ++--- src/fwindow.cpp | 8 +++--- src/include/final/fapplication.h | 4 +-- src/include/final/fscrollview.h | 4 +-- src/include/final/fstringstream.h | 6 ++--- src/include/final/fwidget.h | 4 +-- src/include/final/fwindow.h | 4 +-- 14 files changed, 57 insertions(+), 63 deletions(-) diff --git a/examples/string-operations.cpp b/examples/string-operations.cpp index faacd892..b6ad7132 100644 --- a/examples/string-operations.cpp +++ b/examples/string-operations.cpp @@ -370,7 +370,7 @@ void stringConcatenationExample() // Test: concatenate a FString and a wide string (operator +) const finalcut::FString& add3 = finalcut::FString("FString + ") - + const_cast(L"wchar_t*"); + + L"wchar_t*"; std::cout << " add: " << add3 << std::endl; // Test: concatenate a FString and a c++ string (operator +) @@ -402,7 +402,7 @@ void stringConcatenationExample() std::cout << " add: " << add9 << std::endl; // Test: concatenate a c-string and a FString (operator +) - const finalcut::FString& add10 = const_cast("char*") + const finalcut::FString& add10 = "char*" + finalcut::FString(" + FString"); std::cout << " add: " << add10 << std::endl; @@ -412,7 +412,7 @@ void stringConcatenationExample() std::cout << " add: " << add11 << std::endl; // Test: concatenate a wide string and a FString (operator +) - const finalcut::FString& add12 = const_cast(L"wchar_t*") + const finalcut::FString& add12 = L"wchar_t*" + finalcut::FString(" + FString"); std::cout << " add: " << add12 << std::endl; diff --git a/examples/ui.cpp b/examples/ui.cpp index 66a324f1..70db4801 100644 --- a/examples/ui.cpp +++ b/examples/ui.cpp @@ -300,7 +300,7 @@ class MyDialog final : public finalcut::FDialog void cb_copyClipboard(); void cb_pasteClipboard(); void cb_clearInput(); - void cb_switchTheme (const finalcut::FCheckMenuItem*) const; + void cb_switchTheme (const finalcut::FCheckMenuItem*); void cb_input2buttonText ( finalcut::FButton& , const finalcut::FLineEdit& ) const; void cb_setTitlebar (const finalcut::FLineEdit&); @@ -936,7 +936,7 @@ void MyDialog::cb_clearInput() } //---------------------------------------------------------------------- -void MyDialog::cb_switchTheme (const finalcut::FCheckMenuItem* check_menu) const +void MyDialog::cb_switchTheme (const finalcut::FCheckMenuItem* check_menu) { if ( check_menu->isChecked() ) finalcut::FApplication::setDarkTheme(); diff --git a/src/fapplication.cpp b/src/fapplication.cpp index 47393032..6b52a493 100644 --- a/src/fapplication.cpp +++ b/src/fapplication.cpp @@ -762,7 +762,7 @@ inline bool FApplication::sendKeyUpEvent (FWidget* widget) const } //---------------------------------------------------------------------- -inline void FApplication::sendKeyboardAccelerator() const +inline void FApplication::sendKeyboardAccelerator() { if ( FWidget::getOpenMenu() ) return; @@ -782,7 +782,7 @@ inline void FApplication::sendKeyboardAccelerator() const // Global keyboard accelerator if ( ! accpt ) { - auto root_widget = static_cast(getRootWidget()); + auto root_widget = getRootWidget(); if ( root_widget ) processAccelerator (root_widget); @@ -1319,17 +1319,17 @@ void FApplication::performTimerAction (FObject* receiver, FEvent* event) } //---------------------------------------------------------------------- -bool FApplication::isEventProcessable ( const FObject* receiver +bool FApplication::isEventProcessable ( FObject* receiver , const FEvent* event ) { if ( ! receiver->isWidget() ) // No restrictions for non-widgets return true; - const auto widget = static_cast(receiver); + auto widget = static_cast(receiver); if ( getModalDialogCounter() > 0 ) { - const FWidget* window; + FWidget* window; if ( widget->isWindowWidget() ) window = widget; diff --git a/src/flistview.cpp b/src/flistview.cpp index 3b55d8fb..67a3d026 100644 --- a/src/flistview.cpp +++ b/src/flistview.cpp @@ -2849,11 +2849,8 @@ void FListView::cb_vbarChange (const FWidget*) break; case FScrollbar::scrollJump: - { - int value = vbar->getValue(); - scrollToY (value); + scrollToY (vbar->getValue()); break; - } case FScrollbar::scrollWheelUp: wheelUp (wheel_distance); diff --git a/src/fscrollview.cpp b/src/fscrollview.cpp index d0483e76..ac696328 100644 --- a/src/fscrollview.cpp +++ b/src/fscrollview.cpp @@ -694,9 +694,9 @@ void FScrollView::copy2area() // private methods of FScrollView //---------------------------------------------------------------------- -inline FPoint FScrollView::getViewportCursorPos() const +inline FPoint FScrollView::getViewportCursorPos() { - const auto& window = FWindow::getWindowWidget(this); + auto window = FWindow::getWindowWidget(this); if ( window ) { @@ -846,7 +846,7 @@ void FScrollView::setVerticalScrollBarVisibility() const } //---------------------------------------------------------------------- -void FScrollView::setViewportCursor() const +void FScrollView::setViewportCursor() { if ( ! isChild(getFocusWidget()) ) return; diff --git a/src/fstring.cpp b/src/fstring.cpp index 4dc74658..67fab8a4 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -128,10 +128,8 @@ FString::FString (fc::SpecialCharacter c) { if ( c ) { - wchar_t s[2]; - s[0] = static_cast(c); - s[1] = L'\0'; - _assign (s); + std::array s{{ static_cast(c), L'\0' }}; + _assign (s.data()); } } @@ -140,10 +138,8 @@ FString::FString (const wchar_t c) { if ( c ) { - wchar_t s[2]; - s[0] = c; - s[1] = L'\0'; - _assign (s); + std::array s{{ c, L'\0' }}; + _assign (s.data()); } } @@ -152,10 +148,8 @@ FString::FString (const char c) { if ( c ) { - wchar_t s[2]; - s[0] = wchar_t(c & 0xff); - s[1] = L'\0'; - _assign (s); + std::array s{{ wchar_t(c & 0xff), L'\0' }}; + _assign (s.data()); } } @@ -403,7 +397,10 @@ char* FString::c_str() if ( length > 0 ) return const_cast(_to_cstring(string)); else if ( string ) - return const_cast(""); + { + static char empty_string[] = ""; + return empty_string; + } else return nullptr; } @@ -769,7 +766,7 @@ FString& FString::setString (const FString& s) //---------------------------------------------------------------------- FString& FString::setNumber (sInt64 num) { - wchar_t buf[30]{}; + std::array buf{}; wchar_t* s = &buf[29]; // Pointer to the last character auto abs_num = static_cast(num); @@ -813,7 +810,7 @@ FString& FString::setNumber (uInt64 num) //---------------------------------------------------------------------- FString& FString::setNumber (lDouble f_num, int precision) { - wchar_t format[20]{}; // = "%.Lg" + std::array format{}; // = "%.Lg" wchar_t* s = &format[0]; *s++ = L'%'; *s++ = L'.'; @@ -838,14 +835,14 @@ FString& FString::setNumber (lDouble f_num, int precision) *s++ = L'g'; *s = L'\0'; - return sprintf(format, f_num); + return sprintf(format.data(), f_num); } //---------------------------------------------------------------------- FString& FString::setFormatedNumber (sInt64 num, char separator) { int n{0}; - wchar_t buf[30]{}; + std::array buf{}; wchar_t* s = &buf[29]; // Pointer to the last character auto abs_num = static_cast(num); @@ -1515,7 +1512,7 @@ inline const wchar_t* FString::_extractToken ( wchar_t* rest[] if ( ! token[0] ) return nullptr; - *rest = std::wcspbrk(token, delim); + *rest = std::wcspbrk(std::move(token), delim); if ( *rest ) *(*rest)++ = '\0'; @@ -1555,9 +1552,9 @@ std::ostream& operator << (std::ostream& outstr, const FString& s) //---------------------------------------------------------------------- std::istream& operator >> (std::istream& instr, FString& s) { - char buf[FString::INPBUFFER + 1]{}; - instr.getline (buf, FString::INPBUFFER); - const wchar_t* wc_str = s._to_wcstring(buf); + std::array buf{}; + instr.getline (buf.data(), FString::INPBUFFER); + const wchar_t* wc_str = s._to_wcstring(buf.data()); if ( wc_str ) { @@ -1589,9 +1586,9 @@ std::wostream& operator << (std::wostream& outstr, const FString& s) //---------------------------------------------------------------------- std::wistream& operator >> (std::wistream& instr, FString& s) { - wchar_t buf[FString::INPBUFFER + 1]{}; - instr.getline (buf, FString::INPBUFFER); - s._assign (buf); + std::array buf{}; + instr.getline (buf.data(), FString::INPBUFFER); + s._assign (buf.data()); return instr; } diff --git a/src/ftermdetection.cpp b/src/ftermdetection.cpp index d4d545de..eca5aa73 100644 --- a/src/ftermdetection.cpp +++ b/src/ftermdetection.cpp @@ -211,7 +211,7 @@ bool FTermDetection::getTTYtype() term_basename++; std::FILE* fp{}; - char str[BUFSIZ]{}; + std::array str{}; if ( ! fsystem ) return false; @@ -220,11 +220,11 @@ bool FTermDetection::getTTYtype() return false; // Read and parse the file - while ( fgets(str, sizeof(str) - 1, fp) != nullptr ) + while ( fgets(str.data(), str.size() - 1, fp) != nullptr ) { const char* type{nullptr}; // nullptr == not found const char* name{nullptr}; - char* p = str; + char* p = str.data(); while ( *p ) { @@ -232,7 +232,7 @@ bool FTermDetection::getTTYtype() *p = '\0'; else if ( type == nullptr ) type = p; - else if ( name == nullptr && p != str && p[-1] == '\0' ) + else if ( name == nullptr && p != str.data() && p[-1] == '\0' ) name = p; p++; diff --git a/src/fwidget.cpp b/src/fwidget.cpp index 279f9c8c..1ea7e97f 100644 --- a/src/fwidget.cpp +++ b/src/fwidget.cpp @@ -141,9 +141,9 @@ FWidget::~FWidget() // destructor // public methods of FWidget //---------------------------------------------------------------------- -FWidget* FWidget::getRootWidget() const +FWidget* FWidget::getRootWidget() { - auto obj = const_cast(this); + FWidget* obj = this; auto p_obj = getParentWidget(); while ( ! obj->isRootWidget() && p_obj ) @@ -1322,7 +1322,7 @@ void FWidget::adjustSize() } //---------------------------------------------------------------------- -void FWidget::adjustSizeGlobal() const +void FWidget::adjustSizeGlobal() { if ( ! isRootWidget() ) { diff --git a/src/fwindow.cpp b/src/fwindow.cpp index b1b50c8d..13e87b93 100644 --- a/src/fwindow.cpp +++ b/src/fwindow.cpp @@ -483,7 +483,7 @@ void FWindow::delWindow (const FWidget* obj) } //---------------------------------------------------------------------- -FWindow* FWindow::getWindowWidget (const FWidget* obj) +FWindow* FWindow::getWindowWidget (FWidget* obj) { // returns the window object to the given widget obj auto p_obj = obj->getParentWidget(); @@ -495,17 +495,17 @@ FWindow* FWindow::getWindowWidget (const FWidget* obj) } if ( obj->isWindowWidget() ) - return const_cast(reinterpret_cast(obj)); + return static_cast(obj); else return nullptr; } //---------------------------------------------------------------------- -int FWindow::getWindowLayer (const FWidget* obj) +int FWindow::getWindowLayer (FWidget* obj) { // returns the window layer from the widget obj - const FWidget* window; + FWidget* window; if ( ! getWindowList() ) return -1; diff --git a/src/include/final/fapplication.h b/src/include/final/fapplication.h index ec24e493..ab8d3e42 100644 --- a/src/include/final/fapplication.h +++ b/src/include/final/fapplication.h @@ -186,7 +186,7 @@ class FApplication : public FWidget bool sendKeyDownEvent (FWidget*) const; bool sendKeyPressEvent (FWidget*) const; bool sendKeyUpEvent (FWidget*) const; - void sendKeyboardAccelerator() const; + void sendKeyboardAccelerator(); void processKeyboardEvent() const; bool processDialogSwitchAccelerator() const; bool processAccelerator (const FWidget* const&) const; @@ -216,7 +216,7 @@ class FApplication : public FWidget void processLogger() const; bool processNextEvent(); void performTimerAction (FObject*, FEvent*) override; - static bool isEventProcessable (const FObject*, const FEvent*); + static bool isEventProcessable (FObject*, const FEvent*); static bool isNextEventTimeout(); // Data members diff --git a/src/include/final/fscrollview.h b/src/include/final/fscrollview.h index c0f6eccb..a019e2f1 100644 --- a/src/include/final/fscrollview.h +++ b/src/include/final/fscrollview.h @@ -157,7 +157,7 @@ class FScrollView : public FWidget static constexpr int horizontal_border_spacing = 2; // Accessors - FPoint getViewportCursorPos() const; + FPoint getViewportCursorPos(); // Methods void init(); @@ -169,7 +169,7 @@ class FScrollView : public FWidget , Callback ); void setHorizontalScrollBarVisibility() const; void setVerticalScrollBarVisibility() const; - void setViewportCursor() const; + void setViewportCursor(); // Callback methods void cb_vbarChange (const FWidget*); diff --git a/src/include/final/fstringstream.h b/src/include/final/fstringstream.h index bd4e9425..2904ce7e 100644 --- a/src/include/final/fstringstream.h +++ b/src/include/final/fstringstream.h @@ -83,7 +83,7 @@ class FStringStream : public std::wiostream virtual FString getClassName() const; void swap (FStringStream&) noexcept; void clear(); - std::wstringbuf* rdbuf() const; + std::wstringbuf* rdbuf(); FString str() const; private: @@ -101,8 +101,8 @@ inline void FStringStream::clear() { buffer.str(L""); } //---------------------------------------------------------------------- -inline std::wstringbuf* FStringStream::rdbuf() const -{ return const_cast(&buffer); } +inline std::wstringbuf* FStringStream::rdbuf() +{ return &buffer; } //---------------------------------------------------------------------- inline FString FStringStream::str() const diff --git a/src/include/final/fwidget.h b/src/include/final/fwidget.h index 20e8c62e..493d6703 100644 --- a/src/include/final/fwidget.h +++ b/src/include/final/fwidget.h @@ -184,7 +184,7 @@ class FWidget : public FVTerm, public FObject // Accessors FString getClassName() const override; - FWidget* getRootWidget() const; + FWidget* getRootWidget(); FWidget* getParentWidget() const; static FWidget*& getMainWidget(); static FWidget*& getActiveWindow(); @@ -364,7 +364,7 @@ class FWidget : public FVTerm, public FObject void initTerminal() override; void initDesktop(); virtual void adjustSize(); - void adjustSizeGlobal() const; + void adjustSizeGlobal(); void hideArea (const FSize&); virtual bool focusNextChild(); // Change child... virtual bool focusPrevChild(); // ...focus diff --git a/src/include/final/fwindow.h b/src/include/final/fwindow.h index f2e61c8c..72f36150 100644 --- a/src/include/final/fwindow.h +++ b/src/include/final/fwindow.h @@ -84,8 +84,8 @@ class FWindow : public FWidget // Accessors FString getClassName() const override; - static FWindow* getWindowWidget (const FWidget*); - static int getWindowLayer (const FWidget*); + static FWindow* getWindowWidget (FWidget*); + static int getWindowLayer (FWidget*); FWidget* getWindowFocusWidget() const; // Mutators