Skip to content

Commit

Permalink
added import paths logic (#29)
Browse files Browse the repository at this point in the history
* imports are resolved as C++ #include
  • Loading branch information
tonda-kriz authored Nov 14, 2024
1 parent d61cd5f commit 41af7b1
Show file tree
Hide file tree
Showing 29 changed files with 734 additions and 482 deletions.
3 changes: 1 addition & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BinPackParameters: 'true'
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakConstructorInitializersBeforeComma: 'true'
ColumnLimit: '0'
ColumnLimit: 100
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
Cpp11BracedListStyle: 'false'
IndentWidth: '4'
Expand All @@ -38,7 +38,6 @@ SpacesInSquareBrackets: 'true'
Standard: Cpp11
TabWidth: '4'
UseTab: Never

...
Language: Proto
IndentWidth: '4'
Expand Down
3 changes: 2 additions & 1 deletion example/addressbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ auto main( int argc, char * argv[] ) -> int

try
{
auto address_book = spb::json::deserialize< tutorial::AddressBook >( load_file( argv[ 1 ] ) );
auto address_book =
spb::json::deserialize< tutorial::AddressBook >( load_file( argv[ 1 ] ) );

// Add an address.
PromptForAddress( address_book.people.emplace_back( ) );
Expand Down
15 changes: 6 additions & 9 deletions include/spb/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ concept container = requires( T container ) {
};

template < class T >
concept proto_field_bytes = container< T > &&
std::is_same_v< typename std::decay_t< T >::value_type, std::byte >;
concept proto_field_bytes =
container< T > && std::is_same_v< typename std::decay_t< T >::value_type, std::byte >;

template < class T >
concept proto_field_bytes_resizable = proto_field_bytes< T > && requires( T obj ) {
Expand All @@ -39,8 +39,8 @@ concept proto_field_bytes_resizable = proto_field_bytes< T > && requires( T obj
};

template < class T >
concept proto_field_string = container< T > &&
std::is_same_v< typename std::decay_t< T >::value_type, char >;
concept proto_field_string =
container< T > && std::is_same_v< typename std::decay_t< T >::value_type, char >;

template < class T >
concept proto_field_string_resizable = proto_field_string< T > && requires( T obj ) {
Expand Down Expand Up @@ -76,10 +76,7 @@ concept proto_enum = std::is_enum_v< T >;
* @brief proto `message` converted to C++ struct
*/
template < class T >
concept proto_message = std::is_class_v< T > &&
!proto_field_string< T > &&
!proto_field_bytes< T > &&
!proto_label_repeated< T > &&
!proto_label_optional< T >;
concept proto_message = std::is_class_v< T > && !proto_field_string< T > &&
!proto_field_bytes< T > && !proto_label_repeated< T > && !proto_label_optional< T >;

}// namespace spb::detail
10 changes: 6 additions & 4 deletions include/spb/from_chars.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct from_chars_result
std::errc ec;
};

static inline auto from_chars( const char * first, const char * last, std::integral auto & number, int base = 10 ) -> from_chars_result
static inline auto from_chars( const char * first, const char * last, std::integral auto & number,
int base = 10 ) -> from_chars_result
{
assert( first <= last );
char buffer[ 32 ];
Expand All @@ -52,8 +53,8 @@ static inline auto from_chars( const char * first, const char * last, std::integ
if constexpr( sizeof( number ) < 4 )
{
auto tmp = strtol( buffer, ( char ** ) &end, base );
if( tmp <= std::numeric_limits< T >::max( ) &&
tmp >= std::numeric_limits< T >::min( ) ) [[likely]]
if( tmp <= std::numeric_limits< T >::max( ) && tmp >= std::numeric_limits< T >::min( ) )
[[likely]]
{
result = T( tmp );
}
Expand Down Expand Up @@ -107,7 +108,8 @@ static inline auto from_chars( const char * first, const char * last, std::integ
return { end, std::errc( ) };
}

static inline auto from_chars( const char * first, const char * last, std::floating_point auto & number ) -> from_chars_result
static inline auto from_chars( const char * first, const char * last,
std::floating_point auto & number ) -> from_chars_result
{
assert( first <= last );
char buffer[ 256 ];
Expand Down
3 changes: 1 addition & 2 deletions include/spb/io/buffer-io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class buffered_reader
{
shift_data_to_start( );

while( bytes_in_buffer( ) < SPB_READ_BUFFER_SIZE &&
!eof_reached )
while( bytes_in_buffer( ) < SPB_READ_BUFFER_SIZE && !eof_reached )
{
auto bytes_in = on_read( &buffer[ end_index ], space_left_in_buffer( ) );
eof_reached |= bytes_in == 0;
Expand Down
22 changes: 11 additions & 11 deletions include/spb/io/function_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Name : function_ref *
* Description : non-owning reference to a callable *
* Author : LLVM *
* Reference : https://llvm.org/doxygen/classllvm_1_1function__ref_3_01Ret_07Params_8_8_8_08_4.html *
* Reference : https://llvm.org/doxygen/classllvm_1_1function__ref_3_01Ret_07Params_8_8_8_08_4.html
*
* ------------------------------------------------------------------------- *
* This is free software; you can redistribute it and/or modify it under the *
* terms of the MIT license. A copy of the license can be found in the file *
Expand Down Expand Up @@ -41,16 +42,15 @@ class function_ref< Ret( Params... ) >
}

template < typename Callable >
function_ref(
Callable && callable,
// This is not the copy-constructor.
std::enable_if_t< !std::is_same< std::remove_cvref_t< Callable >,
function_ref >::value > * = nullptr,
// Functor must be callable and return a suitable type.
std::enable_if_t< std::is_void< Ret >::value ||
std::is_convertible< decltype( std::declval< Callable >( )(
std::declval< Params >( )... ) ),
Ret >::value > * = nullptr )
function_ref( Callable && callable,
// This is not the copy-constructor.
std::enable_if_t< !std::is_same< std::remove_cvref_t< Callable >,
function_ref >::value > * = nullptr,
// Functor must be callable and return a suitable type.
std::enable_if_t< std::is_void< Ret >::value ||
std::is_convertible< decltype( std::declval< Callable >( )(
std::declval< Params >( )... ) ),
Ret >::value > * = nullptr )
: callback( callback_fn< std::remove_reference_t< Callable > > )
, callable( reinterpret_cast< intptr_t >( &callable ) )
{
Expand Down
3 changes: 2 additions & 1 deletion include/spb/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ static inline void deserialize( auto & result, spb::io::reader on_read )
*/
static inline void deserialize( auto & message, std::string_view json )
{
auto reader = [ ptr = json.data( ), end = json.data( ) + json.size( ) ]( void * data, size_t size ) mutable -> size_t
auto reader = [ ptr = json.data( ), end = json.data( ) + json.size( ) ](
void * data, size_t size ) mutable -> size_t
{
size_t bytes_left = end - ptr;

Expand Down
44 changes: 27 additions & 17 deletions include/spb/json/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace spb::json::detail
template < typename ostream >
static inline void base64_encode( ostream & output, std::span< const std::byte > input )
{
static constexpr char encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static constexpr char encode_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

const auto * p_char = reinterpret_cast< const uint8_t * >( input.data( ) );
//
Expand Down Expand Up @@ -62,23 +63,32 @@ static inline void base64_encode( ostream & output, std::span< const std::byte >
}

template < typename istream >
static inline void base64_decode_string( spb::detail::proto_field_bytes auto & output, istream & stream )
static inline void base64_decode_string( spb::detail::proto_field_bytes auto & output,
istream & stream )
{
static constexpr uint8_t decode_table[ 256 ] = {
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128, 128,
128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,
128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 128, 128, 128, 128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128
};

/*static constexpr uint8_t decode_table2[] = {
62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128, 128,
128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,
128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
};*/

if constexpr( spb::detail::proto_field_bytes_resizable< decltype( output ) > )
Expand All @@ -102,8 +112,7 @@ static inline void base64_decode_string( spb::detail::proto_field_bytes auto & o
auto view = stream.view( UINT32_MAX );
auto length = view.find( '"' );
auto end_found = length < view.npos;
if( ( end_found && length % 4 != 0 ) ||
view.size( ) <= 4 ) [[unlikely]]
if( ( end_found && length % 4 != 0 ) || view.size( ) <= 4 ) [[unlikely]]
{
throw std::runtime_error( "invalid base64" );
}
Expand All @@ -128,9 +137,10 @@ static inline void base64_decode_string( spb::detail::proto_field_bytes auto & o
}
}

auto * p_out = output.data( ) + out_index;
const auto * p_in = reinterpret_cast< const uint8_t * >( view.data( ) );
const auto * p_end = p_in + aligned_length - 4;//- exclude the last 4 chars (possible padding)
auto * p_out = output.data( ) + out_index;
const auto * p_in = reinterpret_cast< const uint8_t * >( view.data( ) );
const auto * p_end =
p_in + aligned_length - 4;//- exclude the last 4 chars (possible padding)

while( p_in < p_end ) [[likely]]
{
Expand Down
36 changes: 20 additions & 16 deletions include/spb/json/deserialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ struct istream
template < typename T >
[[nodiscard]] auto deserialize_bitfield( uint32_t bits ) -> T;
[[nodiscard]] auto deserialize_int( ) -> int32_t;
[[nodiscard]] auto deserialize_string_or_int( size_t min_size, size_t max_size ) -> std::variant< std::string_view, int32_t >;
[[nodiscard]] auto deserialize_string_or_int( size_t min_size, size_t max_size )
-> std::variant< std::string_view, int32_t >;
[[nodiscard]] auto deserialize_key( size_t min_size, size_t max_size ) -> std::string_view;
[[nodiscard]] auto current_key( ) const -> std::string_view;

Expand Down Expand Up @@ -209,8 +210,7 @@ struct istream
return false;
}
auto token_view = reader.view( token.size( ) + 1 ).substr( 0, token.size( ) + 1 );
if( token_view.size( ) == token.size( ) ||
isspace( token_view.back( ) ) ||
if( token_view.size( ) == token.size( ) || isspace( token_view.back( ) ) ||
( !isalnum( token_view.back( ) ) && token_view.back( ) != '_' ) )
{
reader.skip( token.size( ) );
Expand Down Expand Up @@ -276,7 +276,8 @@ static inline void ignore_string( istream & stream )
}
}

static inline auto deserialize_string_view( istream & stream, size_t min_size, size_t max_size ) -> std::string_view
static inline auto deserialize_string_view( istream & stream, size_t min_size,
size_t max_size ) -> std::string_view
{
if( stream.current_char( ) != '"' )
{
Expand All @@ -295,8 +296,7 @@ static inline auto deserialize_string_view( istream & stream, size_t min_size, s
{
stream.skip( length );

if( ( length - 2 ) >= min_size &&
( length - 2 ) <= max_size )
if( ( length - 2 ) >= min_size && ( length - 2 ) <= max_size )
{
return view.substr( 1, length - 2 );
}
Expand All @@ -319,10 +319,10 @@ static inline auto unicode_from_hex( istream & stream ) -> uint16_t
{
throw std::runtime_error( "invalid escape sequence" );
}
auto value = uint16_t( 0 );
auto result = spb_std_emu::from_chars( unicode_view.data( ), unicode_view.data( ) + esc_size, value, 16 );
if( result.ec != std::errc{ } ||
result.ptr != unicode_view.data( ) + esc_size )
auto value = uint16_t( 0 );
auto result =
spb_std_emu::from_chars( unicode_view.data( ), unicode_view.data( ) + esc_size, value, 16 );
if( result.ec != std::errc{ } || result.ptr != unicode_view.data( ) + esc_size )
{
throw std::runtime_error( "invalid escape sequence" );
}
Expand All @@ -333,8 +333,7 @@ static inline auto unicode_from_hex( istream & stream ) -> uint16_t
static inline auto unescape_unicode( istream & stream, char utf8[ 4 ] ) -> uint32_t
{
auto value = uint32_t( unicode_from_hex( stream ) );
if( value >= 0xD800 && value <= 0xDBFF &&
stream.view( 2 ).starts_with( "\\u"sv ) )
if( value >= 0xD800 && value <= 0xDBFF && stream.view( 2 ).starts_with( "\\u"sv ) )
{
stream.skip( 2 );
auto low = unicode_from_hex( stream );
Expand Down Expand Up @@ -454,7 +453,8 @@ static inline void deserialize( istream & stream, spb::detail::proto_field_strin
spb::detail::utf8::validate( std::string_view( value.data( ), value.size( ) ) );
}

static inline void deserialize( istream & stream, spb::detail::proto_field_int_or_float auto & value )
static inline void deserialize( istream & stream,
spb::detail::proto_field_int_or_float auto & value )
{
if( stream.current_char( ) == '"' ) [[unlikely]]
{
Expand Down Expand Up @@ -558,7 +558,8 @@ void deserialize_map_key( istream & stream, T & map_key )
return deserialize( stream, map_key );
}
auto str_key_map = deserialize_string_view( stream, 1, UINT32_MAX );
auto reader = [ ptr = str_key_map.data( ), end = str_key_map.data( ) + str_key_map.size( ) ]( void * data, size_t size ) mutable -> size_t
auto reader = [ ptr = str_key_map.data( ), end = str_key_map.data( ) + str_key_map.size( ) ](
void * data, size_t size ) mutable -> size_t
{
size_t bytes_left = end - ptr;
size = std::min( size, bytes_left );
Expand Down Expand Up @@ -621,7 +622,9 @@ static inline void deserialize( istream & stream, spb::detail::proto_label_optio
}
else
{
deserialize( stream, p_value.emplace( typename std::decay_t< decltype( p_value ) >::value_type( ) ) );
deserialize(
stream,
p_value.emplace( typename std::decay_t< decltype( p_value ) >::value_type( ) ) );
}
}

Expand Down Expand Up @@ -813,7 +816,8 @@ inline auto istream::deserialize_bitfield( uint32_t bits ) -> T
return detail::deserialize_bitfield< T >( *this, bits );
}

inline auto istream::deserialize_string_or_int( size_t min_size, size_t max_size ) -> std::variant< std::string_view, int32_t >
inline auto istream::deserialize_string_or_int( size_t min_size, size_t max_size )
-> std::variant< std::string_view, int32_t >
{
if( current_char( ) == '"' )
{
Expand Down
Loading

0 comments on commit 41af7b1

Please sign in to comment.