Classes | Macros | Typedefs | Functions | Variables
Tools

C++ utility and idioms, such as noncopyable base classes, foreach macros or safe bool idiom. More...

Classes

class  aurora::Any
 Type-erased class holding any value. More...
 
class  aurora::Exception
 Exception base class. More...
 
class  aurora::FunctionCallException
 Exception class for failed function calls. More...
 
struct  aurora::PairHasher
 Hash object for std::pair. More...
 
class  aurora::NonCopyable
 Non-copyable base class. More...
 
struct  aurora::NulloptType
 Null literal for optional objects. More...
 
struct  aurora::InplaceType
 Tag to construct optional objects in-place. More...
 
class  aurora::Optional< T >
 Represents optional values. More...
 
class  aurora::PImpl< T, Size, Align >
 Fast PImpl idiom. More...
 

Macros

#define AURORA_FOREACH(declaration, container)
 Macro that emulates C++11 range-based for loop. More...
 
#define AURORA_NT_EQUAL(TupleName, typeVarPairs)
 Comparison operator == for named tuples. More...
 
#define AURORA_NT_LESS(TupleName, typeVarPairs)
 Comparison operator < for named tuples. More...
 
#define AURORA_NT_HASHER(TupleName, typeVarPairs)
 Hash functor for named tuples. More...
 
#define AURORA_NT_DEFAULT_CTOR(TupleName, typeVarPairs)
 Default constructor for named tuples. More...
 
#define AURORA_NAMED_TUPLE(TupleName, typeVarPairs)
 Named tuple definition. More...
 
#define AURORA_NAMED_TUPLE_EXT(TupleName, typeVarPairs, extensions)
 Named tuple definition with extended functionality. More...
 
#define AURORA_GLOBAL_SWAP(Class)
 Macro to implement a global overload of swap(lhs, rhs) to allow argument-dependent lookup. More...
 

Typedefs

typedef void(detail::SafeBoolHolder::* aurora::SafeBool) ()
 SafeBool type.
 

Functions

template<typename T >
bool aurora::equivalent (const T &lhs, const T &rhs)
 Determines whether two values are considered equivalent in sorting. More...
 
template<typename ForwardIterator , typename T >
ForwardIterator aurora::binarySearch (ForwardIterator first, ForwardIterator last, const T &value)
 Binary search for value in iterator range. More...
 
template<typename Container , typename Iterator >
void aurora::eraseUnordered (Container &c, Iterator itr)
 Erase element using swap-and-pop_back idiom. More...
 
template<typename Container , typename Value >
void aurora::remove (Container &c, const Value &v)
 Erase-remove idiom. More...
 
template<typename Container , typename Predicate >
void aurora::removeIf (Container &c, const Predicate &p)
 Erase-remove-if idiom. More...
 
template<typename Queue >
Queue::value_type aurora::pop (Queue &q)
 Pop from queue with return value. More...
 
template<typename Queue >
void aurora::clearQueue (Queue &q)
 Clear std::queue. More...
 
template<typename AssocContainer , typename Key >
AssocContainer::mapped_type & aurora::mapAt (AssocContainer &map, const Key &k)
 Returns the value type for a specific key. More...
 
template<typename To , typename From >
To aurora::downcast (From &base)
 Safe polymorphic downcast for references. More...
 
template<typename To , typename From >
To aurora::downcast (From *base)
 Safe polymorphic downcast for pointers. More...
 
template<typename T >
std::size_t aurora::hashValue (const T &object)
 Computes the hash value of an object (short-hand notation).
 
template<typename T >
void aurora::hashCombine (std::size_t &seed, const T &object)
 Combines a hash with the hash value of another object.
 
template<typename Itr >
void aurora::hashRange (std::size_t &seed, Itr begin, Itr end)
 Combines a hash with the hash value of a range of objects.
 
SafeBool aurora::toSafeBool (bool condition)
 Conversion function from bool to SafeBool.
 
template<typename T >
void adlSwap (T &lhs, T &rhs)
 swap() function with argument-dependent lookup More...
 
template<typename T >
std::type_index aurora::typeIndex (T &reference)
 Safe typeid operator for references. More...
 
template<typename T >
std::type_index aurora::typeIndex ()
 Safe typeid operator for types. More...
 
template<typename T >
void swap (Optional< T > &lhs, Optional< T > &rhs)
 Swaps two optionals.
 
template<typename T , typename... Args>
Optional< T > makeOptional (Args &&...args)
 Emplaces an object directly inside the aurora::Optional object. More...
 
template<typename T >
bool operator== (const Optional< T > &lhs, const Optional< T > &rhs)
 Equality comparison. More...
 
template<typename T >
bool operator!= (const Optional< T > &lhs, const Optional< T > &rhs)
 Inequality comparison. More...
 
