0.12.0
Freundlich's C++ toolkit
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Classes | Macros | Functions
fcppt.various
fcppt

Description

Documents smaller features that do not warrant their own modules.

Classes

struct  fcppt::null_ptr_t
 A class that is implicitly convertible to null pointers. More...
 

Macros

#define FCPPT_ASSERT_COMPLETE(type)
 Assert at compile time that a type is complete.
 
#define FCPPT_FOREACH_ENUMERATOR(name, enum_)
 Iterates over an enum.
 
#define FCPPT_FOREACH_ENUMERATOR_START(name, enum_, start)
 Iterates over an enum with a designated start.
 
#define FCPPT_FOREACH_ENUMERATOR_START_END(name, enum_, start, end)
 Iterates over an enum with designated start and end values.
 
#define FCPPT_NONASSIGNABLE(classname)
 Makes a class nonassignable.
 
#define FCPPT_NONCOPYABLE(classname)
 Makes a class noncopyable.
 
#define FCPPT_SAFE_BOOL(classname)
 Implements the safe bool idiom for a class.
 
#define FCPPT_STATIC_ASSERT_EXPRESSION(cond)
 Assert at compile time that a condition is true, using an expression.
 
#define FCPPT_STATIC_ASSERT_STATEMENT(cond)
 Assert at compile time that a condition is true, using a statement.
 

Functions

fcppt::null_ptr_t const fcppt::null_ptr ()
 Partial replacement for C++11's nullptr.
 

Macro Definition Documentation

#define FCPPT_ASSERT_COMPLETE (   type)
Value:
static_cast<\
void\
>(\
sizeof(type)\
)

Assert at compile time that a type is complete.

In some cases, a type must be complete, which bascially means that it is not void and if it is a class, the definition must already be known. For example, delete has undefined behaviour if it is invoked on a type that is not complete but has a non trivial destructor.

This macro generates a compile time error if a type is not complete. An example of its usage is shown below.

namespace
{
template<
typename T
>
void
std_delete(
T *const _param
)
{
T
);
delete _param;
}
#define FCPPT_FOREACH_ENUMERATOR (   name,
  enum_ 
)
Value:
name,\
enum_, \
static_cast<enum_::type>(0)\
)

Iterates over an enum.

Iterates over the type enum_::type, giving the loop variable the name name, starting at static_cast<enum_::type>(0) and ending before enum_::size (which means that enum_::size is not included).

This macro generates a for loop that hides some enum casting behind its scenes. It is important that the enumerators in the enum are contiguous as in the following example:

namespace my_enum
{
enum type // the nested name type is important
{
enum0,
enum1,
enum2,
enum3,
size // note the additional size, which is needed for FOREACH_ENUMERATOR to work
};
}
// Iterates over (enum0, enum1, enum2, enum3)
// Prints 0, 1, 2, 3
loop_var,
my_enum
)
print_enum_value(
loop_var
);
See Also
FCPPT_FOREACH_ENUMERATOR_START_END
#define FCPPT_FOREACH_ENUMERATOR_START (   name,
  enum_,
  start 
)
Value:
name,\
enum_,\
start,\
)

Iterates over an enum with a designated start.

Iterates over the type enum_::type, giving the loop variable the name name, starting at the enumerator start and ending before enum_::size (which means that enum_::size is not included).

This macro generates a for loop that hides some enum casting behind its scenes. It is important that the enumerators in the enum are contiguous as in the following example:

namespace my_enum
{
enum type // the nested name type is important
{
enum0,
enum1,
enum2,
enum3,
size // note the additional size, which is needed for FOREACH_ENUMERATOR to work
};
}
// Iterates over (enum1, enum2, enum3).
// Prints 1, 2, 3
loop_var,
my_enum,
my_enum::enum1
)
print_enum_value(
loop_var
);
See Also
FCPPT_FOREACH_ENUMERATOR_START_END
#define FCPPT_FOREACH_ENUMERATOR_START_END (   name,
  enum_,
  start,
  end 
)
Value:
for(\
enum_::type name = start;\
name < end;\
name = static_cast<enum_::type>(name + 1)\
)

Iterates over an enum with designated start and end values.

Iterates over the type enum_::type, giving the loop variable the name name, starting at the enumerator start and ending before end (which means end itself is not included).

This macro generates a for loop that hides some enum casting behind its scenes. It is important that the enumerators in the enum are contiguous as in the following example:

namespace my_enum
{
enum type // the nested name type is important
{
enum0,
enum1,
enum2,
enum3,
size // note the additional size, which is needed for FOREACH_ENUMERATOR to work
};
}
// Iterates over (enum2, enum3).
// Prints 1, 2
loop_var,
my_enum,
my_enum::enum1,
my_enum::enum3
)
print_enum_value(
loop_var
);
See Also
FCPPT_FOREACH_ENUMERATOR
FCPPT_FOREACH_ENUMERATOR_START_END
#define FCPPT_NONASSIGNABLE (   classname)
Value:
private: \
classname &operator=(classname const &)

