Cogs.Core
Public Types | Public Member Functions | Private Attributes | List of all members
Cogs::Reflection::Method Class Reference

Simple method definition. More...

#include <Method.h>

Public Types

typedef void(ReflectedTarget::* MemberFunction) ()
 

Public Member Functions

template<typename Class , typename Ret , typename... Args>
 Method (const Name &name, Ret(Class::*method)(Args...))
 Creates a new method definition using the given name and the given pointer to member function.
 
 Method (const Method &other)=default
 Default copy constructor.
 
 Method (Method &&other) noexcept=default
 Default move constructor.
 
Methodoperator= (const Method &other)=default
 Default copy assignment operator.
 
Methodoperator= (Method &&other) noexcept=default
 Default move assignment operator.
 
template<typename Ret >
size_t getReturnSize () const
 Register the size of the return type.
 
template<typename... Args>
void setSizes ()
 Register argument sizes.
 
template<typename... Args>
void compareSizes () const
 Compare the sizes of a set of given arguments to the stored argument sizes.
 
template<typename Class , typename... Arg>
void call (Class *object, Arg... arg) const
 Call the method named name on the given object, with the given arguments.
 
template<typename Class , typename Ret , typename... Arg>
Ret callWithReturn (Class *object, Ret, Arg... arg) const
 Call the method named name on the given object, with the given arguments, returning a value of type Ret.
 
const NamegetName () const
 Get the name of this method.
 
template<>
size_t getReturnSize () const
 Specialized for void return type.
 
template<>
void setSizes ()
 Specialized for zero arguments.
 
template<>
void compareSizes () const
 Specialized for zero arguments.
 

Private Attributes

Name name
 Name to use when looking up this method.
 
MemberFunction function
 Pointer to member function for our method.
 
size_t numArguments
 Number of arguments given.
 
size_t returnTypeSize
 Return type size.
 
size_t argumentSizes [kMaxMethodArguments] = {}
 Types of the arguments given.
 

Detailed Description

Simple method definition.

Stores a pointer to member function object with some metadata about the arguments of the member function.

Example:

// Define a custom class
struct MyStruct
{
void foo(int i) { printf("Number: %d", i); }
};
// Specialize to set type name
template<> inline Cogs::StringView getName<MyStruct>() { return "MyStruct"; }
void test()
{
// Initialize methods
Method methods[] = {
Method(Name("foo"), &MyStruct::foo),
};
// Create and retrieve the type.
auto & type = TypeDatabase::createType<MyStruct>().setMethods(methods);
// Instantiate a single MyStruct instance.
auto instance = TypeDatabase::createInstance<MyStruct>("MyStruct");
// Call the foo(int) method on our instance with the parameter 42.
type.getMethod("foo")->call(instance, 42);
}
...
Outputs the following:
Number: 42
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24

Possible future improvements include:

Definition at line 71 of file Method.h.

Member Typedef Documentation

◆ MemberFunction

typedef void(ReflectedTarget::* Cogs::Reflection::Method::MemberFunction) ()

Method call type, See C++ ISO Standard 5.2.10, p10 on how we can store different typed pointers in the same type.

Definition at line 76 of file Method.h.

Constructor & Destructor Documentation

◆ Method()

template<typename Class , typename Ret , typename... Args>
Cogs::Reflection::Method::Method ( const Name name,
Ret(Class::*)(Args...)  method 
)
inline

Creates a new method definition using the given name and the given pointer to member function.

Definition at line 83 of file Method.h.

Member Function Documentation

◆ call()

template<typename Class , typename... Arg>
void Cogs::Reflection::Method::call ( Class *  object,
Arg...  arg 
) const
inline

Call the method named name on the given object, with the given arguments.

In debug builds, the arguments passed to this method will have basic validation performed at runtime. In release builds, no checking is performed.

Definition at line 144 of file Method.h.

Referenced by Cogs::Core::EchoSounder::OctProviderSystem::update().

◆ callWithReturn()

template<typename Class , typename Ret , typename... Arg>
Ret Cogs::Reflection::Method::callWithReturn ( Class *  object,
Ret  ,
Arg...  arg 
) const
inline

Call the method named name on the given object, with the given arguments, returning a value of type Ret.

In debug builds, the arguments passed to this method will have basic validation performed at runtime. In release builds, no checking is performed.

Returns
A value of type Ret.

Definition at line 168 of file Method.h.

◆ compareSizes() [1/2]

template<typename... Args>
void Cogs::Reflection::Method::compareSizes ( ) const
inline

Compare the sizes of a set of given arguments to the stored argument sizes.

Definition at line 127 of file Method.h.

References Cogs::Reflection::kMaxMethodArguments.

◆ compareSizes() [2/2]

template<>
void Cogs::Reflection::Method::compareSizes ( ) const
inline

Specialized for zero arguments.

Definition at line 220 of file Method.h.

◆ getName()

const Name & Cogs::Reflection::Method::getName ( ) const
inline

Get the name of this method.

Definition at line 185 of file Method.h.

◆ getReturnSize() [1/2]

template<typename Ret >
size_t Cogs::Reflection::Method::getReturnSize ( ) const
inline

Register the size of the return type.

Definition at line 108 of file Method.h.

◆ getReturnSize() [2/2]

template<>
size_t Cogs::Reflection::Method::getReturnSize ( ) const
inline

Specialized for void return type.

Definition at line 206 of file Method.h.

◆ setSizes() [1/2]

template<typename... Args>
void Cogs::Reflection::Method::setSizes ( )
inline

Register argument sizes.

Definition at line 115 of file Method.h.

References Cogs::Reflection::kMaxMethodArguments.

◆ setSizes() [2/2]

template<>
void Cogs::Reflection::Method::setSizes ( )
inline

Specialized for zero arguments.

Definition at line 213 of file Method.h.

Member Data Documentation

◆ argumentSizes

size_t Cogs::Reflection::Method::argumentSizes[kMaxMethodArguments] = {}
private

Types of the arguments given.

Definition at line 201 of file Method.h.

◆ function

MemberFunction Cogs::Reflection::Method::function
private

Pointer to member function for our method.

Definition at line 192 of file Method.h.

◆ name

Name Cogs::Reflection::Method::name
private

Name to use when looking up this method.

Definition at line 189 of file Method.h.

◆ numArguments

size_t Cogs::Reflection::Method::numArguments
private

Number of arguments given.

Definition at line 195 of file Method.h.

◆ returnTypeSize

size_t Cogs::Reflection::Method::returnTypeSize
private

Return type size.

Definition at line 198 of file Method.h.


The documentation for this class was generated from the following file: