Tomographer  v5.2
Tomographer C++ Framework Documentation
cxxutil.h File Reference

(v5.2)

Some C++ utilities, with a tad of C++11 tricks. More...

#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <complex>
#include <type_traits>
#include <exception>
#include <stdexcept>
#include <tomographer/tools/cxxdefs.h>
#include <tomographer/tools/conststr.h>
#include <boost/version.hpp>
#include <boost/units/detail/utility.hpp>
#include <Eigen/Core>
+ Include dependency graph for cxxutil.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Tomographer::Tools::Sequence< T,... >
 
struct  Tomographer::Tools::Concatenate< T, S1, S2 >
 
struct  Tomographer::Tools::Concatenate< T, Sequence< T, I1... >, Sequence< T, I2... > >
 
struct  Tomographer::Tools::GenerateSequence< T, N, typename >
 
struct  Tomographer::Tools::GenerateSequence< T, N, typename >
 
struct  Tomographer::Tools::GenerateSequence< T, N, typename std::enable_if<(N==0), void >::type >
 
struct  Tomographer::Tools::GenerateSequence< T, N, typename std::enable_if<(N==1), void >::type >
 
class  Tomographer::Tools::StaticOrDynamic< T_, IsDynamic_, StaticValue_ >
 A type which stores a value possibly known at compile-time. More...
 
class  Tomographer::Tools::StaticOrDynamic< T_, true, StaticValue_ >
 Template Specialization – see StaticOrDynamic<T_,IsDynamic_,StaticValue_> More...
 
struct  Tomographer::Tools::StoreIfEnabled< T_, enabled >
 Utility that stores a data type if a compile-time flag is enabled. More...
 
struct  Tomographer::Tools::StoreIfEnabled< T_, true >
 Specialization of StoreIfEnabled<T_,enabled> for if we're storing a value. More...
 
struct  Tomographer::Tools::ComplexRealScalar< Scalar >
 The Real scalar type corresponding to a std::complex type. More...
 
struct  Tomographer::Tools::ComplexRealScalar< std::complex< RealScalar > >
 Implementation of ComplexRealScalar for complex types. More...
 

Namespaces

 Tomographer
 Base namespace for the Tomographer project.
 
 Tomographer::Tools
 Various useful tools.
 

Macros

#define PRINTF1_ARGS_SAFE
 
#define PRINTF2_ARGS_SAFE
 
#define PRINTF3_ARGS_SAFE
 
#define PRINTF4_ARGS_SAFE
 
#define TOMOGRAPHER_CXX_STACK_FORCE_REALIGN
 
#define TOMO_FUNCTION   __PRETTY_FUNCTION__
 Expands to the current function name or signature. More...
 
#define TOMOGRAPHER_DEFINE_MSG_EXCEPTION(ClassName, ErrPrefix)
 Define a simple exception class. More...
 
#define TOMOGRAPHER_DEFINE_MSG_EXCEPTION_BASE(ClassName, ErrPrefix, BaseClass)
 Define a simple exception class. More...
 

Functions

template<typename F >
tomo_internal::FinalAction< F > Tomographer::Tools::finally (F f)
 implementation of a finally clause, somewhat like in Python More...
 
template<typename T >
std::ostreamTomographer::Tools::operator<< (std::ostream &str, const StoreIfEnabled< T, false > &)
 C++ Stream operators for StoreIfEnabled. More...
 
template<typename T >
std::ostreamTomographer::Tools::operator<< (std::ostream &str, const StoreIfEnabled< T, true > &val)
 C++ Stream operators for StoreIfEnabled. More...
 
template<typename IntType = int>
constexpr bool Tomographer::Tools::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 Tomographer::Tools::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 Tomographer::Tools::isPositive (const X val)
 See isPositive()
 
template<typename X >
bool Tomographer::Tools::isFinite (const X val)
 Test whether floating-point value is finite. More...
 
template<typename IntType >
constexpr bool Tomographer::Tools::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 Tomographer::Tools::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 Tomographer::Tools::multiplicationWillOverflow (IntType a, IntType b)
 Test whether a multiplication of two integers will cause an overflow/underflow for that integer type. More...
 
