File tree Expand file tree Collapse file tree
include/meta/serialization Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -100,7 +100,8 @@ template <typename T> void register_attribute_type(const std::string &name)
100100/* *
101101 * @brief Register all built-in attribute types.
102102 *
103- * Typically called once at application startup or before deserialization.
103+ * Invoked automatically on first use of AttributeFactory::create(); hosts do
104+ * not need to call it. Calling it manually remains safe.
104105 */
105106void register_builtin_types ();
106107
Original file line number Diff line number Diff line change 11/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
22 Public License. The full license is in the file LICENSE, distributed with
33 this software. */
4+ #include < mutex>
5+
46#include " meta/serialization/attribute_factory.hpp"
57#include " meta/core/abstract_attribute.hpp"
68#include " meta/core/attribute_container.hpp"
@@ -21,6 +23,13 @@ std::unique_ptr<AbstractAttribute> AttributeFactory::create(
2123 const std::string &name,
2224 const std::string &attr_name) const
2325{
26+ // Hosts cannot be relied upon to call register_builtin_types() at startup;
27+ // populate the registry before the first lookup. This lives here rather
28+ // than in instance() because register_builtin_types() itself calls
29+ // instance() and would recurse.
30+ static std::once_flag builtin_types_once;
31+ std::call_once (builtin_types_once, register_builtin_types);
32+
2433 Logger::log ()->trace (" AttributeFactory::create: type='{}', name='{}'" ,
2534 name,
2635 attr_name);
Original file line number Diff line number Diff line change 1+ #include < cassert>
12#include < iostream>
23
34#include " meta.hpp"
@@ -46,6 +47,27 @@ int main()
4647{
4748 using namespace meta ;
4849
50+ // ---------------------------------------------------------------------------
51+ // Factory self-registration (issue #22)
52+ // ---------------------------------------------------------------------------
53+
54+ // Must run before anything in this process registers types manually: a host
55+ // that never calls register_builtin_types() still needs the factory to
56+ // reconstruct undeclared attributes from their serialized "type" field.
57+ {
58+ AttributeContainer src;
59+ src.add (" lazy_float" , 0 .25f );
60+ src.add (" lazy_string" , std::string (" abc" ));
61+
62+ AttributeContainer dst; // declares nothing, registers nothing
63+ dst.json_from (src.json_to ());
64+
65+ const float *f = dst.try_value <float >(" lazy_float" );
66+ assert (f && *f == 0 .25f );
67+ const std::string *s = dst.try_value <std::string>(" lazy_string" );
68+ assert (s && *s == " abc" );
69+ }
70+
4971 AttributeContainer container;
5072
5173 // --- Basic attributes
You can’t perform that action at this time.
0 commit comments