Skip to content

Commit cdcd2ee

Browse files
authored
Merge pull request #28 from otto-link/fix/attribute-factory-lazy-registration
fix(factory): self-register built-in types on first factory use
2 parents c4bb6be + bb9b165 commit cdcd2ee

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎Meta/include/meta/serialization/attribute_factory.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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
*/
105106
void register_builtin_types();
106107

‎Meta/src/attribute_factory.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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);

‎tests/test_meta/main.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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

0 commit comments

Comments
 (0)