Classes
util.h File Reference

Interface of auxilliary utility functions for SDHLibrary-CPP. More...

#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include "sdhlibrary_settings.h"
#include "simplevector.h"
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  cSetValueTemporarily< T >
 helper class to set value on construction and reset to previous value on destruction. (RAII-idiom) More...
 

Auxiliary functions

template<typename Function , typename InputIterator >
Function apply (Function f, InputIterator first, InputIterator last)
 
template<typename Function , typename Tp >
void apply (Function f, Tp &sequence)
 
VCC_EXPORT double Approx (double a, double b, double eps)
 
VCC_EXPORT bool Approx (int n, double *a, double *b, double *eps)
 
VCC_EXPORT int CompareReleases (char const *rev1, char const *rev2)
 compare release strings More...
 
#define DEFINE_TO_CASECOMMAND(_c)   case _c: return (#_c)
 
#define DEFINE_TO_CASECOMMAND_MSG(_c, ...)   case _c: return (#_c ": " __VA_ARGS__)
 
VCC_EXPORT double DegToRad (double d)
 
VCC_EXPORT bool InIndex (int v, int max)
 
VCC_EXPORT bool InRange (double v, double min, double max)
 
VCC_EXPORT bool InRange (int n, double const *v, double const *min, double const *max)
 
template<typename Function , typename Tp >
Tp map (Function f, Tp sequence)
 
template<typename T >
std::ostream & operator<< (std::ostream &stream, std::vector< T > const &v)
 
VCC_EXPORT double RadToDeg (double r)
 
VCC_EXPORT void SleepSec (double t)
 
VCC_EXPORT void ToRange (cSimpleVector &v, std::vector< double > const &min, std::vector< double > const &max)
 
VCC_EXPORT double ToRange (double v, double min, double max)
 
VCC_EXPORT void ToRange (int n, double *v, double const *min, double const *max)
 
VCC_EXPORT void ToRange (std::vector< double > &v, std::vector< double > const &min, std::vector< double > const &max)
 

Detailed Description

Interface of auxilliary utility functions for SDHLibrary-CPP.

General file information

Author
Dirk Osswald
Date
2007-02-19

Copyright


Definition in file util.h.

Macro Definition Documentation

◆ DEFINE_TO_CASECOMMAND

#define DEFINE_TO_CASECOMMAND (   _c)    case _c: return (#_c)

Just a macro for the very lazy programmer to convert an enum or a DEFINE macro into a case command that returns the name of the macro as string.

Usage:

char const* eSomeEnumType_ToString( eSomeEnumType rc )
{
switch (rc)
{
DEFINE_TO_CASECOMMAND( AN_OTHER_ENUM );
...
default: return "unknown return code";
}
}
Remarks
You must use the enum or macro directly (not a variable with that value) since CPP-stringification is used.

See also DEFINE_TO_CASECOMMAND_MSG

Definition at line 103 of file util.h.

◆ DEFINE_TO_CASECOMMAND_MSG

