Loading...
Searching...
No Matches
Namespaces | Macros | Typedefs
constructor.h File Reference

Tools for easy creation of a constructors. More...

#include "concepts/bsdf_attribute.h"
#include "util/type_traits.h"
#include "util/reflection.h"
#include "core/args.h"

Go to the source code of this file.

Namespaces

namespace  bbm
 

Macros

#define BBM_DEFAULT_CONSTRUCTOR(ClassName)
 Helper macro to define a default constructor based on Attribute reflection.
 
#define BBM_CONSTRUCTOR(ClassName, Args, ...)
 Helper macro for declaring a constructor based on a list of bbm::arg.
 

Typedefs

template<typename TUP >
using attribute_tuple_to_args_t = typename bbm::detail::attribute_tuple_to_args_t< std::decay_t< TUP > >::type
 Convert an attribute_tuple_t to a bbm::args.
 

Detailed Description

Tools for easy creation of a constructors.

Case 1: a constructor that initializes all class attributes (requires bbm::refletion::attribute<T>)

{
// ...body...
}
#define BBM_DEFAULT_CONSTRUCTOR(ClassName)
Helper macro to define a default constructor based on Attribute reflection.
Definition: constructor.h:122

where 'foo' is the class name, and body is a custom constructor body executed after Attributes have been declared. The constructor uses bbm::args to pass the arguments.

Case 2: declare a constructor that takes a list of bbm::arg

BBM_CONSTRUCTOR(foo, args, ...)
{
// ...body...
}
#define BBM_CONSTRUCTOR(ClassName, Args,...)
Helper macro for declaring a constructor based on a list of bbm::arg.
Definition: constructor.h:167

where 'foo' is again the class name, 'args' is the name given to the bbm::args passed as constructor arguments, followed by a list of bbm::arg (or a single bbm::args).

Macro Definition Documentation

◆ BBM_CONSTRUCTOR

#define BBM_CONSTRUCTOR (   ClassName,
  Args,
  ... 
)
Value:
using constructor_args_t = bbm::add_args_t<__VA_ARGS__>; \
\
BBM_CONSTRUCTOR_FORWARD_ARGS(ClassName, constructor_args_t); \
\
inline ClassName(const constructor_args_t& Args) \
typename add_args< Ts... >::type add_args_t
Definition: args.h:240

Helper macro for declaring a constructor based on a list of bbm::arg.

Parameters
ClassName= name of constructor/class
Args= name to give to bbm::args to access the arguments.
...= lots of bbm::arg (or a defintion of a single bbm::args) used to define the arguments to the constructor

Creates a constructor (with forwarding) that takes a single bbm::args as input. Use BBM_IMPORT_ARGS to create aliases in the body of the constructor of the passed arguments. A constructor_args_t is also created with the signature of the bbm::args.

Example:

{
BBM_IMPORT_ARGS(args, a, b);
// ...body...
}
#define BBM_IMPORT_ARGS(ARGS,...)
Macro for creating aliases of the arguments in args.
Definition: args.h:605
Forward declaration of bbm::arg.
Definition: arg.h:27

This example creates a constructor which is equivalent to the regular C++ constructor foo(float a, float b).

◆ BBM_DEFAULT_CONSTRUCTOR

#define BBM_DEFAULT_CONSTRUCTOR (   ClassName)
Value:
\
inline ClassName(const constructor_args_t& args) \
{ \
auto const_cast_tuple = []<typename TUP, size_t... IDX>(TUP&& tup, std::index_sequence<IDX...>) \
{ \
return std::forward_as_tuple( const_cast<std::decay_t<decltype(std::get<IDX>(tup))>&>( std::get<IDX>(tup) )... ); \
}; \
\
const_cast_tuple(bbm::reflection::attributes(*this), std::make_index_sequence<bbm::reflection::attributes_size<decltype(*this)>>{}) = args.values(); \
constructor_post_init(); \
} \
\
BBM_CONSTRUCTOR_FORWARD_ARGS(ClassName, constructor_args_t); \
\
inline void constructor_post_init(void) \
constexpr decltype(auto) attributes(T &&t)
Definition: reflection.h:200
static constexpr size_t attributes_size
Definition: reflection.h:209
typename bbm::detail::attribute_tuple_to_args_t< std::decay_t< TUP > >::type attribute_tuple_to_args_t
Convert an attribute_tuple_t to a bbm::args.
Definition: constructor.h:90

Helper macro to define a default constructor based on Attribute reflection.

Parameters
ClassName= name of the constructor/class

Creates a constructor that initializes the reflected attributes. The constructor uses bbm::args to pass the constructor arguments. The bbm::args signature is stored in an accessible constructor_args_t alias.

The constructor will copy the corresponding bbm::args to the reflected attributes in the constructor body.

Limitations:

  • The constructor can only be defined after the Attributes (and reflection) have been declared.
  • The declaration must be followed by a function body (constructor_post_init) that is called after all Attributes have been set.
  • All other attributes/parent classes are initialized with an trivial constructor.

Example:

{
// ...body...
}