/* * Copyright (c) 2021 Huawei Device Co., Ltd. * 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 PANDA_VERIFICATION_UTIL_REF_WRAPPER_H_ #define PANDA_VERIFICATION_UTIL_REF_WRAPPER_H_ #include #include #include "invalid_ref.h" #include "macros.h" namespace panda::verifier { template class Ref : public std::reference_wrapper { using Base = std::reference_wrapper; public: using type = T; Ref() : Base(Invalid()) {} Ref(const Ref &) = default; Ref &operator=(const Ref &) = default; Ref(const std::reference_wrapper &ref) : Base(ref) {} Ref &operator=(const std::reference_wrapper &ref) { Base::operator=(ref); return *this; } ~Ref() = default; T &get() const noexcept { T &result = Base::get(); ASSERT(Valid(result)); return result; } operator T &() const noexcept { return get(); } template std::invoke_result_t operator()(Args &&... args) const { #ifndef NDEBUG T &result = Base::get(); ASSERT(Valid(result)); #endif // NDEBUG return Base::operator()(std::forward(args)...); } }; } // namespace panda::verifier #endif // PANDA_VERIFICATION_UTIL_REF_WRAPPER_H_