From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- third_party/wasm2c/include/wabt/cast.h | 109 +++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 third_party/wasm2c/include/wabt/cast.h (limited to 'third_party/wasm2c/include/wabt/cast.h') diff --git a/third_party/wasm2c/include/wabt/cast.h b/third_party/wasm2c/include/wabt/cast.h new file mode 100644 index 0000000000..f089e2f18e --- /dev/null +++ b/third_party/wasm2c/include/wabt/cast.h @@ -0,0 +1,109 @@ +/* + * Copyright 2017 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WABT_CAST_H_ +#define WABT_CAST_H_ + +#include +#include + +#include "wabt/common.h" + +// Modeled after LLVM's dynamic casts: +// http://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates +// +// Use isa(foo) to check whether foo is a T*: +// +// if (isa(car)) { +// ... +// } +// +// Use cast(foo) when you know that foo is a T* -- it will assert that the +// type matches: +// +// switch (car.type) { +// case CarType::Minivan: { +// auto minivan = cast(car); +// ... +// } +// } +// +// Use dyn_cast(foo) as a combination if isa and cast, it will return +// nullptr if the type doesn't match: +// +// if (auto minivan = dyn_cast(car)) { +// ... +// } +// +// +// To use these classes in a type hierarchy, you must implement classof: +// +// enum CarType { Minivan, ... }; +// struct Car { CarType type; ... }; +// struct Minivan : Car { +// static bool classof(const Car* car) { return car->type == Minivan; } +// ... +// }; +// + +namespace wabt { + +template +bool isa(const Base* base) { + WABT_STATIC_ASSERT((std::is_base_of::value)); + return Derived::classof(base); +} + +template +const Derived* cast(const Base* base) { + assert(isa(base)); + return static_cast(base); +}; + +template +Derived* cast(Base* base) { + assert(isa(base)); + return static_cast(base); +}; + +template +const Derived* dyn_cast(const Base* base) { + return isa(base) ? static_cast(base) : nullptr; +}; + +template +Derived* dyn_cast(Base* base) { + return isa(base) ? static_cast(base) : nullptr; +}; + +// Cast functionality for unique_ptr. isa and dyn_cast are not included because +// they won't always pass ownership back to the caller. + +template +std::unique_ptr cast(std::unique_ptr&& base) { + assert(isa(base.get())); + return std::unique_ptr(static_cast(base.release())); +}; + +template +std::unique_ptr cast(std::unique_ptr&& base) { + assert(isa(base.get())); + return std::unique_ptr(static_cast(base.release())); +}; + +} // namespace wabt + +#endif // WABT_CAST_H_ -- cgit v1.2.3