SlideShare a Scribd company logo
1 of 20
www.luxoft.com
Alexey Tsoy
Senior Developer
Meta programming in C++
www.luxoft.com
Introduction
Metaprogramming in C++ (Andrei Alexandrescu, 2002).
Templates:
• one of the most powerful part of C++
• quite new technology (just only 18 years old)
www.luxoft.comwww.luxoft.com
RPC diagram
www.luxoft.comwww.luxoft.com
struct calc_interface {
struct functions {
double add(double, double);
int eval(std::string);
};
struct signals {
void expr_evaluated(int, double);
};
};
Interface definition: Ideal case
www.luxoft.com
Example of usage
struct calc_server: server_t<calc_interface>
{
double add(double a, double b) override
{ return a + b; };
int eval(string expr) override {
static int id(0); ++id;
new std::thread( [this, expr, id] () {
sleep(100);
this->expr_evaluated(expr, 12345);
} );
return id;
}
};
client_t<calc_interface> client;
// subscribe to the signal ..
client.expr_evaluated(
[] (int id, string expr, double res)
{ cout << expr << "→" << res; }
);
// execute functions ..
cout << client.add(1,2) << endl;
client.add(1,2,
[] (double res) { cout << res << endl; })
www.luxoft.comwww.luxoft.com
struct calc_interface
{
RPC_FUNCTION(double(double, double), add);
RPC_FUNCTION(int(std::string), eval);
RPC_SIGNAL (void(int, double), expr_evaluated);
};
Interface definition: implemented case
www.luxoft.com
Usage
server_t<calc_interface> server(transpotr);
server_t::initialize(server)
.add_handler<calc_interface::add> (
[] (double a, double b) { return a + b;} )
.add_handler<calc_interface::eval> (
[] (string expr) → int {
static int id(0); ++id;
new std::thread( [this, expr, id] () {
sleep(100);
server.emit<expr_evaluated>(expr, 12345);
} );
return id;
});
client_t<calc_interface> client(transpotr);
// subscribe to the signal ..
client.subscribe<client_t::expr_evaluated>(
[] (int id, string expr, double res)
{ cout << expr << "→" << res; }
);
// execute functions ..
cout << client.invoke<add>(1,2) << endl;
// async invoke
client.invoke(1,2, [] (double res) { cout << res << endl;
})
www.luxoft.com
What should be taken into account
• Wrong function parameters. e.g. the following should cause a compile time
errors
– client.invoke<add>(“hello” , 123) or client.invoke<add>(1) or
client.invoke<add>(1, 2, 3)
• Execution of nonexistent function
– interface_1 { RPC_FN (void (int), fn1) };
– interface_2 { RPC_FN (void (double), fn2) };
– client_t<interface_1>().invoke<interface_2::fn2>(5.);
www.luxoft.com
Implementation
struct calc_interface
{
// RPC_FUNCTION(double(double, double), add);
struct add: meta_function_t<double (double, double), true>
{ static const char* uniq_name() { return “add”; } };
//RPC_FUNCTION(int(std::string), eval);
struct eval: meta_function_t<double (double, double), true>
{ static const char* uniq_name() { return “eval”; };
//RPC_SIGNAL (void(int, double), expr_evaluated);
struct expr_evaluated: meta_function_t<double (double, double), false>
{ static const char* uniq_name() { return “expr_evaluated”; };
};
meta_function_t < R (Arg0, …, ArgN) , bool> :: result_type → R;
meta_function_t < R (Arg0, …, ArgN) , bool> :: args_count → N;
meta_function_t < R (Arg0, …, ArgN) , bool> :: arg<0>::type → Arg0;
...
meta_function_t < R (Arg0, …, ArgN) , bool> :: arg<N>::type → ArgN;
meta_function_t < R (Arg0, …, ArgN) , bool> ::is_function → bool
www.luxoft.comwww.luxoft.com
namespace details {
template<typename... Args>
struct args_count
{ enum { value = 0 }; };
template<typename A1, typename ... Args>
struct args_count<A1, Args...>
{ enum { value = 1 + args_count<Args...>::value }; };}
cout << detail::args_count < >::value ; → 0
cout << detail::args_count < int >::value ; → 1
cout << detail::args_count < int , float>::value ; → 2
www.luxoft.comwww.luxoft.com
namepsace detail {
template<unsigned int N, typename ...Args>
struct arg;
template<typename A0, typename ...Args>
struct arg<0, A0, Args...>
{ typedef A0 type; };
template<unsigned int N, typename A0, typename ...Args>
struct arg<N, A0, Args...>
{ typedef typename arg<N - 1, Args...>::type type; };
}
detail::arg<0, int>::type → int
detail::arg<0, int, double>::type → int
detail::arg<1, int, double>::type → double
www.luxoft.comwww.luxoft.com
template<typename FnSygn, bool>
struct meta_function;
template<typename UniqType, bool isFn, typename R, typename ...Args>
struct meta_function<R(Args…), isFn>
{
typedef R result_type;
enum { args_count = detail::args_count<Args...>::value };
template<unsigned int N>
struct arg
{ typedef typename detail::arg<N, Args...>::type type; };
enum { is_function = isFn };
};
www.luxoft.com
client.invoke
// calc_interface
struct add: meta_function_t<double (double, double), true> { … };
// client_t<calc_interface> client:
…
auto result = client.invoke<add> ( … ) :
1) check arguments
2) serialize arguments
3) serialize function ID
4) send data
5) wait for response
6) deserialize and return result
www.luxoft.com
client.invoke
template< typename MetaFn, typename ...Args>
typename MetaFn::result_type invoke
( Args const&... args)
{
check if <MetaFn> declared in <calc_interface> ;
static_assert(MetaFn::is_function == true);
static_assert(MetaFn::args_count == detail::args_count<Args...>::value);
typename MetaFn::arg<0>::type a0 = get<0>(args...);
...
typename MetaFn::arg<N>::type a1 = get<N>(args...);
// ---->>>
auto ostream = transport.get_ostream( );
ostream << MetaFn::uniq_name()
<< arg0 << … << argN;
int sequence_id = transport.send(ostream);
transport_wait_for_response(sequence_id);
auto istream = transport.get_istream(sequence_id);
MetaFn::result_type result;
istream >> result;
return result;
}
www.luxoft.com
client.invoke
template<typename MetaFn, unsigned int N, typename IStream, typename ...Args>
void serrialize_all(IStream & , Args ...args) { };
template<typename MetaFn, unsigned int N, typename IStream, typename Arg0, typename ...Args>
void serrialize_all(IStream & stream, Arg0 const& arg0,Args ...args)
{
typename typedef MetaFn::arg<N>::type arg_to_send = arg0;
stream << arg0;
serrialize_all<MetaFn, N + 1> (stream, args...);
}
www.luxoft.com
client.invoke
template< typename MetaFn, typename ...Args>
typename MetaFn::result_type invoke ( Args const&... args )
{
check if <MetaFn> declared in <calc_interface> ;
static_assert(MetaFn::is_function == true);
static_assert(MetaFn::args_count == detail::args_count<Args...>::value);
auto ostream = transport.get_ostream( );
serrialize_all<MetaFn,0>(ostream, MetaFn::uniq_name(), args...);
int sequence_id = transport.send(ostream);
transport_wait_for_response(sequence_id);
auto istream = transport.get_istream(sequence_id);
MetaFn::result_type result;
istream >> result;
return result;
}
www.luxoft.com
Exists or not ?
struct calc_interface {
long __registrate_meta_type(…);
// RPC_FUNCTION(double(double, double), add);
struct add: meta_function_t<double (double, double), true>
{ static const char* uniq_name() { return “add”; } };
char __registrate_meta_type(add );
...
char __registrate_meta_type(expr_evaluated);
};
template< typename Interface, typename MetaFn> struct is_present {
enum { value = sizeof( ((Interface*)0)→
__registrate_meta_type(MetaFn())) == 1 ? 1 : 0 }; };
std::cout << is_present<calc_interface,
calc_interface::add>::value → 1
std::cout << is_present<calc_interface,
calc_interface::eval>::value → 1
std::cout << is_present<calc_interface, int>::value → 0
www.luxoft.com
Client.invoke – final version
//client_t<calc_interface> client;
// template<Interface> client_t { typedef Interface interface; };
template< typename MetaFn, typename ...Args>0
typename MetaFn::result_type invoke( Args const&... args ) {
static_assert(is_present<interface, MetaFn> :: value == 1);
static_assert(MetaFn::is_function == true);
static_assert(MetaFn::args_count == detail::args_count<Args...>::value);
auto ostream = transport.get_ostream( );
serrialize_all<MetaFn,0>(ostream, MetaFn::uniq_name(), args...);
int sequence_id = transport.send(ostream);
transport_wait_for_response(sequence_id);
auto istream = transport.get_istream(sequence_id);
MetaFn::result_type result;
istream >> result;
return result;
}
www.luxoft.comwww.luxoft.com
Tsoy Alexey
Mails:
atsoy@luxoft.com
tsoy.alexey@gmail.com
Contact details
www.luxoft.com
Thank you

