Skip to content

Remove shared pointer behaviour: take 2 - #20

Open
OliverWoolland wants to merge 3 commits into
mainfrom
entity-cleanup
Open

OliverWoolland wants to merge 3 commits into
mainfrom
entity-cleanup

Conversation

@OliverWoolland

Copy link
Copy Markdown
Contributor

So I think this approach to removing the shared pointer oddity might work a bit better ergonomically

I've made the Entity constructor is private and only accessed via the addEntity method of the ROCrate class, which now requires the id at creation.

so for creation

-    Entity alice({"Person"});
+    Entity& alice = crate.addEntity("#alice", {"Person"});
     alice.set("name", "Alice");
     alice.set("description", "One of hopefully many Contextual Entities");
-    crate.addEntity("#alice", alice);

and fetching

-    Entity root = crate.getEntity("ro-crate-metadata.json");
+    Entity& root = crate.getEntity("ro-crate-metadata.json");

This pull request refactors the Entity lifecycle in the RO-Crate C++ library to ensure that all entities are created and owned by an ROCrate instance via the addEntity method. Direct instantiation of Entity objects is now disallowed outside the crate, and all entity manipulations are performed through references returned by the crate. The documentation and tests have been updated to reflect this new pattern, making entity ownership and mutability clearer and safer.

Key changes include:

Entity Creation and Ownership Model:

  • The Entity constructor is now private; entities can only be created via ROCrate::addEntity, which returns a mutable reference to the entity. Direct instantiation (e.g., Entity({"Person"})) is no longer allowed. (include/ro-crate.hpp, [1] [2]
  • The addEntity method on ROCrate now constructs and registers the entity internally, returning a reference; it no longer accepts an existing entity object as an argument. (include/ro-crate.hpp, [1] [2]

API and Internal Data Structure Updates:

  • All internal references to entity properties now use value semantics instead of shared_ptr, simplifying the property management. (include/ro-crate.hpp, [1] [2]
  • The logic for adding and retrieving entities in the crate has been updated to use the new pattern, including during deserialization. (include/ro-crate.hpp, include/ro-crate.hppL376-L386)

Documentation and Usage Examples:

  • The README.md has been rewritten to explain the new pattern: entities are always created and owned by the crate, and all property updates occur via references returned from addEntity or getEntity. It warns about copying entities (which breaks the reference to the crate). (README.md, [1] [2] [3] [4] [5]

Test and Example Refactoring:

  • All tests and code examples have been updated to use the new entity creation pattern, ensuring consistency and demonstrating correct usage. (tests/example.cpp, [1]; tests/integration.cpp, [2]

Minor Cleanup:

@OliverWoolland

OliverWoolland commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

would close #13 #8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant