Skip to content

Commit

Permalink
Remove operator += and -=, can be added as use-specific use-case as f…
Browse files Browse the repository at this point in the history
…ree operators
  • Loading branch information
d-frey committed May 18, 2016
1 parent 3c9f293 commit 5df4679
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 156 deletions.
41 changes: 0 additions & 41 deletions include/tao/json/internal/pair.hh

This file was deleted.

36 changes: 0 additions & 36 deletions include/tao/json/internal/single.hh

This file was deleted.

38 changes: 38 additions & 0 deletions include/tao/json/pair.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#ifndef TAOCPP_JSON_INCLUDE_PAIR_HH
#define TAOCPP_JSON_INCLUDE_PAIR_HH

#include <string>
#include <utility>
#include <type_traits>

#include "traits.hh"

namespace tao
{
namespace json
{
template< typename T >
struct pair
{
mutable std::string key;
mutable T value;

template< typename U >
pair( U && v ) : key( traits< typename std::decay< U >::type >::default_key ), value( std::forward< U >( v ) ) {}

pair( std::string && k, T && v ) : key( std::move( k ) ), value( std::move( v ) ) {}
pair( std::string && k, const T & v ) : key( std::move( k ) ), value( v ) {}
pair( const std::string & k, T && v ) : key( k ), value( std::move( v ) ) {}
pair( const std::string & k, const T & v ) : key( k ), value( v ) {}
pair( const char * k, T && v ) : key( k ), value( std::move( v ) ) {}
pair( const char * k, const T & v ) : key( k ), value( v ) {}
};

} // json

} // tao

#endif
32 changes: 32 additions & 0 deletions include/tao/json/single.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/json/

#ifndef TAOCPP_JSON_INCLUDE_SINGLE_HH
#define TAOCPP_JSON_INCLUDE_SINGLE_HH

#include <utility>

#include "pair.hh"

namespace tao
{
namespace json
{
template< typename T >
struct single
{
mutable T value;

template< typename U >
single( U && v ) : value( std::forward< U >( v ) ) {}

single( std::initializer_list< pair< T > > && l ) : value( std::move( l ) ) {}
single( const std::initializer_list< pair< T > > & l ) : value( l ) {}
single( std::initializer_list< pair< T > > & l ) : value( l ) {}
};

} // json

} // tao

#endif
89 changes: 28 additions & 61 deletions include/tao/json/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

#include "internal/totally_ordered.hh"
#include "internal/value_union.hh"
#include "internal/pair.hh"
#include "internal/single.hh"
#include "internal/get_by_enum.hh"
#include "internal/throw.hh"

#include "type.hh"
#include "traits.hh"
#include "pair.hh"
#include "single.hh"

namespace tao
{
Expand Down Expand Up @@ -73,33 +73,33 @@ namespace tao
unsafe_assign( std::forward< T >( v ) );
}

basic_value( std::initializer_list< internal::pair< basic_value > > && l )
basic_value( std::initializer_list< pair< basic_value > > && l )
{
unsafe_assign( std::move( l ) );
}

basic_value( const std::initializer_list< internal::pair< basic_value > > & l )
basic_value( const std::initializer_list< pair< basic_value > > & l )
{
unsafe_assign( l );
}

basic_value( std::initializer_list< internal::pair< basic_value > > & l )
: basic_value( static_cast< const std::initializer_list< internal::pair< basic_value > > & >( l ) )
basic_value( std::initializer_list< pair< basic_value > > & l )
: basic_value( static_cast< const std::initializer_list< pair< basic_value > > & >( l ) )
{ }

~basic_value() noexcept
{
unsafe_destroy();
}

static basic_value array( std::initializer_list< internal::single< basic_value > > && l )
static basic_value array( std::initializer_list< single< basic_value > > && l )
{
basic_value v;
v.append( std::move( l ) );
return v;
}

static basic_value array( const std::initializer_list< internal::single< basic_value > > & l )
static basic_value array( const std::initializer_list< single< basic_value > > & l )
{
basic_value v;
v.append( l );
Expand Down Expand Up @@ -431,21 +431,31 @@ namespace tao
Traits< D >::assign( *this, std::forward< T >( v ) );
}