More Related Content

What's hot

Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)BoneyGawande
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical filePranav Ghildiyal
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9Andrey Zakharevich
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Anton Bikineev
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196Mahmoud Samir Fayed
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a BossBob Tiernay
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184Mahmoud Samir Fayed
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架jeffz
 
The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210Mahmoud Samir Fayed
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019corehard_by
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQKnoldus Inc.
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)BoneyGawande
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)jeffz
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Datagreenwop
 

What's hot (20)

Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
C program
C programC program
C program
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
C programs
C programsC programs
C programs
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQ
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Jason parsing
Jason parsingJason parsing
Jason parsing
 
java sockets
 java sockets java sockets
java sockets
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 

Similar to Meta Programming C++ RPC

Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimizedWoody Pewitt
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedPascal-Louis Perez
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用Felinx Lee
 
Apache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationApache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationBartosz Konieczny
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript IntroductionDmitry Sheiko
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012Sandeep Joshi
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheeltcurdt
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperChester Chen
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfsktambifortune
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfsnewfashion
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsPVS-Studio
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsphanleson
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsleminhvuong
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 ReviewSperasoft
 
RESTful API using scalaz (3)
RESTful API using scalaz (3)RESTful API using scalaz (3)
RESTful API using scalaz (3)Yeshwanth Kumar
 

