task payload, initialize jsonValue in constructor
We should do this because it means that variables are only initialized
once. In the simplest case:
struct MyClass{
std::string myString
MyClass(){
myString = "foo";
}
}
in the language, myString is constructed twice, once with empty string,
then a second time with "foo". If you do the construction in the
initializer list for the class the construction only happens once.
Now, the above case is contrived, the optimizer can see through it and
likely optimizes this case because std::string is relatively simple, but
for more complex structures, it's possible this generates less and
bettercompiled code, and this is worth having the check for, and making
our existing code correct.
Tested: cppcheck passing.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Ie5c7f293598408d437e7bf7b3fc93b0819e25f9f
diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 2087e54..3add4f1 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -380,10 +380,9 @@
explicit Subscription(
const std::shared_ptr<boost::beast::tcp_stream>& adaptor) :
- eventSeqNum(1)
- {
- sseConn = std::make_shared<crow::ServerSentEvents>(adaptor);
- }
+ eventSeqNum(1),
+ sseConn(std::make_shared<crow::ServerSentEvents>(adaptor))
+ {}
~Subscription() = default;
diff --git a/redfish-core/lib/task.hpp b/redfish-core/lib/task.hpp
index be48a5b..08573d5 100644
--- a/redfish-core/lib/task.hpp
+++ b/redfish-core/lib/task.hpp
@@ -41,8 +41,8 @@
{
explicit Payload(const crow::Request& req) :
targetUri(req.url), httpOperation(req.methodString()),
- httpHeaders(nlohmann::json::array())
-
+ httpHeaders(nlohmann::json::array()),
+ jsonBody(nlohmann::json::parse(req.body, nullptr, false))
{
using field_ns = boost::beast::http::field;
constexpr const std::array<boost::beast::http::field, 7>
@@ -51,7 +51,6 @@
field_ns::connection, field_ns::content_length,
field_ns::upgrade};
- jsonBody = nlohmann::json::parse(req.body, nullptr, false);
if (jsonBody.is_discarded())
{
jsonBody = nullptr;
diff --git a/redfish-core/ut/lock_test.cpp b/redfish-core/ut/lock_test.cpp
index c27ba68..33c5354 100644
--- a/redfish-core/ut/lock_test.cpp
+++ b/redfish-core/ut/lock_test.cpp
@@ -43,32 +43,31 @@
LockRequest record;
public:
- LockTest()
- {
- record = {
- "xxxxx", "hmc-id", "Read", 234, {{"DontLock", 2}, {"DontLock", 4}}};
+ LockTest() :
// lockrequest with multiple lockrequests
- request = {{"xxxxx",
- "hmc-id",
- "Read",
- 234,
- {{"DontLock", 2}, {"DontLock", 4}}},
- {"xxxxx",
- "hmc-id",
- "Read",
- 234,
- {{"DontLock", 2}, {"DontLock", 4}}}};
- request1 = {{"xxxxx",
- "hmc-id",
- "Read",
- 234,
- {{"DontLock", 2}, {"DontLock", 4}}}};
- request2 = {{"xxxxx",
- "hmc-id",
- "Write",
- 234,
- {{"LockAll", 2}, {"DontLock", 4}}}};
- }
+ request{{"xxxxx",
+ "hmc-id",
+ "Read",
+ 234,
+ {{"DontLock", 2}, {"DontLock", 4}}},
+ {"xxxxx",
+ "hmc-id",
+ "Read",
+ 234,
+ {{"DontLock", 2}, {"DontLock", 4}}}},
+ request1{{"xxxxx",
+ "hmc-id",
+ "Read",
+ 234,
+ {{"DontLock", 2}, {"DontLock", 4}}}},
+ request2{{"xxxxx",
+ "hmc-id",
+ "Write",
+ 234,
+ {{"LockAll", 2}, {"DontLock", 4}}}},
+ record{
+ "xxxxx", "hmc-id", "Read", 234, {{"DontLock", 2}, {"DontLock", 4}}}
+ {}
~LockTest() override = default;