From 86c2c75b01f6c2619562a823c59497c201dba4e9 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Mon, 4 Mar 2024 13:32:56 +0100 Subject: [PATCH] Coding style --- include/openspace/events/eventengine.h | 3 +- src/documentation/documentationengine.cpp | 402 +++++++++++----------- src/engine/openspaceengine.cpp | 3 +- src/events/eventengine.cpp | 8 +- src/interaction/actionmanager.cpp | 1 + 5 files changed, 210 insertions(+), 207 deletions(-) diff --git a/include/openspace/events/eventengine.h b/include/openspace/events/eventengine.h index aee899f575..a94a87e680 100644 --- a/include/openspace/events/eventengine.h +++ b/include/openspace/events/eventengine.h @@ -143,7 +143,8 @@ public: * * \return The unordered map of all registered actions */ - const std::unordered_map>& eventActions() const; + const std::unordered_map>& + eventActions() const; /** * Enables the event identified by the \p identifier. If the event is already enabled, diff --git a/src/documentation/documentationengine.cpp b/src/documentation/documentationengine.cpp index 8ae1d37560..b4d8f9e8d3 100644 --- a/src/documentation/documentationengine.cpp +++ b/src/documentation/documentationengine.cpp @@ -34,8 +34,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -46,215 +46,218 @@ #include #include #include - #include #include -namespace openspace::documentation { -constexpr std::string_view _loggerCat = "DocumentationEngine"; +namespace { + constexpr std::string_view _loggerCat = "DocumentationEngine"; -// General keys -constexpr const char* NameKey = "name"; -constexpr const char* IdentifierKey = "identifier"; -constexpr const char* DescriptionKey = "description"; -constexpr const char* DataKey = "data"; -constexpr const char* TypeKey = "type"; -constexpr const char* DocumentationKey = "documentation"; -constexpr const char* ActionKey = "action"; -constexpr const char* IdKey = "id"; + // General keys + constexpr const char* NameKey = "name"; + constexpr const char* IdentifierKey = "identifier"; + constexpr const char* DescriptionKey = "description"; + constexpr const char* DataKey = "data"; + constexpr const char* TypeKey = "type"; + constexpr const char* DocumentationKey = "documentation"; + constexpr const char* ActionKey = "action"; + constexpr const char* IdKey = "id"; -// Actions -constexpr const char* ActionTitle = "Actions"; // Title in documentation sidebar -constexpr const char* GuiNameKey = "guiName"; -constexpr const char* CommandKey = "command"; + // Actions + constexpr const char* ActionTitle = "Actions"; + constexpr const char* GuiNameKey = "guiName"; + constexpr const char* CommandKey = "command"; -// Factory -constexpr const char* FactoryTitle = "Asset Types"; // Title in documentation sidebar -constexpr const char* MembersKey = "members"; -constexpr const char* OptionalKey = "optional"; -constexpr const char* ReferenceKey = "reference"; -constexpr const char* FoundKey = "found"; -constexpr const char* RestrictionsKey = "restrictions"; -constexpr const char* ClassesKey = "classes"; + // Factory + constexpr const char* FactoryTitle = "Asset Types"; + constexpr const char* MembersKey = "members"; + constexpr const char* OptionalKey = "optional"; + constexpr const char* ReferenceKey = "reference"; + constexpr const char* FoundKey = "found"; + constexpr const char* RestrictionsKey = "restrictions"; + constexpr const char* ClassesKey = "classes"; -constexpr const char* OtherName = "Other"; -constexpr const char* OtherIdentifierName = "other"; -constexpr const char* propertyOwnerName = "propertyOwner"; -constexpr const char* categoryName = "category"; + constexpr const char* OtherName = "Other"; + constexpr const char* OtherIdentifierName = "other"; + constexpr const char* propertyOwnerName = "propertyOwner"; + constexpr const char* categoryName = "category"; -// Properties -constexpr const char* SettingsTitle = "Settings"; // Title in documentation sidebar -constexpr const char* SceneTitle = "Scene"; // Title in documentation sidebar -constexpr const char* PropertiesKeys = "properties"; -constexpr const char* PropertyOwnersKey = "propertyOwners"; -constexpr const char* TagsKey = "tags"; -constexpr const char* UriKey = "uri"; + // Properties + constexpr const char* SettingsTitle = "Settings"; + constexpr const char* SceneTitle = "Scene"; + constexpr const char* PropertiesKeys = "properties"; + constexpr const char* PropertyOwnersKey = "propertyOwners"; + constexpr const char* TagsKey = "tags"; + constexpr const char* UriKey = "uri"; -// Scripting -constexpr const char* ScriptingTitle = "Scripting API"; // Title in documentation sidebar -constexpr const char* DefaultValueKey = "defaultValue"; -constexpr const char* ArgumentsKey = "arguments"; -constexpr const char* ReturnTypeKey = "returnType"; -constexpr const char* HelpKey = "help"; -constexpr const char* FileKey = "file"; -constexpr const char* LineKey = "line"; -constexpr const char* LibraryKey = "library"; -constexpr const char* FullNameKey = "fullName"; -constexpr const char* FunctionsKey = "functions"; -constexpr const char* SourceLocationKey = "sourceLocation"; -constexpr const char* OpenSpaceScriptingKey = "openspace"; + // Scripting + constexpr const char* ScriptingTitle = "Scripting API"; + constexpr const char* DefaultValueKey = "defaultValue"; + constexpr const char* ArgumentsKey = "arguments"; + constexpr const char* ReturnTypeKey = "returnType"; + constexpr const char* HelpKey = "help"; + constexpr const char* FileKey = "file"; + constexpr const char* LineKey = "line"; + constexpr const char* LibraryKey = "library"; + constexpr const char* FullNameKey = "fullName"; + constexpr const char* FunctionsKey = "functions"; + constexpr const char* SourceLocationKey = "sourceLocation"; + constexpr const char* OpenSpaceScriptingKey = "openspace"; -// Licenses -constexpr const char* LicensesTitle = "Licenses"; // Title in documentation sidebar -constexpr const char* ProfileName = "Profile"; -constexpr const char* AssetsName = "Assets"; -constexpr const char* LicensesName = "Licenses"; -constexpr const char* NoLicenseName = "No License"; -constexpr const char* NoDataName = ""; + // Licenses + constexpr const char* LicensesTitle = "Licenses"; + constexpr const char* ProfileName = "Profile"; + constexpr const char* AssetsName = "Assets"; + constexpr const char* LicensesName = "Licenses"; + constexpr const char* NoLicenseName = "No License"; + constexpr const char* NoDataName = ""; -constexpr const char* ProfileNameKey = "profileName"; -constexpr const char* VersionKey = "version"; -constexpr const char* AuthorKey = "author"; -constexpr const char* UrlKey = "url"; -constexpr const char* LicenseKey = "license"; -constexpr const char* NoLicenseKey = "noLicense"; -constexpr const char* IdentifiersKey = "identifiers"; -constexpr const char* PathKey = "path"; -constexpr const char* AssetKey = "assets"; -constexpr const char* LicensesKey = "licenses"; + constexpr const char* ProfileNameKey = "profileName"; + constexpr const char* VersionKey = "version"; + constexpr const char* AuthorKey = "author"; + constexpr const char* UrlKey = "url"; + constexpr const char* LicenseKey = "license"; + constexpr const char* NoLicenseKey = "noLicense"; + constexpr const char* IdentifiersKey = "identifiers"; + constexpr const char* PathKey = "path"; + constexpr const char* AssetKey = "assets"; + constexpr const char* LicensesKey = "licenses"; -// Keybindings -constexpr const char* KeybindingsTitle = "Keybindings"; // Title in documentation sidebar -constexpr const char* KeybindingsKey = "keybindings"; + // Keybindings + constexpr const char* KeybindingsTitle = "Keybindings"; + constexpr const char* KeybindingsKey = "keybindings"; -// Events -constexpr const char* EventsTitle = "Events"; // Title in documentation sidebar -constexpr const char* FiltersKey = "filters"; -constexpr const char* ActionsKey = "actions"; + // Events + constexpr const char* EventsTitle = "Events"; + constexpr const char* FiltersKey = "filters"; + constexpr const char* ActionsKey = "actions"; + nlohmann::json generateJsonDocumentation( + const openspace::documentation::Documentation& d) + { + using namespace openspace::documentation; -nlohmann::json generateJsonDocumentation(const Documentation & d) { - nlohmann::json json; + nlohmann::json json; - json[NameKey] = d.name; - json[IdentifierKey] = d.id; - json[DescriptionKey] = d.description; - json[MembersKey] = nlohmann::json::array(); + json[NameKey] = d.name; + json[IdentifierKey] = d.id; + json[DescriptionKey] = d.description; + json[MembersKey] = nlohmann::json::array(); - for (const DocumentationEntry& p : d.entries) { - nlohmann::json entry; - entry[NameKey] = p.key; - entry[OptionalKey] = p.optional.value; - entry[TypeKey] = p.verifier->type(); - entry[DocumentationKey] = p.documentation; + for (const DocumentationEntry& p : d.entries) { + nlohmann::json entry; + entry[NameKey] = p.key; + entry[OptionalKey] = p.optional.value; + entry[TypeKey] = p.verifier->type(); + entry[DocumentationKey] = p.documentation; - TableVerifier* tv = dynamic_cast(p.verifier.get()); - ReferencingVerifier* rv = dynamic_cast(p.verifier.get()); + auto* tv = dynamic_cast(p.verifier.get()); + auto* rv = dynamic_cast(p.verifier.get()); - if (rv) { - const std::vector& documentations = DocEng.documentations(); - auto it = std::find_if( - documentations.begin(), - documentations.end(), - [rv](const Documentation& doc) { return doc.id == rv->identifier; } - ); + if (rv) { + const std::vector& doc = DocEng.documentations(); + auto it = std::find_if( + doc.begin(), + doc.end(), + [rv](const Documentation& doc) { return doc.id == rv->identifier; } + ); - if (it == documentations.end()) { - entry[ReferenceKey][FoundKey] = false; + if (it == doc.end()) { + entry[ReferenceKey][FoundKey] = false; + } + else { + nlohmann::json reference; + reference[FoundKey] = true; + reference[NameKey] = it->name; + reference[IdentifierKey] = rv->identifier; + + entry[ReferenceKey] = reference; + } + } + else if (tv) { + Documentation doc = { .entries = tv->documentations }; + + // Since this is a table we need to recurse this function to extract data + nlohmann::json restrictions = generateJsonDocumentation(doc); + entry[RestrictionsKey] = restrictions; } else { - nlohmann::json reference; - reference[FoundKey] = true; - reference[NameKey] = it->name; - reference[IdentifierKey] = rv->identifier; - - entry[ReferenceKey] = reference; + entry[DescriptionKey] = p.verifier->documentation(); } + json[MembersKey].push_back(entry); } - else if (tv) { - Documentation doc = { .entries = tv->documentations }; + openspace::sortJson(json[MembersKey], NameKey); - // Since this is a table we need to recurse this function to extract all data - nlohmann::json restrictions = generateJsonDocumentation(doc); - entry[RestrictionsKey] = restrictions; + return json; + } + + nlohmann::json createPropertyJson(openspace::properties::PropertyOwner* owner) { + ZoneScoped; + + using namespace openspace; + nlohmann::json json; + json[NameKey] = !owner->guiName().empty() ? owner->guiName() : owner->identifier(); + + json[DescriptionKey] = owner->description(); + json[PropertiesKeys] = nlohmann::json::array(); + json[PropertyOwnersKey] = nlohmann::json::array(); + json[TypeKey] = owner->type(); + json[TagsKey] = owner->tags(); + + for (properties::Property* p : owner->properties()) { + nlohmann::json propertyJson; + std::string name = !p->guiName().empty() ? p->guiName() : p->identifier(); + propertyJson[NameKey] = name; + propertyJson[TypeKey] = p->className(); + propertyJson[UriKey] = p->fullyQualifiedIdentifier(); + propertyJson[IdentifierKey] = p->identifier(); + propertyJson[DescriptionKey] = p->description(); + + json[PropertiesKeys].push_back(propertyJson); } - else { - entry[DescriptionKey] = p.verifier->documentation(); + sortJson(json[PropertiesKeys], NameKey); + + for (properties::PropertyOwner* o : owner->propertySubOwners()) { + nlohmann::json propertyOwner; + json[PropertyOwnersKey].push_back(createPropertyJson(o)); } - json[MembersKey].push_back(entry); - } - sortJson(json[MembersKey], NameKey); + sortJson(json[PropertyOwnersKey], NameKey); - return json; -} - -nlohmann::json createPropertyJson(openspace::properties::PropertyOwner* owner) { - ZoneScoped; - - using namespace openspace; - nlohmann::json json; - json[NameKey] = !owner->guiName().empty() ? owner->guiName() : owner->identifier(); - - json[DescriptionKey] = owner->description(); - json[PropertiesKeys] = nlohmann::json::array(); - json[PropertyOwnersKey] = nlohmann::json::array(); - json[TypeKey] = owner->type(); - json[TagsKey] = owner->tags(); - - const std::vector& properties = owner->properties(); - for (properties::Property* p : properties) { - nlohmann::json propertyJson; - std::string name = !p->guiName().empty() ? p->guiName() : p->identifier(); - propertyJson[NameKey] = name; - propertyJson[TypeKey] = p->className(); - propertyJson[UriKey] = p->fullyQualifiedIdentifier(); - propertyJson[IdentifierKey] = p->identifier(); - propertyJson[DescriptionKey] = p->description(); - - json[PropertiesKeys].push_back(propertyJson); - } - sortJson(json[PropertiesKeys], NameKey); - - auto propertyOwners = owner->propertySubOwners(); - for (properties::PropertyOwner* o : propertyOwners) { - nlohmann::json propertyOwner; - json[PropertyOwnersKey].push_back(createPropertyJson(o)); - } - sortJson(json[PropertyOwnersKey], NameKey); - - return json; -} - -nlohmann::json LuaFunctionToJson(const openspace::scripting::LuaLibrary::Function& f, - bool includeSourceLocation) -{ - using namespace openspace; - using namespace openspace::scripting; - nlohmann::json function; - function[NameKey] = f.name; - nlohmann::json arguments = nlohmann::json::array(); - - for (const LuaLibrary::Function::Argument& arg : f.arguments) { - nlohmann::json argument; - argument[NameKey] = arg.name; - argument[TypeKey] = arg.type; - argument[DefaultValueKey] = arg.defaultValue.value_or(NoDataName); - arguments.push_back(argument); + return json; } - function[ArgumentsKey] = arguments; - function[ReturnTypeKey] = f.returnType; - function[HelpKey ] = f.helpText; + nlohmann::json luaFunctionToJson(const openspace::scripting::LuaLibrary::Function& f, + bool includeSourceLocation) + { + using namespace openspace::scripting; - if (includeSourceLocation) { - nlohmann::json sourceLocation; - sourceLocation[FileKey] = f.sourceLocation.file; - sourceLocation[LineKey] = f.sourceLocation.line; - function[SourceLocationKey] = sourceLocation; + nlohmann::json function; + function[NameKey] = f.name; + nlohmann::json arguments = nlohmann::json::array(); + + for (const LuaLibrary::Function::Argument& arg : f.arguments) { + nlohmann::json argument; + argument[NameKey] = arg.name; + argument[TypeKey] = arg.type; + argument[DefaultValueKey] = arg.defaultValue.value_or(NoDataName); + arguments.push_back(argument); + } + + function[ArgumentsKey] = arguments; + function[ReturnTypeKey] = f.returnType; + function[HelpKey] = f.helpText; + + if (includeSourceLocation) { + nlohmann::json sourceLocation; + sourceLocation[FileKey] = f.sourceLocation.file; + sourceLocation[LineKey] = f.sourceLocation.line; + function[SourceLocationKey] = sourceLocation; + } + + return function; } +} // namespace - return function; -} +namespace openspace::documentation { DocumentationEngine* DocumentationEngine::_instance = nullptr; @@ -294,13 +297,11 @@ DocumentationEngine& DocumentationEngine::ref() { nlohmann::json DocumentationEngine::generateScriptEngineJson() const { ZoneScoped; - using namespace openspace; - using namespace scripting; + using namespace openspace::scripting; const std::vector libraries = global::scriptEngine->allLuaLibraries(); nlohmann::json json; for (const LuaLibrary& l : libraries) { - nlohmann::json library; std::string libraryName = l.name; // Keep the library key for backwards compatability @@ -310,13 +311,13 @@ nlohmann::json DocumentationEngine::generateScriptEngineJson() const { library[FullNameKey] = libraryName.empty() ? os : os + "." + libraryName; for (const LuaLibrary::Function& f : l.functions) { - bool hasSourceLocation = true; - library[FunctionsKey].push_back(LuaFunctionToJson(f, hasSourceLocation)); + constexpr bool HasSourceLocation = true; + library[FunctionsKey].push_back(luaFunctionToJson(f, HasSourceLocation)); } for (const LuaLibrary::Function& f : l.documentations) { - bool hasSourceLocation = false; - library[FunctionsKey].push_back(LuaFunctionToJson(f, hasSourceLocation)); + constexpr bool HasSourceLocation = false; + library[FunctionsKey].push_back(luaFunctionToJson(f, HasSourceLocation)); } sortJson(library[FunctionsKey], NameKey); json.push_back(library); @@ -375,12 +376,12 @@ nlohmann::json DocumentationEngine::generateLicensesGroupedByLicense() const { assetsJson[NameKey] = AssetsName; assetsJson[TypeKey] = LicensesName; - using K = const std::string; + using K = std::string; using V = nlohmann::json; - for (std::pair& assetLicense : assetLicenses) { + for (const std::pair& assetLicense : assetLicenses) { nlohmann::json entry; - entry[NameKey] = std::move(assetLicense.first); - entry[AssetKey] = std::move(assetLicense.second); + entry[NameKey] = assetLicense.first; + entry[AssetKey] = assetLicense.second; sortJson(entry[AssetKey], NameKey); assetsJson[LicensesKey].push_back(entry); } @@ -389,7 +390,6 @@ nlohmann::json DocumentationEngine::generateLicensesGroupedByLicense() const { nlohmann::json result; result[NameKey] = LicensesTitle; result[DataKey] = json; - return result; } @@ -426,14 +426,14 @@ nlohmann::json DocumentationEngine::generateLicenseList() const { assetJson[LicenseKey] = meta->license; assetJson[IdentifiersKey] = meta->identifiers; assetJson[PathKey] = asset->path().string(); - json.push_back(assetJson); } return json; } nlohmann::json DocumentationEngine::generateEventJson() const { - const std::unordered_map>& eventActions = + using Type = events::Event::Type; + const std::unordered_map>& eventActions = global::eventEngine->eventActions(); nlohmann::json events; @@ -462,7 +462,7 @@ nlohmann::json DocumentationEngine::generateEventJson() const { std::string filtersString = ""; for (std::string_view key : keys) { std::string value = filters.value(key); - filtersString += fmt::format("{} = {}, ", std::string(key), value); + filtersString += fmt::format("{} = {}, ", key, value); } filtersString.pop_back(); // Remove last space from last entry filtersString.pop_back(); // Remove last comma from last entry @@ -500,9 +500,8 @@ nlohmann::json DocumentationEngine::generateFactoryManagerJson() const { auto factoryDoc = std::find_if( docs.begin(), docs.end(), - [&factoryInfo](const Documentation& d) { - return d.name == factoryInfo.name; - }); + [&factoryInfo](const Documentation& d) { return d.name == factoryInfo.name; } + ); if (factoryDoc != docs.end()) { nlohmann::json documentation = generateJsonDocumentation(*factoryDoc); factory[ClassesKey].push_back(documentation); @@ -522,9 +521,8 @@ nlohmann::json DocumentationEngine::generateFactoryManagerJson() const { auto found = std::find_if( docs.begin(), docs.end(), - [&c](const Documentation& d) { - return d.name == c; - }); + [&c](const Documentation& d) { return d.name == c; } + ); if (found != docs.end()) { nlohmann::json documentation = generateJsonDocumentation(*found); factory[ClassesKey].push_back(documentation); @@ -582,9 +580,12 @@ nlohmann::json DocumentationEngine::generateKeybindingsJson() const { } nlohmann::json DocumentationEngine::generatePropertyOwnerJson( - properties::PropertyOwner* owner) const { + properties::PropertyOwner* owner) const +{ ZoneScoped; + ghoul_assert(owner, "Owner must not be nullptr"); + nlohmann::json json; std::vector subOwners = owner->propertySubOwners(); for (properties::PropertyOwner* o : subOwners) { @@ -653,9 +654,8 @@ void DocumentationEngine::writeDocumentation() const { nlohmann::json result; result[DocumentationKey] = documentation; - std::ofstream out(absPath("${DOCUMENTATION}/documentationData.js")); + std::ofstream out = std::ofstream(absPath("${DOCUMENTATION}/documentationData.js")); out << "var data = " << result.dump(); - out.close(); } nlohmann::json DocumentationEngine::generateActionJson() const { diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 7b62746d0b..cd568f2e9f 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -773,7 +773,8 @@ void OpenSpaceEngine::loadAssets() { _writeDocumentationTask = std::async( - &documentation::DocumentationEngine::writeDocumentation, DocEng + &documentation::DocumentationEngine::writeDocumentation, + DocEng ); LTRACE("OpenSpaceEngine::loadAsset(end)"); diff --git a/src/events/eventengine.cpp b/src/events/eventengine.cpp index 1e6e303b93..087b55ff89 100644 --- a/src/events/eventengine.cpp +++ b/src/events/eventengine.cpp @@ -31,7 +31,7 @@ namespace { constexpr std::string_view _loggerCat = "EventEngine"; -} +} // namespace namespace openspace { @@ -54,8 +54,7 @@ void EventEngine::postFrameCleanup() { #endif // _DEBUG } -void EventEngine::registerEventAction(events::Event::Type type, - std::string identifier, +void EventEngine::registerEventAction(events::Event::Type type, std::string identifier, std::optional filter) { ActionInfo ai; @@ -171,7 +170,8 @@ std::vector EventEngine::registeredActions() const { } const std::unordered_map>& const -EventEngine::eventActions() const { +EventEngine::eventActions() const +{ return _eventActions; } diff --git a/src/interaction/actionmanager.cpp b/src/interaction/actionmanager.cpp index a3900159ae..608ce69005 100644 --- a/src/interaction/actionmanager.cpp +++ b/src/interaction/actionmanager.cpp @@ -128,4 +128,5 @@ scripting::LuaLibrary ActionManager::luaLibrary() { } }; } + } // namespace openspace::interaction