Tomographer  v5.4
Tomographer C++ Framework Documentation
Tomographer::Tools Namespace Reference

Various useful tools. More...

Classes

class  BadFmtsFormat
 Exception for bad printf format. More...
 
struct  ComplexRealScalar
 The Real scalar type corresponding to a std::complex type. More...
 
struct  ComplexRealScalar< std::complex< RealScalar > >
 Implementation of ComplexRealScalar for complex types. More...
 
struct  Concatenate
 
struct  Concatenate< T, Sequence< T, I1... >, Sequence< T, I2... > >
 
class  ConsoleFormatterHelper
 Minimal tool for formatting console stuff with fixed line width. More...
 
class  conststr
 A constexpr string type. More...
 
struct  EigenAlignedOperatorNewProvider
 Provides correct operator-new implementation for Eigen types via the NeedOwnOperatorNew mechanism. More...
 
class  EigenAssertException
 Exception that is thrown upon failed eigen_assert. More...
 
struct  EigenStdVector
 Use this if you need an std::vector of any Eigen type. More...
 
class  FmtFootnotes
 Helper to format footnotes in Fmt. More...
 
struct  GenerateSequence
 
struct  GenerateSequence< T, N, typename std::enable_if<(N==0), void >::type >
 
struct  GenerateSequence< T, N, typename std::enable_if<(N==1), void >::type >
 
struct  hasOStreamOp
 Traits class to see whether an object exposes a "<<" operator overload for std::ostream. More...
 
struct  NeedEigenAlignedOperatorNew
 Helper to specialize NeedOwnOperatorNew for Eigen types. More...
 
struct  NeedEigenAlignedOperatorNew< false >
 
struct  NeedOwnOperatorNew
 Provide appropriate operator new() definitions for a structure which has a member of the given stored types. More...
 
struct  NeedOwnOperatorNew< Eigen::Array< Scalar, FixedRows, FixedCols, Options, MaxRows, MaxCols > >
 Specialize NeedOwnOperatorNew for Eigen types. More...
 
struct  NeedOwnOperatorNew< Eigen::Matrix< Scalar, FixedRows, FixedCols, Options, MaxRows, MaxCols > >
 Specialize NeedOwnOperatorNew for Eigen types. More...
 
struct  NeedOwnOperatorNew< T >
 Specialization of NeedOwnOperatorNew for a single type. More...
 
struct  NeedOwnOperatorNew< Type1, Type2, OtherTypes... >
 
struct  NoSpecialOperatorNew
 Helper for NoSpecialOperatorNewProvider. More...
 
struct  NoSpecialOperatorNewProvider
 Provider for regular types which don't need any special operator-new implementation (see NeedOwnOperatorNew) More...
 
struct  Sequence
 
struct  SigHandlerTaskDispatcherStatusReporter
 A generic handler which requests a status report from an TaskDispatcher Interface. More...
 
struct  SignalHandler
 An abstract signal handler (C++ interface) More...
 
class  SimpleWordWrapper
 
class  StaticOrDynamic
 A type which stores a value possibly known at compile-time. More...
 
class  StaticOrDynamic< T_, true, StaticValue_ >
 Template Specialization – see StaticOrDynamic<T_,IsDynamic_,StaticValue_> More...
 
struct  StatusProvider
 Template, specializable class to provide status reports for different objects. More...
 
struct  StatusProvider< MHRWMovingAverageAcceptanceRatioStatsCollector< CountIntType_ > >
 Provide status reporting for a MHRWMovingAverageAcceptanceRatioStatsCollector. More...
 
struct  StatusProvider< MHRWMultipleControllers< Args... > >
 Provide status reporting for a MHRWMultipleControllers. More...
 
struct  StatusProvider< MHRWStepSizeController< MHRWMovingAverageAcceptanceRatioStatsCollectorType, BaseLoggerType, StepRealType, IterCountIntType > >
 
struct  StatusProvider< MultipleMHRWStatsCollectors< Args... > >
 Provide status reporting for a MultipleMHRWStatsCollectors. More...
 
struct  StatusProvider< ValueHistogramMHRWStatsCollector< ValueCalculator_, LoggerType_, HistogramType_ > >
 Provide status reporting for a ValueHistogramMHRWStatsCollector. More...
 
struct  StatusProvider< ValueHistogramWithBinningMHRWStatsCollector< Params_, LoggerType_ > >
 Provide status reporting for a ValueHistogramWithBinningMHRWStatsCollector. More...
 
