Files
test2/source/blender/nodes/NOD_menu_value.hh
Jacques Lucke c90a137a27 Nodes: wrap int in MenuValue type for menu sockets
Previously, we always just used `int` when dealing with menu values on the C++
side. It's currently the only type where we have the same base type (`int`) for
two different socket types (integer and menu sockets). This has some downsides:
* It requires special cases in some places.
* There is no function from static base type to socket type (which is useful for
  some utilities like `SocketValueVariant`).
* It implicitly allows operations on menu values that shouldn't be allowed such
  as arithmetic operations and conversions to and from other types.

This patch adds a new `MenuValue` type that is used for menu sockets in Geometry
Nodes and the (CPU) Compositor, clarifying the distinction between integer and
menu values.

Pull Request: https://projects.blender.org/blender/blender/pulls/144476
2025-08-13 15:43:37 +02:00

36 lines
815 B
C++

/* SPDX-FileCopyrightText: 2025 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "BLI_hash.hh"
#include "BLI_struct_equality_utils.hh"
namespace blender::nodes {
/**
* Don't use integer for menus directly, so that there is a each static single value type maps to
* exactly one socket type. Also it avoids accidentally casting the menu value to other types.
*/
struct MenuValue {
int value = 0;
MenuValue() = default;
explicit MenuValue(const int value) : value(value) {}
template<typename EnumT, BLI_ENABLE_IF((std::is_enum_v<EnumT>))>
MenuValue(const EnumT value) : value(int(value))
{
}
uint64_t hash() const
{
return get_default_hash(this->value);
}
BLI_STRUCT_EQUALITY_OPERATORS_1(MenuValue, value)
};
} // namespace blender::nodes