Makes a class nonassignable.

Makes the class called classname nonassignable. The macro must be placed inside the class definition and called with the name of the class. It makes the assignment operator inaccessible.

Often, classes are not assignable but copyable, for example when they contain constant elements or references. To make it clear to the compiler and to the users of the class that it should not be possible to assign it, this macro should be used.

// The following is not assignable because it has a reference and a constant
class test_nonassignable
{
test_nonassignable
);
public:
test_nonassignable(
int &_some_ref,
float const _some_float
)
:
some_ref_(
_some_ref
),
float_constant_(
_some_float
)
{
}
private:
int &some_ref_;
float const float_constant_;
};
Parameters
classnameThe name of the class
Note
The macro leaves a private scope behind.
#define FCPPT_NONCOPYABLE (   classname)
Value:
private: \
classname(classname const &); \
classname &operator=(classname const &)

Makes a class noncopyable.

Makes the class called classname noncopyable. The macro must be placed inside the class definition and called with the name of the class. It makes the copy constructor and the assignment operator inaccessible.

By default, C++ generates a copy constructor and assignment operator for every class where this is possible (when its elements can all be copy constructed and assigned). However, this is not a good default in a lot of situations where it can be dangerous to copy an object:

  • If a class has already a non trivial constructor or destructor, for example when it has to manage dynamic resources or it implements some scoped behaviour, like for example a scoped_lock.

  • If an object of a class in a hierarchy gets copied, the object will be sliced and not cloned. Therefore, it is often advisable to make all classes in a hierarchy noncopyable.
class test_noncopyable
{
test_noncopyable
);
public:
test_noncopyable()
:
ptr_(new int(42))
{
}
~test_noncopyable()
{
delete ptr_;
}
private:
int *ptr_;
};
Parameters
classnameThe name of the class
Note
The macro leaves a private scope behind.
#define FCPPT_SAFE_BOOL (   classname)
Value:
private: \
\
typedef void (classname::*bool_type)() const; \
\
void \
safe_bool_reference_function() const \
{}\
\
public: \
\
operator bool_type() const \
{\
return \
classname::boolean_test()\
?\
&classname::safe_bool_reference_function \
:\
bool_type() \
;\
}

Implements the safe bool idiom for a class.

Implements the safe bool idiom for a class called classname. The macro must be placed inside the class definition and called with the name of the class.

It generates a member function that makes it possible to use objects of this class as a condition like in if(object). However, objects of this class are not convertible to bool or any other integral types.

To tell when the expression object should evaluate to true, the member function bool boolean_test() const must be implemented.

In the following example, a class is defined using the safe bool idiom:

class my_class
{
// Implement the safe bool idiom for this class
my_class
)
private:
// We must also implement this function to tell when the object is
// supposed to be true.
bool
boolean_test() const
{
return isset_;
}
public:
explicit
my_class(
bool const _isset
)
:
isset_(
_isset
)
{
}
private:
bool isset_;
};

An object of this class can then be used in an if statement:

my_class test(
true
);
if(
test
)
std::cout << "test is set\n";
Parameters
classnameThe name of the class
Note
The macro leaves a public scope behind.
#define FCPPT_STATIC_ASSERT_EXPRESSION (   cond)
Value:
static_cast<\
void\
>(\
::fcppt::detail::static_assert_failure<\
static_cast<\
bool\
>(\
cond\
)\
>() \
)

Assert at compile time that a condition is true, using an expression.

This macro asserts at compile that cond is true, using an expression. It is therefore preferred to use this macro to assert a condition in a function. It will not generate any typedefs that might lead to warnings.

#define FCPPT_STATIC_ASSERT_STATEMENT (   cond)
Value:
typedef \
::fcppt::detail::static_assert_test<\
sizeof(\
::fcppt::detail::static_assert_failure<\
static_cast<\
bool\
>(\
cond\
)\
>\
)\
>\
BOOST_PP_CAT(\
fcppt_static_assert_typedef_,\
__LINE__\
)

Assert at compile time that a condition is true, using a statement.

This macro asserts at compile that cond is true, using a statement. It must be used whenever an expression is not allowed.

Function Documentation

fcppt::null_ptr_t const fcppt::null_ptr ( )
inline

Partial replacement for C++11's nullptr.

This function is a partial replacement for C++11's nullptr. It can be used for pointers to objects and pointers to functions only. Member function pointers are currently not supported.

The reason for this function's existance is that the literal 0 or the macro NULL can be misused in C++ because they are convertible to a lot of other things. Here is an example:

void
pointer_func(
int *
);
void
int_func(
int
);
// false is convertible to a pointer
pointer_func(
false
);
// NULL is convertible to an int
int_func(
NULL
);
// Make the intent clear
pointer_func(
);
// Does not compile
/*
int_func(
fcppt::null_ptr()
);
*/
Returns
An object that is implicitly convertible to null pointers