From: Jan Olszak Date: Fri, 11 Apr 2014 13:08:21 +0000 (+0200) Subject: Operator of conversion to bool for LibvirtDomain and LibvirtConnection X-Git-Tag: submit/tizen/20140723.154014~64 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bef42d83cf9186fc9b69d196756b940d69eefebc;p=platform%2Fcore%2Fsecurity%2Fvasum.git Operator of conversion to bool for LibvirtDomain and LibvirtConnection [Bug/Feature] Easy check if the connection is not NULL. [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: I261d0a8485f2e2bb7f2260b24c95efb139433f7b --- diff --git a/common/libvirt/connection.cpp b/common/libvirt/connection.cpp index 01b1a7b..1fada98 100644 --- a/common/libvirt/connection.cpp +++ b/common/libvirt/connection.cpp @@ -58,6 +58,11 @@ virConnectPtr LibvirtConnection::get() return mCon; } +LibvirtConnection::operator bool() const +{ + return mCon != NULL; +} + } // namespace libvirt } // namespace security_containers diff --git a/common/libvirt/connection.hpp b/common/libvirt/connection.hpp index 0cf76da..70cfa3d 100644 --- a/common/libvirt/connection.hpp +++ b/common/libvirt/connection.hpp @@ -48,6 +48,11 @@ public: */ virConnectPtr get(); + /** + * @return connection pointer is not NULL + */ + operator bool() const; + private: virConnectPtr mCon = NULL; }; diff --git a/common/libvirt/domain.cpp b/common/libvirt/domain.cpp index ec29d7b..1c6c52d 100644 --- a/common/libvirt/domain.cpp +++ b/common/libvirt/domain.cpp @@ -64,6 +64,10 @@ virDomainPtr LibvirtDomain::get() return mDom; } +LibvirtDomain::operator bool() const +{ + return mDom != NULL; +} } // namespace libvirt } // namespace security_containers diff --git a/common/libvirt/domain.hpp b/common/libvirt/domain.hpp index e11e3e0..4e22a3f 100644 --- a/common/libvirt/domain.hpp +++ b/common/libvirt/domain.hpp @@ -48,6 +48,11 @@ public: */ virDomainPtr get(); + /** + * @return libvirt domain pointer is not NULL + */ + operator bool() const; + private: LibvirtConnection mCon; virDomainPtr mDom = NULL; diff --git a/server/container-admin.cpp b/server/container-admin.cpp index 3f4a6ca..b530e4e 100644 --- a/server/container-admin.cpp +++ b/server/container-admin.cpp @@ -90,7 +90,7 @@ const std::string& ContainerAdmin::getId() const void ContainerAdmin::start() { - assert(mDom.get() != NULL); + assert(mDom); if (isRunning()) { return; @@ -116,7 +116,7 @@ void ContainerAdmin::start() void ContainerAdmin::stop() { - assert(mDom.get() != NULL); + assert(mDom); if (isStopped()) { return; @@ -135,7 +135,7 @@ void ContainerAdmin::stop() void ContainerAdmin::shutdown() { - assert(mDom.get() != NULL); + assert(mDom); if (isStopped()) { return; @@ -168,7 +168,7 @@ bool ContainerAdmin::isStopped() void ContainerAdmin::suspend() { - assert(mDom.get() != NULL); + assert(mDom); if (isPaused()) { return; @@ -184,7 +184,7 @@ void ContainerAdmin::suspend() void ContainerAdmin::resume() { - assert(mDom.get() != NULL); + assert(mDom); if (!isPaused()) { return; @@ -206,7 +206,7 @@ bool ContainerAdmin::isPaused() int ContainerAdmin::getState() { - assert(mDom.get() != NULL); + assert(mDom); int state; @@ -241,7 +241,7 @@ void ContainerAdmin::setSchedulerLevel(SchedulerLevel sched) void ContainerAdmin::setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota) { - assert(mDom.get() != NULL); + assert(mDom); int maxParams = 3; int numParamsBuff = 0; @@ -264,7 +264,7 @@ void ContainerAdmin::setSchedulerParams(std::uint64_t cpuShares, std::uint64_t v std::int64_t ContainerAdmin::getSchedulerQuota() { - assert(mDom.get() != NULL); + assert(mDom); int numParamsBuff; std::unique_ptr type(virDomainGetSchedulerType(mDom.get(), &numParamsBuff), free); diff --git a/unit_tests/libvirt/connection.cpp b/unit_tests/libvirt/connection.cpp index 09376d7..c4ba8ff 100644 --- a/unit_tests/libvirt/connection.cpp +++ b/unit_tests/libvirt/connection.cpp @@ -40,28 +40,29 @@ using namespace security_containers::libvirt; const std::string CORRECT_URI_STRING = LIBVIRT_LXC_ADDRESS; const std::string BUGGY_URI_STRING = "some_random_string"; -BOOST_AUTO_TEST_CASE(ConstructorTest) -{ - std::unique_ptr con; - BOOST_REQUIRE_NO_THROW(con.reset(new LibvirtConnection(CORRECT_URI_STRING))); -} -BOOST_AUTO_TEST_CASE(DestructorTest) +BOOST_AUTO_TEST_CASE(ConstructorDestructorTest) { - std::unique_ptr con(new LibvirtConnection(CORRECT_URI_STRING)); - BOOST_REQUIRE_NO_THROW(con.reset()); + std::unique_ptr conPtr; + BOOST_REQUIRE_NO_THROW(conPtr.reset(new LibvirtConnection(CORRECT_URI_STRING))); + BOOST_REQUIRE_NO_THROW(conPtr.reset()); } BOOST_AUTO_TEST_CASE(BuggyConfigTest) { - std::unique_ptr con; - BOOST_REQUIRE_THROW(con.reset(new LibvirtConnection(BUGGY_URI_STRING)), LibvirtOperationException); + BOOST_REQUIRE_THROW(LibvirtConnection con(BUGGY_URI_STRING), LibvirtOperationException); } BOOST_AUTO_TEST_CASE(ConnectionTest) { - std::unique_ptr con(new LibvirtConnection(CORRECT_URI_STRING)); - BOOST_CHECK(con->get() != NULL); + LibvirtConnection con(CORRECT_URI_STRING); + BOOST_CHECK(con.get() != NULL); +} + +BOOST_AUTO_TEST_CASE(BoolTest) +{ + LibvirtConnection con(CORRECT_URI_STRING); + BOOST_CHECK(con); } BOOST_AUTO_TEST_SUITE_END() diff --git a/unit_tests/libvirt/domain.cpp b/unit_tests/libvirt/domain.cpp index 4a3829b..b05fa5a 100644 --- a/unit_tests/libvirt/domain.cpp +++ b/unit_tests/libvirt/domain.cpp @@ -54,15 +54,11 @@ const std::string BUGGY_CONFIG_XML = "<>"; } // namespace -BOOST_AUTO_TEST_CASE(ConstructorTest) +BOOST_AUTO_TEST_CASE(ConstructorDestructorTest) { - BOOST_REQUIRE_NO_THROW(LibvirtDomain dom(CORRECT_CONFIG_XML)); -} - -BOOST_AUTO_TEST_CASE(DestructorTest) -{ - std::unique_ptr dom(new LibvirtDomain(CORRECT_CONFIG_XML)); - BOOST_REQUIRE_NO_THROW(dom.reset()); + std::unique_ptr domPtr; + BOOST_REQUIRE_NO_THROW(domPtr.reset(new LibvirtDomain(CORRECT_CONFIG_XML))); + BOOST_REQUIRE_NO_THROW(domPtr.reset()); } BOOST_AUTO_TEST_CASE(BuggyConfigTest) @@ -72,8 +68,14 @@ BOOST_AUTO_TEST_CASE(BuggyConfigTest) BOOST_AUTO_TEST_CASE(DefinitionTest) { - std::unique_ptr dom(new LibvirtDomain(CORRECT_CONFIG_XML)); - BOOST_CHECK(dom->get() != NULL); + LibvirtDomain dom(CORRECT_CONFIG_XML); + BOOST_CHECK(dom.get() != NULL); +} + +BOOST_AUTO_TEST_CASE(BoolTest) +{ + LibvirtDomain dom(CORRECT_CONFIG_XML); + BOOST_CHECK(dom); } BOOST_AUTO_TEST_SUITE_END()