Skip to content

fix(Identifier): set default name as uuid #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
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
38 changes: 28 additions & 10 deletions src/geode/basic/identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <fstream>

#include <bitsery/ext/std_optional.h>

#include <geode/basic/bitsery_archive.hpp>
#include <geode/basic/pimpl_impl.hpp>
#include <geode/basic/uuid.hpp>
Expand All @@ -34,16 +36,18 @@ namespace geode
class Identifier::Impl
{
public:
Impl() = default;

const uuid& id() const
{
return id_;
}

std::string_view name() const
{
return name_;
if( name_ )
{
return name_.value();
}
return id_.string();
}

void set_id( const uuid& unique_id )
Expand Down Expand Up @@ -91,20 +95,34 @@ namespace geode
"[Identifier::load] Error while reading file: ", filename );
}

private:
friend class bitsery::Access;
template < typename Archive >
void serialize( Archive& archive )
{
archive.ext( *this, Growable< Archive, Impl >{
{ []( Archive& local_archive, Impl& impl ) {
local_archive.object( impl.id_ );
local_archive.text1b(
impl.name_, impl.name_.max_size() );
} } } );
archive.ext( *this,
Growable< Archive, Impl >{
{ []( Archive& local_archive, Impl& impl ) {
local_archive.object( impl.id_ );
std::string old_name;
local_archive.text1b( old_name, old_name.max_size() );
impl.name_.emplace( std::move( old_name ) );
},
[]( Archive& local_archive, Impl& impl ) {
local_archive.object( impl.id_ );
local_archive.ext( impl.name_,
bitsery::ext::StdOptional{},
[]( Archive& local_archive2,
std::string& name ) {
local_archive2.text1b(
name, name.max_size() );
} );
} } } );
}

private:
uuid id_;
std::string name_ = std::string{ DEFAULT_NAME };
std::optional< std::string > name_;
};

Identifier::Identifier() = default;
Expand Down