int Tomographer::Tools::getWidthForTerminalOutput (int max_width=0)
 Return a suitable width for displaying stuff on the standard output. More...
 
constexpr conststr Tomographer::Tools::extractFuncName (const conststr &funcname)
 Extract the function name from its signature. More...
 
template<typename ExceptionClass = std::runtime_error>
void Tomographer::Tools::tomographerEnsure (bool condition, std::string message)
 Ensure that a condition is met, or throw an exception. More...
 

Detailed Description

Some C++ utilities, with a tad of C++11 tricks.

Definition in file cxxutil.h.

Macro Definition Documentation

§ TOMO_FUNCTION

#define TOMO_FUNCTION   __PRETTY_FUNCTION__

Expands to the current function name or signature.

For Tomographer::Logger::LocalLogger, use TOMO_ORIGIN.

Definition at line 608 of file cxxutil.h.

§ TOMOGRAPHER_CXX_STACK_FORCE_REALIGN

#define TOMOGRAPHER_CXX_STACK_FORCE_REALIGN

Force stack realign for some functions, where the stacks may not aligned by default (e.g. when using threads for Windows—set this attribute to any external entry points as http://stackoverflow.com/a/6718067/1694896 as well as http://www.peterstock.co.uk/games/mingw_sse/)

Todo:
Look up the exact conditions (compilers, platforms, stack sizes etc.) to make sure this macro has a correct value in all cases.

Definition at line 528 of file cxxutil.h.

§ TOMOGRAPHER_DEFINE_MSG_EXCEPTION

#define TOMOGRAPHER_DEFINE_MSG_EXCEPTION (   ClassName,
  ErrPrefix 
)
Value:
class TOMOGRAPHER_EXPORT ClassName : public std::exception { \
std::string _msg; \
public: \
ClassName(std::string msg) : _msg(std::string(ErrPrefix) + std::move(msg)) { } \
virtual ~ClassName() throw() { } \
std::string msg() const { return _msg; } \
const char * what() const throw() { return _msg.c_str(); } \
};
STL namespace.
STL class.
T what(T... args)
STL class.
T move(T... args)

Define a simple exception class.

This macro defines a class named ClassName which derives from std::exception. The constructor is given a string (the error message) which it prefixes with the given ErrPrefix to form the full error message.

The exception class consists of:

  • a constructor taking a single argument of type std::string. The constructor stores the string internally, prefixing it with the error prefix.
  • the usual method const char * what() const noexcept retrieves the string passed to the constructor (including the error prefix).
  • another method std::string msg() const noexcept allows to retrieve the message string as an std::string (also including the error prefix).

Example usage:

TOMOGRAPHER_DEFINE_MSG_EXCEPTION(invalid_input, "Invalid input: ") ;
...
try {
...
if (dim != matrix.rows()) { // for example
throw invalid_input("Matrix dimensions are incorrect!");
}
...
} catch (const std::exception & e) {
std::cerr << "An error occurred, aborting.\n"
<< e.what() << "\n";
::exit(1);
}

Definition at line 712 of file cxxutil.h.

§ TOMOGRAPHER_DEFINE_MSG_EXCEPTION_BASE

#define TOMOGRAPHER_DEFINE_MSG_EXCEPTION_BASE (   ClassName,
  ErrPrefix,
  BaseClass 
)
Value:
class TOMOGRAPHER_EXPORT ClassName : public BaseClass { \
public: \
ClassName(std::string msg) : BaseClass(std::string(ErrPrefix) + std::move(msg)) { } \
virtual ~ClassName() throw() { } \
};
STL namespace.
STL class.
T move(T... args)

Define a simple exception class.

In contrast to TOMOGRAPHER_DEFINE_MSG_EXCEPTION, the newly defined class does not itself store the error message. Rather, it passes it on to the base class BaseClass which is responsible of storing it and returning a meaningful what() string.

There is also no msg() method provided.

Definition at line 730 of file cxxutil.h.