Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions compiler/fory_compiler/generators/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"""C++ code generator."""

from typing import Dict, List, Optional, Set, Tuple
from typing import Dict, List, Optional, Set
import typing

from fory_compiler.generators.base import BaseGenerator, GeneratedFile
Expand Down Expand Up @@ -120,15 +120,13 @@ def get_namespaced_type_name(
return f"{namespace}::{qualified_name}"
return qualified_name

def get_field_config_type_and_alias(
def get_field_config_type(
self,
type_name: str,
parent_stack: List[Message],
) -> Tuple[str, str]:
"""Get type name and token-safe alias for FORY_FIELD_CONFIG."""
qualified_name = self.get_namespaced_type_name(type_name, parent_stack)
alias = f"ForyType_{qualified_name.replace('::', '_')}"
return qualified_name, alias
) -> str:
"""Get type name for FORY_FIELD_CONFIG."""
return self.get_namespaced_type_name(type_name, parent_stack)

def generate_header(self) -> GeneratedFile:
"""Generate a C++ header file with all types."""
Expand Down Expand Up @@ -784,13 +782,11 @@ def generate_message_definition(
field_members = ", ".join(
self.get_field_member_name(f) for f in message.fields
)
field_config_type_name = self.get_field_config_type_and_alias(
field_config_type_name = self.get_field_config_type(
message.name, parent_stack
)
field_config_macros.append(
self.generate_field_config_macro(
message, field_config_type_name[0], field_config_type_name[1]
)
self.generate_field_config_macro(message, field_config_type_name)
)
lines.append(
f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});"
Expand Down Expand Up @@ -1348,7 +1344,6 @@ def generate_field_config_macro(
self,
message: Message,
qualified_name: str,
alias_name: str,
) -> str:
"""Generate FORY_FIELD_CONFIG macro for a message."""
entries = []
Expand All @@ -1357,7 +1352,7 @@ def generate_field_config_macro(
meta = self.get_field_meta(field)
entries.append(f"({field_name}, {meta})")
joined = ", ".join(entries)
return f"FORY_FIELD_CONFIG({qualified_name}, {alias_name}, {joined});"
return f"FORY_FIELD_CONFIG({qualified_name}, {joined});"

def get_field_meta(self, field: Field) -> str:
"""Build FieldMeta expression for a field."""
Expand Down
24 changes: 13 additions & 11 deletions cpp/fory/meta/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -1389,11 +1389,11 @@ struct GetFieldTagEntry<T, Index,
// ============================================================================
//
// Usage:
// FORY_FIELD_CONFIG(MyStruct, MyStruct,
// (field1, F(0)), // Simple: just ID
// (field2, F(1).nullable()), // With nullable
// (field3, F(2).varint()), // With encoding
// (field4, F(3).nullable().ref()), // Multiple options
// FORY_FIELD_CONFIG(MyStruct,
// (field1, fory::F(0)), // Simple: just ID
// (field2, fory::F(1).nullable()), // With nullable
// (field3, fory::F(2).varint()), // With encoding
// (field4, fory::F(3).nullable().ref()), // Multiple options
// (field5, 4) // Backward compatible: integer
// ID
// );
Expand Down Expand Up @@ -1908,18 +1908,20 @@ struct GetFieldTagEntry<T, Index,

// Main FORY_FIELD_CONFIG macro
// Creates a constexpr tuple of FieldEntry objects with member pointer
// verification. Alias is a token-safe name without '::'.
#define FORY_FC_DESCRIPTOR_NAME(Alias) \
FORY_PP_CONCAT(ForyFieldConfigDescriptor_, Alias)
#define FORY_FIELD_CONFIG(Type, Alias, ...) \
struct FORY_FC_DESCRIPTOR_NAME(Alias) { \
// verification. Descriptor name uses a unique line-based token.
#define FORY_FC_DESCRIPTOR_NAME(line) \
FORY_PP_CONCAT(ForyFieldConfigDescriptor_, line)
#define FORY_FIELD_CONFIG(Type, ...) \
FORY_FIELD_CONFIG_IMPL(__LINE__, Type, __VA_ARGS__)
#define FORY_FIELD_CONFIG_IMPL(line, Type, ...) \
struct FORY_FC_DESCRIPTOR_NAME(line) { \
static constexpr bool has_config = true; \
static inline constexpr auto entries = \
std::make_tuple(FORY_FC_ENTRIES(Type, __VA_ARGS__)); \
static constexpr size_t field_count = \
std::tuple_size_v<std::decay_t<decltype(entries)>>; \
}; \
constexpr auto ForyFieldConfig(::fory::meta::Identity<Type>) { \
return FORY_FC_DESCRIPTOR_NAME(Alias){}; \
return FORY_FC_DESCRIPTOR_NAME(line){}; \
} \
static_assert(true)
4 changes: 2 additions & 2 deletions cpp/fory/serialization/namespace_macro_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ class EnumContainer final {

FORY_ENUM(EnumContainer::Kind, Alpha, Beta);

FORY_FIELD_CONFIG(Configured, Configured, (id_, fory::F().id(1).varint()));
FORY_FIELD_CONFIG(Configured, (id_, fory::F().id(1).varint()));
FORY_FIELD_TAGS(OptionalHolder, (name_, 1));
FORY_FIELD_CONFIG(Partial, Partial, (count_, fory::F().id(7).varint()));
FORY_FIELD_CONFIG(Partial, (count_, fory::F().id(7).varint()));
FORY_FIELD_TAGS(Partial, (id_, 5));

FORY_UNION(Choice, (std::string, text, fory::F(1)),
Expand Down
30 changes: 15 additions & 15 deletions cpp/fory/serialization/xlang_test_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ struct UnsignedSchemaConsistentSimple {
FORY_STRUCT(UnsignedSchemaConsistentSimple, u64Tagged, u64TaggedNullable);
};
FORY_FIELD_CONFIG(UnsignedSchemaConsistentSimple,
UnsignedSchemaConsistentSimple,
(u64Tagged, fory::F().tagged()),
(u64TaggedNullable, fory::F().nullable().tagged()));

Expand Down Expand Up @@ -649,9 +648,8 @@ struct UnsignedSchemaConsistent {
u64TaggedNullableField);
};
// Use new FORY_FIELD_CONFIG with builder pattern for encoding specification
FORY_FIELD_CONFIG(UnsignedSchemaConsistent, UnsignedSchemaConsistent,
(u8Field, fory::F()), (u16Field, fory::F()),
(u32VarField, fory::F().varint()),
FORY_FIELD_CONFIG(UnsignedSchemaConsistent, (u8Field, fory::F()),
(u16Field, fory::F()), (u32VarField, fory::F().varint()),
(u32FixedField, fory::F().fixed()),
(u64VarField, fory::F().varint()),
(u64FixedField, fory::F().fixed()),
Expand Down Expand Up @@ -710,17 +708,19 @@ struct UnsignedSchemaCompatible {
// Use new FORY_FIELD_CONFIG with builder pattern for encoding specification
// Group 1: nullable in C++ (std::optional), non-nullable in Java
// Group 2: non-nullable in C++, nullable in Java
FORY_FIELD_CONFIG(
UnsignedSchemaCompatible, UnsignedSchemaCompatible,
(u8Field1, fory::F().nullable()), (u16Field1, fory::F().nullable()),
(u32VarField1, fory::F().nullable().varint()),
(u32FixedField1, fory::F().nullable().fixed()),
(u64VarField1, fory::F().nullable().varint()),
(u64FixedField1, fory::F().nullable().fixed()),
(u64TaggedField1, fory::F().nullable().tagged()), (u8Field2, fory::F()),
(u16Field2, fory::F()), (u32VarField2, fory::F().varint()),
(u32FixedField2, fory::F().fixed()), (u64VarField2, fory::F().varint()),
(u64FixedField2, fory::F().fixed()), (u64TaggedField2, fory::F().tagged()));
FORY_FIELD_CONFIG(UnsignedSchemaCompatible, (u8Field1, fory::F().nullable()),
(u16Field1, fory::F().nullable()),
(u32VarField1, fory::F().nullable().varint()),
(u32FixedField1, fory::F().nullable().fixed()),
(u64VarField1, fory::F().nullable().varint()),
(u64FixedField1, fory::F().nullable().fixed()),
(u64TaggedField1, fory::F().nullable().tagged()),
(u8Field2, fory::F()), (u16Field2, fory::F()),
(u32VarField2, fory::F().varint()),
(u32FixedField2, fory::F().fixed()),
(u64VarField2, fory::F().varint()),
(u64FixedField2, fory::F().fixed()),
(u64TaggedField2, fory::F().tagged()));

namespace fory {
namespace serialization {
Expand Down
Loading