You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
throwstd::invalid_argument("Property name cannot be empty.");
if (value.empty())
throwstd::invalid_argument("Property value cannot be empty.");
This doesn't represent much of a problem while the library is small, but such messages are likely to proliferate as the library grows and will require being kept in sync when changes are made.
An alternative is to have a single template form of such strings as a static class member...
classEntity{
private:staticconstexpr std::string_view err_propempty{"Property {} cannot be empty"};
};
This can then be consumed where required using std::format to customise the message:
if (property.empty()){
throwstd::invalid_argument(std::format(err_propempty, "name"));
}
The same approach can be used for all messages where some component of the text varies, e.g...
throwstd::runtime_error("Entity with id '" + id + "' already exists in the RO-Crate.");
}
This requires only a single string to maintain for all errors of a particular type, and having a level of indirection between the exception ctor and the string makes localisation/translation much easier. A more advanced (extreme..?) version of this could have string templates held in a std::map member and then looked up dynamically.
...it is necessary to check if our PropertyValue is the reference or literal type and then return the appropriate form of the value accordingly.
This can be avoided by having PropertyValue as a base class with LiteralProperty and ReferenceProperty which both provide an overridden value() method which returns a type-specific nlohmann::json representation of their value.
With this hierarchy, the ROCrate::serializePropertyValue method can take a const PropertyValue& pv parameter and simply call the pv.value() method irrespective of the argument's type. E.g...
throwstd::runtime_error("Entity with id '" + id + "' already exists in the RO-Crate.");
}
...to ensure that entities are unique within the map, there is std::unordered_map which is a genuine hash table with unique keys. Lookups (via .at() or operator[]) for this are constant time (unless there's a collision).
[2] C++20 std::map.contains()
As there is target_compile_features(ro-crate-cpp INTERFACE cxx_std_20) in CMakeLists.txt, the .contains() method of std::map is available which is a simpler boolean test of membership for a key which can be used in circumstances like the extract above where we don't need the particular value returned by .find() and are just testing the iterator.
At present, an ROCrate constructed in code can be serialized to json, but an ROCrate represented in json cannot be parsed as an object. Also, the serialization function...
// Write the JSON representation of the RO-Crate to a file
std::ofstream outFile(path);
if (!outFile)
throwstd::runtime_error("Failed to open file for writing: " + path);
outFile << outCrate.dump(4); // Pretty print with 4 spaces indentation
}
...combines serialization and writing to a file by iterating over all entities.
The nlohmann::json library has the facility to automatically invoke a serialization/deserialization for a custom type with the to_json/from_json functions. By providing these in a namespace, you can automate conversion between ROCrate and nlohmann::json...
Activity
pmslavin commentedon Sep 9, 2026
CMake FetchContent Module
CMake contains a FetchContent Module which allows requirements to be automatically retrieved and made available to the build procedure.
To apply this to
Catch2, theCMakeLists.txtshould include....A definition of the source and version for
Catch2can then be added withFetchContent_Declare()......and then the package retrieved with
FetchContent_MakeAvailable()...FetchContent_MakeAvailable(Catch2)This has the advantage of not requiring "FindCatch2.cmake" in CMAKE_MODULE_PATH or requiring the user to point to a
Catch2Config.cmakefile.pmslavin commentedon Sep 9, 2026
Error strings as static members
There are a few occasions where the text of error messages is repeated, such as:
ro-crate-cpp/include/ro-crate.hpp
Line 103 in 5cc0ebf
...and...
ro-crate-cpp/include/ro-crate.hpp
Line 117 in 5cc0ebf
...and other instances where the same error is repeated for different types (here
PropertyandValue)ro-crate-cpp/include/ro-crate.hpp
Lines 102 to 105 in 5cc0ebf
This doesn't represent much of a problem while the library is small, but such messages are likely to proliferate as the library grows and will require being kept in sync when changes are made.
An alternative is to have a single template form of such strings as a static class member...
This can then be consumed where required using
std::formatto customise the message:The same approach can be used for all messages where some component of the text varies, e.g...
ro-crate-cpp/include/ro-crate.hpp
Lines 233 to 235 in 5cc0ebf
This requires only a single string to maintain for all errors of a particular type, and having a level of indirection between the exception ctor and the string makes localisation/translation much easier. A more advanced (extreme..?) version of this could have string templates held in a
std::mapmember and then looked up dynamically.pmslavin commentedon Sep 9, 2026
PropertyValueas a polymorphic typeIn tests such as...
ro-crate-cpp/include/ro-crate.hpp
Lines 271 to 279 in 5cc0ebf
...it is necessary to check if our
PropertyValueis the reference or literal type and then return the appropriate form of the value accordingly.This can be avoided by having
PropertyValueas a base class withLiteralPropertyandReferencePropertywhich both provide an overriddenvalue()method which returns a type-specificnlohmann::jsonrepresentation of their value.E.g...
With this hierarchy, the
ROCrate::serializePropertyValuemethod can take aconst PropertyValue& pvparameter and simply call thepv.value()method irrespective of the argument's type. E.g...pmslavin commentedon Sep 11, 2026
std::mapthings[1]
std::unordered_mapSince in
ROCrate::addEntity()we have...ro-crate-cpp/include/ro-crate.hpp
Lines 233 to 235 in 5cc0ebf
...to ensure that entities are unique within the map, there is
std::unordered_mapwhich is a genuine hash table with unique keys. Lookups (via.at()oroperator[]) for this are constant time (unless there's a collision).[2] C++20
std::map.contains()As there is
target_compile_features(ro-crate-cpp INTERFACE cxx_std_20)inCMakeLists.txt, the.contains()method ofstd::mapis available which is a simpler boolean test of membership for a key which can be used in circumstances like the extract above where we don't need the particular value returned by.find()and are just testing the iterator.pmslavin commentedon Sep 11, 2026
nlohmann::jsonthings[1] Converting to and from json
At present, an
ROCrateconstructed in code can be serialized to json, but anROCraterepresented in json cannot be parsed as an object. Also, the serialization function...ro-crate-cpp/include/ro-crate.hpp
Lines 252 to 269 in 5cc0ebf
...combines serialization and writing to a file by iterating over all entities.
The
nlohmann::jsonlibrary has the facility to automatically invoke a serialization/deserialization for a custom type with theto_json/from_jsonfunctions. By providing these in a namespace, you can automate conversion betweenROCrateandnlohmann::json......rather than have to directly call a dedicated (de)serialization function every time.