Skip to content

Commit

Permalink
Use shared_ptr to manage memory
Browse files Browse the repository at this point in the history
Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu committed Jan 3, 2025
1 parent fb0f46b commit c40ab80
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 93 deletions.
15 changes: 8 additions & 7 deletions include/cql2cpp/ast_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once

#include <glog/logging.h>
#include <unistd.h>

#include <memory>
#include <vector>
Expand All @@ -31,30 +32,30 @@ class AstNode {
std::string id_;
NodeType type_;
Operator op_;
std::vector<AstNode*> children_;
std::vector<AstNodePtr> children_;
ValueT origin_value_;
mutable ValueT value_;
static std::ostream* ous_;

public:
static void set_ostream(std::ostream* ous) { ous_ = ous; }

AstNode(NodeType type, Operator op, const std::vector<AstNode*> children)
AstNode(NodeType type, Operator op, const std::vector<AstNodePtr> children)
: type_(type), op_(op), children_(children) {
id_ = idg.Gen();
*ous_ << "AstNode " << ToString() << std::endl;
LOG(INFO) << "AstNode " << ToString() << std::endl;
}

AstNode(NodeType type, const ValueT& value)
: type_(type), op_(NullOp), origin_value_(value), value_(NullValue) {
id_ = idg.Gen();
*ous_ << "AstNode " << ToString() << std::endl;
LOG(INFO) << "AstNode " << ToString() << std::endl;
}

AstNode(const ValueT& value)
: type_(Literal), op_(NullOp), origin_value_(value), value_(NullValue) {
id_ = idg.Gen();
*ous_ << "AstNode " << ToString() << std::endl;
LOG(INFO) << "AstNode " << ToString() << std::endl;
}

const std::string& id() const { return id_; }
Expand All @@ -63,8 +64,8 @@ class AstNode {

Operator op() const { return op_; }

const std::vector<AstNode*>& children() const { return children_; }
void append(AstNode* node) { children_.emplace_back(node); }
const std::vector<AstNodePtr>& children() const { return children_; }
void append(AstNodePtr node) { children_.emplace_back(node); }

const ValueT& origin_value() const { return origin_value_; }
ValueT value() const { return value_; }
Expand Down
11 changes: 2 additions & 9 deletions include/cql2cpp/cql2_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class Cql2Parser : public cql2cpp::Cql2ParserBase {
private:
cql2cpp::AstNode* root_;
cql2cpp::AstNodePtr root_;

public:
Cql2Parser() : cql2cpp::Cql2ParserBase(&root_) {}
Expand All @@ -26,12 +26,5 @@ class Cql2Parser : public cql2cpp::Cql2ParserBase {
LOG(ERROR) << "Cql2Parser Error: " << msg << std::endl;
}

cql2cpp::AstNode* root() const { return root_; }

void DeConstructRoot() { DeConstructAST(root_); }

static void DeConstructAST(cql2cpp::AstNode* node) {
for (auto* child : node->children()) DeConstructAST(child);
delete node;
}
cql2cpp::AstNodePtr root() const { return root_; }
};
14 changes: 4 additions & 10 deletions include/cql2cpp/cql2cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Cql2Cpp {
bool filter(const std::string& cql2_query,
std::vector<FeatureType>* result) const {
// Parse
AstNode* root;
AstNodePtr root;
if (not Parse(cql2_query, &root, &error_msg_)) return false;

// Prepare evaluator
Expand All @@ -70,7 +70,6 @@ class Cql2Cpp {
LOG(ERROR) << "evaluation error: " << evaluator_.error_msg();
}
}
Cql2Parser::DeConstructAST(root);

return result;
}
Expand All @@ -80,7 +79,7 @@ class Cql2Cpp {
bool Evaluate(const std::string& cql2_query, const FeatureSource& fs,
bool* result, std::string* error_msg, std::string* dot) {
// Parse
AstNode* root;
AstNodePtr root;
if (not Parse(cql2_query, &root, error_msg)) return false;

// Evaluate
Expand All @@ -93,33 +92,29 @@ class Cql2Cpp {
cql2cpp::Tree2Dot::GenerateDot(ss, root);
*dot = ss.str();
}
Cql2Parser::DeConstructAST(root);
return true;
} else {
if (error_msg != nullptr) *error_msg = evaluator_.error_msg();
Cql2Parser::DeConstructAST(root);
return false;
}
}

static bool ToDot(const std::string& cql2_query, std::string* dot,
std::string* error_msg) {
// Parse
AstNode* root;
AstNodePtr root;
if (not Parse(cql2_query, &root, error_msg)) return false;

// to dot
std::stringstream ss;
cql2cpp::Tree2Dot::GenerateDot(ss, root);
*dot = ss.str();

Cql2Parser::DeConstructAST(root);

return true;
}

private:
static bool Parse(const std::string& cql2_query, AstNode** root,
static bool Parse(const std::string& cql2_query, AstNodePtr* root,
std::string* error_msg) {
std::istringstream iss(cql2_query);
std::ostringstream oss;
Expand All @@ -135,7 +130,6 @@ class Cql2Cpp {
*root = parser.root();
return true;
} else {
parser.DeConstructRoot();
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/cql2cpp/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Evaluator {
eval_func.Register(functor);
}

bool Evaluate(const AstNode* root, const FeatureSource* fs,
bool Evaluate(const AstNodePtr root, const FeatureSource* fs,
ValueT* result) const {
if (type_evaluator_.find(root->type()) == type_evaluator_.end() ||
type_evaluator_.at(root->type()).find(root->op()) ==
Expand All @@ -73,7 +73,7 @@ class Evaluator {
}

std::vector<ValueT> child_values;
for (AstNode* child : root->children()) {
for (AstNodePtr child : root->children()) {
ValueT value;
if (Evaluate(child, fs, &value))
child_values.emplace_back(value);
Expand Down
2 changes: 1 addition & 1 deletion include/cql2cpp/evaluator_ast_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace cql2cpp {

using NodeEval =
std::function<bool(const AstNode*, const std::vector<ValueT>&,
std::function<bool(const AstNodePtr, const std::vector<ValueT>&,
const FeatureSource*, ValueT*, std::string* error_msg)>;

class EvaluatorAstNode {
Expand Down
4 changes: 2 additions & 2 deletions include/cql2cpp/evaluator_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class EvaluatorIn : public EvaluatorAstNode {
*errmsg = "left hand side is not scalar type";
return false;
}
AstNode* in_list = n->children()[1];
for (const AstNode* grand_child : in_list->children()) {
AstNodePtr in_list = n->children()[1];
for (const AstNodePtr& grand_child : in_list->children()) {
if (isVariantEqual(vs.at(0), grand_child->value())) {
*value = true;
return true;
Expand Down
12 changes: 6 additions & 6 deletions include/cql2cpp/tree_dot.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ namespace cql2cpp {

class Tree2Dot {
public:
static std::string node_name(const AstNode* node) {
static std::string node_name(const AstNodePtr node) {
if (node->op() != NullOp)
return OpName.at(node->op());
else
return TypeName.at(node->type());
}

static bool GenerateDot(std::ostream& ous, const AstNode* node) {
static bool GenerateDot(std::ostream& ous, const AstNodePtr node) {
ous << "digraph G {" << std::endl;
GenerateDotNode(ous, node);
ous << std::endl;
Expand All @@ -34,21 +34,21 @@ class Tree2Dot {
return true;
}

static bool GenerateDotNode(std::ostream& ous, const AstNode* node) {
static bool GenerateDotNode(std::ostream& ous, const AstNodePtr node) {
if (node == nullptr) return true;

ous << " \"" << node->id() << "\" [label=\"" << node->id() << ". "
<< node_name(node) << "(" << value_str(node->value()) << ")"
<< "\"];" << std::endl;
for (const auto child : node->children()) GenerateDotNode(ous, child);
for (const auto& child : node->children()) GenerateDotNode(ous, child);

return true;
}

static bool GenerateDotEdge(std::ostream& ous, const AstNode* node) {
static bool GenerateDotEdge(std::ostream& ous, const AstNodePtr node) {
if (node == nullptr) return true;

for (const auto child : node->children()) {
for (const auto& child : node->children()) {
ous << " \"" << node->id() << "\" -> \"" << child->id() << "\";\n";
GenerateDotEdge(ous, child);
}
Expand Down
Loading

0 comments on commit c40ab80

Please sign in to comment.