# toml++ — Node System ## Overview The node system is the core of toml++'s data model. Every element in a TOML document — tables, arrays, and leaf values — is represented as a `toml::node`. This document covers the base class interface, `node_view` for safe access, type checking mechanisms, value retrieval strategies, and visitation patterns. --- ## `toml::node` — The Base Class `toml::node` is an abstract base class (`TOML_ABSTRACT_INTERFACE`) declared in `include/toml++/impl/node.hpp`. It cannot be instantiated directly; only its derived classes (`table`, `array`, `value`) can. ### Source Tracking Every node stores a `source_region` tracking its origin in the parsed document: ```cpp class node { private: source_region source_{}; public: const source_region& source() const noexcept; }; ``` For programmatically-constructed nodes, `source()` returns a default-constructed region (all zeros). For parsed nodes, it contains the file path and begin/end line/column. ### Lifetime ```cpp protected: node() noexcept; node(const node&) noexcept; // copies source_region node(node&&) noexcept; // moves source_region node& operator=(const node&) noexcept; node& operator=(node&&) noexcept; public: virtual ~node() noexcept; ``` Constructors are protected — you create nodes by constructing `table`, `array`, or `value` objects. --- ## Type Checking ### Virtual Type Checks Every `node` provides a full set of virtual type-checking methods: ```cpp 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; ``` `is_number()` returns `true` for both integers and floating-point values. `is_array_of_tables()` returns `true` only for arrays where every element is a table. ### Template Type Check: `is()` ```cpp template bool is() const noexcept; ``` Accepts any TOML node or value type. Uses `if constexpr` internally to dispatch: ```cpp node.is() // equivalent to node.is_table() node.is() // equivalent to node.is_array() node.is() // equivalent to node.is_string() node.is() // equivalent to node.is_integer() node.is() // equivalent to node.is_floating_point() node.is() // equivalent to node.is_boolean() node.is() // equivalent to node.is_date() node.is() // equivalent to node.is_time() node.is() // equivalent to node.is_date_time() ``` You can also use the wrapped `value` type: ```cpp node.is>() // same as node.is() ``` The `impl::unwrap_node` trait unwraps `value` → `T` and `node_view` → `T`. ### Compile-Time Type Traits The `toml` namespace provides type traits usable with `if constexpr`: ```cpp // Type traits for use in generic/template code toml::is_table // true if val is table or node_view of table toml::is_array // true if val is array or node_view of array toml::is_string // true if val is value toml::is_integer // true if val is value toml::is_floating_point // true if val is value toml::is_number // integer or floating-point toml::is_boolean // true if val is value toml::is_date // true if val is value toml::is_time // true if val is value