C++ Logo

sg7

Advanced search

Re: [SG7] Reflection Usecases

From: Matus Chochlik <chochlik_at_[hidden]>
Date: Wed, 12 Jan 2022 18:08:52 +0100
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 17:09:05