Similar to Meta Programming C++ RPC (20)

Bind me if you can
Bind me if you canBind me if you can
Bind me if you can
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 
JPA 2.0
JPA 2.0JPA 2.0
JPA 2.0
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Apache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationApache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customization
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code Contracts
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
ECMAScript 6 Review
ECMAScript 6 ReviewECMAScript 6 Review
ECMAScript 6 Review
 
WD programs descriptions.docx
WD programs descriptions.docxWD programs descriptions.docx
WD programs descriptions.docx
 
RESTful API using scalaz (3)
RESTful API using scalaz (3)RESTful API using scalaz (3)
RESTful API using scalaz (3)
 

More from LogeekNightUkraine

Autonomous driving on your developer pc. technologies, approaches, future
Autonomous driving on your developer pc. technologies, approaches, futureAutonomous driving on your developer pc. technologies, approaches, future
Autonomous driving on your developer pc. technologies, approaches, futureLogeekNightUkraine
 
Orkhan Gasimov "High Performance System Design"
Orkhan Gasimov "High Performance System Design" Orkhan Gasimov "High Performance System Design"
Orkhan Gasimov "High Performance System Design" LogeekNightUkraine
 
Vitalii Korzh "Managed Workflows or How to Master Data"
Vitalii Korzh "Managed Workflows or How to Master Data" Vitalii Korzh "Managed Workflows or How to Master Data"
Vitalii Korzh "Managed Workflows or How to Master Data" LogeekNightUkraine
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"LogeekNightUkraine
 
