Function Binding. Special thanks to: John Edwards. Randy Gaul

Similar documents
CCReflect has a few interesting features that are quite desirable for DigiPen game projects:

C++ Important Questions with Answers

template <typename T> // unless it s an unqualified pointer struct IsPtr<T *> { enum { r = true }; };

C++ For Science and Engineering Lecture 15

Introduction to Programming (Java) 4/12

How the Adapters and Binders Work

COMP 2355 Introduction to Systems Programming

Simulating Partial Specialization

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

C++14 Reflections Without Macros, Markup nor External Tooling

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Advanced Systems Programming

Structured bindings with polymorphic lambas

Introduction to C: Pointers

Object Oriented Programming. Solved MCQs - Part 2

An Introduction to Template Metaprogramming

An application: foreign function bindings

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

Design Patterns in C++

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

Reminder: compiling & linking

Design Patterns in C++

1d: tests knowing about bitwise fields and union/struct differences.

Inheritance and Overloading. Week 11

Advanced C++ Topics. Alexander Warg, 2017

Introduction to C++11 and its use inside Qt

G52CPP C++ Programming Lecture 18

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

A brief introduction to C++

EL2310 Scientific Programming

G52CPP C++ Programming Lecture 9

STRUCTURING OF PROGRAM

Tokens, Expressions and Control Structures

The Nifty Way to Call Hell from Heaven ANDREAS LÖSCHER AND KONSTANTINOS SAGONAS UPPSAL A UNIVERSIT Y

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Module Contact: Dr Anthony J. Bagnall, CMP Copyright of the University of East Anglia Version 2

ENERGY 211 / CME 211. Functions

Chapter 15 - C++ As A "Better C"

Programming in C - Part 2

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

COEN244: Class & function templates

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Array. Prepared By - Rifat Shahriyar

Exception Handling Alternatives (Part 2)

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Programming in C++ 4. The lexical basis of C++

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

MODERN AND LUCID C++ ADVANCED

CS240: Programming in C

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

EL2310 Scientific Programming

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Auto - a necessary evil?

Digging into the GAT API

