diff options
author | Jacques Lucke <jacques@blender.org> | 2022-11-12 20:33:31 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-11-12 20:33:31 +0300 |
commit | a6c822733ac7da4921297804475adadd50c08ed9 (patch) | |
tree | b34cc2924d7effa33d9ca9abfa026c8917fb953a /source/blender/functions/intern/field_cpp_type.cc | |
parent | a145b96396c4d2b8dd5ae027c676e838df0bd701 (diff) |
BLI: improve CPPType system
* Support bidirectional type lookups. E.g. finding the base type of a
field was supported, but not the other way around. This also removes
the todo in `get_vector_type`. To achieve this, types have to be
registered up-front.
* Separate `CPPType` from other "type traits". For example, previously
`ValueOrFieldCPPType` adds additional behavior on top of `CPPType`.
Previously, it was a subclass, now it just contains a reference to the
`CPPType` it corresponds to. This follows the composition-over-inheritance
idea. This makes it easier to have self-contained "type traits" without
having to put everything into `CPPType`.
Differential Revision: https://developer.blender.org/D16479
Diffstat (limited to 'source/blender/functions/intern/field_cpp_type.cc')
-rw-r--r-- | source/blender/functions/intern/field_cpp_type.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/source/blender/functions/intern/field_cpp_type.cc b/source/blender/functions/intern/field_cpp_type.cc new file mode 100644 index 00000000000..9057bbb887a --- /dev/null +++ b/source/blender/functions/intern/field_cpp_type.cc @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "FN_field_cpp_type.hh" + +namespace blender::fn { + +static auto &get_from_self_map() +{ + static Map<const CPPType *, const ValueOrFieldCPPType *> map; + return map; +} + +static auto &get_from_value_map() +{ + static Map<const CPPType *, const ValueOrFieldCPPType *> map; + return map; +} + +void ValueOrFieldCPPType::register_self() +{ + get_from_value_map().add_new(&this->value, this); + get_from_self_map().add_new(&this->self, this); +} + +const ValueOrFieldCPPType *ValueOrFieldCPPType::get_from_self(const CPPType &self) +{ + const ValueOrFieldCPPType *type = get_from_self_map().lookup_default(&self, nullptr); + BLI_assert(type == nullptr || type->self == self); + return type; +} + +const ValueOrFieldCPPType *ValueOrFieldCPPType::get_from_value(const CPPType &value) +{ + const ValueOrFieldCPPType *type = get_from_value_map().lookup_default(&value, nullptr); + BLI_assert(type == nullptr || type->value == value); + return type; +} + +} // namespace blender::fn |