DynaMix  1.3.7
A new take on polymorphism in C++
mixin.hpp File Reference
#include "global.hpp"
#include "domain.hpp"
#include "mixin_type_info.hpp"
#include "feature.hpp"
#include "preprocessor.hpp"

Macros

#define I_DYNAMIX_MIXIN_NAME_FEATURE   ::dynamix::internal::noop_feature
 
#define DYNAMIX_DECLARE_EXPORTED_MIXIN(export, mixin_type)
 Declares a dynamic library class as a mixin. More...
 
#define DYNAMIX_DECLARE_MIXIN(mixin_type)   DYNAMIX_DECLARE_EXPORTED_MIXIN(I_DYNAMIX_PP_EMPTY(), mixin_type)
 Declares a class as a mixin. More...
 
#define DYNAMIX_DEFINE_MIXIN(mixin_type, mixin_features)
 defines a mixin More...
 
#define dm_this   ::dynamix::object_of(this)
 a pointer to the owning object of the current mixin More...
 

Functions

template<typename Mixin >
object * dynamix::object_of (Mixin *mixin_addr)
 gets the object of a mixin More...
 
template<typename Mixin >
const object * dynamix::object_of (const Mixin *mixin_addr)
 gets the object of a mixin More...
 

Detailed Description

Functions and macros associated with mixin declaration, definition, and usage.

Macro Definition Documentation

#define DYNAMIX_DECLARE_EXPORTED_MIXIN (   export,
  mixin_type 
)
Value:
class mixin_type; \
extern export ::dynamix::internal::mixin_type_info& _dynamix_get_mixin_type_info(const mixin_type* m)

Declares a dynamic library class as a mixin.

Parameters
exportis the type of export. For example __declspec(dllexport) in Visual C++.
mixin_typeis the class name of the declared mixin.

When using mixins from dynamic libraries, use this macro in your headers to forward declare a class as a mixin. This may be included separately and doesn't need to be in the same header as the actual mixin class definition.

If the first parameter is empty, the macro is equivalent to DYNAMIX_DECLARE_MIXIN

Example:
1 // Assuming MY_LIB_API is a macro that expands accordingly to the
2 // export/import symbols for the compiler you're using.
3 DYNAMIX_DECLARE_EXPORTED_MIXIN(MY_LIB_API, my_mixin_type);
#define DYNAMIX_DECLARE_MIXIN (   mixin_type)    DYNAMIX_DECLARE_EXPORTED_MIXIN(I_DYNAMIX_PP_EMPTY(), mixin_type)

Declares a class as a mixin.

Parameters
mixin_typeis the class name of the declared mixin.

Call this in header files to forward declare a mixin type. This may be included separately and doesn't need to be in the same header as the actual mixin class definition.

#define DYNAMIX_DEFINE_MIXIN (   mixin_type,
  mixin_features 
)
Value:
\
/* create a function that will reference mixin_type_info_instance static registrator to guarantee its instantiation */ \
inline void _dynamix_register_mixin(mixin_type*) { ::dynamix::internal::mixin_type_info_instance<mixin_type>::registrator.unused = true; } \
/* create a mixin_type_info getter for this type */ \
::dynamix::internal::mixin_type_info& _dynamix_get_mixin_type_info(const mixin_type*) { return ::dynamix::internal::mixin_type_info_instance<mixin_type>::info(); } \
/* create a features parsing function */ \
/* features can be parsed multiple times by different parsers */ \
template <typename FeaturesParser> \
void _dynamix_parse_mixin_features(const mixin_type*, FeaturesParser& parser) { parser & I_DYNAMIX_MIXIN_NAME_FEATURE(#mixin_type) & mixin_features; }

defines a mixin

Parameters
mixin_typeis the class name of the declared mixin.
mixin_featuresthe mixin features

The macro defines a mixin in the domain. Call this once per mixin in a compilation unit (.cpp file). A good idea is, if possible, to place it in the compilation unit of the mixin class itself.

To work properly, the macro needs to see a forward declaration of the mixin (by DYNAMIX_DECLARE_MIXIN or DYNAMIX_DECLARE_EXPORED_MIXIN).

Mixin features:
The features argument is an ampersand (&) separated list of the features - messages, allocators, and others - that the mixin will support. If the features have a special suffix (as messages do), it applies here.

If the mixin has no features, then dynamix::none should be set as the last argument

Example:
1 DYNAMIX_DEFINE_MIXIN(mymixin, foo_msg & priority(1, bar_msg) & allocator<myalloc>);
2 DYNAMIX_DEFINE_MIXIN(simple_mixin, none);
#define dm_this   ::dynamix::object_of(this)

a pointer to the owning object of the current mixin

Much like this is a pointer to the current class, dm_this is a macro that, for mixins, points to the current object.

It is nothing more than dynamix::object_of(this)

Note
You can disable the definition of this macro by defining DYNAMIX_NO_DM_THIS before including the library's headers.
See also
object_of()

Function Documentation

template<typename Mixin >
object* dynamix::object_of ( Mixin *  mixin_addr)

gets the object of a mixin

Parameters
[in]mixin_addrthe address of the mixin
Returns
A pointer to the object of the given mixin

Returns the owning object of a given mixin.

Warning
This function just makes a pointer offset and cast. It will work with any object that's been given to it without a warning or an error. Even, say int*. It is a source of potential bugs if you don't make sure that the input pointer is a mixin, that is a part of an object
Example:
mymixin* ptr = myobject->get<mymixin>();
object_of(ptr); // == myobject
See also
dm_this
template<typename Mixin >
const object* dynamix::object_of ( const Mixin *  mixin_addr)

gets the object of a mixin

Parameters
[in]mixin_addrthe address of the mixin
Returns
A pointer to the object of the given mixin

Returns the owning object of a given mixin.

Warning
This function just makes a pointer offset and cast. It will work with any object that's been given to it without a warning or an error. Even, say int*. It is a source of potential bugs if you don't make sure that the input pointer is a mixin, that is a part of an object
Example:
mymixin* ptr = myobject->get<mymixin>();
object_of(ptr); // == myobject
See also
dm_this