Operator of conversion to bool for LibvirtDomain and LibvirtConnection
authorJan Olszak <j.olszak@samsung.com>
Fri, 11 Apr 2014 13:08:21 +0000 (15:08 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 19 May 2014 11:47:15 +0000 (13:47 +0200)
[Bug/Feature]   Easy check if the connection is not NULL.
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install, run tests

Change-Id: I261d0a8485f2e2bb7f2260b24c95efb139433f7b

common/libvirt/connection.cpp
common/libvirt/connection.hpp
common/libvirt/domain.cpp
common/libvirt/domain.hpp
server/container-admin.cpp
unit_tests/libvirt/connection.cpp
unit_tests/libvirt/domain.cpp

index 01b1a7b..1fada98 100644 (file)
@@ -58,6 +58,11 @@ virConnectPtr LibvirtConnection::get()
     return mCon;
 }
 
+LibvirtConnection::operator bool() const
+{
+    return mCon != NULL;
+}
+
 
 } // namespace libvirt
 } // namespace security_containers
index 0cf76da..70cfa3d 100644 (file)
@@ -48,6 +48,11 @@ public:
      */
     virConnectPtr get();
 
+    /**
+     * @return connection pointer is not NULL
+     */
+    operator bool() const;
+
 private:
     virConnectPtr mCon = NULL;
 };
index ec29d7b..1c6c52d 100644 (file)
@@ -64,6 +64,10 @@ virDomainPtr LibvirtDomain::get()
     return mDom;
 }
 
+LibvirtDomain::operator bool() const
+{
+    return mDom != NULL;
+}
 
 } // namespace libvirt
 } // namespace security_containers
index e11e3e0..4e22a3f 100644 (file)
@@ -48,6 +48,11 @@ public:
      */
     virDomainPtr get();
 
+    /**
+     * @return libvirt domain pointer is not NULL
+     */
+    operator bool() const;
+
 private:
     LibvirtConnection mCon;
     virDomainPtr mDom = NULL;
index 3f4a6ca..b530e4e 100644 (file)
@@ -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<char, void(*)(void*)> type(virDomainGetSchedulerType(mDom.get(), &numParamsBuff), free);
index 09376d7..c4ba8ff 100644 (file)
@@ -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<LibvirtConnection> 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<LibvirtConnection> con(new LibvirtConnection(CORRECT_URI_STRING));
-    BOOST_REQUIRE_NO_THROW(con.reset());
+    std::unique_ptr<LibvirtConnection> 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<LibvirtConnection> 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<LibvirtConnection> 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()
index 4a3829b..b05fa5a 100644 (file)
@@ -54,15 +54,11 @@ const std::string BUGGY_CONFIG_XML = "<><TRASH>";
 
 } // 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<LibvirtDomain> dom(new LibvirtDomain(CORRECT_CONFIG_XML));
-    BOOST_REQUIRE_NO_THROW(dom.reset());
+    std::unique_ptr<LibvirtDomain> 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<LibvirtDomain> 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()