# toml++ — Architecture ## Overview toml++ implements a tree-based data model for TOML documents. A parsed TOML document becomes a tree of `toml::node` objects, with `toml::table` as the root. The architecture centers on: 1. A polymorphic node hierarchy (`node` → `table`, `array`, `value`) 2. A recursive-descent parser that builds trees 3. Formatter classes that serialize trees back to text 4. A path system for structured navigation All public types live in the `toml` namespace. Internal implementation details live in `toml::impl` (an ABI-namespaced detail namespace). --- ## Class Hierarchy ``` toml::node (abstract base) ├── toml::table — ordered map of key → node* ├── toml::array — vector of node* └── toml::value — leaf node holding a value ├── value ├── value ├── value ├── value ├── value ├── value └── value ``` Supporting types: ``` toml::node_view — non-owning optional reference to a node toml::key — string + source_region metadata toml::path — vector of path_component toml::path_component — key string or array index toml::source_position — line + column toml::source_region — begin + end positions + path toml::parse_error — error description + source_region toml::parse_result — table | parse_error (no-exceptions mode) ``` Formatter hierarchy: ``` impl::formatter (base, protected) ├── toml::toml_formatter — TOML output ├── toml::json_formatter — JSON output └── toml::yaml_formatter — YAML output ``` --- ## `toml::node` — The Abstract Base Class Defined in `include/toml++/impl/node.hpp`, `toml::node` is the polymorphic base of all TOML tree nodes. It is declared as `TOML_ABSTRACT_INTERFACE`, meaning it has pure virtual methods and cannot be instantiated directly. ### Private Members ```cpp class node { private: source_region source_{}; template decltype(auto) get_value_exact() const noexcept(...); // ref_type_ and ref_type — template aliases for ref() return types // do_ref() — static helper for ref() implementation ``` The `source_` member records where this node was defined in the original TOML document (line, column, file path). ### Protected Members ```cpp protected: node() noexcept; node(const node&) noexcept; node(node&&) noexcept; node& operator=(const node&) noexcept; node& operator=(node&&) noexcept; // ref_cast() — unsafe downcast helpers (all four ref-qualifications) template ref_cast_type ref_cast() & noexcept; template ref_cast_type ref_cast() && noexcept; template ref_cast_type ref_cast() const& noexcept; template ref_cast_type ref_cast() const&& noexcept; ``` Constructors and assignment operators are `protected` to prevent direct instantiation. `ref_cast()` performs `reinterpret_cast`-based downcasts, used internally by `ref()`. ### Public Interface — Type Checks Every `node` provides a complete set of virtual type-checking methods: ```cpp public: virtual ~node() noexcept; // Homogeneity checks virtual bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept = 0; virtual bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept = 0; virtual bool is_homogeneous(node_type ntype) const noexcept = 0; template bool is_homogeneous() const noexcept; // Type identity virtual node_type type() const noexcept = 0; virtual bool is_table() const noexcept = 0; virtual bool is_array() const noexcept = 0; virtual bool is_array_of_tables() const noexcept; virtual bool is_value() const noexcept = 0; virtual bool is_string() const noexcept = 0; virtual bool is_integer() const noexcept = 0; virtual bool is_floating_point() const noexcept = 0; virtual bool is_number() const noexcept = 0; virtual bool is_boolean() const noexcept = 0; virtual bool is_date() const noexcept = 0; virtual bool is_time() const noexcept = 0; virtual bool is_date_time() const noexcept = 0; // Template type check template bool is() const noexcept; ``` The `is()` template dispatches to the appropriate virtual method using `if constexpr`: ```cpp template bool is() const noexcept { using type = impl::remove_cvref>; if constexpr (std::is_same_v) return is_table(); else if constexpr (std::is_same_v) return is_array(); else if constexpr (std::is_same_v) return is_string(); // ... etc for int64_t, double, bool, date, time, date_time } ``` ### Public Interface — Type Casts ```cpp // Downcasts — return nullptr if type doesn't match virtual table* as_table() noexcept = 0; virtual array* as_array() noexcept = 0; virtual toml::value* as_string() noexcept = 0; virtual toml::value* as_integer() noexcept = 0; virtual toml::value* as_floating_point() noexcept = 0; virtual toml::value* as_boolean() noexcept = 0; virtual toml::value* as_date() noexcept = 0; virtual toml::value