template<typename T , std::size_t Size, std::size_t Align>
void swap (PImpl< T, Size, Align > &lhs, PImpl< T, Size, Align > &rhs)
 Swaps two implementation objects.
 

Variables

const SafeBool aurora::SafeBoolTrue
 SafeBool literal, evaluates to true.
 
const SafeBool aurora::SafeBoolFalse
 SafeBool literal, evaluates to false.
 

Detailed Description

C++ utility and idioms, such as noncopyable base classes, foreach macros or safe bool idiom.

Macro Definition Documentation

#define AURORA_FOREACH (   declaration,
  container 
)

Macro that emulates C++11 range-based for loop.

Code example:

1 std::vector<int> v = createVector();
2 AURORA_FOREACH(int& i, v)
3 {
4  i += 2;
5 }
#define AURORA_GLOBAL_SWAP (   Class)
Value:
inline void swap(Class& lhs, Class& rhs) \
{ \
lhs.swap(rhs); \
}
void swap(CopiedPtr< T > &lhs, CopiedPtr< T > &rhs)
Swaps the contents of two CopiedPtr instances.
Definition: CopiedPtr.hpp:312

Macro to implement a global overload of swap(lhs, rhs) to allow argument-dependent lookup.

Accesses the member function void Class::swap(Class&).

#define AURORA_NAMED_TUPLE (   TupleName,
  typeVarPairs 
)

Named tuple definition.

Defines a struct type with a specified list of public members and a corresponding constructor.

Parameters
TupleNameName of the struct
typeVarPairsParenthesized sequence of (Type, variable) pairs, such as ((int, i), (double, d))

Example:

1 // Defines a named tuple called Index2D with two members x and y of type std::size_t
2 AURORA_NAMED_TUPLE(Index2D,
3 (
4  (std::size_t, x),
5  (std::size_t, y)
6 ))
7 
8 // Defines a named tuple called Tile with members index (of type Index2D) and visible (of type bool)
9 AURORA_NAMED_TUPLE(Tile,
10 (
11  (Index2D, index),
12  (bool, visible)
13 ))
14 
15 // Create tuple object using constructor
16 Tile tile(Index2D(2, 3), true);
17 
18 // Access members
19 tile.index.x = 40;
20 if (tile.visible)
21  ...
#define AURORA_NAMED_TUPLE_EXT (   TupleName,
  typeVarPairs,
  extensions 
)

Named tuple definition with extended functionality.

Defines a struct type with a specified list of public members and a corresponding constructor.

Parameters
TupleNameName of the struct
typeVarPairsParenthesized sequence of (Type, variable) pairs, such as ((int, i), (double, d))
extensionsParenthesized sequence of extensions, such as (AURORA_NT_EQUAL, AURORA_NT_LESS). Possible arguments are all macros that begin with AURORA_NT_.

Example:

1 // Defines a named tuple called Index2D, with a default constructor and operator<
2 AURORA_NAMED_TUPLE_EXT(Index2D,
3 (
4  (std::size_t, x),
5  (std::size_t, y)
6 ),
7 (AURORA_NT_DEFAULT_CTOR, AURORA_NT_LESS))
8 
9 // Use constructor with arguments and default constructor
10 Index2D i(2, 1);
11 Index2D j;
12 
13 // Access members
14 j.x = 1;
15 j.y = 3;
16 
17 // Use operator< for key in map
18 std::map<Index2D, Tile> tileMap;
#define AURORA_NT_DEFAULT_CTOR (   TupleName,
  typeVarPairs 
)

Default constructor for named tuples.

Supplies the named tuple with a default constructor calls each members' default constructor. Do not invoke this macro directly. It is passed to AURORA_NAMED_TUPLE_EXT.

#define AURORA_NT_EQUAL (   TupleName,
  typeVarPairs 
)

Comparison operator == for named tuples.

Supplies the named tuple with an operator== in the surrounding namespace, which compares member-wise. Do not invoke this macro directly. It is passed to AURORA_NAMED_TUPLE_EXT.

#define AURORA_NT_HASHER (   TupleName,
  typeVarPairs 
)

Hash functor for named tuples.

Supplies the named tuple with a member typedef Hasher inside the tuple class. Do not invoke this macro directly. It is passed to AURORA_NAMED_TUPLE_EXT.

#define AURORA_NT_LESS (   TupleName,
  typeVarPairs 
)

Comparison operator < for named tuples.

Supplies the named tuple with an operator< in the surrounding namespace, which compares lexicographically. Do not invoke this macro directly. It is passed to AURORA_NAMED_TUPLE_EXT.

Function Documentation

template<typename T >
void adlSwap ( T &  lhs,
T &  rhs 
)

swap() function with argument-dependent lookup

Chooses the best overload of swap(lhs, rhs) with argument-dependent lookup. If none is found, std::swap() will be called.