Oleksii Kuchuk "Reading gauge values with open cv imgproc"
Oleksii Kuchuk "Reading gauge values with open cv imgproc"Oleksii Kuchuk "Reading gauge values with open cv imgproc"
Oleksii Kuchuk "Reading gauge values with open cv imgproc"LogeekNightUkraine
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...LogeekNightUkraine
 
Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"LogeekNightUkraine
 
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"LogeekNightUkraine
 
Iurii Antykhovych "Java and performance tools and toys"
Iurii Antykhovych "Java and performance tools and toys"Iurii Antykhovych "Java and performance tools and toys"
Iurii Antykhovych "Java and performance tools and toys"LogeekNightUkraine
 
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...LogeekNightUkraine
 
Aleksandr Kutsan "Managing Dependencies in C++"
Aleksandr Kutsan "Managing Dependencies in C++"Aleksandr Kutsan "Managing Dependencies in C++"
Aleksandr Kutsan "Managing Dependencies in C++"LogeekNightUkraine
 
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"LogeekNightUkraine
 
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"
Alexandr Golyak, Nikolay Chertkov  "Automotive Testing vs Test Automatio"Alexandr Golyak, Nikolay Chertkov  "Automotive Testing vs Test Automatio"
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"LogeekNightUkraine
 
Michal Kordas "Docker: Good, Bad or Both"
Michal Kordas "Docker: Good, Bad or Both"Michal Kordas "Docker: Good, Bad or Both"
Michal Kordas "Docker: Good, Bad or Both"LogeekNightUkraine
 
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"LogeekNightUkraine
 
Shestakov Illia "The Sandbox Theory"
Shestakov Illia "The Sandbox Theory"Shestakov Illia "The Sandbox Theory"
Shestakov Illia "The Sandbox Theory"LogeekNightUkraine
 
Dmytro Kochergin “Autotest with CYPRESS”
Dmytro Kochergin “Autotest with CYPRESS”Dmytro Kochergin “Autotest with CYPRESS”
Dmytro Kochergin “Autotest with CYPRESS”LogeekNightUkraine
 
Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Ivan Dryzhyruk “Ducks Don’t Like Bugs”Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Ivan Dryzhyruk “Ducks Don’t Like Bugs”LogeekNightUkraine
 

More from LogeekNightUkraine (20)

Face recognition with c++
Face recognition with c++ Face recognition with c++
Face recognition with c++
 
C++20 features
C++20 features C++20 features
C++20 features
 
Autonomous driving on your developer pc. technologies, approaches, future
Autonomous driving on your developer pc. technologies, approaches, futureAutonomous driving on your developer pc. technologies, approaches, future
Autonomous driving on your developer pc. technologies, approaches, future
 
Orkhan Gasimov "High Performance System Design"
Orkhan Gasimov "High Performance System Design" Orkhan Gasimov "High Performance System Design"
Orkhan Gasimov "High Performance System Design"
 
Vitalii Korzh "Managed Workflows or How to Master Data"
Vitalii Korzh "Managed Workflows or How to Master Data" Vitalii Korzh "Managed Workflows or How to Master Data"
Vitalii Korzh "Managed Workflows or How to Master Data"
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"
 
Oleksii Kuchuk "Reading gauge values with open cv imgproc"
Oleksii Kuchuk "Reading gauge values with open cv imgproc"Oleksii Kuchuk "Reading gauge values with open cv imgproc"
Oleksii Kuchuk "Reading gauge values with open cv imgproc"
 
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...Oleksandr Kutsan "Using katai struct to describe the process of working with ...
Oleksandr Kutsan "Using katai struct to describe the process of working with ...
 
Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"Pavlo Zhdanov "Mastering solid and base principles for software design"
Pavlo Zhdanov "Mastering solid and base principles for software design"
 
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
Serhii Zemlianyi "Error Retries with Exponential Backoff Using RabbitMQ"
 
