Skip to content

Commit

Permalink
fix: use unqiue_ptr
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Oleshko <vlad@dragonflydb.io>
  • Loading branch information
dranikpg committed May 2, 2023
1 parent c1248f5 commit 42da4a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
47 changes: 36 additions & 11 deletions src/core/search/ast_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <ostream>
#include <vector>

#include "base/logging.h"

namespace dfly {

namespace search {
Expand All @@ -26,16 +28,38 @@ class AstNode {
virtual std::string Debug() const = 0;
};

using NodePtr = std::shared_ptr<AstNode>;
using AstExpr = NodePtr;
using AstNodePtr = std::unique_ptr<AstNode>;

// Do we have to pollute all our parser file with moves?
struct AstExpr {
AstExpr() : ptr{} {
}
AstExpr(AstNodePtr ptr) : ptr{move(ptr)} {
}
AstExpr(const AstExpr& other) {
ptr = move(other.ptr);
}
AstExpr& operator=(const AstExpr& other) {
ptr = move(other.ptr);
return *this;
}
operator AstNodePtr() {
return move(ptr);
}
const AstNode* operator->() const {
return ptr.get();
}
mutable AstNodePtr ptr;
};

template <typename T, typename... Ts> AstExpr MakeExpr(Ts... ts) {
auto ptr = std::make_shared<T>(std::forward<Ts>(ts)...);
return AstExpr{std::reinterpret_pointer_cast<AstNode>(move(ptr))};
AstNode* node = dynamic_cast<AstNode*>(new T(std::forward<Ts>(ts)...));
CHECK(node);
return std::unique_ptr<AstNode>{node};
}

// AST term node, matches only if input contains term.
class AstTermNode : AstNode {
class AstTermNode : public AstNode {
public:
AstTermNode(std::string term) : term_{move(term)} {
}
Expand All @@ -47,28 +71,29 @@ class AstTermNode : AstNode {
};

// Ast negation node, matches only if its sub node didn't match.
class AstNegateNode : AstNode {
class AstNegateNode : public AstNode {
public:
AstNegateNode(NodePtr node) : node_{node} {
AstNegateNode(AstNodePtr node) : node_{move(node)} {
}
virtual bool Check(std::string_view needle) const;
virtual std::string Debug() const;

private:
NodePtr node_;
AstNodePtr node_;
};

// Ast logical operation node, matches only if sub nodes match
// in respect to logical operation (and/or).
class AstLogicalNode : AstNode {
class AstLogicalNode : public AstNode {
public:
AstLogicalNode(NodePtr l, NodePtr r, bool disjunction) : l_{l}, r_{r}, disjunction_{disjunction} {
AstLogicalNode(AstNodePtr l, AstNodePtr r, bool disjunction)
: l_{move(l)}, r_{move(r)}, disjunction_{disjunction} {
}
virtual bool Check(std::string_view needle) const;
virtual std::string Debug() const;

private:
NodePtr l_, r_;
AstNodePtr l_, r_;
bool disjunction_;
};

Expand Down
10 changes: 5 additions & 5 deletions src/core/search/query_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class QueryDriver {

Parser::location_type location;

void Set(AstExpr expr) {
expr_ = expr;
void Set(AstNodePtr expr) {
expr_ = move(expr);
}

AstExpr Get() {
return expr_;
AstNodePtr Get() {
return move(expr_);
}

private:
AstExpr expr_;
AstNodePtr expr_;

std::string cur_str_;
std::unique_ptr<Scanner> scanner_;
Expand Down
2 changes: 1 addition & 1 deletion src/core/search/search_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SearchParserTest : public ::testing::Test {
return expr_->Check(input);
}

AstExpr expr_;
AstNodePtr expr_;
QueryDriver query_driver_;
};

Expand Down

0 comments on commit 42da4a8

Please sign in to comment.