Skip to content

Architecture: Meta classes

Lars Toenning edited this page Dec 6, 2024 · 2 revisions

Abstract

We use a system to attach declarative metadata to the data members of value classes. The intention is to minimize the number of places where the data members must be listed. Without this system, each class would need separate lists of its data members for operator<, operator==, stringification, streaming to DBus, streaming to JSON, etc. The meta class system maintains one list of members for each class, with optional metadata, and the methods can refer back to this one list.

Usage

In order for a class or class template to use the tuple system, it must include the SWIFT_METACLASS(..) macro somewhere in its private: section. This macro receives the class name, as well as a list of SWIFT_METAMEMBER(..) or SWIFT_METAMEMBER_NAMED(..).

The SWIFT_METAMEMBER macro expects at least one argument: the name of the member variable without the leading m_ prefix. It assumes that the datamember inside the class has this prefix! Further flags can be passed for each data member as described below.

SWIFT_METAMEMBER_NAMED expects the same arguments as SWIFT_METAMEMBER but also allows to pass in a string as a name used for serialization.

Flags

The following flags are supported

  • DisabledForComparison: Do not use the member for comparison operations
  • DisabledForMarshalling: Do not use the member when marshalling the class
  • DisabledForDebugging
  • DisabledForHashing: Do not use the member when hashing the class
  • DisabledForJson: Do not add the member when serializing the class to JSON
  • CaseInsensitiveComparison
  • LosslessMarshalling
  • RequiredForJson: Require the data member when deserializing from JSON

Example

SWIFT_METACLASS(
    CFoo,
    SWIFT_METAMEMBER(orange, 0, DisabledForJson),
    SWIFT_METAMEMBER(banana),
    SWIFT_METAMEMBER_NAMED(apple, "apple_key")
)

In this example, metadata for the class CFoo is added. The class has three data member (m_orange, m_banana and m_apple). The "orange" data member is not used when serializing the class for JSON. For "apple", a specific key name if serialized to JSON is specified (instead of using the auto-generated one).

Clone this wiki locally