C++ Logo

sg7

Advanced search

Re: [SG7] Reflection Usecases

From: Jonathan O'Connor <jonathan.oconnor_at_[hidden]>
Date: Wed, 12 Jan 2022 18:47:41 +0000
Matus,
As all the interesting work is in creating the select statement, it might be easier and quicker to just generate the string! Way easier to write tests too!

From what I can see from the ruby world, people use the name of the class, function or parameter to generate strings and code. I would guess the tricky one is getting the name of the parameter, rather than an argument (I had to look up the difference. Parameter name is given in the declaration and argument name is given in the definition.)

The examples you've shown so far seem to be writing a reflecting function thatreads, writes or calls members of a struct. My table proxy example would require you to generate a class and define member functions.Will that be possible with your code? I have to read more of the existing SG7 papers.

Regards,
Jonathan

Sent with [ProtonMail](https://protonmail.com/) Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, January 12th, 2022 at 17:09, Matus Chochlik via SG7 <sg7_at_[hidden]> wrote:

> Hi Jonathan,
>
> On Wed, Jan 12, 2022 at 4:58 PM Jonathan O'Connor via SG7 <sg7_at_[hidden]> wrote:
>
>> Hi everyone,
>> I'm the new official Irish delegate on WG21, and I joined as I'm specifically interested in SG7.
>> I promised Matus Chochlik on twitter the other day that I would search for example reflection use cases in "Metaprogramming Ruby" by Paolo Perrotta. An email is more appropriate than multiple answers in twitter.
>> Sadly, and probably not surprisingly, I found few examples of reflection that are appropriate to C++.
>>
>> The one trick that is used a lot in the Ruby on Rails framework is generating method code based on the name of the method. This trick is often used to generate SQL finders:
>>
>> For example:
>> ```ruby
>> User.findByName("Jonathan") # generates a SELECT * from users where name = 'Jonathan'
>> ```
>> Converting this into C++ might require an interface class:
>>
>> ```c++
>> struct IUserTable : public MagicDBTable {
>> std::vector<User> findByName(std::string_view name);
>> };
>>
>> // The getTable function generates an implementation of the IUserTable class that
>> // generates the appropriate select statement for each member function.
>> // It uses the class name to figure out the DB table name. The where clause is
>> // generated from the member function name.
>> auto userTable = MagicDB::getTableProxy<IUserTable>();
>> auto jonathans = userTable.findByName("Jonathan");
>> ```
>
> Thanks for the suggestion, I'll set up a DB server and try to implement this.
> BTW. here is the current set of examples/use-cases:
> https://github.com/matus-chochlik/mirror/tree/develop/example/mirror
>
> Some might look somewhat silly, but I use this also to test various parts of the compiler implementation.
> Cheers,
> --Matus

Received on 2022-01-12 18:47:46