Iurii Antykhovych "Java and performance tools and toys"
Iurii Antykhovych "Java and performance tools and toys"Iurii Antykhovych "Java and performance tools and toys"
Iurii Antykhovych "Java and performance tools and toys"
 
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
Eugene Bova "Dapr (Distributed Application Runtime) in a Microservices Archit...
 
Aleksandr Kutsan "Managing Dependencies in C++"
Aleksandr Kutsan "Managing Dependencies in C++"Aleksandr Kutsan "Managing Dependencies in C++"
Aleksandr Kutsan "Managing Dependencies in C++"
 
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
Yevhen Tatarynov "My .NET Application Allocates too Much Memory. What Can I Do?"
 
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"
Alexandr Golyak, Nikolay Chertkov  "Automotive Testing vs Test Automatio"Alexandr Golyak, Nikolay Chertkov  "Automotive Testing vs Test Automatio"
Alexandr Golyak, Nikolay Chertkov "Automotive Testing vs Test Automatio"
 
Michal Kordas "Docker: Good, Bad or Both"
Michal Kordas "Docker: Good, Bad or Both"Michal Kordas "Docker: Good, Bad or Both"
Michal Kordas "Docker: Good, Bad or Both"
 
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
Kolomiyets Dmytro "Dealing with Multiple Caches, When Developing Microservices"
 
Shestakov Illia "The Sandbox Theory"
Shestakov Illia "The Sandbox Theory"Shestakov Illia "The Sandbox Theory"
Shestakov Illia "The Sandbox Theory"
 
Dmytro Kochergin “Autotest with CYPRESS”
Dmytro Kochergin “Autotest with CYPRESS”Dmytro Kochergin “Autotest with CYPRESS”
Dmytro Kochergin “Autotest with CYPRESS”
 
Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Ivan Dryzhyruk “Ducks Don’t Like Bugs”Ivan Dryzhyruk “Ducks Don’t Like Bugs”
Ivan Dryzhyruk “Ducks Don’t Like Bugs”
 

Recently uploaded

原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证
原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证
原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证jjrehjwj11gg
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptDineshKumar4165
 
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样umasea
 
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full NightCall Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024AHOhOops1
 
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxUNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxDineshKumar4165
 
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样gfghbihg
 
What Causes DPF Failure In VW Golf Cars & How Can They Be Prevented
What Causes DPF Failure In VW Golf Cars & How Can They Be PreventedWhat Causes DPF Failure In VW Golf Cars & How Can They Be Prevented
What Causes DPF Failure In VW Golf Cars & How Can They Be PreventedAutobahn Automotive Service
 
907MTAMount Coventry University Bachelor's Diploma in Engineering
907MTAMount Coventry University Bachelor's Diploma in Engineering907MTAMount Coventry University Bachelor's Diploma in Engineering
907MTAMount Coventry University Bachelor's Diploma in EngineeringFi sss
 
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxUNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxDineshKumar4165
 
Call Girls in Karachi | +923081633338 | Karachi Call Girls
Call Girls in Karachi  | +923081633338 | Karachi Call GirlsCall Girls in Karachi  | +923081633338 | Karachi Call Girls
Call Girls in Karachi | +923081633338 | Karachi Call GirlsAyesha Khan
 
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样whjjkkk
 
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERUNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERDineshKumar4165
 
Dubai Call Girls Services Call 09900000000
Dubai Call Girls Services Call 09900000000Dubai Call Girls Services Call 09900000000
Dubai Call Girls Services Call 09900000000Komal Khan
 
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERUNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERunosafeads
 
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCRsoniya singh
 
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Digamma / CertiCon Company Presentation
Digamma / CertiCon Company  PresentationDigamma / CertiCon Company  Presentation
Digamma / CertiCon Company PresentationMihajloManjak
 