void unsafe_assign( std::initializer_list< internal::pair< basic_value > > && l )
void unsafe_assign( std::initializer_list< pair< basic_value > > && l )
{
unsafe_emplace_object();
*this += std::move( l );
for( auto & e : l ) {
const auto r = unsafe_emplace( std::move( e.key ), std::move( e.value ) );
if ( ! r.second ) {
throw std::runtime_error( "duplicate key detected: " + r.first->first );
}
}
}

void unsafe_assign( const std::initializer_list< internal::pair< basic_value > > & l )
void unsafe_assign( const std::initializer_list< pair< basic_value > > & l )
{
unsafe_emplace_object();
*this += l;
for( auto & e : l ) {
const auto r = unsafe_emplace( e.key, e.value );
if ( ! r.second ) {
throw std::runtime_error( "duplicate key detected: " + r.first->first );
}
}
}

void unsafe_assign( std::initializer_list< internal::pair< basic_value > > & l )
void unsafe_assign( std::initializer_list< pair< basic_value > > & l )
{
unsafe_assign( static_cast< const std::initializer_list< internal::pair< basic_value > > & >( l ) );
unsafe_assign( static_cast< const std::initializer_list< pair< basic_value > > & >( l ) );
}

void unsafe_assign_null() noexcept
Expand Down Expand Up @@ -584,67 +594,24 @@ namespace tao
m_type = json::type::POINTER;
}

void append( std::initializer_list< internal::single< basic_value > > && l )
void append( std::initializer_list< single< basic_value > > && l )
{
unsafe_emplace_back_prepare();
auto & v = unsafe_get_array();
v.reserve( v.size() + l.size() );
for( auto & e : l ) {
unsafe_emplace_back( std::move( e.e ) );
unsafe_emplace_back( std::move( e.value ) );
}
}

void append( const std::initializer_list< internal::single< basic_value > > & l )
void append( const std::initializer_list< single< basic_value > > & l )
{
unsafe_emplace_back_prepare();
auto & v = unsafe_get_array();
v.reserve( v.size() + l.size() );
for( const auto & e : l ) {
unsafe_emplace_back( e.e );
}
}

basic_value & operator+= ( std::initializer_list< internal::pair< basic_value > > && l )
{
unsafe_emplace_prepare();
for( auto & e : l ) {
const auto r = unsafe_emplace( std::move( e.e.first ), std::move( e.e.second ) );
if ( ! r.second ) {
throw std::runtime_error( "duplicate key detected: " + r.first->first );
}
}
return *this;
}

basic_value & operator+= ( const std::initializer_list< internal::pair< basic_value > > & l )
{
unsafe_emplace_prepare();
for( const auto & e : l ) {
const auto r = unsafe_emplace( e.e.first, e.e.second );
if ( ! r.second ) {
throw std::runtime_error( "duplicate key detected: " + r.first->first );
}
}
return *this;
}

basic_value & operator-= ( const std::string & k )
{
if ( get_object().erase( k ) == 0 ) {
throw std::runtime_error( "key not found: " + k );
}
return *this;
}

basic_value & operator-= ( std::initializer_list< std::string > l )
{
auto & v = get_object();
for ( const auto & k : l ) {
if ( v.erase( k ) == 0 ) {
throw std::runtime_error( "key not found: " + k );
}
unsafe_emplace_back( e.value );
}
return *this;
}

bool empty() const noexcept
Expand Down
18 changes: 0 additions & 18 deletions src/test/json/create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,24 +469,6 @@ namespace tao
value b( a );
TEST_ASSERT( a == b );
TEST_ASSERT( a.get_object() == b.get_object() );
} {
value a;
a += { { "foo", 5 } };
TEST_ASSERT( a.type() == type::OBJECT );
TEST_ASSERT( a[ "foo" ] == 5 );
value b( a );
TEST_ASSERT( a == b );
TEST_ASSERT( a.get_object() == b.get_object() );
a += { { "bar", 42 } };
TEST_ASSERT( a[ "bar" ] == 42 );
TEST_THROWS( a.at( "baz" ) );
a -= "bar";
TEST_THROWS( a.at( "bar" ) );
a += { { "bar", 42 } };
a -= { "bar", "foo" };
a += { { "foo", 5 } };
TEST_THROWS(( a += { { "foo", 5 } } ));
TEST_THROWS( a -= "bar" );
} {
const value a( "foo" );
const value b( a );
Expand Down

0 comments on commit 5df4679

Please sign in to comment.