2017-12-20 17:58:32 +08:00
// sol2
2017-07-01 23:02:15 +08:00
// The MIT License (MIT)
// Copyright (c) 2013-2017 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ifndef SOL_CONTAINER_TRAITS_HPP
# define SOL_CONTAINER_TRAITS_HPP
2017-07-10 00:00:57 +08:00
# include "traits.hpp"
2017-07-01 23:02:15 +08:00
# include "stack.hpp"
# include <unordered_map>
namespace sol {
2017-08-06 07:20:28 +08:00
template < typename T >
struct container_traits ;
2017-08-11 15:24:17 +08:00
template < typename T >
struct as_container_t {
T source ;
2017-09-13 14:46:56 +08:00
as_container_t ( T value )
: source ( std : : move ( value ) ) {
}
2017-08-11 15:24:17 +08:00
2017-09-13 14:46:56 +08:00
operator std : : add_rvalue_reference_t < T > ( ) {
2017-08-11 15:24:17 +08:00
return std : : move ( source ) ;
}
2017-09-13 14:46:56 +08:00
operator std : : add_lvalue_reference_t < std : : add_const_t < T > > ( ) const {
2017-08-11 15:24:17 +08:00
return source ;
}
} ;
template < typename T >
struct as_container_t < T & > {
std : : reference_wrapper < T > source ;
2017-09-13 14:46:56 +08:00
as_container_t ( T & value )
: source ( value ) {
}
2017-08-11 15:24:17 +08:00
operator T & ( ) {
return source ;
}
} ;
template < typename T >
auto as_container ( T & & value ) {
return as_container_t < T > ( std : : forward < T > ( value ) ) ;
}
2017-07-10 00:00:57 +08:00
namespace container_detail {
2017-08-11 15:24:17 +08:00
2017-08-06 07:20:28 +08:00
template < typename T >
struct has_clear_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : clear ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
2017-07-10 00:00:57 +08:00
template < typename T >
2017-08-11 15:24:17 +08:00
struct has_empty_test {
2017-08-06 07:20:28 +08:00
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : empty ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_erase_after_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( std : : declval < C > ( ) . erase_after ( std : : declval < std : : add_rvalue_reference_t < typename C : : const_iterator > > ( ) ) ) * ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T , typename = void >
struct has_find_test {
2017-07-10 00:00:57 +08:00
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( std : : declval < C > ( ) . find ( std : : declval < std : : add_rvalue_reference_t < typename C : : value_type > > ( ) ) ) * ) ;
template < typename C >
static two test ( . . . ) ;
2017-07-10 00:00:57 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
2017-08-06 07:20:28 +08:00
struct has_find_test < T , std : : enable_if_t < meta : : is_lookup < T > : : value > > {
2017-07-10 00:00:57 +08:00
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( std : : declval < C > ( ) . find ( std : : declval < std : : add_rvalue_reference_t < typename C : : key_type > > ( ) ) ) * ) ;
template < typename C >
static two test ( . . . ) ;
2017-07-10 00:00:57 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
2017-08-06 07:20:28 +08:00
struct has_erase_test {
2017-07-10 00:00:57 +08:00
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( std : : declval < C > ( ) . erase ( std : : declval < typename C : : iterator > ( ) ) ) * ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_find_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : find ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-07-10 00:00:57 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
2017-08-06 07:20:28 +08:00
struct has_traits_insert_test {
2017-07-10 00:00:57 +08:00
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : insert ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-07-10 00:00:57 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
2017-08-06 07:20:28 +08:00
template < typename T >
struct has_traits_erase_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : erase ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_index_set_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : index_set ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_index_get_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : index_get ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_set_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : set ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_get_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : get ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
struct has_traits_pairs_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : pairs ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
2017-09-07 03:09:51 +08:00
template < typename T >
struct has_traits_ipairs_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : ipairs ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-09-07 03:09:51 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
2017-08-06 07:20:28 +08:00
template < typename T >
struct has_traits_add_test {
private :
typedef std : : array < char , 1 > one ;
typedef std : : array < char , 2 > two ;
2017-09-13 14:46:56 +08:00
template < typename C >
static one test ( decltype ( & C : : add ) ) ;
template < typename C >
static two test ( . . . ) ;
2017-08-06 07:20:28 +08:00
public :
static const bool value = sizeof ( test < T > ( 0 ) ) = = sizeof ( char ) ;
} ;
template < typename T >
2017-08-11 15:24:17 +08:00
using has_clear = meta : : boolean < has_clear_test < T > : : value > ;
2017-08-06 07:20:28 +08:00
template < typename T >
2017-08-11 15:24:17 +08:00
using has_empty = meta : : boolean < has_empty_test < T > : : value > ;
2017-08-06 07:20:28 +08:00
template < typename T >
using has_find = meta : : boolean < has_find_test < T > : : value > ;
template < typename T >
using has_erase = meta : : boolean < has_erase_test < T > : : value > ;
template < typename T >
using has_erase_after = meta : : boolean < has_erase_after_test < T > : : value > ;
template < typename T >
using has_traits_get = meta : : boolean < has_traits_get_test < T > : : value > ;
template < typename T >
using has_traits_set = meta : : boolean < has_traits_set_test < T > : : value > ;
template < typename T >
using has_traits_index_get = meta : : boolean < has_traits_index_get_test < T > : : value > ;
template < typename T >
using has_traits_index_set = meta : : boolean < has_traits_index_set_test < T > : : value > ;
template < typename T >
using has_traits_pairs = meta : : boolean < has_traits_pairs_test < T > : : value > ;
2017-09-07 03:09:51 +08:00
template < typename T >
using has_traits_ipairs = meta : : boolean < has_traits_ipairs_test < T > : : value > ;
2017-08-06 07:20:28 +08:00
template < typename T >
using has_traits_add = meta : : boolean < has_traits_add_test < T > : : value > ;
template < typename T >
2017-08-11 15:24:17 +08:00
using has_traits_size = meta : : has_size < T > ;
2017-08-06 07:20:28 +08:00
template < typename T >
using has_traits_clear = has_clear < T > ;
2017-08-11 15:24:17 +08:00
template < typename T >
using has_traits_empty = has_empty < T > ;
2017-08-06 07:20:28 +08:00
template < typename T >
using has_traits_find = meta : : boolean < has_traits_find_test < T > : : value > ;
template < typename T >
using has_traits_insert = meta : : boolean < has_traits_insert_test < T > : : value > ;
template < typename T >
using has_traits_erase = meta : : boolean < has_traits_erase_test < T > : : value > ;
2017-08-11 15:24:17 +08:00
template < typename T >
struct is_forced_container : is_container < T > { } ;
template < typename T >
struct is_forced_container < as_container_t < T > > : std : : true_type { } ;
template < typename T >
struct container_decay {
typedef T type ;
} ;
template < typename T >
struct container_decay < as_container_t < T > > {
typedef T type ;
} ;
template < typename T >
using container_decay_t = typename container_decay < meta : : unqualified_t < T > > : : type ;
2017-08-06 07:20:28 +08:00
template < typename T >
decltype ( auto ) get_key ( std : : false_type , T & & t ) {
return std : : forward < T > ( t ) ;
}
template < typename T >
decltype ( auto ) get_key ( std : : true_type , T & & t ) {
return t . first ;
}
template < typename T >
decltype ( auto ) get_value ( std : : false_type , T & & t ) {
return std : : forward < T > ( t ) ;
}
template < typename T >
decltype ( auto ) get_value ( std : : true_type , T & & t ) {
return t . second ;
}
2017-12-07 21:24:50 +08:00
struct error_result {
int results ;
const char * fmt ;
std : : array < const char * , 4 > args ;
2017-12-07 22:16:56 +08:00
error_result ( ) : results ( 0 ) , fmt ( nullptr ) {
2017-12-07 21:24:50 +08:00
}
2017-12-07 22:16:56 +08:00
error_result ( int results ) : results ( results ) , fmt ( nullptr ) {
2017-12-07 21:24:50 +08:00
}
2017-12-07 22:36:12 +08:00
error_result ( const char * fmt , const char * msg ) : results ( 0 ) , fmt ( fmt ) {
args [ 0 ] = msg ;
2017-12-07 21:24:50 +08:00
}
} ;
inline int handle_errors ( lua_State * L , const error_result & er ) {
if ( er . fmt = = nullptr ) {
return er . results ;
}
return luaL_error ( L , er . fmt , er . args [ 0 ] , er . args [ 1 ] , er . args [ 2 ] , er . args [ 3 ] ) ;
}
2017-07-10 00:00:57 +08:00
template < typename X , typename = void >
struct container_traits_default {
private :
typedef std : : remove_pointer_t < meta : : unwrap_unqualified_t < X > > T ;
2017-09-13 14:46:56 +08:00
2017-07-10 00:00:57 +08:00
public :
typedef lua_nil_t iterator ;
typedef lua_nil_t value_type ;
2017-08-06 07:20:28 +08:00
static int get ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'get(key) ' on type ' % s ' : it is not recognized as a container " , detail::demangle<T>().c_str()) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int index_get ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'container[key]' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int set ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'set(key, value) ' on type ' % s ' : it is not recognized as a container " , detail::demangle<T>().c_str()) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int index_set ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'container[key] = value' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int add ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'add' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int insert ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'insert' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int find ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'find' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int size ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'end' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int clear ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'clear' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-08-11 15:24:17 +08:00
static int empty ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'empty' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-08-06 07:20:28 +08:00
static int erase ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'erase' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int pairs ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call '__pairs' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-09-07 03:09:51 +08:00
static int ipairs ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call '__ipairs' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-08-07 14:27:08 +08:00
static iterator begin ( lua_State * L , T & ) {
2017-08-06 07:20:28 +08:00
luaL_error ( L , " sol: cannot call 'being' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
return lua_nil ;
}
2017-08-07 14:27:08 +08:00
static iterator end ( lua_State * L , T & ) {
2017-08-06 07:20:28 +08:00
luaL_error ( L , " sol: cannot call 'end' on type '%s': it is not recognized as a container " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
return lua_nil ;
}
} ;
2017-08-06 07:20:28 +08:00
2017-07-10 00:00:57 +08:00
template < typename X >
2017-09-13 14:46:56 +08:00
struct container_traits_default < X , std : : enable_if_t < meta : : all < is_forced_container < meta : : unqualified_t < X > > , meta : : has_value_type < meta : : unqualified_t < container_decay_t < X > > > , meta : : has_iterator < meta : : unqualified_t < container_decay_t < X > > > > : : value > > {
2017-07-10 00:00:57 +08:00
private :
2017-08-11 15:24:17 +08:00
typedef std : : remove_pointer_t < meta : : unwrap_unqualified_t < container_decay_t < X > > > T ;
2017-09-13 14:46:56 +08:00
2017-08-06 07:20:28 +08:00
private :
2017-08-11 15:24:17 +08:00
typedef container_traits < X > deferred_traits ;
typedef meta : : is_associative < T > is_associative ;
2017-08-06 07:20:28 +08:00
typedef meta : : is_lookup < T > is_lookup ;
2017-12-20 17:58:32 +08:00
typedef meta : : is_matched_lookup < T > is_matched_lookup ;
2017-07-10 00:00:57 +08:00
typedef typename T : : iterator iterator ;
2017-08-06 07:20:28 +08:00
typedef typename T : : value_type value_type ;
2017-12-20 17:58:32 +08:00
typedef std : : conditional_t < is_matched_lookup : : value ,
std : : pair < value_type , value_type > ,
std : : conditional_t < is_associative : : value | | is_lookup : : value ,
value_type ,
std : : pair < std : : ptrdiff_t , value_type >
>
> KV ;
2017-07-10 00:00:57 +08:00
typedef typename KV : : first_type K ;
typedef typename KV : : second_type V ;
2018-01-24 14:10:35 +08:00
typedef std : : conditional_t < is_matched_lookup : : value , std : : ptrdiff_t , K > next_K ;
2017-08-06 07:20:28 +08:00
typedef decltype ( * std : : declval < iterator & > ( ) ) iterator_return ;
2017-12-20 17:58:32 +08:00
typedef std : : conditional_t < is_associative : : value | | is_matched_lookup : : value ,
std : : add_lvalue_reference_t < V > ,
std : : conditional_t < is_lookup : : value ,
V ,
iterator_return
>
> captured_type ;
2017-08-06 07:20:28 +08:00
typedef typename meta : : iterator_tag < iterator > : : type iterator_category ;
typedef std : : is_same < iterator_category , std : : input_iterator_tag > is_input_iterator ;
2017-09-13 14:46:56 +08:00
typedef std : : conditional_t < is_input_iterator : : value ,
2017-08-07 14:07:21 +08:00
V ,
2017-12-20 17:58:32 +08:00
decltype ( detail : : deref ( std : : declval < captured_type > ( ) ) )
> push_type ;
2017-08-06 07:20:28 +08:00
typedef std : : is_copy_assignable < V > is_copyable ;
typedef meta : : neg < meta : : any <
2017-12-20 17:58:32 +08:00
std : : is_const < V > , std : : is_const < std : : remove_reference_t < iterator_return > > , meta : : neg < is_copyable >
> > is_writable ;
2017-08-06 07:20:28 +08:00
typedef meta : : unqualified_t < decltype ( get_key ( is_associative ( ) , std : : declval < std : : add_lvalue_reference_t < value_type > > ( ) ) ) > key_type ;
typedef meta : : all < std : : is_integral < K > , meta : : neg < meta : : any < is_associative , is_lookup > > > is_linear_integral ;
struct iter {
T & source ;
iterator it ;
2017-09-07 03:09:51 +08:00
std : : size_t i ;
2017-08-06 07:20:28 +08:00
2017-09-13 14:46:56 +08:00
iter ( T & source , iterator it )
: source ( source ) , it ( std : : move ( it ) ) , i ( 0 ) {
}
2017-08-06 07:20:28 +08:00
} ;
2017-07-10 00:00:57 +08:00
static auto & get_src ( lua_State * L ) {
# ifdef SOL_SAFE_USERTYPE
2017-12-11 04:56:49 +08:00
auto p = stack : : check_get < T * > ( L , 1 ) ;
2017-08-11 15:24:17 +08:00
if ( ! p ) {
luaL_error ( L , " sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type) " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
if ( p . value ( ) = = nullptr ) {
luaL_error ( L , " sol: 'self' argument is nil (pass 'self' as first argument with ':' or call on a '%s' type) " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
return * p . value ( ) ;
# else
2017-12-20 17:58:32 +08:00
return stack : : get < T > ( L , 1 ) ;
2017-07-10 00:00:57 +08:00
# endif // Safe getting with error
}
2017-12-07 21:24:50 +08:00
static error_result get_associative ( std : : true_type , lua_State * L , iterator & it ) {
2017-08-06 07:20:28 +08:00
auto & v = * it ;
2017-08-11 15:24:17 +08:00
return stack : : stack_detail : : push_reference < push_type > ( L , detail : : deref ( v . second ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-07-10 00:00:57 +08:00
2017-12-07 21:24:50 +08:00
static error_result get_associative ( std : : false_type , lua_State * L , iterator & it ) {
2017-08-11 15:24:17 +08:00
return stack : : stack_detail : : push_reference < push_type > ( L , detail : : deref ( * it ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result get_category ( std : : input_iterator_tag , lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
if ( key < 1 ) {
return stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
auto it = deferred_traits : : begin ( L , self ) ;
auto e = deferred_traits : : end ( L , self ) ;
2017-08-06 07:20:28 +08:00
if ( it = = e ) {
return stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
while ( key > 1 ) {
- - key ;
+ + it ;
if ( it = = e ) {
2017-07-10 00:00:57 +08:00
return stack : : push ( L , lua_nil ) ;
}
}
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-12-07 21:24:50 +08:00
static error_result get_category ( std : : random_access_iterator_tag , lua_State * L , T & self , K & key ) {
2017-08-07 14:07:21 +08:00
std : : ptrdiff_t len = static_cast < std : : ptrdiff_t > ( size_start ( L , self ) ) ;
2017-08-06 07:20:28 +08:00
if ( key < 1 | | key > len ) {
return stack : : push ( L , lua_nil ) ;
}
- - key ;
2017-12-07 21:24:50 +08:00
auto it = std : : next ( deferred_traits : : begin ( L , self ) , key ) ;
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-12-07 21:24:50 +08:00
static error_result get_it ( std : : true_type , lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
return get_category ( iterator_category ( ) , L , self , key ) ;
}
2017-12-07 21:24:50 +08:00
static error_result get_comparative ( std : : true_type , lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
auto fx = [ & ] ( const value_type & r ) - > bool {
return key = = get_key ( is_associative ( ) , r ) ;
} ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
auto it = std : : find_if ( deferred_traits : : begin ( L , self ) , e , std : : ref ( fx ) ) ;
2017-08-06 07:20:28 +08:00
if ( it = = e ) {
return stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-07-10 00:00:57 +08:00
2017-12-07 21:24:50 +08:00
static error_result get_comparative ( std : : false_type , lua_State * , T & , K & ) {
return error_result ( " cannot get this key on '%s': no suitable way to increment iterator and compare to key value '%s' " , detail : : demangle < T > ( ) . data ( ) , detail : : demangle < K > ( ) . data ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result get_it ( std : : false_type , lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
return get_comparative ( meta : : supports_op_equal < K , key_type > ( ) , L , self , key ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative ( std : : true_type , iterator & it , stack_object value ) {
2017-08-06 07:20:28 +08:00
auto & v = * it ;
v . second = value . as < V > ( ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative ( std : : false_type , iterator & it , stack_object value ) {
2017-08-06 07:20:28 +08:00
auto & v = * it ;
v = value . as < V > ( ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_writable ( std : : true_type , lua_State * , T & , iterator & it , stack_object value ) {
return set_associative ( is_associative ( ) , it , std : : move ( value ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_writable ( std : : false_type , lua_State * , T & , iterator & , stack_object ) {
return error_result ( " cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const) " , detail::demangle<T>().data()) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_category ( std : : input_iterator_tag , lua_State * L , T & self , stack_object okey , stack_object value ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) key = okey . as < K > ( ) ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
auto it = deferred_traits : : begin ( L , self ) ;
2017-08-06 07:20:28 +08:00
auto backit = it ;
for ( ; key > 1 & & it ! = e ; - - key , + + it ) {
backit = it ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
if ( it = = e ) {
if ( key = = 1 ) {
2017-12-07 21:24:50 +08:00
return add_copyable ( is_copyable ( ) , L , self , std : : move ( value ) , meta : : has_insert_after < T > : : value ? backit : it ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
return error_result ( " out of bounds (too big) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
return set_writable ( is_writable ( ) , L , self , it , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_category ( std : : random_access_iterator_tag , lua_State * L , T & self , stack_object okey , stack_object value ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) key = okey . as < K > ( ) ;
if ( key < 1 ) {
2017-12-07 21:24:50 +08:00
return error_result ( " sol: out of bounds (too small) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
- - key ;
2017-08-07 14:07:21 +08:00
std : : ptrdiff_t len = static_cast < std : : ptrdiff_t > ( size_start ( L , self ) ) ;
2017-08-06 07:20:28 +08:00
if ( key = = len ) {
2017-12-07 21:24:50 +08:00
return add_copyable ( is_copyable ( ) , L , self , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
else if ( key > len ) {
2017-12-07 21:24:50 +08:00
return error_result ( " sol: out of bounds (too big) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
auto it = std : : next ( deferred_traits : : begin ( L , self ) , key ) ;
return set_writable ( is_writable ( ) , L , self , it , std : : move ( value ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 22:04:19 +08:00
static error_result set_comparative ( std : : true_type , lua_State * L , T & self , stack_object okey , stack_object value ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) key = okey . as < K > ( ) ;
if ( ! is_writable : : value ) {
2017-12-07 21:24:50 +08:00
return error_result ( " cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const) " , detail::demangle<T>().data()) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
auto fx = [ & ] ( const value_type & r ) - > bool {
return key = = get_key ( is_associative ( ) , r ) ;
} ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
auto it = std : : find_if ( deferred_traits : : begin ( L , self ) , e , std : : ref ( fx ) ) ;
2017-08-06 07:20:28 +08:00
if ( it = = e ) {
2017-12-07 21:24:50 +08:00
return { } ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
return set_writable ( is_writable ( ) , L , self , it , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_comparative ( std : : false_type , lua_State * , T & , stack_object , stack_object ) {
return error_result ( " cannot set this value on '%s': no suitable way to increment iterator or compare to '%s' key " , detail : : demangle < T > ( ) . data ( ) , detail : : demangle < K > ( ) . data ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative_insert ( std : : true_type , lua_State * , T & self , iterator & it , K & key , stack_object value ) {
2017-08-06 07:20:28 +08:00
self . insert ( it , value_type ( key , value . as < V > ( ) ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative_insert ( std : : false_type , lua_State * , T & self , iterator & it , K & key , stack_object ) {
2017-08-06 07:20:28 +08:00
self . insert ( it , key ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative_find ( std : : true_type , lua_State * L , T & self , stack_object okey , stack_object value ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) key = okey . as < K > ( ) ;
auto it = self . find ( key ) ;
2017-12-07 21:24:50 +08:00
if ( it = = deferred_traits : : end ( L , self ) ) {
return set_associative_insert ( is_associative ( ) , L , self , it , key , std : : move ( value ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
return set_writable ( is_writable ( ) , L , self , it , std : : move ( value ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_associative_find ( std : : false_type , lua_State * L , T & self , stack_object key , stack_object value ) {
return set_comparative ( meta : : supports_op_equal < K , key_type > ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_it ( std : : true_type , lua_State * L , T & self , stack_object key , stack_object value ) {
return set_category ( iterator_category ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result set_it ( std : : false_type , lua_State * L , T & self , stack_object key , stack_object value ) {
return set_associative_find ( meta : : all < has_find < T > , meta : : any < is_associative , is_lookup > > ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result find_has_associative_lookup ( std : : true_type , lua_State * L , T & self ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) key = stack : : get < K > ( L , 2 ) ;
auto it = self . find ( key ) ;
2017-12-07 21:24:50 +08:00
if ( it = = deferred_traits : : end ( L , self ) ) {
2017-08-06 07:20:28 +08:00
return stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_has_associative_lookup ( std : : false_type , lua_State * L , T & self ) {
2017-08-06 07:20:28 +08:00
decltype ( auto ) value = stack : : get < V > ( L , 2 ) ;
auto it = self . find ( value ) ;
2017-12-07 21:24:50 +08:00
if ( it = = deferred_traits : : end ( L , self ) ) {
2017-08-06 07:20:28 +08:00
return stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_has ( std : : true_type , lua_State * L , T & self ) {
2017-08-06 07:20:28 +08:00
return find_has_associative_lookup ( meta : : any < is_lookup , is_associative > ( ) , L , self ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_associative_lookup ( std : : true_type , lua_State * L , iterator & it , std : : size_t ) {
2017-08-06 07:20:28 +08:00
return get_associative ( is_associative ( ) , L , it ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_associative_lookup ( std : : false_type , lua_State * L , iterator & , std : : size_t index ) {
2017-08-06 07:20:28 +08:00
return stack : : push ( L , index ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_comparative ( std : : false_type , lua_State * , T & ) {
return error_result ( " cannot call 'find' on '%s': there is no 'find' function and the value_type is not equality comparable " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result find_comparative ( std : : true_type , lua_State * L , T & self ) {
2017-08-11 15:24:17 +08:00
decltype ( auto ) value = stack : : get < V > ( L , 2 ) ;
2017-12-07 21:24:50 +08:00
auto it = deferred_traits : : begin ( L , self ) ;
auto e = deferred_traits : : end ( L , self ) ;
2017-08-06 07:20:28 +08:00
std : : size_t index = 1 ;
for ( ; ; + + it , + + index ) {
if ( it = = e ) {
return stack : : push ( L , lua_nil ) ;
}
if ( value = = get_value ( is_associative ( ) , * it ) ) {
break ;
}
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
return find_associative_lookup ( meta : : any < is_lookup , is_associative > ( ) , L , it , index ) ;
}
2017-12-07 21:24:50 +08:00
static error_result find_has ( std : : false_type , lua_State * L , T & self ) {
2017-08-06 07:20:28 +08:00
return find_comparative ( meta : : supports_op_equal < V > ( ) , L , self ) ;
}
2017-12-07 21:24:50 +08:00
static error_result add_insert_after ( std : : false_type , lua_State * L , T & self , stack_object value , iterator & ) {
return add_insert_after ( std : : false_type ( ) , L , self , value ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert_after ( std : : false_type , lua_State * , T & , stack_object ) {
return error_result ( " cannot call 'add' on type '%s': no suitable insert/push_back C++ functions " , detail : : demangle < T > ( ) . data ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert_after ( std : : true_type , lua_State * , T & self , stack_object value , iterator & at ) {
2017-08-06 07:20:28 +08:00
self . insert_after ( at , value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert_after ( std : : true_type , lua_State * L , T & self , stack_object value ) {
2017-08-06 07:20:28 +08:00
auto backit = self . before_begin ( ) ;
{
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
for ( auto it = deferred_traits : : begin ( L , self ) ; it ! = e ; + + backit , + + it ) {
2017-09-13 14:46:56 +08:00
}
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
return add_insert_after ( std : : true_type ( ) , L , self , value , backit ) ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert ( std : : true_type , lua_State * , T & self , stack_object value , iterator & at ) {
2017-08-06 07:20:28 +08:00
self . insert ( at , value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-07-10 00:00:57 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert ( std : : true_type , lua_State * L , T & self , stack_object value ) {
auto at = deferred_traits : : end ( L , self ) ;
return add_insert ( std : : true_type ( ) , L , self , value , at ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_insert ( std : : false_type , lua_State * L , T & self , stack_object value , iterator & at ) {
2017-08-06 07:20:28 +08:00
return add_insert_after ( meta : : has_insert_after < T > ( ) , L , self , std : : move ( value ) , at ) ;
}
2017-12-07 21:24:50 +08:00
static error_result add_insert ( std : : false_type , lua_State * L , T & self , stack_object value ) {
2017-08-06 07:20:28 +08:00
return add_insert_after ( meta : : has_insert_after < T > ( ) , L , self , std : : move ( value ) ) ;
}
2017-12-07 21:24:50 +08:00
static error_result add_push_back ( std : : true_type , lua_State * , T & self , stack_object value , iterator & ) {
2017-08-06 07:20:28 +08:00
self . push_back ( value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_push_back ( std : : true_type , lua_State * , T & self , stack_object value ) {
2017-08-06 07:20:28 +08:00
self . push_back ( value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_push_back ( std : : false_type , lua_State * L , T & self , stack_object value , iterator & at ) {
return add_insert ( meta : : has_insert < T > ( ) , L , self , value , at ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_push_back ( std : : false_type , lua_State * L , T & self , stack_object value ) {
return add_insert ( meta : : has_insert < T > ( ) , L , self , value ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_associative ( std : : true_type , lua_State * L , T & self , stack_object key , iterator & at ) {
2017-08-06 07:20:28 +08:00
self . insert ( at , value_type ( key . as < K > ( ) , stack : : get < V > ( L , 3 ) ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_associative ( std : : true_type , lua_State * L , T & self , stack_object key ) {
auto at = deferred_traits : : end ( L , self ) ;
return add_associative ( std : : true_type ( ) , L , self , std : : move ( key ) , at ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_associative ( std : : false_type , lua_State * L , T & self , stack_object value , iterator & at ) {
return add_push_back ( meta : : has_push_back < T > ( ) , L , self , value , at ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_associative ( std : : false_type , lua_State * L , T & self , stack_object value ) {
return add_push_back ( meta : : has_push_back < T > ( ) , L , self , value ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_copyable ( std : : true_type , lua_State * L , T & self , stack_object value , iterator & at ) {
return add_associative ( is_associative ( ) , L , self , std : : move ( value ) , at ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_copyable ( std : : true_type , lua_State * L , T & self , stack_object value ) {
return add_associative ( is_associative ( ) , L , self , value ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_copyable ( std : : false_type , lua_State * L , T & self , stack_object value , iterator & ) {
return add_copyable ( std : : false_type ( ) , L , self , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result add_copyable ( std : : false_type , lua_State * , T & , stack_object ) {
return error_result ( " cannot call 'add' on '%s': value_type is non-copyable " , detail : : demangle < T > ( ) . data ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_lookup ( std : : true_type , lua_State * L , T & self , stack_object , stack_object value ) {
2017-08-06 07:20:28 +08:00
// TODO: should we warn or error about someone calling insert on an ordered / lookup container with no associativity?
2017-12-07 21:24:50 +08:00
return add_copyable ( std : : true_type ( ) , L , self , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_lookup ( std : : false_type , lua_State * L , T & self , stack_object where , stack_object value ) {
auto it = deferred_traits : : begin ( L , self ) ;
2017-08-06 07:20:28 +08:00
auto key = where . as < K > ( ) ;
- - key ;
std : : advance ( it , key ) ;
self . insert ( it , value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_after_has ( std : : true_type , lua_State * L , T & self , stack_object where , stack_object value ) {
2017-08-06 07:20:28 +08:00
auto key = where . as < K > ( ) ;
auto backit = self . before_begin ( ) ;
{
- - key ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
for ( auto it = deferred_traits : : begin ( L , self ) ; key > 0 ; + + backit , + + it , - - key ) {
2017-08-06 07:20:28 +08:00
if ( backit = = e ) {
2017-12-07 21:24:50 +08:00
return error_result ( " sol: out of bounds (too big) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
2017-08-06 07:20:28 +08:00
}
}
}
self . insert_after ( backit , value . as < V > ( ) ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_after_has ( std : : false_type , lua_State * , T & , stack_object , stack_object ) {
return error_result ( " cannot call 'insert' on '%s': no suitable or similar functionality detected on this container " , detail : : demangle < T > ( ) . data ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_has ( std : : true_type , lua_State * L , T & self , stack_object key , stack_object value ) {
2018-01-24 14:10:35 +08:00
return insert_lookup ( meta : : any < is_associative , is_lookup > ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_has ( std : : false_type , lua_State * L , T & self , stack_object where , stack_object value ) {
return insert_after_has ( meta : : has_insert_after < T > ( ) , L , self , where , value ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_copyable ( std : : true_type , lua_State * L , T & self , stack_object key , stack_object value ) {
return insert_has ( meta : : has_insert < T > ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result insert_copyable ( std : : false_type , lua_State * , T & , stack_object , stack_object ) {
return error_result ( " cannot call 'insert' on '%s': value_type is non-copyable " , detail : : demangle < T > ( ) . data ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_integral ( std : : true_type , lua_State * L , T & self , K & key ) {
auto it = deferred_traits : : begin ( L , self ) ;
2017-08-06 07:20:28 +08:00
- - key ;
std : : advance ( it , key ) ;
self . erase ( it ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_integral ( std : : false_type , lua_State * L , T & self , const K & key ) {
2017-08-06 07:20:28 +08:00
auto fx = [ & ] ( const value_type & r ) - > bool {
return key = = r ;
} ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
auto it = std : : find_if ( deferred_traits : : begin ( L , self ) , e , std : : ref ( fx ) ) ;
2017-08-06 07:20:28 +08:00
if ( it = = e ) {
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
self . erase ( it ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_associative_lookup ( std : : true_type , lua_State * , T & self , const K & key ) {
2017-08-06 07:20:28 +08:00
self . erase ( key ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_associative_lookup ( std : : false_type , lua_State * L , T & self , K & key ) {
return erase_integral ( std : : is_integral < K > ( ) , L , self , key ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_after_has ( std : : true_type , lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
auto backit = self . before_begin ( ) ;
{
- - key ;
2017-12-07 21:24:50 +08:00
auto e = deferred_traits : : end ( L , self ) ;
for ( auto it = deferred_traits : : begin ( L , self ) ; key > 0 ; + + backit , + + it , - - key ) {
2017-08-06 07:20:28 +08:00
if ( backit = = e ) {
2017-12-07 21:24:50 +08:00
return error_result ( " sol: out of bounds for erase on '%s' " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-08-06 07:20:28 +08:00
}
}
}
self . erase_after ( backit ) ;
2017-12-07 21:24:50 +08:00
return { } ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_after_has ( std : : false_type , lua_State * , T & , const K & ) {
return error_result ( " sol: cannot call erase on '%s' " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_has ( std : : true_type , lua_State * L , T & self , K & key ) {
return erase_associative_lookup ( meta : : any < is_associative , is_lookup > ( ) , L , self , key ) ;
2017-08-06 07:20:28 +08:00
}
2017-12-07 21:24:50 +08:00
static error_result erase_has ( std : : false_type , lua_State * L , T & self , K & key ) {
return erase_after_has ( has_erase_after < T > ( ) , L , self , key ) ;
2017-08-06 07:20:28 +08:00
}
static auto size_has ( std : : false_type , lua_State * L , T & self ) {
2017-08-11 15:24:17 +08:00
return std : : distance ( deferred_traits : : begin ( L , self ) , deferred_traits : : end ( L , self ) ) ;
2017-08-06 07:20:28 +08:00
}
static auto size_has ( std : : true_type , lua_State * , T & self ) {
return self . size ( ) ;
}
static void clear_has ( std : : true_type , lua_State * , T & self ) {
self . clear ( ) ;
}
static void clear_has ( std : : false_type , lua_State * L , T & ) {
luaL_error ( L , " sol: cannot call clear on '%s' " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-08-11 15:24:17 +08:00
static bool empty_has ( std : : true_type , lua_State * , T & self ) {
return self . empty ( ) ;
}
static bool empty_has ( std : : false_type , lua_State * L , T & self ) {
return deferred_traits : : begin ( L , self ) = = deferred_traits : : end ( L , self ) ;
}
2017-12-07 21:24:50 +08:00
static error_result get_start ( lua_State * L , T & self , K & key ) {
2017-08-06 07:20:28 +08:00
return get_it ( is_linear_integral ( ) , L , self , key ) ;
}
2017-12-07 21:24:50 +08:00
static error_result set_start ( lua_State * L , T & self , stack_object key , stack_object value ) {
return set_it ( is_linear_integral ( ) , L , self , std : : move ( key ) , std : : move ( value ) ) ;
2017-08-06 07:20:28 +08:00
}
static std : : size_t size_start ( lua_State * L , T & self ) {
2017-08-11 15:24:17 +08:00
return size_has ( meta : : has_size < T > ( ) , L , self ) ;
2017-08-06 07:20:28 +08:00
}
static void clear_start ( lua_State * L , T & self ) {
clear_has ( has_clear < T > ( ) , L , self ) ;
}
2017-08-11 15:24:17 +08:00
static bool empty_start ( lua_State * L , T & self ) {
return empty_has ( has_empty < T > ( ) , L , self ) ;
}
2017-12-07 21:24:50 +08:00
static error_result erase_start ( lua_State * L , T & self , K & key ) {
return erase_has ( has_erase < T > ( ) , L , self , key ) ;
2017-08-06 07:20:28 +08:00
}
2017-09-07 03:09:51 +08:00
template < bool ip >
2017-08-06 07:20:28 +08:00
static int next_associative ( std : : true_type , lua_State * L ) {
2017-07-10 00:00:57 +08:00
iter & i = stack : : get < user < iter > > ( L , 1 ) ;
auto & source = i . source ;
auto & it = i . it ;
2017-08-11 15:24:17 +08:00
if ( it = = deferred_traits : : end ( L , source ) ) {
2017-07-10 00:00:57 +08:00
return 0 ;
}
int p ;
2017-09-07 03:09:51 +08:00
if ( ip ) {
+ + i . i ;
p = stack : : push_reference ( L , i . i ) ;
}
2017-10-31 02:38:16 +08:00
else {
p = stack : : push_reference ( L , it - > first ) ;
}
2017-08-11 15:24:17 +08:00
p + = stack : : stack_detail : : push_reference < push_type > ( L , detail : : deref ( it - > second ) ) ;
2017-07-10 00:00:57 +08:00
std : : advance ( it , 1 ) ;
return p ;
}
2017-09-07 03:09:51 +08:00
template < bool >
2017-08-06 07:20:28 +08:00
static int next_associative ( std : : false_type , lua_State * L ) {
2017-07-10 00:00:57 +08:00
iter & i = stack : : get < user < iter > > ( L , 1 ) ;
auto & source = i . source ;
auto & it = i . it ;
2018-01-24 14:10:35 +08:00
next_K k = stack : : get < next_K > ( L , 2 ) ;
2017-08-11 15:24:17 +08:00
if ( it = = deferred_traits : : end ( L , source ) ) {
2017-07-10 00:00:57 +08:00
return 0 ;
}
int p ;
p = stack : : push_reference ( L , k + 1 ) ;
2017-08-11 15:24:17 +08:00
p + = stack : : stack_detail : : push_reference < push_type > ( L , detail : : deref ( * it ) ) ;
2017-07-10 00:00:57 +08:00
std : : advance ( it , 1 ) ;
return p ;
}
2017-09-07 03:09:51 +08:00
template < bool ip >
2018-01-24 14:10:35 +08:00
static int next ( lua_State * L ) {
typedef meta : : any < is_associative , meta : : all < is_lookup , meta : : neg < is_matched_lookup > > > is_assoc ;
return next_associative < ip > ( is_assoc ( ) , L ) ;
}
template < bool ip >
static int pairs_associative ( std : : true_type , lua_State * L ) {
2017-07-10 00:00:57 +08:00
auto & src = get_src ( L ) ;
2017-09-07 03:09:51 +08:00
stack : : push ( L , next < ip > ) ;
2017-08-11 15:24:17 +08:00
stack : : push < user < iter > > ( L , src , deferred_traits : : begin ( L , src ) ) ;
2018-01-24 14:10:35 +08:00
stack : : push ( L , lua_nil ) ;
2017-07-10 00:00:57 +08:00
return 3 ;
}
2017-09-07 03:09:51 +08:00
template < bool ip >
2018-01-24 14:10:35 +08:00
static int pairs_associative ( std : : false_type , lua_State * L ) {
auto & src = get_src ( L ) ;
stack : : push ( L , next < ip > ) ;
stack : : push < user < iter > > ( L , src , deferred_traits : : begin ( L , src ) ) ;
stack : : push ( L , 0 ) ;
return 3 ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
public :
static int get ( lua_State * L ) {
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er ;
{
decltype ( auto ) key = stack : : get < K > ( L ) ;
er = get_start ( L , self , key ) ;
}
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int index_get ( lua_State * L ) {
return get ( L ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int set ( lua_State * L ) {
stack_object value = stack_object ( L , raw_index ( 3 ) ) ;
2017-09-22 07:24:21 +08:00
if ( type_of ( L , 3 ) = = type : : lua_nil ) {
2017-08-06 07:20:28 +08:00
return erase ( L ) ;
}
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er = set_start ( L , self , stack_object ( L , raw_index ( 2 ) ) , std : : move ( value ) ) ;
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int index_set ( lua_State * L ) {
return set ( L ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int add ( lua_State * L ) {
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er = add_copyable ( is_copyable ( ) , L , self , stack_object ( L , raw_index ( 2 ) ) ) ;
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int insert ( lua_State * L ) {
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er = insert_copyable ( is_copyable ( ) , L , self , stack_object ( L , raw_index ( 2 ) ) , stack_object ( L , raw_index ( 3 ) ) ) ;
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int find ( lua_State * L ) {
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er = find_has ( has_find < T > ( ) , L , self ) ;
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static iterator begin ( lua_State * , T & self ) {
using std : : begin ;
return begin ( self ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static iterator end ( lua_State * , T & self ) {
using std : : end ;
return end ( self ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int size ( lua_State * L ) {
auto & self = get_src ( L ) ;
std : : size_t r = size_start ( L , self ) ;
return stack : : push ( L , r ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int clear ( lua_State * L ) {
auto & self = get_src ( L ) ;
clear_start ( L , self ) ;
return 0 ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int erase ( lua_State * L ) {
auto & self = get_src ( L ) ;
2017-12-07 21:24:50 +08:00
error_result er ;
{
decltype ( auto ) key = stack : : get < K > ( L , 2 ) ;
er = erase_start ( L , self , key ) ;
}
return handle_errors ( L , er ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-11 15:24:17 +08:00
static int empty ( lua_State * L ) {
auto & self = get_src ( L ) ;
return stack : : push ( L , empty_start ( L , self ) ) ;
}
2017-08-06 07:20:28 +08:00
static int pairs ( lua_State * L ) {
2017-12-20 17:58:32 +08:00
typedef meta : : any < is_associative , meta : : all < is_lookup , meta : : neg < is_matched_lookup > > > is_assoc ;
return pairs_associative < false > ( is_assoc ( ) , L ) ;
2017-09-07 03:09:51 +08:00
}
static int ipairs ( lua_State * L ) {
2017-12-20 17:58:32 +08:00
typedef meta : : any < is_associative , meta : : all < is_lookup , meta : : neg < is_matched_lookup > > > is_assoc ;
return pairs_associative < true > ( is_assoc ( ) , L ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
} ;
2017-07-10 00:00:57 +08:00
2017-08-06 07:20:28 +08:00
template < typename X >
struct container_traits_default < X , std : : enable_if_t < std : : is_array < std : : remove_pointer_t < meta : : unwrap_unqualified_t < X > > > : : value > > {
private :
typedef std : : remove_pointer_t < meta : : unwrap_unqualified_t < X > > T ;
2017-08-11 15:24:17 +08:00
typedef container_traits < X > deferred_traits ;
2017-09-13 14:46:56 +08:00
2017-08-06 07:20:28 +08:00
public :
typedef std : : remove_extent_t < T > value_type ;
typedef value_type * iterator ;
private :
struct iter {
T & source ;
iterator it ;
2017-09-13 14:46:56 +08:00
iter ( T & source , iterator it )
: source ( source ) , it ( std : : move ( it ) ) {
}
2017-08-06 07:20:28 +08:00
} ;
static auto & get_src ( lua_State * L ) {
auto p = stack : : check_get < T * > ( L , 1 ) ;
2017-08-07 14:07:21 +08:00
# ifdef SOL_SAFE_USERTYPE
2017-08-06 07:20:28 +08:00
if ( ! p | | p . value ( ) = = nullptr ) {
luaL_error ( L , " sol: 'self' argument is nil or not of type '%s' (pass 'self' as first argument with ':' or call on proper type) " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
# endif // Safe getting with error
2017-08-07 14:07:21 +08:00
return * p . value ( ) ;
2017-08-06 07:20:28 +08:00
}
static int find ( std : : true_type , lua_State * L ) {
T & self = get_src ( L ) ;
decltype ( auto ) value = stack : : get < value_type > ( L , 2 ) ;
std : : size_t N = std : : extent < T > : : value ;
for ( std : : size_t idx = 0 ; idx < N ; + + idx ) {
const auto & v = self [ idx ] ;
if ( v = = value ) {
return stack : : push ( L , idx + 1 ) ;
}
}
return stack : : push ( L , lua_nil ) ;
}
static int find ( std : : false_type , lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'find' on '%s': no supported comparison operator for the value type " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int next ( lua_State * L ) {
iter & i = stack : : get < user < iter > > ( L , 1 ) ;
auto & source = i . source ;
auto & it = i . it ;
std : : size_t k = stack : : get < std : : size_t > ( L , 2 ) ;
2017-08-11 15:24:17 +08:00
if ( it = = deferred_traits : : end ( L , source ) ) {
2017-08-06 07:20:28 +08:00
return 0 ;
}
int p ;
p = stack : : push_reference ( L , k + 1 ) ;
2017-08-11 15:24:17 +08:00
p + = stack : : push_reference ( L , detail : : deref ( * it ) ) ;
2017-08-06 07:20:28 +08:00
std : : advance ( it , 1 ) ;
return p ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
public :
static int clear ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'clear' on type '%s': cannot remove all items from a fixed array " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int erase ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'erase' on type '%s': cannot remove an item from fixed arrays " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
2017-09-13 14:46:56 +08:00
2017-08-06 07:20:28 +08:00
static int add ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'add' on type '%s': cannot add to fixed arrays " , detail : : demangle < T > ( ) . c_str ( ) ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int insert ( lua_State * L ) {
return luaL_error ( L , " sol: cannot call 'insert' on type '%s': cannot insert new entries into fixed arrays " , detail : : demangle < T > ( ) . c_str ( ) ) ;
}
static int get ( lua_State * L ) {
T & self = get_src ( L ) ;
2017-08-07 14:07:21 +08:00
std : : ptrdiff_t idx = stack : : get < std : : ptrdiff_t > ( L , 2 ) ;
2017-08-07 19:54:43 +08:00
if ( idx > static_cast < std : : ptrdiff_t > ( std : : extent < T > : : value ) | | idx < 1 ) {
2017-08-06 07:20:28 +08:00
return stack : : push ( L , lua_nil ) ;
}
- - idx ;
2017-08-11 15:24:17 +08:00
return stack : : push_reference ( L , detail : : deref ( self [ idx ] ) ) ;
2017-08-06 07:20:28 +08:00
}
static int index_get ( lua_State * L ) {
return get ( L ) ;
}
static int set ( lua_State * L ) {
T & self = get_src ( L ) ;
2017-08-07 14:07:21 +08:00
std : : ptrdiff_t idx = stack : : get < std : : ptrdiff_t > ( L , 2 ) ;
2017-08-07 19:54:43 +08:00
if ( idx > static_cast < std : : ptrdiff_t > ( std : : extent < T > : : value ) ) {
2017-08-07 14:07:21 +08:00
return luaL_error ( L , " sol: index out of bounds (too big) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
}
if ( idx < 1 ) {
return luaL_error ( L , " sol: index out of bounds (too small) for set on ' % s ' " , detail::demangle<T>().c_str()) ;
2017-08-06 07:20:28 +08:00
}
- - idx ;
self [ idx ] = stack : : get < value_type > ( L , 3 ) ;
2017-07-10 00:00:57 +08:00
return 0 ;
}
2017-08-06 07:20:28 +08:00
static int index_set ( lua_State * L ) {
return set ( L ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int find ( lua_State * L ) {
return find ( meta : : supports_op_equal < value_type , value_type > ( ) , L ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-06 07:20:28 +08:00
static int size ( lua_State * L ) {
return stack : : push ( L , std : : extent < T > : : value ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-11 15:24:17 +08:00
static int empty ( lua_State * L ) {
return stack : : push ( L , std : : extent < T > : : value > 0 ) ;
}
2017-08-06 07:20:28 +08:00
static int pairs ( lua_State * L ) {
auto & src = get_src ( L ) ;
stack : : push ( L , next ) ;
2017-08-11 15:24:17 +08:00
stack : : push < user < iter > > ( L , src , deferred_traits : : begin ( L , src ) ) ;
2017-08-06 07:20:28 +08:00
stack : : push ( L , 0 ) ;
return 3 ;
2017-07-10 00:00:57 +08:00
}
2017-09-07 04:50:27 +08:00
static int ipairs ( lua_State * L ) {
return pairs ( L ) ;
}
2017-08-07 14:27:08 +08:00
static iterator begin ( lua_State * , T & self ) {
2017-08-06 07:20:28 +08:00
return std : : addressof ( self [ 0 ] ) ;
2017-07-10 00:00:57 +08:00
}
2017-08-07 14:27:08 +08:00
static iterator end ( lua_State * , T & self ) {
2017-08-08 01:58:06 +08:00
return std : : addressof ( self [ 0 ] ) + std : : extent < T > : : value ;
2017-07-10 00:00:57 +08:00
}
} ;
2017-08-11 15:24:17 +08:00
template < typename X >
struct container_traits_default < container_traits < X > > : container_traits_default < X > { } ;
2017-09-13 14:46:56 +08:00
} // namespace container_detail
2017-07-10 00:00:57 +08:00
template < typename T >
struct container_traits : container_detail : : container_traits_default < T > { } ;
2017-09-13 14:46:56 +08:00
} // namespace sol
2017-07-01 23:02:15 +08:00
# endif // SOL_CONTAINER_TRAITS_HPP