Code View

fson / source / fson-1.1.0.0 / libs / internal / sdk / fson / Array.cpp
// SPDX-License-Identifier: MIT
#include "Array.hh"

#include <stdexcept>

using namespace std;

namespace fedem
{
  namespace fson
  {
    // ── Element ──────────────────────────────────────────────────────────

    Element::Element( unique_ptr< Value > value )
    : value( std::move( value ) )
    {
      if( !this->value )
        throw invalid_argument( "fson::Element: value must not be null" );
    }

    Element Element::clone( ) const
    {
      Element copy( value->clone( ) );
      copy.leading  = leading;
      copy.trailing = trailing;
      return copy;
    }

    Value& Element::getValue( ) noexcept
    {
      return *value;
    }

    Value const& Element::getValue( ) const noexcept
    {
      return *value;
    }

    void Element::setValue( unique_ptr< Value > value )
    {
      if( !value )
        throw invalid_argument( "fson::Element: value must not be null" );
      this->value = std::move( value );
    }

    CommentList& Element::getLeadingComments( ) noexcept
    {
      return leading;
    }

    CommentList const& Element::getLeadingComments( ) const noexcept
    {
      return leading;
    }

    optional< Comment >& Element::getTrailingComment( ) noexcept
    {
      return trailing;
    }

    optional< Comment > const& Element::getTrailingComment( ) const noexcept
    {
      return trailing;
    }

    // ── Array ────────────────────────────────────────────────────────────

    unique_ptr< Value > Array::clone( ) const
    {
      auto copy = make_unique< Array >( );
      copy->elements.reserve( elements.size( ) );
      for( auto const& element : elements )
        copy->elements.push_back( element.clone( ) );
      copy->dangling = dangling;
      return copy;
    }

    Value& Array::at( size_t index )
    {
      return getElement( index ).getValue( );
    }

    Value const& Array::at( size_t index ) const
    {
      return getElement( index ).getValue( );
    }

    Element& Array::getElement( size_t index )
    {
      if( index >= elements.size( ) )
        throw out_of_range( "fson::Array: index out of range" );
      return elements[ index ];
    }

    Element const& Array::getElement( size_t index ) const
    {
      if( index >= elements.size( ) )
        throw out_of_range( "fson::Array: index out of range" );
      return elements[ index ];
    }

    Value& Array::append( unique_ptr< Value > value )
    {
      elements.emplace_back( std::move( value ) );
      return elements.back( ).getValue( );
    }

    Value& Array::insert( size_t index, unique_ptr< Value > value )
    {
      if( index > elements.size( ) )
        throw out_of_range( "fson::Array: insertion index out of range" );
      auto const position = elements.insert(
        elements.begin( ) + static_cast< ptrdiff_t >( index ),
        Element( std::move( value ) ) );
      return position->getValue( );
    }

    void Array::erase( size_t index )
    {
      if( index >= elements.size( ) )
        throw out_of_range( "fson::Array: index out of range" );
      elements.erase( elements.begin( ) + static_cast< ptrdiff_t >( index ) );
    }

    void Array::clear( ) noexcept
    {
      elements.clear( );
    }
  }  // end namespace fson
}  // end namespace fedem