Skip to content

Commit

Permalink
(#290) Дефолтный флаг для cast
Browse files Browse the repository at this point in the history
  • Loading branch information
dak151449 committed Nov 10, 2023
1 parent 0cf6c96 commit 0684467
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions libs/Objects/include/Objects/BackRefRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class BackRefRegex : public AlgExpression {
BackRefRegex(const BackRefRegex&);

// dynamic_cast к типу BackRefRegex*
template <typename T> static BackRefRegex* cast(T* ptr);
template <typename T> static const BackRefRegex* cast(const T* ptr);
template <typename T> static BackRefRegex* cast(T* ptr, bool NotNullPtr = true);
template <typename T> static const BackRefRegex* cast(const T* ptr, bool NotNullPtr = true);

string to_txt() const override;
};
4 changes: 2 additions & 2 deletions libs/Objects/include/Objects/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Regex : public AlgExpression {
Regex(const Regex&) = default;

// dynamic_cast к типу Regex*
template <typename T> static Regex* cast(T* ptr);
template <typename T> static const Regex* cast(const T* ptr);
template <typename T> static Regex* cast(T* ptr, bool NotNullPtr = true);
template <typename T> static const Regex* cast(const T* ptr, bool NotNullPtr = true);
// dynamic_cast каждого элемента вектора к типу Regex*
template <typename T> static vector<Regex*> cast(vector<T*> ptr);

Expand Down
13 changes: 5 additions & 8 deletions libs/Objects/src/BackRefRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ BackRefRegex* BackRefRegex::make() const {
return new BackRefRegex;
}

template <typename T> BackRefRegex* BackRefRegex::cast(T* ptr) {
template <typename T> BackRefRegex* BackRefRegex::cast(T* ptr, bool NotNullPtr) {
auto* r = dynamic_cast<BackRefRegex*>(ptr);
if (!r) {
if (!r && NotNullPtr) {
throw std::runtime_error("Failed to cast to BackRefRegex");
}

return r;
}

template <typename T> const BackRefRegex* BackRefRegex::cast(const T* ptr) {
template <typename T> const BackRefRegex* BackRefRegex::cast(const T* ptr, bool NotNullPtr) {
auto* r = dynamic_cast<const BackRefRegex*>(ptr);
if (!r) {
if (!r && NotNullPtr) {
throw std::runtime_error("Failed to cast to BackRefRegex");
}

Expand Down Expand Up @@ -153,10 +153,7 @@ BackRefRegex* BackRefRegex::expr(const vector<AlgExpression::Lexeme>& lexemes, i
if (!p) {
p = scan_square_br(lexemes, index_start, index_end);
}
if (!p) {
return nullptr;
}
return cast(p);
return cast(p, false);
}

BackRefRegex* BackRefRegex::scan_ref(const vector<AlgExpression::Lexeme>& lexemes, int index_start,
Expand Down
10 changes: 5 additions & 5 deletions libs/Objects/src/Regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ Regex* Regex::make() const {
return new Regex;
}

template <typename T> Regex* Regex::cast(T* ptr) {
template <typename T> Regex* Regex::cast(T* ptr, bool NotNullPtr) {
auto* r = dynamic_cast<Regex*>(ptr);
if (!r) {
if (!r && NotNullPtr) {
throw runtime_error("Failed to cast to Regex");
}

return r;
}

template <typename T> const Regex* Regex::cast(const T* ptr) {
template <typename T> const Regex* Regex::cast(const T* ptr, bool NotNullPtr) {
auto* r = dynamic_cast<const Regex*>(ptr);
if (!r) {
if (!r && NotNullPtr) {
throw runtime_error("Failed to cast to Regex");
}

Expand Down Expand Up @@ -96,7 +96,7 @@ Regex* Regex::expr(const vector<AlgExpression::Lexeme>& lexemes, int index_start
p = scan_par(lexemes, index_start, index_end);
}

return cast(p);
return cast(p, false);
}

Regex* Regex::scan_minus(const vector<AlgExpression::Lexeme>& lexemes,
Expand Down

0 comments on commit 0684467

Please sign in to comment.