Do not throw std::bad_cast from antlrcpp::Any::is
The antlrcpp::Any::is function should not throw a std::bad_cast exception if the contained type can't be cast to the requested type, but should instead just return a boolean result. Add a boolean parameter to the private getDerived helper function to allow callers to specify whether or not they want the cast results checked. In the is() function, pass false for this parameter; in the as() functions, pass true.
This commit is contained in:
parent
11d601348e
commit
562eab817c
|
@ -46,21 +46,21 @@ struct ANTLR4CPP_PUBLIC Any
|
|||
|
||||
template<class U>
|
||||
bool is() const {
|
||||
auto derived = getDerived<U>();
|
||||
auto derived = getDerived<U>(false);
|
||||
|
||||
return derived != nullptr;
|
||||
}
|
||||
|
||||
template<class U>
|
||||
StorageType<U>& as() {
|
||||
auto derived = getDerived<U>();
|
||||
auto derived = getDerived<U>(true);
|
||||
|
||||
return derived->value;
|
||||
}
|
||||
|
||||
template<class U>
|
||||
const StorageType<U>& as() const {
|
||||
auto derived = getDerived<U>();
|
||||
auto derived = getDerived<U>(true);
|
||||
|
||||
return derived->value;
|
||||
}
|
||||
|
@ -143,12 +143,12 @@ private:
|
|||
}
|
||||
|
||||
template<class U>
|
||||
Derived<StorageType<U>>* getDerived() const {
|
||||
Derived<StorageType<U>>* getDerived(bool checkCast) const {
|
||||
typedef StorageType<U> T;
|
||||
|
||||
auto derived = dynamic_cast<Derived<T>*>(_ptr);
|
||||
|
||||
if (!derived)
|
||||
if (checkCast && !derived)
|
||||
throw std::bad_cast();
|
||||
|
||||
return derived;
|
||||
|
|
Loading…
Reference in New Issue