Skip to content

Commit

Permalink
Slightly clean up api
Browse files Browse the repository at this point in the history
  • Loading branch information
florianbecker committed Oct 13, 2020
1 parent 0b88469 commit bdc60d1
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 113 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 30 additions & 30 deletions examples/double/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <iostream>

/* modern.cpp.core header */
#include <DoubleExtras.h>
#include <Double.h>

int main() {

Expand All @@ -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<double, double> 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;
Expand Down
14 changes: 7 additions & 7 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
34 changes: 17 additions & 17 deletions source/DoubleExtras.cpp → source/Double.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@
#include <vector>

/* 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<double>::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<double>::epsilon() ) {

Expand All @@ -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<double>::epsilon() ) {

Expand All @@ -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<double> v = { 1, 10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8 };
return std::floor( _value * v[ _precision ] + 0.5 ) / v[ _precision ];
}

std::pair<double, double> doubleSplit( double _value ) {
std::pair<double, double> split( double _value ) {

double integral = 0.0;
double fraction = std::modf( _value, &integral );
Expand Down
30 changes: 15 additions & 15 deletions source/DoubleExtras.h → source/Double.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand All @@ -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.
Expand All @@ -79,24 +79,24 @@ 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.
* @param _value Value to round.
* @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<double, double> doubleSplit( double _value );
std::pair<double, double> split( double _value );
}
4 changes: 2 additions & 2 deletions source/Exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace vx {
#endif
}

int result() { return m_resultCode; }

std::string exec( const std::string &_command ) {

std::array<char, bufferSize> buffer {};
Expand All @@ -81,4 +79,6 @@ namespace vx {
}
return result;
}

int result() { return m_resultCode; }
}
16 changes: 8 additions & 8 deletions source/Exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 );
}
4 changes: 2 additions & 2 deletions source/KeyState.cpp → source/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
#endif

/* local header */
#include "KeyState.h"
#include "Keyboard.h"

namespace vx {

bool KeyState::isCapsLockActive() {
bool isCapsLockActive() {

bool isActive = false;

Expand Down
Loading

0 comments on commit bdc60d1

Please sign in to comment.