/* * 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_