struct  StatusQuery
 Query status from different objects. More...
 
struct  StoreIfEnabled
 Utility that stores a data type if a compile-time flag is enabled. More...
 
struct  StoreIfEnabled< T_, true >
 Specialization of StoreIfEnabled<T_,enabled> for if we're storing a value. More...
 

Functions

template<typename F >
tomo_internal::FinalAction< F > finally (F f)
 implementation of a finally clause, somewhat like in Python More...
 
template<typename T >
std::ostreamoperator<< (std::ostream &str, const StoreIfEnabled< T, false > &)
 C++ Stream operators for StoreIfEnabled. More...
 
template<typename T >
std::ostreamoperator<< (std::ostream &str, const StoreIfEnabled< T, true > &val)
 C++ Stream operators for StoreIfEnabled. More...
 
template<typename IntType = int>
constexpr bool isPowerOfTwo (IntType N)
 Return true if the argument is a power of two, false otherwise.
 
template<typename X >
std::enable_if< std::is_unsigned< X >::value, bool >::type isPositive (const X)
 Test whether the given value is positive or zero. More...
 
template<typename X >
std::enable_if<!std::is_unsigned< X >::value, bool >::type isPositive (const X val)
 See isPositive()
 
template<typename X >
bool isFinite (const X val)
 Test whether floating-point value is finite. More...
 
template<typename IntType >
constexpr bool additionWillOverflow (IntType a, IntType b)
 Test whether the sum of two integers will cause an overflow/underflow for that integer type. More...
 
template<typename IntType , typename ... IntTypes>
constexpr bool additionWillOverflow (IntType a, IntType b, IntType c, IntTypes ... rest)
 Test whether the sum of several integers will cause an overflow/underflow for that integer type. More...
 
template<typename IntType >
constexpr bool multiplicationWillOverflow (IntType a, IntType b)
 Test whether a multiplication of two integers will cause an overflow/underflow for that integer type. More...
 
int getWidthForTerminalOutput (int max_width=0)
 Return a suitable width for displaying stuff on the standard output. More...
 
constexpr conststr extractFuncName (const conststr &funcname)
 Extract the function name from its signature. More...
 
template<typename ExceptionClass = std::runtime_error>
void tomographerEnsure (bool condition, std::string message)
 Ensure that a condition is met, or throw an exception. More...
 
template<typename Der , typename Rng , typename RndDist , typename... IndexTypes>
auto denseRandom (Rng &rng, RndDist &rnddist, IndexTypes... sizes) -> const Eigen::CwiseNullaryOp< tomo_internal::random_generator< Rng, RndDist, typename Eigen::internal::traits< Der >::Scalar >, Der >
 a matrix populated with random entries using C++'s random framework More...
 
template<typename Der , typename IndexType >
auto canonicalBasisVec (IndexType k, IndexType size) -> const Eigen::CwiseNullaryOp< tomo_internal::can_basis_vec_generator< typename Eigen::internal::traits< Der >::Scalar, IndexType >, Der >
 Expression for the k-th canonical basis vector of given dimension. More...
 
template<typename Der , typename IndexType >
auto canonicalBasisVec (IndexType k, IndexType j, IndexType rows, IndexType cols) -> const Eigen::CwiseNullaryOp< tomo_internal::can_basis_vec_generator< typename Eigen::internal::traits< Der >::Scalar, IndexType >, Der >
 Expression for the (k,j)-th canonical basis matrix of given dimension. More...
 
template<typename Der , typename... IndexTypes>
auto powersOfTwo (IndexTypes... sizes) -> const Eigen::CwiseNullaryOp< tomo_internal::powers_of_two_generator< typename Eigen::internal::traits< Der >::Scalar >, Der >
 Expression for a 1-D expression of powers of two. More...
 
template<int RowFactorCTime, int ColFactorCTime, typename Derived , typename std::enable_if<(RowFactorCTime==Eigen::Dynamic||ColFactorCTime==Eigen::Dynamic), bool >::type dummy = true>
auto replicated (const Eigen::DenseBase< Derived > &x, int row_factor, int col_factor) -> const Eigen::Replicate< Derived, Eigen::Dynamic, Eigen::Dynamic >
 Replicate a Eigen Dense object; same call for compile-time & run-time dimensions. More...
 
std::string vfmts (const char *fmt, va_list vl)
 printf- formatting to a std::string, with va_list pointer More...
 
