Skip to content

Commit

Permalink
Simplify.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinH committed Mar 9, 2024
1 parent c91fac9 commit e6f0bd9
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions include/tao/config/internal/phase1_append.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@
namespace tao::config::internal
{
template< typename T >
bool phase1_append( concat& c, const key1& path, const T& thing, const phase1_mode mode );
void phase1_append( concat& c, const key1& path, const T& thing, const phase1_mode mode );

template< typename T >
bool phase1_append_star( concat& c, const pegtl::position& p, const key1& path, const T& thing, const phase1_mode mode )
void phase1_append_star( concat& c, const pegtl::position& p, const key1& path, const T& thing, const phase1_mode mode )
{
c.back_ensure_kind( entry_kind::concat, p );
return phase1_append( c.concat.back().get_concat(), path, thing, mode );
phase1_append( c.concat.back().get_concat(), path, thing, mode );
}

template< typename T >
bool phase1_append_name( concat& c, const pegtl::position& p, const std::string& name, const key1& path, const T& thing, const phase1_mode mode )
void phase1_append_name( concat& c, const pegtl::position& p, const std::string& name, const key1& path, const T& thing, const phase1_mode mode )
{
c.back_ensure_kind( entry_kind::object, p );
const auto pair = c.concat.back().get_object().object.try_emplace( name, p );
pair.first->second.implicit = ( mode == phase1_mode::implicit ) && ( pair.second || pair.first->second.implicit );
return phase1_append( pair.first->second, path, thing, mode );
phase1_append( pair.first->second, path, thing, mode );
}

template< typename T >
bool phase1_append_index( concat& c, const pegtl::position& p, const std::size_t index, const key1& path, const T& thing, const phase1_mode mode )
void phase1_append_index( concat& c, const pegtl::position& p, const std::size_t index, const key1& path, const T& thing, const phase1_mode mode )
{
std::size_t n = index;

Expand All @@ -59,7 +59,8 @@ namespace tao::config::internal
throw pegtl::parse_error( "cannot index (across) reference", p );
case entry_kind::array:
if( e.get_array().array.size() > n ) {
return phase1_append( *std::next( e.get_array().array.begin(), n ), path, thing, mode );
phase1_append( *std::next( e.get_array().array.begin(), n ), path, thing, mode );
return;
}
n -= e.get_array().array.size();
continue;
Expand All @@ -73,39 +74,43 @@ namespace tao::config::internal
}

template< typename T >
bool phase1_append_append( concat& c, const pegtl::position& p, const std::uint64_t g, const key1& path, const T& thing, const phase1_mode mode )
void phase1_append_append( concat& c, const pegtl::position& p, const std::uint64_t g, const key1& path, const T& thing, const phase1_mode mode )
{
c.back_ensure_kind( entry_kind::array, p );
auto& a = c.concat.back().get_array();
if( g > c.generation ) {
c.generation = g;
concat& d = a.array.emplace_back( p );
d.remove = false; // TODO: Make consistent with entry::expand()?
return phase1_append( d, path, thing, mode );
phase1_append( d, path, thing, mode );
return;
}
assert( !a.array.empty() );

return phase1_append( a.array.back(), path, thing, mode );
phase1_append( a.array.back(), path, thing, mode );
}

template< typename T >
bool phase1_append( concat& c, const key1& path, const T& thing, const phase1_mode mode )
void phase1_append( concat& c, const key1& path, const T& thing, const phase1_mode mode )
{
if( path.empty() ) {
thing( c );
return true;
return;
}
const auto& part = path.at( 0 );

switch( part.kind() ) {
case key1_kind::star:
return phase1_append_star( c, part.position, pop_front( path ), thing, mode );
phase1_append_star( c, part.position, pop_front( path ), thing, mode );
return;
case key1_kind::name:
return phase1_append_name( c, part.position, part.get_name(), pop_front( path ), thing, mode );
phase1_append_name( c, part.position, part.get_name(), pop_front( path ), thing, mode );
return;
case key1_kind::index:
return phase1_append_index( c, part.position, part.get_index(), pop_front( path ), thing, mode );
phase1_append_index( c, part.position, part.get_index(), pop_front( path ), thing, mode );
return;
case key1_kind::append:
return phase1_append_append( c, part.position, part.get_generation(), pop_front( path ), thing, mode );
phase1_append_append( c, part.position, part.get_generation(), pop_front( path ), thing, mode );
return;
}
throw std::logic_error( "code should be unreachable" ); // LCOV_EXCL_LINE
}
Expand Down

0 comments on commit e6f0bd9

Please sign in to comment.