/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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 #include #include #include #include #include namespace apache { namespace thrift { namespace compiler { /** * Represents an enum definition. */ class t_enum : public t_type { public: // An ~arbitrary, unlikely yet small number. static constexpr int32_t default_unused = 113; t_enum(t_program* program, std::string name) : t_type(program, std::move(name)) {} void set_values(t_enum_value_list values); void append_value(std::unique_ptr enum_value); node_list_view values() { return values_; } node_list_view values() const { return values_; } // A value that does not currently have an associated name. int32_t unused() const { return unused_; } // Returns the enum_value with the given value, or nullptr. const t_enum_value* find_value(int32_t enum_value) const; // The t_consts associated with each value. node_list_view consts() const { return constants_; } const t_const* find_const_by_name(std::string_view name) const; private: t_enum_value_list values_; node_list constants_; std::map value_map_; std::map consts_by_name_; int32_t unused_ = default_unused; // TODO(afuller): These methods are only provided for backwards // compatibility. Update all references and remove everything below. std::vector values_raw_; void update_unused(int32_t val); public: void append( std::unique_ptr enum_value, std::unique_ptr constant) { update_unused(enum_value->get_value()); values_raw_.push_back(enum_value.get()); value_map_.emplace(enum_value->get_value(), enum_value.get()); consts_by_name_.emplace(enum_value->get_name(), constant.get()); values_.push_back(std::move(enum_value)); constants_.push_back(std::move(constant)); } void append(std::unique_ptr enum_value) { append_value(std::move(enum_value)); } const std::vector& get_enum_values() const { return values_raw_; } bool is_enum() const override { return true; } type get_type_value() const override { return type::t_enum; } }; } // namespace compiler } // namespace thrift } // namespace apache