std::string fmts (const char *fmt,...)
 printf- format to a std::string More...
 
template<typename T >
_Unspecified streamIfPossible (const T &obj)
 Utility to stream an object, but only if "<<" overload exists. More...
 
template<typename T >
_Unspecified streamIfPossible (const T &obj, std::string before, std::string after=std::string(), std::string alternative="<"+std::string(typeid(T).name())+">")
 A slightly more sophisticated alternative to streamIfPossible() More...
 
std::string fmtDuration (double seconds)
 Format a number of seconds into a human-readable string. More...
 
template<typename Rep , typename Period >
std::string fmtDuration (std::chrono::duration< Rep, Period > dt)
 Format a std::chrono::duration into a human-readable string. More...
 
std::ostreamoperator<< (std::ostream &stream, const FmtFootnotes &f)
 
void installSignalHandler (int signum, SignalHandler *sobj)
 Installs the given signal handler to catch the signal signum. More...
 
template<typename TaskDispatcher , typename LoggerT >
SigHandlerTaskDispatcherStatusReporter< TaskDispatcher, LoggerT > makeSigHandlerTaskDispatcherStatusReporter (TaskDispatcher *tasks, LoggerT &logger)
 
template<typename TaskDispatcher , typename LoggerT , typename CallbackFn >
SigHandlerTaskDispatcherStatusReporter< TaskDispatcher, LoggerT > makeSigHandlerTaskDispatcherStatusReporter (TaskDispatcher *tasks, LoggerT &logger, CallbackFn &&fn)
 

Detailed Description

Various useful tools.

Function Documentation

§ additionWillOverflow() [1/2]

template<typename IntType >
constexpr bool Tomographer::Tools::additionWillOverflow ( IntType  a,
IntType  b 
)
inline

Test whether the sum of two integers will cause an overflow/underflow for that integer type.

Return true if the sum a+b cannot be represented using IntType, false otherwise.

Works for signed and unsigned integer types.

Definition at line 395 of file cxxutil.h.

§ additionWillOverflow() [2/2]

template<typename IntType , typename ... IntTypes>
constexpr bool Tomographer::Tools::additionWillOverflow ( IntType  a,
IntType  b,
IntType  c,
IntTypes ...  rest 
)
inline

Test whether the sum of several integers will cause an overflow/underflow for that integer type.

Provided for convenience. See additionWillOverflow(IntType,IntType)

Definition at line 409 of file cxxutil.h.

§ canonicalBasisVec() [1/2]

template<typename Der , typename IndexType >
auto Tomographer::Tools::canonicalBasisVec ( IndexType  k,
IndexType  size 
) -> const Eigen::CwiseNullaryOp< tomo_internal::can_basis_vec_generator<typename Eigen::internal::traits<Der>::Scalar, IndexType>, Der >
inline

Expression for the k-th canonical basis vector of given dimension.

Definition at line 214 of file eigenutil.h.

§ canonicalBasisVec() [2/2]

template<typename Der , typename IndexType >
auto Tomographer::Tools::canonicalBasisVec ( IndexType  k,
IndexType  j,
IndexType  rows,
IndexType  cols 
) -> const Eigen::CwiseNullaryOp< tomo_internal::can_basis_vec_generator<typename Eigen::internal::traits<Der>::Scalar, IndexType>, Der >
inline

Expression for the (k,j)-th canonical basis matrix of given dimension.

This is a matrix of zeros except for the entry (k,j) which is one.

Definition at line 232 of file eigenutil.h.

§ denseRandom()

template<typename Der , typename Rng , typename RndDist , typename... IndexTypes>
auto Tomographer::Tools::denseRandom ( Rng &  rng,
RndDist &  rnddist,
IndexTypes...  sizes 
) -> const Eigen::CwiseNullaryOp< tomo_internal::random_generator<Rng, RndDist, typename Eigen::internal::traits<Der>::Scalar>, Der >
inline

a matrix populated with random entries using C++'s random framework

Parameters
rngthe std::random generator, such as a std::mt19937 instance
rnddistthe std::random distribution to sample from, such as a std::normal_distribution instance
sizeseither no parameter, one parameter or two parameters specifying the sizes of the dense object to return.

Definition at line 154 of file eigenutil.h.

§ extractFuncName()

constexpr conststr Tomographer::Tools::extractFuncName ( const conststr funcname)
inline

Extract the function name from its signature.

The argument is parsed as a function signature, and the name of the function (including scope such as namespaces and classes) is returned.

