LCOV - code coverage report
Current view: top level - src/config - toml_table_view.cpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 51.8 % 168 87
Test Date: 2026-05-20 02:24:49 Functions: 65.4 % 26 17
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright 2026 HPActor Contributors
       2                 :             : //
       3                 :             : // Licensed under the Apache License, Version 2.0 (the "License");
       4                 :             : // you may not use this file except in compliance with the License.
       5                 :             : // You may obtain a copy of the License at
       6                 :             : //
       7                 :             : //     http://www.apache.org/licenses/LICENSE-2.0
       8                 :             : //
       9                 :             : // Unless required by applicable law or agreed to in writing, software
      10                 :             : // distributed under the License is distributed on an "AS IS" BASIS,
      11                 :             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :             : // See the License for the specific language governing permissions and
      13                 :             : // limitations under the License.
      14                 :             : 
      15                 :             : #include <hpactor/config/toml_table_view.hpp>
      16                 :             : 
      17                 :             : #include <toml.hpp>
      18                 :             : 
      19                 :             : namespace hpactor::config {
      20                 :             : 
      21                 :             : // -----------------------------------------------------------------------------
      22                 :             : // TomlValueView
      23                 :             : // -----------------------------------------------------------------------------
      24                 :          11 : TomlValueView::TomlValueView(const void* node) noexcept : node_(node) {}
      25                 :             : 
      26                 :           0 : TomlValueView::Kind TomlValueView::kind() const noexcept {
      27                 :           0 :     if (!node_)
      28                 :           0 :         return Kind::Missing;
      29                 :           0 :     const auto* n = static_cast<const toml::node*>(node_);
      30                 :           0 :     if (n->is_string())
      31                 :           0 :         return Kind::String;
      32                 :           0 :     if (n->is_integer())
      33                 :           0 :         return Kind::Integer;
      34                 :           0 :     if (n->is_floating_point())
      35                 :           0 :         return Kind::FloatingPoint;
      36                 :           0 :     if (n->is_boolean())
      37                 :           0 :         return Kind::Boolean;
      38                 :           0 :     if (n->is_array())
      39                 :           0 :         return Kind::Array;
      40                 :           0 :     if (n->is_table())
      41                 :           0 :         return Kind::Table;
      42                 :           0 :     return Kind::Missing;
      43                 :             : }
      44                 :             : 
      45                 :          11 : bool TomlValueView::is_string() const noexcept {
      46                 :          11 :     return node_ && static_cast<const toml::node*>(node_)->is_string();
      47                 :             : }
      48                 :             : 
      49                 :           0 : bool TomlValueView::is_integer() const noexcept {
      50                 :           0 :     return node_ && static_cast<const toml::node*>(node_)->is_integer();
      51                 :             : }
      52                 :             : 
      53                 :           0 : bool TomlValueView::is_floating_point() const noexcept {
      54                 :           0 :     return node_ && static_cast<const toml::node*>(node_)->is_floating_point();
      55                 :             : }
      56                 :             : 
      57                 :           0 : bool TomlValueView::is_boolean() const noexcept {
      58                 :           0 :     return node_ && static_cast<const toml::node*>(node_)->is_boolean();
      59                 :             : }
      60                 :             : 
      61                 :          11 : std::string TomlValueView::as_string(std::string_view fallback) const {
      62                 :          11 :     if (!node_)
      63                 :           0 :         return std::string{fallback};
      64                 :          11 :     const auto* n = static_cast<const toml::node*>(node_);
      65                 :          11 :     if (!n->is_string())
      66                 :           0 :         return std::string{fallback};
      67                 :          33 :     return std::string{n->value<std::string>().value_or(std::string{fallback})};
      68                 :             : }
      69                 :             : 
      70                 :           0 : int64_t TomlValueView::as_int64(int64_t fallback) const noexcept {
      71                 :           0 :     if (!node_)
      72                 :           0 :         return fallback;
      73                 :           0 :     const auto* n = static_cast<const toml::node*>(node_);
      74                 :           0 :     if (!n->is_integer())
      75                 :           0 :         return fallback;
      76                 :           0 :     return n->value<int64_t>().value_or(fallback);
      77                 :             : }
      78                 :             : 
      79                 :           0 : double TomlValueView::as_double(double fallback) const noexcept {
      80                 :           0 :     if (!node_)
      81                 :           0 :         return fallback;
      82                 :           0 :     const auto* n = static_cast<const toml::node*>(node_);
      83                 :           0 :     if (n->is_floating_point())
      84                 :           0 :         return n->value<double>().value_or(fallback);
      85                 :           0 :     if (n->is_integer()) {
      86                 :           0 :         auto val = n->value<int64_t>();
      87                 :           0 :         if (val)
      88                 :           0 :             return static_cast<double>(*val);
      89                 :             :     }
      90                 :           0 :     return fallback;
      91                 :             : }
      92                 :             : 
      93                 :           0 : bool TomlValueView::as_bool(bool fallback) const noexcept {
      94                 :           0 :     if (!node_)
      95                 :           0 :         return fallback;
      96                 :           0 :     const auto* n = static_cast<const toml::node*>(node_);
      97                 :           0 :     if (!n->is_boolean())
      98                 :           0 :         return fallback;
      99                 :           0 :     return n->value<bool>().value_or(fallback);
     100                 :             : }
     101                 :             : 
     102                 :             : // -----------------------------------------------------------------------------
     103                 :             : // TomlTableView
     104                 :             : // -----------------------------------------------------------------------------
     105                 :         193 : TomlTableView::TomlTableView(const void* table) noexcept : table_(table) {}
     106                 :             : 
     107                 :          38 : TomlTableView make_toml_table_view(const void* table) noexcept {
     108                 :          38 :     return TomlTableView{table};
     109                 :             : }
     110                 :             : 
     111                 :         488 : bool TomlTableView::valid() const noexcept {
     112                 :         488 :     return table_ != nullptr;
     113                 :             : }
     114                 :             : 
     115                 :           3 : bool TomlTableView::contains(std::string_view key) const {
     116                 :           3 :     if (!table_)
     117                 :           0 :         return false;
     118                 :           3 :     return static_cast<const toml::table*>(table_)->contains(key);
     119                 :             : }
     120                 :             : 
     121                 :           0 : TomlValueView TomlTableView::value(std::string_view key) const {
     122                 :           0 :     if (!table_)
     123                 :           0 :         return TomlValueView{};
     124                 :           0 :     auto* node = static_cast<const toml::table*>(table_)->get(key);
     125                 :           0 :     return TomlValueView{node};
     126                 :             : }
     127                 :             : 
     128                 :         523 : TomlTableView TomlTableView::table(std::string_view key) const {
     129                 :         523 :     if (!table_)
     130                 :           0 :         return TomlTableView{};
     131                 :         523 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     132                 :         523 :     if (!node || !node->is_table())
     133                 :         426 :         return TomlTableView{};
     134                 :          97 :     return TomlTableView{node->as_table()};
     135                 :             : }
     136                 :             : 
     137                 :             : std::string
     138                 :         433 : TomlTableView::read_string(std::string_view key, std::string_view fallback) const {
     139                 :         433 :     if (!table_)
     140                 :           0 :         return std::string{fallback};
     141                 :         433 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     142                 :         433 :     if (!node || !node->is_string())
     143                 :         474 :         return std::string{fallback};
     144                 :         588 :     return std::string{node->value<std::string>().value_or(std::string{fallback})};
     145                 :             : }
     146                 :             : 
     147                 :         415 : uint32_t TomlTableView::read_uint32(std::string_view key,
     148                 :             :                                     uint32_t fallback) const noexcept {
     149                 :         415 :     if (!table_)
     150                 :           0 :         return fallback;
     151                 :         415 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     152                 :         415 :     if (!node || !node->is_integer())
     153                 :         361 :         return fallback;
     154                 :          54 :     auto val = node->value<int64_t>();
     155                 :          54 :     if (!val || *val < 0)
     156                 :           0 :         return fallback;
     157                 :          54 :     return static_cast<uint32_t>(*val);
     158                 :             : }
     159                 :             : 
     160                 :         126 : bool TomlTableView::read_bool(std::string_view key, bool fallback) const noexcept {
     161                 :         126 :     if (!table_)
     162                 :           0 :         return fallback;
     163                 :         126 :     auto* node = static_cast<const toml::table*>(table_)->get(key);
     164                 :         126 :     if (!node || !node->is_boolean())
     165                 :         106 :         return fallback;
     166                 :          20 :     return node->value<bool>().value_or(fallback);
     167                 :             : }
     168                 :             : 
     169                 :             : double
     170                 :           8 : TomlTableView::read_double(std::string_view key, double fallback) const noexcept {
     171                 :           8 :     if (!table_)
     172                 :           0 :         return fallback;
     173                 :           8 :     auto* node = static_cast<const toml::table*>(table_)->get(key);
     174                 :           8 :     if (!node)
     175                 :           0 :         return fallback;
     176                 :           8 :     if (node->is_floating_point())
     177                 :           8 :         return node->value<double>().value_or(fallback);
     178                 :           0 :     if (node->is_integer()) {
     179                 :           0 :         auto val = node->value<int64_t>();
     180                 :           0 :         if (val)
     181                 :           0 :             return static_cast<double>(*val);
     182                 :             :     }
     183                 :           0 :     return fallback;
     184                 :             : }
     185                 :             : 
     186                 :          36 : void TomlTableView::for_each_string_array(std::string_view key,
     187                 :             :                                           const StringArrayVisitor& visitor) const {
     188                 :          36 :     if (!table_)
     189                 :           0 :         return;
     190                 :          36 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     191                 :          36 :     if (!node || !node->is_array())
     192                 :          32 :         return;
     193                 :          20 :     for (const auto& v : *node->as_array()) {
     194                 :           6 :         if (v.is_string())
     195                 :           6 :             visitor(std::string_view{v.value<std::string>().value_or("")});
     196                 :             :     }
     197                 :             : }
     198                 :             : 
     199                 :          74 : void TomlTableView::for_each_table_array(std::string_view key,
     200                 :             :                                          const TableArrayVisitor& visitor) const {
     201                 :          74 :     if (!table_)
     202                 :           0 :         return;
     203                 :          74 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     204                 :          74 :     if (!node || !node->is_array())
     205                 :          35 :         return;
     206                 :         186 :     for (const auto& v : *node->as_array()) {
     207                 :          54 :         if (v.is_table())
     208                 :          54 :             visitor(TomlTableView{v.as_table()});
     209                 :             :     }
     210                 :             : }
     211                 :             : 
     212                 :          37 : void TomlTableView::for_each_subtable(std::string_view key,
     213                 :             :                                       const NamedTableVisitor& visitor) const {
     214                 :          37 :     if (!table_)
     215                 :           0 :         return;
     216                 :          37 :     auto node = static_cast<const toml::table*>(table_)->get(key);
     217                 :          37 :     if (!node || !node->is_table())
     218                 :          33 :         return;
     219                 :          20 :     for (const auto& [k, v] : *node->as_table()) {
     220                 :           4 :         if (v.is_table())
     221                 :           8 :             visitor(std::string_view{k.str()}, TomlTableView{v.as_table()});
     222                 :             :     }
     223                 :             : }
     224                 :             : 
     225                 :           0 : void TomlTableView::for_each_key_value(std::string_view key,
     226                 :             :                                        const KeyValueVisitor& visitor) const {
     227                 :           0 :     if (!table_)
     228                 :           0 :         return;
     229                 :           0 :     auto* node = static_cast<const toml::table*>(table_)->get(key);
     230                 :           0 :     if (!node || !node->is_table())
     231                 :           0 :         return;
     232                 :           0 :     for (const auto& [k, v] : *node->as_table()) {
     233                 :           0 :         visitor(std::string_view{k.str()}, TomlValueView{&v});
     234                 :             :     }
     235                 :             : }
     236                 :             : 
     237                 :           2 : void TomlTableView::for_each_integer_array(std::string_view key,
     238                 :             :                                            const IntegerArrayVisitor& visitor) const {
     239                 :           2 :     if (!table_)
     240                 :           0 :         return;
     241                 :           2 :     auto* node = static_cast<const toml::table*>(table_)->get(key);
     242                 :           2 :     if (!node || !node->is_array())
     243                 :           1 :         return;
     244                 :           6 :     for (const auto& v : *node->as_array()) {
     245                 :           2 :         if (v.is_integer()) {
     246                 :           2 :             auto val = v.value<int64_t>();
     247                 :           2 :             if (val)
     248                 :           2 :                 visitor(*val);
     249                 :             :         }
     250                 :             :     }
     251                 :             : }
     252                 :             : 
     253                 :           7 : void TomlTableView::for_each_entry(const KeyValueVisitor& visitor) const {
     254                 :           7 :     if (!table_)
     255                 :           0 :         return;
     256                 :           7 :     const auto* tbl = static_cast<const toml::table*>(table_);
     257                 :          32 :     for (const auto& [k, v] : *tbl) {
     258                 :          22 :         visitor(std::string_view{k.str()}, TomlValueView{&v});
     259                 :             :     }
     260                 :             : }
     261                 :             : 
     262                 :             : } // namespace hpactor::config
        

Generated by: LCOV version 2.0-1