Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flatdata-cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ C++ 11 implementation of `flatdata`

## Building

First, build `flatdata-cpp` and install the requirements of the generator.
First, install the requirements of the generator and build `flatdata-cpp`.

```shell
pip3 install -r requirements.txt
mkdir build
cd build
pip3 install -r flatdata-generator/requirements.txt
cd flatdata-cpp
mkdir build && cd build
cmake ..
make
make test # optional
Expand Down
52 changes: 40 additions & 12 deletions flatdata-cpp/include/flatdata/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Archive
/**
* @brief Returns text description of the archive and its resources' state.
*/
std::string describe( ) const;
std::string describe( size_t nest_level = 0u ) const;

/**
* @brief Returns archive name. Is implemented by the concrete archive instances.
Expand All @@ -60,13 +60,15 @@ class Archive

protected:
template < typename ResourceType >
static void describe_resource( std::ostream& stream,
static void describe_resource( size_t nest_level,
std::ostream& stream,
const char* name,
const ResourceType& resource,
bool too_large = false );

template < typename ResourceType >
static void describe_resource( std::ostream& stream,
static void describe_resource( size_t nest_level,
std::ostream& stream,
const char* name,
const boost::optional< ResourceType >& resource,
bool too_large = false );
Expand Down Expand Up @@ -107,7 +109,9 @@ class Archive
bool optional,
bool loaded,
const char* details,
bool too_large );
bool are_details_nested,
bool too_large,
size_t nest_level );

private:
/**
Expand All @@ -120,7 +124,7 @@ class Archive
* @brief Describes all resources provided by the archive.
* Is implemented by the concrete archive instances.
*/
virtual void describe_resources( std::ostream& stream ) const = 0;
virtual void describe_resources( std::ostream& stream, size_t nest_level ) const = 0;

private:
std::shared_ptr< flatdata::ResourceStorage > m_storage;
Expand All @@ -130,28 +134,52 @@ class Archive

// -------------------------------------------------------------------------------------------------

template < typename ResourceType >
std::string
get_description( const ResourceType& resource, bool is_archive, size_t nest_level )
{
std::string description;
if ( is_archive )
{
++nest_level;
}
description = resource.describe( nest_level );
return description;
}

template < typename ResourceType >
void
Archive::describe_resource( std::ostream& stream,
Archive::describe_resource( size_t nest_level,
std::ostream& stream,
const char* name,
const ResourceType& resource,
bool too_large )
{
auto initialized = static_cast< bool >( resource );
describe_impl( stream, name, false, static_cast< bool >( resource ),
initialized ? resource.describe( ).c_str( ) : "N/A", too_large );
const auto initialized = static_cast< bool >( resource );
const bool is_archive = std::is_base_of< Archive, ResourceType >::value;

describe_impl( stream, name, false, initialized,
get_description( resource, is_archive, nest_level ).c_str( ), is_archive,
too_large, nest_level );
}

template < typename ResourceType >
void
Archive::describe_resource( std::ostream& stream,
Archive::describe_resource( size_t nest_level,
std::ostream& stream,
const char* name,
const boost::optional< ResourceType >& resource,
bool too_large )
{
auto initialized = static_cast< bool >( resource );
const auto initialized = static_cast< bool >( resource );
const bool is_archive = std::is_base_of< Archive, ResourceType >::value;

const ResourceType ref = initialized ? *resource // valid ref
: ResourceType( ); // ref to dummy, not used

describe_impl( stream, name, true, initialized ? static_cast< bool >( *resource ) : false,
initialized ? resource->describe( ).c_str( ) : "N/A", too_large );
get_description( ref, is_archive, nest_level ).c_str( ), is_archive, too_large,
nest_level );
}

template < typename ResourceType >
Expand Down
2 changes: 1 addition & 1 deletion flatdata-cpp/include/flatdata/ArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ArrayView

size_t size_in_bytes( ) const;
size_t size( ) const;
std::string describe( ) const;
std::string describe( size_t nest_level = 0u ) const;

bool empty( ) const;

Expand Down
11 changes: 9 additions & 2 deletions flatdata-cpp/include/flatdata/MemoryDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ struct MemoryDescriptor
}

std::string
describe( ) const
describe( size_t nest_level = 0 ) const
{
std::ostringstream ss;
ss << "Raw data of size " << m_size;
if ( this->operator bool( ) )
{
ss << "Raw data of size " << m_size;
}
else
{
ss << "Uninitialized Raw data";
}
return ss.str( );
}

Expand Down
14 changes: 10 additions & 4 deletions flatdata-cpp/include/flatdata/MultiArrayView.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class MultiArrayView
}

size_t size( ) const;
std::string describe( ) const;
std::string describe( size_t unused = 0 ) const;
explicit operator bool( ) const;

template < typename ElementType >
Expand Down Expand Up @@ -195,11 +195,17 @@ MultiArrayView< IndexType, Args... >::size( ) const
}

template < typename IndexType, typename... Args >
std::string
MultiArrayView< IndexType, Args... >::describe( ) const
std::string MultiArrayView< IndexType, Args... >::describe( size_t /*unused*/ ) const
{
std::ostringstream ss;
ss << "MultiArray of size " << size( ) << ", with index: " << m_index.describe( );
if ( this->operator bool( ) )
{
ss << "MultiArray of size " << size( ) << ", with index: " << m_index.describe( );
}
else
{
ss << "Uninitialized MultiArray";
}
return ss.str( );
}

Expand Down
12 changes: 9 additions & 3 deletions flatdata-cpp/include/flatdata/internal/ArrayView.inl
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,17 @@ ArrayView< T >::end( ) const
}

template < typename T >
std::string
ArrayView< T >::describe( ) const
std::string ArrayView< T >::describe( size_t /*unused*/ ) const
{
std::ostringstream ss;
ss << "Array of size: " << size( ) << " in " << size_in_bytes( ) << " bytes";
if ( !empty( ) )
{
ss << "Array of size: " << size( ) << " in " << size_in_bytes( ) << " bytes";
}
else
{
ss << "Uninitialized Array";
}
return ss.str( );
}

Expand Down
83 changes: 56 additions & 27 deletions flatdata-cpp/src/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace flatdata
{
constexpr size_t TAB_WIDTH = 4;

Archive::Archive( std::shared_ptr< flatdata::ResourceStorage > storage )
: m_storage( std::move( storage ) )
{
Expand Down Expand Up @@ -226,43 +228,69 @@ compute_diff( const char* expected, const char* found )
}

std::string
Archive::describe( ) const
Archive::describe( size_t nest_level ) const
{
const auto newl = std::string( "\n" );
const auto hline = std::string( 80, '=' ) + newl;
const auto empty = std::string( "" );
const bool is_root_node = ( nest_level == 0 );

std::ostringstream result;
static const char* hline
= "================================================================================";
result << hline << std::endl
<< "Flatdata Archive: " << name( ) << std::endl
<< hline << std::endl;

if ( !m_storage )
{
result << " FATAL: Resource storage not initialized. Please check archive path."
<< std::endl
<< hline << std::endl;
return result.str( );
if ( is_root_node )
{
result << hline << "FATAL: Resource storage not initialized. Please check archive path."
<< newl;
}
else
{
result << "Uninitialized Archive " << name( );
}
}

if ( !m_signature )
if ( m_storage && !m_signature )
{
result << " FATAL: Archive signature does not match software expectations." << std::endl
<< hline << std::endl;
result << ( is_root_node ? hline : empty )
<< "FATAL: Archive signature does not match software expectations."
<< ( is_root_node ? newl : empty ) << hline;
result << compute_diff(
schema( ),
m_storage->read_schema( internal::signature_name( name( ) ).c_str( ) ).char_ptr( ) );
}

if ( !m_is_open )
if ( m_storage && !m_is_open )
{
result << " FATAL: Archive initialization failed. Failed loading mandatory resources."
// Error propagated to root and storage is not initialized in respective child. No root
// check needed.
result << hline
<< "FATAL: Archive initialization failed. Failed loading mandatory resources."
<< std::endl;
}

result << std::endl
<< "Resource Optional Too Large Loaded Details"
<< std::endl
<< hline << std::endl;
describe_resources( result );
result << hline << std::endl;
if ( is_root_node )
{
result << hline << "Flatdata Archive: " << name( ) << std::endl
<< hline
<< "Resource Optional Too Large Loaded Details"
<< std::endl
<< hline;
}
else
{
const std::string indent( ( nest_level - 1 ) * TAB_WIDTH, ' ' );
result << newl + indent + std::string( "|" ) + newl + indent + std::string( "|->" )
<< " Flatdata Archive: " << name( ) << std::endl;
}

describe_resources( result, nest_level );

if ( is_root_node )
{
result << hline;
}

return result.str( );
}

Expand All @@ -272,17 +300,18 @@ Archive::describe_impl( std::ostream& stream,
bool optional,
bool loaded,
const char* details,
bool too_large )
bool has_nested_details,
bool too_large,
size_t nest_level )
{
auto oldw = stream.width( );
auto oldfill = stream.fill( );
stream << std::left << std::setw( 37 ) << std::setfill( ' ' )
const std::string indent( nest_level * TAB_WIDTH, ' ' );
size_t offset = indent.size( );
stream << indent << std::left << std::setw( 37 - offset ) << std::setfill( ' ' )
<< std::string( name ).substr( 0, 30 ) << std::left << std::setw( 10 )
<< std::setfill( ' ' ) << ( optional ? "YES" : "NO" ) << std::left << std::setw( 11 )
<< std::setfill( ' ' ) << ( too_large ? "YES" : "NO" ) << std::left << std::setw( 10 )
<< std::setfill( ' ' ) << ( static_cast< bool >( loaded ) ? "YES" : "NO" ) << details
<< std::endl;
stream << std::setw( oldw ) << std::setfill( oldfill );
<< ( has_nested_details ? "" : "\n" );
}

} // namespace flatdata
Loading