I am writing a library which which has an Entityspace. The Entityspace can contain different entities and these entities can be linked (as parent or a child).
I've written some code below and would like to know if I can make the interface more intuitive.
Something like this may make the user code easier, more succinct:
1 2 3 4 5 6 7 8 9 10 11
entlib::Entityspace test;
// register three LinkTags with an initisalizer_list
test.registerLinkTags( { "Ordinary", "Preference", "Other" } );
// add entity
auto father = test.addEntity( "Father Ltd", entlib::EType::LTD);
// add entity and automagically add the appropriate links too
auto son = test.addEntity( "Son LLP", entlib::EType::LLP, entlib::SON_OF(father,"Ordinary") );
auto mother = test.addEntity( "Mother Plc", entlib::EType::PLC, entlib::PARENT_OF(son,"Ordinary"));
What happens in entlib::TagType("Ordinary") if "Ordinary" hasn't been registered? Ideally your code should make errors like that difficult, if not impossible.
You could make entlib::EntityID a class and pass the TagType ID:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
entlib::EntityID father = test.addEntity("Father Ltd", entlib::EType::LTD);
entlib::EntityID son = test.addEntity("Son LLP", entlib::EType::LLP);
entlib::EntityID mother = test.addEntity("Mother Plc", entlib::EType::PLC);
// register LinkTags
test::TagID ordinary{test.registerLinkTag("Ordinary")};
test::TagID preference{test.registerLinkTag("Preference")};
test::TagID other{test.registerLinkTag("Other")};
// make links
son.linkAsParent(father, 40, ordinary);
entlib::link newlnk = entlib::link{son,40,ordinary, entlib::RType::PARENT_OF}; // another way
father.AddLink(newlnk);