From bdc60d1cec1b99b608181edb8bcf106c0ca66613 Mon Sep 17 00:00:00 2001 From: Florian Becker Date: Tue, 13 Oct 2020 18:18:28 +0200 Subject: [PATCH] Slightly clean up api --- README.md | 7 ++- examples/double/main.cpp | 60 +++++++++---------- source/CMakeLists.txt | 14 ++--- source/{DoubleExtras.cpp => Double.cpp} | 34 +++++------ source/{DoubleExtras.h => Double.h} | 30 +++++----- source/Exec.cpp | 4 +- source/Exec.h | 16 ++--- source/{KeyState.cpp => Keyboard.cpp} | 4 +- source/{KeyState.h => Keyboard.h} | 9 ++- source/{StringExtras.cpp => StringExtra.cpp} | 16 ++--- source/{StringExtras.h => StringExtra.h} | 15 +++-- .../{SingletonBase.h => Singleton.h} | 20 +++---- 12 files changed, 116 insertions(+), 113 deletions(-) rename source/{DoubleExtras.cpp => Double.cpp} (77%) rename source/{DoubleExtras.h => Double.h} (84%) rename source/{KeyState.cpp => Keyboard.cpp} (97%) rename source/{KeyState.h => Keyboard.h} (95%) rename source/{StringExtras.cpp => StringExtra.cpp} (84%) rename source/{StringExtras.h => StringExtra.h} (84%) rename source/templates/{SingletonBase.h => Singleton.h} (85%) diff --git a/README.md b/README.md index 3f165cc..2ae4a26 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,16 @@ make -j`nproc` ## Classes - **CPU** - Get CPU information. -- **DoubleExtras** - Less, Greater, Equal, Between, Round, Split. +- **Double** - Less, Greater, Equal, Between, Round, Split. - **Exec** - Run command and return stdout or mixed (stdout and stderr) and result code. -- **KeyState** - Check for caps lock state. +- **Keyboard** - Check for caps lock state. - **Serial** - Serial communication class. -- **StringExtras** - LeftTrim, RightTrim, Trim. +- **StringExtra** - LeftTrim, RightTrim, Trim. - **Timing** - Measuring time, cpu and real time. ## Template classes - **CSVWriter** - Write out comma-separated values. +- **Singleton** - Singleton templace class. - **Timer** - Timeout thread on time or interval. ## Unixservice class diff --git a/examples/double/main.cpp b/examples/double/main.cpp index fc407f7..73dac23 100644 --- a/examples/double/main.cpp +++ b/examples/double/main.cpp @@ -32,7 +32,7 @@ #include /* modern.cpp.core header */ -#include +#include int main() { @@ -48,108 +48,108 @@ int main() { /* equals */ std::cout << "----- Equal" << std::endl; - result = vx::doubleEqual( first, second ); + result = vx::equal( first, second ); std::cout << first << " == " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleEqual( second, third ); + result = vx::equal( second, third ); std::cout << second << " == " << third << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleEqual( third, fourth ); + result = vx::equal( third, fourth ); std::cout << third << " == " << fourth << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; /* less */ std::cout << "----- Less" << std::endl; - result = vx::doubleLess( first, second ); + result = vx::less( first, second ); std::cout << first << " < " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleLess( second, third ); + result = vx::less( second, third ); std::cout << second << " < " << third << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleLess( third, fourth ); + result = vx::less( third, fourth ); std::cout << third << " < " << fourth << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleLess( first, second, true ); + result = vx::less( first, second, true ); std::cout << first << " <= " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleLess( second, third, true ); + result = vx::less( second, third, true ); std::cout << second << " <= " << third << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleLess( third, fourth, true ); + result = vx::less( third, fourth, true ); std::cout << third << " <= " << fourth << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; /* greater */ std::cout << "----- Greater" << std::endl; - result = vx::doubleGreater( first, second ); + result = vx::greater( first, second ); std::cout << first << " > " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleGreater( second, third ); + result = vx::greater( second, third ); std::cout << second << " > " << third << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleGreater( third, fourth ); + result = vx::greater( third, fourth ); std::cout << third << " > " << fourth << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleGreater( first, second, true ); + result = vx::greater( first, second, true ); std::cout << first << " >= " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleGreater( second, third, true ); + result = vx::greater( second, third, true ); std::cout << second << " >= " << third << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleGreater( third, fourth, true ); + result = vx::greater( third, fourth, true ); std::cout << third << " >= " << fourth << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; /* between */ std::cout << "----- Between" << std::endl; - result = vx::doubleBetween( first, first, second ); + result = vx::between( first, first, second ); std::cout << first << " > " << first << " && " << first << " < " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleBetween( second, first, second ); + result = vx::between( second, first, second ); std::cout << second << " > " << first << " && " << second << " < " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleBetween( fourth, first, second ); + result = vx::between( fourth, first, second ); std::cout << fourth << " > " << first << " && " << fourth << " < " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleBetween( first, first, second, true ); + result = vx::between( first, first, second, true ); std::cout << first << " >= " << first << " && " << first << " <= " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleBetween( second, first, second, true ); + result = vx::between( second, first, second, true ); std::cout << second << " >= " << first << " && " << second << " <= " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; - result = vx::doubleBetween( fourth, first, second, true ); + result = vx::between( fourth, first, second, true ); std::cout << fourth << " >= " << first << " && " << fourth << " <= " << second << " result: " << std::boolalpha << result << std::noboolalpha << std::endl; /* round */ std::cout << "----- Round" << std::endl; double rounded = 0.0; - rounded = vx::doubleRound( first, 2 ); + rounded = vx::round( first, 2 ); std::cout << "round(2) " << first << " result: " << rounded << std::endl; - rounded = vx::doubleRound( second, 2 ); + rounded = vx::round( second, 2 ); std::cout << "round(2) " << second << " result: " << rounded << std::endl; - rounded = vx::doubleRound( third, 5 ); + rounded = vx::round( third, 5 ); std::cout << "round(5) " << third << " result: " << rounded << std::endl; - rounded = vx::doubleRound( fourth, 5 ); + rounded = vx::round( fourth, 5 ); std::cout << "round(5) " << fourth << " result: " << rounded << std::endl; /* split */ std::cout << "----- Split" << std::endl; std::pair splited = {}; - splited = vx::doubleSplit( first ); + splited = vx::split( first ); std::cout << "split " << first << " result: " << splited.first << " " << splited.second << std::endl; - splited = vx::doubleSplit( second ); + splited = vx::split( second ); std::cout << "split " << second << " result: " << splited.first << " " << splited.second << std::endl; - splited = vx::doubleSplit( third ); + splited = vx::split( third ); std::cout << "split " << third << " result: " << splited.first << " " << splited.second << std::endl; - splited = vx::doubleSplit( fourth ); + splited = vx::split( fourth ); std::cout << "split " << fourth << " result: " << splited.first << " " << splited.second << std::endl; return EXIT_SUCCESS; diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 3fbf3af..548e265 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -38,18 +38,18 @@ add_library(${PROJECT_NAME} ../README.md CPU.cpp CPU.h - DoubleExtras.cpp - DoubleExtras.h + Double.cpp + Double.h Exec.cpp Exec.h - KeyState.cpp - KeyState.h - StringExtras.cpp - StringExtras.h + Keyboard.cpp + Keyboard.h + StringExtra.cpp + StringExtra.h Timing.cpp Timing.h templates/CSVWriter.h - templates/SingletonBase.h + templates/Singleton.h templates/Timer.h ${${PROJECT_NAME}_os_src} ) diff --git a/source/DoubleExtras.cpp b/source/Double.cpp similarity index 77% rename from source/DoubleExtras.cpp rename to source/Double.cpp index 2a9c869..548dc0d 100644 --- a/source/DoubleExtras.cpp +++ b/source/Double.cpp @@ -36,19 +36,19 @@ #include /* local header */ -#include "DoubleExtras.h" +#include "Double.h" namespace vx { - bool doubleEqual( double _left, - double _right ) { + bool equal( double _left, + double _right ) { return ( std::fabs( _left - _right ) < std::numeric_limits::epsilon() ); } - bool doubleLess( double _left, - double _right, - bool _orEqual ) { + bool less( double _left, + double _right, + bool _orEqual ) { if ( std::fabs( _left - _right ) < std::numeric_limits::epsilon() ) { @@ -57,9 +57,9 @@ namespace vx { return ( _left < _right ); } - bool doubleGreater( double _left, - double _right, - bool _orEqual ) { + bool greater( double _left, + double _right, + bool _orEqual ) { if ( std::fabs( _left - _right ) < std::numeric_limits::epsilon() ) { @@ -68,22 +68,22 @@ namespace vx { return ( _left > _right ); } - bool doubleBetween( double _value, - double _min, - double _max, - bool _orEqual ) { + bool between( double _value, + double _min, + double _max, + bool _orEqual ) { - return ( doubleGreater( _value, _min, _orEqual ) && doubleLess( _value, _max, _orEqual ) ); + return ( greater( _value, _min, _orEqual ) && less( _value, _max, _orEqual ) ); } - double doubleRound( double _value, - std::size_t _precision ) { + double round( double _value, + std::size_t _precision ) { std::vector v = { 1, 10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8 }; return std::floor( _value * v[ _precision ] + 0.5 ) / v[ _precision ]; } - std::pair doubleSplit( double _value ) { + std::pair split( double _value ) { double integral = 0.0; double fraction = std::modf( _value, &integral ); diff --git a/source/DoubleExtras.h b/source/Double.h similarity index 84% rename from source/DoubleExtras.h rename to source/Double.h index 3a0f525..411f1e5 100644 --- a/source/DoubleExtras.h +++ b/source/Double.h @@ -44,8 +44,8 @@ namespace vx { * @param _right The second value. * @return True, if _left and _right are equal - otherwise false. */ - bool doubleEqual( double _left, - double _right ); + bool equal( double _left, + double _right ); /** * @brief Is _left less than _right or _orEqual? @@ -55,9 +55,9 @@ namespace vx { * @return True, if _left is less than _right or _left and _right are equal and * _orEqual is set to true - otherwise false. */ - bool doubleLess( double _left, - double _right, - bool _orEqual = false ); + bool less( double _left, + double _right, + bool _orEqual = false ); /** * @brief Is _left greater than _right or _orEqual? @@ -67,9 +67,9 @@ namespace vx { * @return True, if _left is greater than _right or _left and _right are equal and * _orEqual is set to true - otherwise false. */ - bool doubleGreater( double _left, - double _right, - bool _orEqual = false ); + bool greater( double _left, + double _right, + bool _orEqual = false ); /** * @brief Is _value between _min and _max or _orEqual. @@ -79,10 +79,10 @@ namespace vx { * @param _orEqual Is _value == _min or _value == _max * @return True, if _value is between _min and _max _orEqual - otherwise false. */ - bool doubleBetween( double _value, - double _min, - double _max, - bool _orEqual = false ); + bool between( double _value, + double _min, + double _max, + bool _orEqual = false ); /** * @brief Round a double _value by _precision. Rounded by default to two decimal places. @@ -90,13 +90,13 @@ namespace vx { * @param _precision Decimal places to round. * @return The rounded value. */ - double doubleRound( double _value, - std::size_t _precision = 2 ); + double round( double _value, + std::size_t _precision = 2 ); /** * @brief Split a double _value to its integer and decimal places. * @param _value Value to split. * @return The integer and decimal places. */ - std::pair doubleSplit( double _value ); + std::pair split( double _value ); } diff --git a/source/Exec.cpp b/source/Exec.cpp index 713eebe..4ad171d 100644 --- a/source/Exec.cpp +++ b/source/Exec.cpp @@ -59,8 +59,6 @@ namespace vx { #endif } - int result() { return m_resultCode; } - std::string exec( const std::string &_command ) { std::array buffer {}; @@ -81,4 +79,6 @@ namespace vx { } return result; } + + int result() { return m_resultCode; } } diff --git a/source/Exec.h b/source/Exec.h index fd9dd5c..6e029b5 100644 --- a/source/Exec.h +++ b/source/Exec.h @@ -38,6 +38,12 @@ */ namespace vx { + /** + * @brief Overload for close. + * @param _file File handle to close. + */ + void close( std::FILE *_file ); + /** * @brief Execute the external application and return the stdout output. * @param _command Command to run. @@ -46,14 +52,8 @@ namespace vx { std::string exec( const std::string &_command ); /** - * @brief result Exit code of the command. - * @return The resultint code. + * @brief Result Exit code of the command. + * @return The result code. */ int result(); - - /** - * @brief Overload for pclose. - * @param _file File handle to close. - */ - void close( std::FILE *_file ); } diff --git a/source/KeyState.cpp b/source/Keyboard.cpp similarity index 97% rename from source/KeyState.cpp rename to source/Keyboard.cpp index 1b1b95e..e5f6b90 100644 --- a/source/KeyState.cpp +++ b/source/Keyboard.cpp @@ -35,11 +35,11 @@ #endif /* local header */ -#include "KeyState.h" +#include "Keyboard.h" namespace vx { - bool KeyState::isCapsLockActive() { + bool isCapsLockActive() { bool isActive = false; diff --git a/source/KeyState.h b/source/Keyboard.h similarity index 95% rename from source/KeyState.h rename to source/Keyboard.h index dea8992..52588a8 100644 --- a/source/KeyState.h +++ b/source/Keyboard.h @@ -30,11 +30,10 @@ #pragma once +/** + * @brief vx (VX Apps) namespace. + */ namespace vx { - class KeyState { - - public: - static bool isCapsLockActive(); - }; + bool isCapsLockActive(); } diff --git a/source/StringExtras.cpp b/source/StringExtra.cpp similarity index 84% rename from source/StringExtras.cpp rename to source/StringExtra.cpp index 8fb8092..8f3738d 100644 --- a/source/StringExtras.cpp +++ b/source/StringExtra.cpp @@ -29,30 +29,30 @@ */ /* local header */ -#include "StringExtras.h" +#include "StringExtra.h" namespace vx { constexpr auto trimmed = " \t\n\r\f\v"; - std::string &stringRightTrim( std::string &_string, - const std::string &_trim ) { + std::string &rightTrim( std::string &_string, + const std::string &_trim ) { _string.erase( _string.find_last_not_of( _trim.empty() ? trimmed : _trim ) + 1 ); return _string; } - std::string &stringLeftTrim( std::string &_string, - const std::string &_trim ) { + std::string &leftTrim( std::string &_string, + const std::string &_trim ) { _string.erase( 0, _string.find_first_not_of( _trim.empty() ? trimmed : _trim ) ); return _string; } - std::string &stringTrim( std::string &_string, - const std::string &_trim ) { + std::string &trim( std::string &_string, + const std::string &_trim ) { - return stringLeftTrim( stringRightTrim( _string, _trim.empty() ? trimmed : _trim ), _trim.empty() ? trimmed : _trim ); + return leftTrim( rightTrim( _string, _trim.empty() ? trimmed : _trim ), _trim.empty() ? trimmed : _trim ); } #ifdef __APPLE__ diff --git a/source/StringExtras.h b/source/StringExtra.h similarity index 84% rename from source/StringExtras.h rename to source/StringExtra.h index 606cc9b..0dab959 100644 --- a/source/StringExtras.h +++ b/source/StringExtra.h @@ -36,19 +36,22 @@ /* stl header */ #include +/** + * @brief vx (VX Apps) namespace. + */ namespace vx { // trim from end of string (right) - std::string &stringRightTrim( std::string &_string, - const std::string &_trim = {} ); + std::string &rightTrim( std::string &_string, + const std::string &_trim = {} ); // trim from beginning of string (left) - std::string &stringLeftTrim( std::string &_string, - const std::string &_trim = {} ); + std::string &leftTrim( std::string &_string, + const std::string &_trim = {} ); // trim from both ends of string (right then left) - std::string &stringTrim( std::string &_string, - const std::string &_trim = {} ); + std::string &trim( std::string &_string, + const std::string &_trim = {} ); #ifdef __APPLE__ std::string fromCFStringRef( CFStringRef _stringRef ); diff --git a/source/templates/SingletonBase.h b/source/templates/Singleton.h similarity index 85% rename from source/templates/SingletonBase.h rename to source/templates/Singleton.h index 86afb8e..552aa8e 100644 --- a/source/templates/SingletonBase.h +++ b/source/templates/Singleton.h @@ -37,7 +37,7 @@ namespace vx { * @author Florian Becker (VX Apps) */ template - class SingletonBase { + class Singleton { public: /** @@ -62,7 +62,7 @@ namespace vx { * @~german * @brief Entfernt den verschobenen Konstruktor. */ - SingletonBase( SingletonBase && ) = delete; + Singleton( Singleton && ) = delete; /** * @~english @@ -73,7 +73,7 @@ namespace vx { * @brief Entfernt die kopierte Zuweisung. * @return Keine Rückgabe. */ - SingletonBase &operator=( SingletonBase const & ) = delete; + Singleton &operator=( Singleton const & ) = delete; /** * @~english @@ -84,25 +84,25 @@ namespace vx { * @brief Entfernt die verschobene Zuweisung. * @return Keine Rückgabe. */ - SingletonBase &operator=( SingletonBase && ) = delete; + Singleton &operator=( Singleton && ) = delete; protected: /** * @~english - * @brief Default constructor for InstanceBase. + * @brief Default constructor for Singleton. * * @~german - * @brief Standardkonstruktur für InstanceBase. + * @brief Standardkonstruktur für Singleton. */ - SingletonBase() = default; + Singleton() = default; /** * @~english - * @brief Default destructor for InstanceBase. + * @brief Default destructor for Singleton. * * @~german - * @brief Standarddestruktor für InstanceBase. + * @brief Standarddestruktor für Singleton. */ - virtual ~SingletonBase() = default; + virtual ~Singleton() = default; }; }