如何使用元编程生成代码? [英] How to use meta-programming to generate code?

查看:43
本文介绍了如何使用元编程生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在我的系统中,有很多统计计算器封装为类.我的工作是将这些计算器中的值合并为一个名为总计"的值.

Problem: In my system there are a lot of statistic calculators wrapped as classes. My job is to combine the values from these calculators into one single value called 'total'.

解释这些计算器应该如何组合的模型"以表格形式给出——csv文件或MySQL中的表格

The 'model' that explains how these calculators should be combined is given as a table --- either a csv file or a table in MySQL

instance_name,accessor,argument,post_processing_function_name
fleet_statistics,getCash,2017,func1
city_statistics,getPopulation,2016,func2
....
....

我目前的解决方案:我写了一个python脚本来读取csv文件并生成这样的c++代码:

My current solution: I wrote a python script to read the csv file and generate c++ code like this:

double computeTotal()
{
    double total = 0;
    total += func1(fleet_statistics->getCash(2017));
    total += func2(city_statistics->getPopulation(2016));
    ....
    ....
}

使用python生成代码非常方便,但问题是,它不再是一个纯"的c++程序.我需要破解 makefile 以确保生成的 C++ 文件是最新的.

Using python to generate the code is very convenient, but the problem is that, it is no longer a 'pure' c++ program. I need to hack the makefile to make sure that the generated c++ file is up to date.

一个更优雅的解决方案是使用元编程来使用 c++ 而不是 python 生成 c++ 中的代码.谁能告诉我怎么做?

A more elegant solution is to use meta-programming to generate the code in c++ using c++ rather than python. Can anyone show me how to do this?

只提几点:

  1. CSV 文件是由一些业务逻辑相关的脚本生成的.它有几千行.手动编写c++代码是不可能的.

  1. the CSV file is generated for by some business-logics-related-scripts. It has a couple thousands of rows. It is impossible to write the c++ code manually.

每次我们发布新版本时,CSV 文件都会发生变化.

The CSV file can change every time when we make a new release.

我以 CSV 格式为例.如果这会让事情变得更容易,它可以是 MySQL 中的一个表

I use CSV format as an example. It could be a table in MySQL if that will make things easier

instance_name 列中的对象不一定来自同一个 BaseClass.访问器的函数签名也是变体.所以不方便使用函数指针

objects in the instance_name column are not necessarily from the same BaseClass. The accessor's function signature is variant as well. So it is not convenient to use a function pointer

谢谢!

推荐答案

CSV 文件更改时,应该不需要生成和编译 C++ 代码.

There should be no need to generate and compile C++ code when the CSV file changes.

您应该做的就是编写 C++ 代码一次,解析 CSV 文件,通过在 C++ 对象上调用 C++ 函数来执行 CSV 文件中指定的计算.

All you should do is write C++ code once that parses the CSV file performs the calculations specified in the CSV file by invoking C++ functions on C++ objects.

所以,粗略地说,您的 fleet_statisticscity_statistics 类应该实现一些具有 double getYearValueByName( String name, int year ) 函数,它检查给定的名称并在自身上调用相应的同名函数以获取该年份的值.

So, roughly speaking, your fleet_statistics and city_statistics classes should implement some common interface that has a double getYearValueByName( String name, int year ) function, which checks the given name and invokes on itself a corresponding function of the same name to get a value for that year.

我知道这种方法很简单,实际上事情比这更复杂;您只需要继续扩展该模型,根据需要使其更加复杂,直到您拥有解析 CSV 文件所需的所有功能并按要求执行操作.

I understand that this approach is simplistic, and in reality things are more complicated than that; you will need to just keep extending that model, making it more complicated as necessary, until you have all the functionality needed to parse your CSV file and do as it requires.

这篇关于如何使用元编程生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