Digamma - CertiCon Team Skills and Qualifications
Digamma - CertiCon Team Skills and QualificationsDigamma - CertiCon Team Skills and Qualifications
Digamma - CertiCon Team Skills and QualificationsMihajloManjak
 

Recently uploaded (20)

原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证
原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证
原版工艺美国普林斯顿大学毕业证Princeton毕业证成绩单修改留信学历认证
 
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.pptUNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
UNIT-1-VEHICLE STRUCTURE AND ENGINES.ppt
 
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样
办理学位证(MLU文凭证书)哈勒 维滕贝格大学毕业证成绩单原版一模一样
 
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full NightCall Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
Call Girls Vastrapur 7397865700 Ridhima Hire Me Full Night
 
BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024BLUE VEHICLES the kids picture show 2024
BLUE VEHICLES the kids picture show 2024
 
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptxUNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
UNIT-IV-STEERING, BRAKES AND SUSPENSION SYSTEMS.pptx
 
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样
(办理学位证)(Toledo毕业证)托莱多大学毕业证成绩单修改留信学历认证原版一模一样
 
What Causes DPF Failure In VW Golf Cars & How Can They Be Prevented
What Causes DPF Failure In VW Golf Cars & How Can They Be PreventedWhat Causes DPF Failure In VW Golf Cars & How Can They Be Prevented
What Causes DPF Failure In VW Golf Cars & How Can They Be Prevented
 
907MTAMount Coventry University Bachelor's Diploma in Engineering
907MTAMount Coventry University Bachelor's Diploma in Engineering907MTAMount Coventry University Bachelor's Diploma in Engineering
907MTAMount Coventry University Bachelor's Diploma in Engineering
 
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptxUNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
UNIT-V-ELECTRIC AND HYBRID VEHICLES.pptx
 
Call Girls in Karachi | +923081633338 | Karachi Call Girls
Call Girls in Karachi  | +923081633338 | Karachi Call GirlsCall Girls in Karachi  | +923081633338 | Karachi Call Girls
Call Girls in Karachi | +923081633338 | Karachi Call Girls
 
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样
(办理学位证)墨尔本大学毕业证(Unimelb毕业证书)成绩单留信学历认证原版一模一样
 
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGERUNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
UNIT-II-ENGINE AUXILIARY SYSTEMS &TURBOCHARGER
 
