From ceb392a198dde4c7d2290034fa99e959ebaac988 Mon Sep 17 00:00:00 2001 From: "Marian W." Date: Fri, 7 Feb 2025 22:27:57 +0100 Subject: [PATCH] Improve type safety in GetValueOrDefault --- minipp/minipp.hpp | 5 ++++- minipp/test.cpp | 2 +- minipp/test.mini | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/minipp/minipp.hpp b/minipp/minipp.hpp index f1a3ab3..806e4f7 100644 --- a/minipp/minipp.hpp +++ b/minipp/minipp.hpp @@ -272,7 +272,10 @@ namespace minipp ValueDataType* value = nullptr; if (GetValue(key, &value) != EResult::Success) return defaultValue; - return value->GetValue(); + auto casted = dynamic_cast(value); + if (casted == nullptr) + return defaultValue; + return casted->GetValue(); } public: diff --git a/minipp/test.cpp b/minipp/test.cpp index 17a2bc6..339d089 100644 --- a/minipp/test.cpp +++ b/minipp/test.cpp @@ -10,7 +10,7 @@ int main() MiniPPFile file; result = file.Parse("test.mini"); - auto& root = file.GetRoot(); + auto& root = file.GetRoot(); MiniPPFile::Section* gameSection = nullptr; result = root.GetSubSection("game", &gameSection); diff --git a/minipp/test.mini b/minipp/test.mini index 5496dcb..ac2c34f 100644 --- a/minipp/test.mini +++ b/minipp/test.mini @@ -1,6 +1,6 @@ [game] name = "Test Game\nNext Line" -ver sion = "1.0.0" +version = "1.0.0" year = 2025 completionPercentage = 50.0f # Should only be true if completionPercentage is 100