Definition at line 664 of file cxxutil.h.

§ finally()

template<typename F >
tomo_internal::FinalAction<F> Tomographer::Tools::finally ( f)
inline

implementation of a finally clause, somewhat like in Python

Example Usage:

SomeResource * ptr = new SomeResource(..)
auto delete_ptr = finally([ptr]() { delete ptr; });
// Now, the pointer ptr will be 'delete'd at end of the current block.
...

Definition at line 93 of file cxxutil.h.

§ fmtDuration() [1/2]

std::string Tomographer::Tools::fmtDuration ( double  seconds)
inline

Format a number of seconds into a human-readable string.

This formats the duration, given in seconds, into a human-readable string with hours, minutes seconds and milliseconds.

Parameters
secondsthe duration in seconds

Definition at line 370 of file fmt.h.

§ fmtDuration() [2/2]

template<typename Rep , typename Period >
std::string Tomographer::Tools::fmtDuration ( std::chrono::duration< Rep, Period >  dt)
inline

Format a std::chrono::duration into a human-readable string.

This formats the duration into a human-readable string with hours, minutes seconds and milliseconds.

Parameters
dtthe duration, a std::chrono::duration

See also fmtDuration(double)

Definition at line 390 of file fmt.h.

§ fmts()

std::string Tomographer::Tools::fmts ( const char *  fmt,
  ... 
)
inline

printf- format to a std::string

Does safe allocation, etc.

Definition at line 128 of file fmt.h.

§ getWidthForTerminalOutput()

int Tomographer::Tools::getWidthForTerminalOutput ( int  max_width = 0)
inline

Return a suitable width for displaying stuff on the standard output.

If max_width > 0, return that value as is.

If max_width <= 0, return the width of the screen (or default width) as given by the environment variable COLUMNS, minus the absolute value of the given number. [E.g. if the screen width is 100, then if maxwidth=-4, then return 96.]

Definition at line 569 of file cxxutil.h.

§ installSignalHandler()

void Tomographer::Tools::installSignalHandler ( int  signum,
SignalHandler sobj 
)
inline

Installs the given signal handler to catch the signal signum.

If other handlers are alreay set for a different signal number, they are not overridden.

Definition at line 190 of file signal_handler.h.

§ isFinite()

template<typename X >
bool Tomographer::Tools::isFinite ( const X  val)
inline

Test whether floating-point value is finite.

Potentially extends std::isfinite() in that we can specialize the template for custom types.

Definition at line 379 of file cxxutil.h.

§ isPositive()

template<typename X >
std::enable_if<std::is_unsigned<X>::value, bool>::type Tomographer::Tools::isPositive ( const X  )
inline

Test whether the given value is positive or zero.

