是.那就对了.我希望能够粘贴一个表达式,如:
"a && b || c"
Run Code Online (Sandbox Code Playgroud)
直接作为字符串源代码:
const std::string expression_text("a && b || c");
Run Code Online (Sandbox Code Playgroud)
使用它创建一个延迟评估的结构:
Expr expr(magical_function(expression_text));
Run Code Online (Sandbox Code Playgroud)
然后在评估中用已知值代替:
evaluate(expr, a, b, c);
Run Code Online (Sandbox Code Playgroud)
我想稍后扩展这个小DSL,所以使用一些非C++语法做一些更复杂的事情,所以我不能简单地用简单的方法对我的表达式进行硬编码.用例是我能够将另一个开发区域中使用的另一个模块中的相同逻辑复制并粘贴到另一种语言中,而不是每次都要调整它以遵循C++语法.
如果有人能让我开始至少如何做1个表达式的上述简单概念和2个布尔运算符,那将是非常感激的.
注意:由于我发布的另一个问题的反馈,我发布了这个问题:如何解析DSL输入到高性能表达模板.在这里,我实际上想要一个稍微不同的问题的答案,但是评论引发了我认为值得发布的这个具体问题,因为潜在的答案真的值得记录.
请考虑以下结构:
struct ThingThatWillGoInSharedMemory {
boost::optional<int> opt_value;
};
Run Code Online (Sandbox Code Playgroud)
我正在使用boost :: interprocess来创建共享内存区域.我对boost :: optional的理解是它是一个有区别的联合而不是一个可空的指针.作为反例,使用堆的std :: map和std :: vector之类的东西需要一个显式的分配器来在进程间内存中使用它们,但是boost :: optional,我相当肯定不使用堆并且相当于写作:
struct ThingThatWillGoInSharedMemory {
bool value_initialised;
int value;
}
Run Code Online (Sandbox Code Playgroud)
所以它可以开箱即用.如果有人确认这一点,我会很高兴 - 我没有看到在boost :: optional文档中明确提到了进程间案例,只是暗示了.
我的团队对特定上下文的指针容器使用存在一些分歧.请考虑:
int main() {
// Top level. This is an important fact to the context
// i.e. that the following instance is at this level
// so that its members are essentially at program scope.
MainClass mainClass;
mainClass.run();
}
// A instance of a class derived from Buffer does something very complex
// (it has various handles to resources/allocated memory) so that you would
// never want to copy an instance.
class Buffer;
class MainClass {
#make_decision_here_based_on_stack_overflow_responses
shared_ptr<Buffer> buffer; // …Run Code Online (Sandbox Code Playgroud) 请考虑两个'boost :: property_tree'-s.
ptree1:
{
"node1" : 1,
"node_that_only_appears_in_this_one" : 2,
"node3" :
{
"nested1" : 3,
"nested2"
{
"double_nested1" : 5,
"double_nested2" : "foo"
}
}
}
Run Code Online (Sandbox Code Playgroud)
ptree2:
{
"node1" : 1,
"node3" :
{
"nested1" : 3,
"nested_that_only_appears_in_this_one" : 5,
"nested2"
{
"double_nested1" : 5,
"double_nested2" : "bar"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何编写一个我可以这样调用的通用函数:
ptree_whats_changed(ptree1, ptree2);
Run Code Online (Sandbox Code Playgroud)
这样,在这种特殊情况下,返回值是另一个属性树,如下所示:
{
"node3" :
{
"nested_that_only_appears_in_this_one" : 5,
"nested2"
{
"double_nested2" : "foo"
}
}
}
Run Code Online (Sandbox Code Playgroud)
从ptree1到ptree2从节点移除无关紧要 - 只需查看已修改或已添加的节点就足够了.
我的理解是,property_tree将所有内容存储为一个平面列表(用.来分隔路径),这样可能比看起来更容易,而不必递归.