#define DEFINE_TO_CASECOMMAND_MSG (   _c,
  ... 
)    case _c: return (#_c ": " __VA_ARGS__)

Just a macro for the very lazy programmer to convert an enum or a DEFINE macro and a message into a case command that returns the name of the macro and the message as string.

Usage:

char const* eSomeEnumType_ToString( eSomeEnumType rc )
{
switch (rc)
{
DEFINE_TO_CASECOMMAND_MSG( AN_ENUM, "some mighty descriptive message" );
DEFINE_TO_CASECOMMAND_MSG( AN_OTHER_ENUM, "guess what" );
...
default: return "unknown return code";
}
}
Remarks
You must use the enum or macro directly (not a variable with that value) since CPP-stringification is used.

See also DEFINE_TO_CASECOMMAND

Definition at line 127 of file util.h.

Function Documentation

◆ apply() [1/2]

template<typename Function , typename InputIterator >
Function apply ( Function  f,
InputIterator  first,
InputIterator  last 
)

Apply a function to every element of a sequence.

Parameters
firstAn input iterator.
lastAn input iterator.
fA unary function object.
Returns
f.

Applies the function object f to each element in the range [first,last). f must not modify the order of the sequence. If f has a return value it is ignored.

Definition at line 249 of file util.h.

◆ apply() [2/2]

template<typename Function , typename Tp >
void apply ( Function  f,
Tp &  sequence 
)

Apply a function to every element of a sequence, the elements of the sequence are modified by f

Parameters
fA unary function object.
sequenceThe iterable sequence to modify.

Applies the function object f to each element of the sequence.

Definition at line 221 of file util.h.

◆ Approx() [1/2]

VCC_EXPORT double Approx ( double  a,
double  b,
double  eps 
)

Return True if a is approximately the same as b. I.E. |a-b| < eps

Definition at line 126 of file util.cpp.

◆ Approx() [2/2]

VCC_EXPORT bool Approx ( int  n,
double *  a,
double *  b,
double *  eps 
)

Return True if list/tuple/array a=(a1,a2,...) is approximately the same as b=(b1,b2,...). I.E. |a_i-b_i| < eps[i]

Definition at line 132 of file util.cpp.

◆ CompareReleases()

VCC_EXPORT int CompareReleases ( char const *  rev1,
char const *  rev2 
)

compare release strings

compare release strings rev1 and rev2.

Returns
-1,0, or 1 if rev1 is older, equal or newer than rev2
Parameters
rev1- a release string like "0.0.1.5" or "0.0.1.11-a"
rev2- another release string

Example:

  • CompareReleases( "0.0.1.5", "0.0.1.5" ) ==> 0
  • CompareReleases( "0.0.1.5", "0.0.1.4" ) ==> 1
  • CompareReleases( "0.0.1.5", "0.0.2.1" ) ==> -1
  • CompareReleases( "0.0.1.5", "0.0.1.5-a" ) ==> -1

Definition at line 236 of file util.cpp.

◆ DegToRad()

VCC_EXPORT double DegToRad ( double  d)

Return d in deg converted to rad

Definition at line 143 of file util.cpp.

◆ InIndex()

VCC_EXPORT bool InIndex ( int  v,
int  max 
)

Return True if v is in range [0 .. max[

Definition at line 74 of file util.cpp.

◆ InRange() [1/2]

VCC_EXPORT bool InRange ( double  v,
double  min,
double  max 
)

Return True if v is in range [min .. max]

Definition at line 80 of file util.cpp.

◆ InRange() [2/2]

VCC_EXPORT bool InRange ( int  n,
double const *  v,
double const *  min,
double const *  max 
)

Return True if in list/tuple/array v=(v1,v2,...) each v_i is in range [min_i..max_i] with min = (min1, min2,...) max = (max1, max2, ..)

Definition at line 86 of file util.cpp.

◆ map()

template<typename Function , typename Tp >
Tp map ( Function  f,
Tp  sequence 
)

map a function to every element of a sequence, returning a copy with the mapped elements

Parameters
f- A unary function object.
sequence- An iterable object.
Returns
copy of sequence with the mapped elements

Definition at line 267 of file util.h.

◆ operator<<()

template<typename T >
std::ostream& operator<< ( std::ostream &  stream,
std::vector< T > const &  v 
)

Overloaded insertion operator for vectors: a comma and space separated list of the vector elements of v is inserted into stream

Parameters
stream- the output stream to insert into
v- the vector of objects to insert into stream
Returns
the stream with the inserted objects
Attention
If you use the SDH namespace then you should be aware that using this overloaded insertion operator can get tricky:
  • If you use a using namespace SDH directive then things are easy and intuitive:
    #include <sdh/util.h>
    using namespace SDH;
    std::vector<int> v;
    std::cout << "this is a std::vector: " << v << "\n";
  • But without the using namespace SDH accessing the operator is tricky, you have to use the 'functional' access operator<<(s,v) in order to be able to apply the scope resolution operator :: correctly:
    #include <sdh/util.h>
    std::vector<int> v;
    SDH::operator<<( std::cout << "this is a std::vector: ", v ) << "\n";
  • The more intuitive approaches do not work:
    std::cout << faa ; // obviously fails with "no match for >>operator<<<< in >>std::cout << faa<<", as expected
    std::cout SDH::<< faa ; // is a syntax error
    std::cout SDH::operator<< faa ; // is a syntax error
    std::cout operator SDH::<< faa ; // is a syntax error

Definition at line 311 of file util.h.

◆ RadToDeg()

VCC_EXPORT double RadToDeg ( double  r)

Return r in rad converted to deg

Definition at line 149 of file util.cpp.

◆ SleepSec()

VCC_EXPORT void SleepSec ( double  t)

Sleep for t seconds. (t is a double!)

Definition at line 155 of file util.cpp.

◆ ToRange() [1/4]

VCC_EXPORT void ToRange ( cSimpleVector v,
std::vector< double > const &  min,
std::vector< double > const &  max 
)

Limit each v_i in v to range [min_i..max_i] with min = (min1, min2,...) max = (max1, max2, ..) This modifies v!

Definition at line 120 of file util.cpp.

◆ ToRange() [2/4]

VCC_EXPORT double ToRange ( double  v,
double  min,
double  max 
)

Return v limited to range [min .. max]. I.e. if v is < min then min is returned, or if v > max then max is returned, else v is returned

Definition at line 97 of file util.cpp.

◆ ToRange() [3/4]

VCC_EXPORT void ToRange ( int  n,
double *  v,
double const *  min,
double const *  max 
)

Limit each v_i in v to range [min_i..max_i] with min = (min1, min2,...) max = (max1, max2, ..) This modifies *v!

Definition at line 105 of file util.cpp.

◆ ToRange() [4/4]

VCC_EXPORT void ToRange ( std::vector< double > &  v,
std::vector< double > const &  min,
std::vector< double > const &  max 
)

Limit each v_i in v to range [min_i..max_i] with min = (min1, min2,...) max = (max1, max2, ..) This modifies v!

Definition at line 114 of file util.cpp.

DEFINE_TO_CASECOMMAND
#define DEFINE_TO_CASECOMMAND(_c)
Definition: util.h:103
operator<<
VCC_EXPORT std::ostream & operator<<(std::ostream &stream, cHexByteString const &s)
output the bytes in s to stream as a list of space separated hex bytes (without 0x prefix)
Definition: dbg.h:344
SDH
DEFINE_TO_CASECOMMAND_MSG
#define DEFINE_TO_CASECOMMAND_MSG(_c,...)
Definition: util.h:127
util.h
Interface of auxilliary utility functions for SDHLibrary-CPP.


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:59