This helper is useful to silence warnings in templates about `comparision of unsigned >= 0 is always true'.

Definition at line 360 of file cxxutil.h.

§ multiplicationWillOverflow()

template<typename IntType >
constexpr bool Tomographer::Tools::multiplicationWillOverflow ( IntType  a,
IntType  b 
)
inline

Test whether a multiplication of two integers will cause an overflow/underflow for that integer type.

Return true if the multiplication a*b cannot be represented using IntType, false otherwise.

Works for signed and unsigned integer types.

Definition at line 453 of file cxxutil.h.

§ operator<<() [1/2]

template<typename T >
std::ostream& Tomographer::Tools::operator<< ( std::ostream str,
const StoreIfEnabled< T, false > &   
)
inline

C++ Stream operators for StoreIfEnabled.

Produces human-readable output.

This implementation kicks in if the value is not streamable, and simply outputs the fixed string "[-]".

Definition at line 294 of file cxxutil.h.

§ operator<<() [2/2]

template<typename T >
std::ostream& Tomographer::Tools::operator<< ( std::ostream str,
const StoreIfEnabled< T, true > &  val 
)
inline

C++ Stream operators for StoreIfEnabled.

Produces human-readable output.

This implementation kicks in if the value is indeed streamable, and outputs the given value into the stream.

Definition at line 307 of file cxxutil.h.

§ powersOfTwo()

template<typename Der , typename... IndexTypes>
auto Tomographer::Tools::powersOfTwo ( IndexTypes...  sizes) -> const Eigen::CwiseNullaryOp< tomo_internal::powers_of_two_generator<typename Eigen::internal::traits<Der>::Scalar>, Der >
inline

Expression for a 1-D expression of powers of two.

An Eigen template expression yielding powers of two as a column vector. The \( i\) -th item is \( 2^i \).

Parameters
sizesYou may either specify the length of the vector (1 parameter), or the size of a column matrix (2 parameters, the second being equal to one).

If you specify a 2-D matrix size here (not a column or row vector), then the elements of the matrix are populated linearly with powers of two (with linear access, i.e. by default column-wise).

Definition at line 307 of file eigenutil.h.

§ replicated()

template<int RowFactorCTime, int ColFactorCTime, typename Derived , typename std::enable_if<(RowFactorCTime==Eigen::Dynamic||ColFactorCTime==Eigen::Dynamic), bool >::type dummy = true>
auto Tomographer::Tools::replicated ( const Eigen::DenseBase< Derived > &  x,
int  row_factor,
int  col_factor 
) -> const Eigen::Replicate<Derived, Eigen::Dynamic, Eigen::Dynamic>
inline

Replicate a Eigen Dense object; same call for compile-time & run-time dimensions.

See replicated<RowFactorCTime, ColFactorCTime, Derived, dummy>

Allow our libraries to use the same syntax, for example

Eigen::Matrix<...> m_replicated = replicated<1,ColFactorAtCompileTime>(matrix, 1, col_factor);

for replicating a matrix for which one might, or might not, know the factor at compile time.

The corresponding factor template parameter should be either Eigen::Dynamic or a fixed value. If a fixed value is given, you need to specify the same value in the runtime argument (done as an eigen_assert() check, i.e. this is compiled out if -DNDEBUG is used)

This is the implementation for fixed RowFactorCTime and ColFactorCTime.

Definition at line 344 of file eigenutil.h.

§ streamIfPossible() [1/2]

template<typename T >
_Unspecified Tomographer::Tools::streamIfPossible ( const T &  obj)
inline

Utility to stream an object, but only if "<<" overload exists.

You may use this function in a sequence of objects passed to a std::ostream, so that the object gets printed if it is streamable and it is ignored if not. The return type of this function is guaranteed to be streamable for any type T.

A typical use of this function is to generate debugging messages in generic templated classes for which the data type is not certain to be streamable.

Example use:

Eigen::Matrix2d m(Eigen::Matrix2d::Identity());
struct NotStreamable { int a, b, c; };
NotStreamable x;
std::cout << "The identity matrix is = \n" << streamIfPossible(m) << "\n"
<< "And the value of x is = " << streamIfPossible(x) << "\n";

Would output:

Definition at line 311 of file fmt.h.

§ streamIfPossible() [2/2]

template<typename T >
_Unspecified Tomographer::Tools::streamIfPossible ( const T &  obj,
std::string  before,
std::string  after = std::string(),
std::string  alternative = "<"+std::string(typeid(T).name())+">" 
)
inline

A slightly more sophisticated alternative to streamIfPossible()

With this wrapper, you can also specify text to add before and after the object if it is streamed, and you can specify the alternative text to display if the object is not streamable.

Example:

MyObjectType obj(..);
std::cout << "Hello !"
<< StreamIfPossibleWrapper(obj, "\nobject is = ", "\n", " [not streamable]\n")
<< "Done !\n";

might give you either

  Hello!
  object is = ..... string representation of MyObjectType .....
  Done !

if the object type MyObjectType is streamable, or

  Hello! [not streamable]
  Done !

if it is not.

Specify the object const reference in obj, the string to display before the object's string representation (before), the string to display after the object's string representation (after), and the alternative string to display if the object is not streamable (alternative).

Definition at line 346 of file fmt.h.

§ tomographerEnsure()

template<typename ExceptionClass = std::runtime_error>
void Tomographer::Tools::tomographerEnsure ( bool  condition,
std::string  message 
)
inline

Ensure that a condition is met, or throw an exception.

If condition is true, no action is taken. If condition is false, then an instance of the exception ExceptionClass is thrown. The exception is instantiated using message as parameter.

Template Parameters
ExceptionClassis the exception to throw if the given condition is false
Parameters
conditioncontrols whether to throw an error (if its value is false)
messageis the string to pass on to the exception class if condition is false

Definition at line 755 of file cxxutil.h.

§ vfmts()

std::string Tomographer::Tools::vfmts ( const char *  fmt,
va_list  vl 
)
inline

printf- formatting to a std::string, with va_list pointer

Does safe allocation, etc.

Definition at line 87 of file fmt.h.