P1267R0: Custom Constraint Diagnostics

} else if( Ellipse *e = dynamic_cast<ellipse *>(shape) ) { } else if( Square *s = dynamic_cast<square *>(shape) ) {

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Metaprogramming. Concepts of Programming Languages. Alexander Schramm. 2. November Institut für Softwaretechnik und Programmiersprachen

Introduce C# as Object Oriented programming language. Explain, tokens,

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Appendix B Boost.Python

Making New Pseudo-Languages with C++

This manual is for Libffi, a portable foreign-function interface library. Copyright c 2008, 2010, 2011 Red Hat, Inc. Permission is granted to copy,

C++ Programming: Polymorphism

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Instantiation of Template class

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Class, Variable, Constructor, Object, Method Questions

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

A Brief Introduction to Using LLVM. Nick Sumner

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

C++ Primer for CS175

GridKa School 2013: Effective Analysis C++ Templates

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CS242: Object-Oriented Design and Programming

Value Semantics and Concept Based Polymorphism Sean Parent Principal Scientist Adobe Systems Incorporated. All Rights Reserved.

Object Reference and Memory Allocation. Questions:

Procedures, Parameters, Values and Variables. Steven R. Bagley

Fast Introduction to Object Oriented Programming and C++

Short Notes of CS201

Exercise Session 2 Simon Gerber

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Interview Questions of C++

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Variables. Data Types.

Final CSE 131B Spring 2004

CSE 333 Midterm Exam 5/10/13

CS201 - Introduction to Programming Glossary By

void setup(){ void loop() { The above setup works, however the function is limited in the fact it can not be reused easily. To make the code more gene

Implementation of Breakpoints in GDB for Sim-nML based Architectures

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

GEA 2017, Week 4. February 21, 2017

Modern and Lucid C++ Advanced for Professional Programmers. Part 12 Advanced Library Design. Department I - C Plus Plus Advanced

C Programming Language Review and Dissection III

I m sure you have been annoyed at least once by having to type out types like this:

CS250 Final Review Questions

The Cut and Thrust of CUDA

Transcription:

Function Binding Special thanks to: John Edwards Randy Gaul

Introspection Generic Variable Function Binding Simple Case Uses Demo Overview Function Binding

Introspection Run-time knows little about types Introspection saves compile-time data Generic code

Introspection Questions we can Answer What methods does this class have What members are in this struct What type of data is this Can I convert type A to type B How can I send this type to script How can I send this type to a file How can I create this type from file What sort of messaging can I achieve

Introspection Register your Data We want tables of data Type Name - "int" Size - 4 bytes Members - none Methods - none Inherits from - none

Introspection Register your Data Store data in a struct struct TypeInfo { const char *name; int size; TypeInfo *inheritsfrom; Method *methods; Members *members; };

Introspection Register your Data Use a macro to register info into your tables void RegisterSomeData( void ) { REGISTER_TYPE( int ); REGISTER_TYPE( SomeStruct ); REGISTER_MEMBER( SomeStruct, x ); }

Magic Macros! Introspection Register your Data #define REGISTER_TYPE( T ) \ Introspection::Get( )->RegisterType<T>( sizeof( T ), #T )

Introspection Register your Data Introspection singleton struct IntrospectionManager { static IntrospectionManager *Get( void ) { static IntrospectionManager instance; return &instance; } template <typename T> TypeInfo *RegisterType( int size, char *name ) { static TypeInfo instance; TypeInfo *typeinfo = &instance; m_typemap.add( name, typeinfo ); return typeinfo; } private: HashTable<const char *, TypeInfo *> m_typemap; };

Introspection Retrieve Data Retrieve data from Singleton Make single Get for a type Wrap this in a GET_TYPE( T ) macro template <typename T> const TypeInfo *IntrospectionManager::GetType( void ) const { static TypeInfo instance; return &instance; }

Introspection The Macros Might want something like these #define REGISTER( T ) #define TYPE( T ) #define TYPE_OF( OBJECT ) #define TYPE_OF_MEMBER( T, MEMBER ) #define STR_TYPE( STR ) #define ADD_MEMBER( T, MEMBER ) #define SET_SERIALIZER( T, SERIALIZER ) #define SET_DESERIALIZER( T, DESERIALIZER )

Variable Generic Variable needed for generic code We want to do this: Variable v; int x = 5; v = x; v.tolua( ); // Send 5 to Lua v.serialize( file ); // write 5 in a file // Valid SomeStruct s; v = s;

TypeInfo * Void * Variable

Variable GetValue template <typename T> T& Variable::GetValue( void ) { return *((T *)m_data )); }

Function Binding Lets create generic Function Lets pass it Variables as arguments

Function Binding Step 1: Lets create generic Function Store function pointer as template type template <typename FunctionType, FunctionType FunctionPtr> Function BuildFunction( void (*fn)( void ) ) { return Function; // Not finished! }

Function Binding Function needs function pointer template type Pass address of Call Helper template <typename FunctionType, FunctionType FunctionPtr> Function BuildFunction( void (*fn)( void ) ) { return Function( &Call<FunctionType, FunctionPtr> ); }

Function Binding Call helper calls the real function pointer template <typename FunctionType, FunctionType FunctionPtr> void Call( Variable *ret, Variable *args, unsigned argcount ) { assert( argcount == 0 ); } (*FunctionPtr)( );

Can now call void (*)( void ) Function Binding Function f = BuildFunction<decltype( &Func ), &Func>( &Func ); f( ); // calls Func

What about arguments? Make some overloads Function Binding template <typename FunctionType, FunctionType FunctionPtr, typename Arg1> Function BuildFunction( void (*fn)( Arg1 ) ) { return Function( &Call<FunctionType, FunctionPtr, Arg1> ); } template <typename FunctionType, FunctionType FunctionPtr, typename Arg1> void Call( Variable *ret, Variable *args, unsigned argcount ) { assert( argcount == 0 ); } (*FunctionPtr)( args[0].getvalue<arg1>( ) );

What about more arguments? Make some more overloads Function Binding

What about non-void return? Function Binding

What about non-void return? MAKE SOME MORE OVERLOADS Function Binding

Function Binding My final function binding: 1783 lines in header Screenshot is about 1/10 th of file

Properties!! Function for Get/Set methods Function Binding - Uses

Function Binding - Uses Visual scripting Attach and pass around Function objects

Bind functions to call from script Function Binding - Uses REGISTER_FUNCTION( glbegin ); REGISTER_FUNCTION( glend ); // in script glbegin( blah );... // Render a beautiful scene glend( );

Messaging omg Function Binding - Uses void Object::SendMessage( Variable v1 ); void Object::SendMessage( Variable v1, Variable v2 ); void Object::SendMessage( Variable v1, Variable v2, Variable v3 );

Function Binding - Uses Listeners Messaging even better super omg void Object::Subscribe( Function& f ); void Object::PostMSG( MSG_ID id ); void Object::PostMSG( MSG_ID id, Variable v1 ); void Object::PostMSG( MSG_ID id, Variable v1, Variable v2 );

And more..? Function Binding - Uses

Demo

Questions?

Resources Template Metaprogramming John Edwards My website Sean middleditche s website Engine Arch online files (Sean made some demos)