template<typename ForwardIterator , typename T >
ForwardIterator aurora::binarySearch ( ForwardIterator  first,
ForwardIterator  last,
const T &  value 
)

Binary search for value in iterator range.

Performs a binary search with a useful return value, in contrast to std::binary_search().

Parameters
first,lastIterator range
valueValue to search for
Returns
Either iterator to first found element, or end-iterator of sequence
template<typename Queue >
void aurora::clearQueue ( Queue &  q)

Clear std::queue.

Calls pop() repeatedly until the queue is empty.

template<typename To , typename From >
To aurora::downcast ( From &  base)

Safe polymorphic downcast for references.

Can be used in place of a static_cast from a base class to a derived class – that is, you expect the downcast to succeed. In debug mode, types are checked at runtime using dynamic_cast. In release mode (with the NDBEBUG macro defined), a static_cast at full speed is used.

template<typename To , typename From >
To aurora::downcast ( From *  base)

Safe polymorphic downcast for pointers.

Can be used in place of a static_cast from a base class to a derived class – that is, you expect the downcast to succeed. In debug mode, types are checked at runtime using dynamic_cast. In release mode (with the NDBEBUG macro defined), a static_cast at full speed is used.

template<typename T >
bool aurora::equivalent ( const T &  lhs,
const T &  rhs 
)

Determines whether two values are considered equivalent in sorting.

Equal implies equivalent, but not vice versa. Two values are considered equivalent if neither appears before the other (w.r.t. sorting criterion, here operator<)

template<typename Container , typename Iterator >
void aurora::eraseUnordered ( Container &  c,
Iterator  itr 
)

Erase element using swap-and-pop_back idiom.

Erases the element at position itr by swapping it with the last element in the container and erasing the last element. This O(1) operation is particularly useful for std::vector or std::deque as long as the element order is not relevant.

template<typename T , typename... Args>
Optional< T > makeOptional ( Args &&...  args)
related

Emplaces an object directly inside the aurora::Optional object.

Example:

// These declarations are semantically equivalent (however different constructors can be called):
aurora::Optional<MyClass> opt(aurora::inplace, arg1, arg2);
auto opt = aurora::makeOptional<MyClass>(arg1, arg2); // may invoke move constructor
aurora::Optional<MyClass> opt = MyClass(arg1, arg2); // may invoke move or copy constructor
Parameters
argsVariable argument list, the single arguments are forwarded to T's constructor.
See also
InplaceType
template<typename AssocContainer , typename Key >
AssocContainer::mapped_type& aurora::mapAt ( AssocContainer &  map,
const Key &  k 
)

Returns the value type for a specific key.

Assumes existence of the key, returns corresponding mapped type without inserting it. In contrast to std::[unordered_]map::at(), this does not throw exceptions. If the key is not available, the behavior is undefined, in favor of optimized performance.

template<typename T >
bool operator!= ( const Optional< T > &  lhs,
const Optional< T > &  rhs 
)
related
template<typename T >
bool operator== ( const Optional< T > &  lhs,
const Optional< T > &  rhs 
)
related

Equality comparison.

Semantics: Returns true if both operands are empty, or if both are non-empty and the expression *lhs == *rhs returns true.

template<typename Queue >
Queue::value_type aurora::pop ( Queue &  q)

Pop from queue with return value.

Combines std::queue's pop() and front() operations.

template<typename Container , typename Value >
void aurora::remove ( Container &  c,
const Value &  v 
)

Erase-remove idiom.

Removes value v from sequential container c (std::vector or std::deque)

template<typename Container , typename Predicate >
void aurora::removeIf ( Container &  c,
const Predicate &  p 
)

Erase-remove-if idiom.

Removes value v from sequential container c (std::vector or std::deque)

template<typename T >
std::type_index aurora::typeIndex ( T &  reference)

Safe typeid operator for references.

The C++ typeid operator can easily be used incorrectly in RTTI scenarios, leading to tedious bugs at runtime:

  • The operand is a value instead of a reference.
  • The operand is a pointer which was not dereferenced.
  • The operand is an incomplete type. The compiler has no knowledge about the polymorphic class hierarchy at this point.
  • The class is not polymorphic. This can happen when a destructor was not declared virtual.

This implementation makes sure that the above-mentioned cases do not occur. Type verification happens at compile time, so there is no performance overhead in using this function. This function is specifically designed for the use of RTTI in polymorphic class hierarchies, it cannot be used in other scenarios (e.g. printing arbitrary type names for debugging).

Usage:

Base& baseRef = ...;
std::type_index t = aurora::typeIndex(baseRef);
template<typename T >
std::type_index aurora::typeIndex ( )

Safe typeid operator for types.

Same as typeIndex(T& reference), but for types.

Usage:

std::type_index t = aurora::typeIndex<Base>();