Skip to content
Open
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
238 changes: 119 additions & 119 deletions include/geode/model/mixin/core/detail/components_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,141 +43,141 @@
#include <geode/model/mixin/core/bitsery_archive.hpp>
#include <geode/model/mixin/core/component_type.hpp>

namespace geode
namespace geode::detail
{
namespace detail
template < typename Component >
class ComponentsStorage
{
template < typename Component >
class ComponentsStorage
{
public:
using ComponentPtr = std::unique_ptr< Component >;
using ComponentsStore = absl::linked_hash_map< uuid, ComponentPtr >;
using Iterator = typename ComponentsStore::const_iterator;
public:
using ComponentPtr = std::unique_ptr< Component >;
using ComponentsStore = absl::linked_hash_map< uuid, ComponentPtr >;
using Iterator = typename ComponentsStore::const_iterator;

[[nodiscard]] index_t nb_components() const
{
return components_.size();
}
[[nodiscard]] index_t nb_components() const
{
return components_.size();
}

[[nodiscard]] bool has_component( const uuid& id ) const
{
return components_.contains( id );
}
[[nodiscard]] bool has_component( const uuid& component_id ) const
{
return components_.contains( component_id );
}

[[nodiscard]] const Component& component( const uuid& id ) const
{
return *components_.at( id );
}
[[nodiscard]] const Component& component(
const uuid& component_id ) const
{
return *components_.at( component_id );
}

[[nodiscard]] Component& component( const uuid& id )
{
return *components_.at( id );
}
[[nodiscard]] Component& component( const uuid& component_id )
{
return *components_.at( component_id );
}

[[nodiscard]] Iterator begin() const
{
return components_.begin();
}
[[nodiscard]] Iterator begin() const
{
return components_.begin();
}

[[nodiscard]] Iterator end() const
{
return components_.end();
}
[[nodiscard]] Iterator end() const
{
return components_.end();
}

void add_component( ComponentPtr component )
{
void add_component( ComponentPtr component )
{
const auto [itr, new_uuid] =
components_.emplace( component->id(), std::move( component ) );
}

void save_components( std::string_view filename ) const
{
std::ofstream file{ to_string( filename ),
std::ofstream::binary };
TContext context{};
BitseryExtensions::register_serialize_pcontext(
std::get< 0 >( context ) );
Serializer archive{ context, file };
archive.object( *this );
archive.adapter().flush();
OpenGeodeModelException::check_exception(
std::get< 1 >( context ).isValid(), nullptr,
OpenGeodeException::TYPE::internal,
"[ComponentsStorage::save_components] Error while writing "
"file: ",
filename );
}
OpenGeodeModelException::check_exception( new_uuid, nullptr,
OpenGeodeException::TYPE::internal,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know how to decide if this is an internal or data error. This code cannot work because of its input parameters. Is it internal?

"[ComponentsStorage::add_component] Component with id ",
itr->first.string(), " already exists" );
}

void delete_component( const uuid& id )
{
components_.erase( components_.find( id ) );
}
void save_components( std::string_view filename ) const
{
std::ofstream file{ to_string( filename ), std::ofstream::binary };
TContext context{};
BitseryExtensions::register_serialize_pcontext(
std::get< 0 >( context ) );
Serializer archive{ context, file };
archive.object( *this );
archive.adapter().flush();
OpenGeodeModelException::check_exception(
std::get< 1 >( context ).isValid(), nullptr,
OpenGeodeException::TYPE::internal,
"[ComponentsStorage::save_components] Error while writing "
"file: ",
filename );
}

void delete_component( const uuid& component_id )
{
components_.erase( components_.find( component_id ) );
}

void load_components( std::string_view filename )
void load_components( std::string_view filename )
{
if( !std::filesystem::exists( to_string( filename ) ) )
{
if( !std::filesystem::exists( to_string( filename ) ) )
{
return;
}
std::ifstream file{ to_string( filename ),
std::ifstream::binary };
TContext context{};
BitseryExtensions::register_deserialize_pcontext(
std::get< 0 >( context ) );
Deserializer archive{ context, file };
archive.object( *this );
const auto& adapter = archive.adapter();
OpenGeodeModelException::check_exception(
adapter.error() == bitsery::ReaderError::NoError
&& adapter.isCompletedSuccessfully()
&& std::get< 1 >( context ).isValid(),
nullptr, OpenGeodeException::TYPE::internal,
"[ComponentsStorage::load_components] Error while reading "
"file: ",
filename );
return;
}

[[nodiscard]] absl::flat_hash_map< std::string, std::string >
file_mapping( std::string_view directory ) const
std::ifstream file{ to_string( filename ), std::ifstream::binary };
TContext context{};
BitseryExtensions::register_deserialize_pcontext(
std::get< 0 >( context ) );
Deserializer archive{ context, file };
archive.object( *this );
const auto& adapter = archive.adapter();
OpenGeodeModelException::check_exception(
adapter.error() == bitsery::ReaderError::NoError
&& adapter.isCompletedSuccessfully()
&& std::get< 1 >( context ).isValid(),
nullptr, OpenGeodeException::TYPE::internal,
"[ComponentsStorage::load_components] Error while reading "
"file: ",
filename );
}

[[nodiscard]] absl::flat_hash_map< std::string, std::string >
file_mapping( std::string_view directory ) const
{
absl::flat_hash_map< std::string, std::string > mapping;
for( const auto& file :
std::filesystem::directory_iterator( to_string( directory ) ) )
{
absl::flat_hash_map< std::string, std::string > mapping;
for( const auto& file : std::filesystem::directory_iterator(
to_string( directory ) ) )
auto path = file.path();
auto filename = path.replace_extension( "" ).string();
if( filename.size() > 36 )

Check warning on line 151 in include/geode/model/mixin/core/detail/components_storage.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/model/mixin/core/detail/components_storage.hpp:151:39 [cppcoreguidelines-avoid-magic-numbers]

36 is a magic number; consider replacing it with a named constant
{
auto path = file.path();
auto filename = path.replace_extension( "" ).string();
if( filename.size() > 36 )
{
auto uuid = filename.substr( filename.size() - 36 );
mapping.emplace(
std::move( uuid ), file.path().string() );
}
auto uuid = filename.substr( filename.size() - 36 );

Check warning on line 153 in include/geode/model/mixin/core/detail/components_storage.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/model/mixin/core/detail/components_storage.hpp:153:68 [cppcoreguidelines-avoid-magic-numbers]

36 is a magic number; consider replacing it with a named constant
mapping.emplace( std::move( uuid ), file.path().string() );
}
return mapping;
}
return mapping;
}

private:
friend class bitsery::Access;
template < typename Archive >
void serialize( Archive& serializer )
{
serializer.ext( *this,
Growable< Archive, ComponentsStorage >{
{ []( Archive& archive, ComponentsStorage& storage ) {
archive.ext( storage.components_,
bitsery::ext::StdMap{
storage.components_.max_size() },
[]( Archive& archive2, uuid& id,
ComponentPtr& item ) {
archive2.object( id );
archive2.ext(
item, bitsery::ext::StdSmartPtr{} );
} );
} } } );
}

private:
ComponentsStore components_;
};
} // namespace detail
} // namespace geode
private:
friend class bitsery::Access;
template < typename Archive >
void serialize( Archive& serializer )
{
serializer.ext( *this,
Growable< Archive, ComponentsStorage >{
{ []( Archive& archive, ComponentsStorage& storage ) {
archive.ext( storage.components_,
bitsery::ext::StdMap{
storage.components_.max_size() },
[]( Archive& archive2, uuid& component_id,
ComponentPtr& item ) {
archive2.object( component_id );
archive2.ext(
item, bitsery::ext::StdSmartPtr{} );

Check failure on line 175 in include/geode/model/mixin/core/detail/components_storage.hpp

View workflow job for this annotation

GitHub Actions / test / tidy

include/geode/model/mixin/core/detail/components_storage.hpp:175:57 [clang-diagnostic-error]

no member named 'StdSmartPtr' in namespace 'bitsery::ext'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing bitsery/ext/std_smart_ptr.h include ?

} );
} } } );
}

private:
ComponentsStore components_;
};
} // namespace geode::detail
Loading