/** * src/class_diagram/visitor/translation_unit_visitor.h * * Copyright (c) 2021-2022 Bartek Kryza * * 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. */ #pragma once #include #include #include #include #include namespace clanguml::common { using id_t = int64_t; template class optional_ref { public: using optional_type = T; optional_ref() : value_{nullptr} { } optional_ref(T *value) { value_ = value; } optional_ref(const T *value) { value_ = value; } optional_ref(T &value) { value_ = &value; } optional_ref(const T &value) { value_ = &value; } optional_ref(optional_ref &right) { value_ = right.get(); } template || std::is_same_v>> optional_ref(const V &t) { value_ = t.get(); } template || std::is_same_v>> optional_ref(V &&t) { value_ = t.get(); t.reset(); } template >> optional_ref(const std::reference_wrapper &t) { value_ = &t.get(); } optional_ref &operator=(const optional_ref &right) { if (this == &right) return *this; value_ = right.value_; return *this; } optional_ref &operator=(optional_ref &&right) noexcept { if (this == &right) return *this; value_ = right.value_; right.reset(); return *this; } bool has_value() const noexcept { return value_ != nullptr; } operator bool() const noexcept { return has_value(); } const T &value() const { assert(value_ != nullptr); return *value_; } T &value() { assert(value_ != nullptr); return *value_; } void reset() { value_ = nullptr; } T *get() const { return value_; } private: T *value_; }; template using reference_vector = std::vector>; template using reference_set = std::unordered_set>; } // namespace clanguml::common