Dubai Call Girls Services Call 09900000000
Dubai Call Girls Services Call 09900000000Dubai Call Girls Services Call 09900000000
Dubai Call Girls Services Call 09900000000
 
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in  G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in G.T.B. Nagar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHERUNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
UNOSAFE ELEVATOR PRIVATE LTD BANGALORE BROUCHER
 
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR
(8264348440) 🔝 Call Girls In Shaheen Bagh 🔝 Delhi NCR
 
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制宾州州立大学毕业证(PSU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Digamma / CertiCon Company Presentation
Digamma / CertiCon Company  PresentationDigamma / CertiCon Company  Presentation
Digamma / CertiCon Company Presentation
 
Digamma - CertiCon Team Skills and Qualifications
Digamma - CertiCon Team Skills and QualificationsDigamma - CertiCon Team Skills and Qualifications
Digamma - CertiCon Team Skills and Qualifications
 

Meta Programming C++ RPC

  • 2. www.luxoft.com Introduction Metaprogramming in C++ (Andrei Alexandrescu, 2002). Templates: • one of the most powerful part of C++ • quite new technology (just only 18 years old)
  • 4. www.luxoft.comwww.luxoft.com struct calc_interface { struct functions { double add(double, double); int eval(std::string); }; struct signals { void expr_evaluated(int, double); }; }; Interface definition: Ideal case
  • 5. www.luxoft.com Example of usage struct calc_server: server_t<calc_interface> { double add(double a, double b) override { return a + b; }; int eval(string expr) override { static int id(0); ++id; new std::thread( [this, expr, id] () { sleep(100); this->expr_evaluated(expr, 12345); } ); return id; } }; client_t<calc_interface> client; // subscribe to the signal .. client.expr_evaluated( [] (int id, string expr, double res) { cout << expr << "→" << res; } ); // execute functions .. cout << client.add(1,2) << endl; client.add(1,2, [] (double res) { cout << res << endl; })
  • 6. www.luxoft.comwww.luxoft.com struct calc_interface { RPC_FUNCTION(double(double, double), add); RPC_FUNCTION(int(std::string), eval); RPC_SIGNAL (void(int, double), expr_evaluated); }; Interface definition: implemented case
  • 7. www.luxoft.com Usage server_t<calc_interface> server(transpotr); server_t::initialize(server) .add_handler<calc_interface::add> ( [] (double a, double b) { return a + b;} ) .add_handler<calc_interface::eval> ( [] (string expr) → int { static int id(0); ++id; new std::thread( [this, expr, id] () { sleep(100); server.emit<expr_evaluated>(expr, 12345); } ); return id; }); client_t<calc_interface> client(transpotr); // subscribe to the signal .. client.subscribe<client_t::expr_evaluated>( [] (int id, string expr, double res) { cout << expr << "→" << res; } ); // execute functions .. cout << client.invoke<add>(1,2) << endl; // async invoke client.invoke(1,2, [] (double res) { cout << res << endl; })
  • 8. www.luxoft.com What should be taken into account • Wrong function parameters. e.g. the following should cause a compile time errors – client.invoke<add>(“hello” , 123) or client.invoke<add>(1) or client.invoke<add>(1, 2, 3) • Execution of nonexistent function – interface_1 { RPC_FN (void (int), fn1) }; – interface_2 { RPC_FN (void (double), fn2) }; – client_t<interface_1>().invoke<interface_2::fn2>(5.);
  • 9. www.luxoft.com Implementation struct calc_interface { // RPC_FUNCTION(double(double, double), add); struct add: meta_function_t<double (double, double), true> { static const char* uniq_name() { return “add”; } }; //RPC_FUNCTION(int(std::string), eval); struct eval: meta_function_t<double (double, double), true> { static const char* uniq_name() { return “eval”; }; //RPC_SIGNAL (void(int, double), expr_evaluated); struct expr_evaluated: meta_function_t<double (double, double), false> { static const char* uniq_name() { return “expr_evaluated”; }; }; meta_function_t < R (Arg0, …, ArgN) , bool> :: result_type → R; meta_function_t < R (Arg0, …, ArgN) , bool> :: args_count → N; meta_function_t < R (Arg0, …, ArgN) , bool> :: arg<0>::type → Arg0; ... meta_function_t < R (Arg0, …, ArgN) , bool> :: arg<N>::type → ArgN; meta_function_t < R (Arg0, …, ArgN) , bool> ::is_function → bool
  • 10. www.luxoft.comwww.luxoft.com namespace details { template<typename... Args> struct args_count { enum { value = 0 }; }; template<typename A1, typename ... Args> struct args_count<A1, Args...> { enum { value = 1 + args_count<Args...>::value }; };} cout << detail::args_count < >::value ; → 0 cout << detail::args_count < int >::value ; → 1 cout << detail::args_count < int , float>::value ; → 2
  • 11. www.luxoft.comwww.luxoft.com namepsace detail { template<unsigned int N, typename ...Args> struct arg; template<typename A0, typename ...Args> struct arg<0, A0, Args...> { typedef A0 type; }; template<unsigned int N, typename A0, typename ...Args> struct arg<N, A0, Args...> { typedef typename arg<N - 1, Args...>::type type; }; } detail::arg<0, int>::type → int detail::arg<0, int, double>::type → int detail::arg<1, int, double>::type → double
  • 12. www.luxoft.comwww.luxoft.com template<typename FnSygn, bool> struct meta_function; template<typename UniqType, bool isFn, typename R, typename ...Args> struct meta_function<R(Args…), isFn> { typedef R result_type; enum { args_count = detail::args_count<Args...>::value }; template<unsigned int N> struct arg { typedef typename detail::arg<N, Args...>::type type; }; enum { is_function = isFn }; };
  • 13. www.luxoft.com client.invoke // calc_interface struct add: meta_function_t<double (double, double), true> { … }; // client_t<calc_interface> client: … auto result = client.invoke<add> ( … ) : 1) check arguments 2) serialize arguments 3) serialize function ID 4) send data 5) wait for response 6) deserialize and return result
  • 14. www.luxoft.com client.invoke template< typename MetaFn, typename ...Args> typename MetaFn::result_type invoke ( Args const&... args) { check if <MetaFn> declared in <calc_interface> ; static_assert(MetaFn::is_function == true); static_assert(MetaFn::args_count == detail::args_count<Args...>::value); typename MetaFn::arg<0>::type a0 = get<0>(args...); ... typename MetaFn::arg<N>::type a1 = get<N>(args...); // ---->>> auto ostream = transport.get_ostream( ); ostream << MetaFn::uniq_name() << arg0 << … << argN; int sequence_id = transport.send(ostream); transport_wait_for_response(sequence_id); auto istream = transport.get_istream(sequence_id); MetaFn::result_type result; istream >> result; return result; }
  • 15. www.luxoft.com client.invoke template<typename MetaFn, unsigned int N, typename IStream, typename ...Args> void serrialize_all(IStream & , Args ...args) { }; template<typename MetaFn, unsigned int N, typename IStream, typename Arg0, typename ...Args> void serrialize_all(IStream & stream, Arg0 const& arg0,Args ...args) { typename typedef MetaFn::arg<N>::type arg_to_send = arg0; stream << arg0; serrialize_all<MetaFn, N + 1> (stream, args...); }
  • 16. www.luxoft.com client.invoke template< typename MetaFn, typename ...Args> typename MetaFn::result_type invoke ( Args const&... args ) { check if <MetaFn> declared in <calc_interface> ; static_assert(MetaFn::is_function == true); static_assert(MetaFn::args_count == detail::args_count<Args...>::value); auto ostream = transport.get_ostream( ); serrialize_all<MetaFn,0>(ostream, MetaFn::uniq_name(), args...); int sequence_id = transport.send(ostream); transport_wait_for_response(sequence_id); auto istream = transport.get_istream(sequence_id); MetaFn::result_type result; istream >> result; return result; }
  • 17. www.luxoft.com Exists or not ? struct calc_interface { long __registrate_meta_type(…); // RPC_FUNCTION(double(double, double), add); struct add: meta_function_t<double (double, double), true> { static const char* uniq_name() { return “add”; } }; char __registrate_meta_type(add ); ... char __registrate_meta_type(expr_evaluated); }; template< typename Interface, typename MetaFn> struct is_present { enum { value = sizeof( ((Interface*)0)→ __registrate_meta_type(MetaFn())) == 1 ? 1 : 0 }; }; std::cout << is_present<calc_interface, calc_interface::add>::value → 1 std::cout << is_present<calc_interface, calc_interface::eval>::value → 1 std::cout << is_present<calc_interface, int>::value → 0
  • 18. www.luxoft.com Client.invoke – final version //client_t<calc_interface> client; // template<Interface> client_t { typedef Interface interface; }; template< typename MetaFn, typename ...Args>0 typename MetaFn::result_type invoke( Args const&... args ) { static_assert(is_present<interface, MetaFn> :: value == 1); static_assert(MetaFn::is_function == true); static_assert(MetaFn::args_count == detail::args_count<Args...>::value); auto ostream = transport.get_ostream( ); serrialize_all<MetaFn,0>(ostream, MetaFn::uniq_name(), args...); int sequence_id = transport.send(ostream); transport_wait_for_response(sequence_id); auto istream = transport.get_istream(sequence_id); MetaFn::result_type result; istream >> result; return result; }