[Parted-commits] [SCM] GNU Parted Library - Language Bindings branch, master, updated. LIBPARTED_0_1-19-gcc7e26b

Debarshi Ray (none) debray at libra.
Fri Nov 23 06:30:19 UTC 2007


The branch, master has been updated
       via  cc7e26bb54148a6e196c10b15861bee77cb8e6de (commit)
      from  432921594d1617bd1595e394633ed9372c9470e4 (commit)


- Shortlog ------------------------------------------------------------
cc7e26b Replacing constructors, which can throw exceptions, with factory methods.

Summary of changes:
 cpp/include/partedpp/device.h |   32 ++++++++++++++++----------------
 cpp/libpartedpp/device.cc     |   23 +++++++++++++++++------
 cpp/tests/test1.cc            |   10 +++++++++-
 cpp/tests/test2.cc            |   12 +++++++-----
 cpp/tests/test3.cc            |   19 +++++++++++++------
 cpp/tests/test4.cc            |   10 +++++++++-
 cpp/tests/test5.cc            |   10 +++++++++-
 7 files changed, 80 insertions(+), 36 deletions(-)
-----------------------------------------------------------------------
Details of changes:

commit cc7e26bb54148a6e196c10b15861bee77cb8e6de
Author: Debarshi Ray <rishi at gnu.org>
Date:   Fri Nov 23 11:55:05 2007 +0530

    Replacing constructors, which can throw exceptions, with factory methods.

diff --git a/cpp/include/partedpp/device.h b/cpp/include/partedpp/device.h
index 80de52a..322933c 100644
--- a/cpp/include/partedpp/device.h
+++ b/cpp/include/partedpp/device.h
@@ -21,6 +21,7 @@ namespace Ped {
         friend class Geometry;
 
         private:
+        Device( );
         Device(const PedDevice *device);
         Device(const Device &device);
         Device& operator=(const Device &device);
@@ -44,34 +45,33 @@ namespace Ped {
 
         public:
         /**
-         * The default device constructor.
-         * It tries to detect all devices and constructs a list and
-         * automatically executes get_next(). Thus, you don't need to do that.
-         * The constructor hrows an runtime_error exception if no device was
-         * found.
+         * The default Device destructor.
+         * Internally it destroys the underlying C device.
+         */
+        // ped_device_destroy(PedDevice *dev)
+        ~Device                             ( );
+
+        /**
+         * Tries to detect all devices and constructs a list and automatically
+         * executes get_next(). Thus, you don't need to do that.
+         * @return The first device in the list. Throws a runtime_error
+         * exception if no device was found.
          * @see get_next()
          */
         // ped_device_probe_all()
-        explicit Device                     ( ) throw (std::runtime_error);
+        static Device*      probe_all       ( ) throw (std::runtime_error);
 
         /**
-         * Device constructor which needs a string as an argument.
+         * Needs a string as an argument.
          * @param dev Usually a block device, e.g. /dev/hda
-         * @return The device for the specific parameter. Throws an
+         * @return The device for the specific parameter. Throws a
          * runtime_error exception if device doesn't exist.
          */
         // ped_device_get(char *name)
-        explicit Device                     (const std::string &dev)
+        static Device*      get             (const std::string &dev)
             throw (std::runtime_error);
 
         /**
-         * The default Device destructor.
-         * Internally it destroys the underlying C device.
-         */
-        // ped_device_destroy(PedDevice *dev)
-        ~Device                             ( );
-
-        /**
          * Tries to get the next device.
          * The method tries to get the next block device.
          * @return Next device, if it's the last one throws a runtime_error.
diff --git a/cpp/libpartedpp/device.cc b/cpp/libpartedpp/device.cc
index 3a3b510..63d3360 100644
--- a/cpp/libpartedpp/device.cc
+++ b/cpp/libpartedpp/device.cc
@@ -24,6 +24,9 @@ using namespace Ped;
  * private *
  * ------- */
 
+Device::Device() {
+}
+
 Device::Device(const PedDevice *device)
     : device_(ped_device_get(device->path)) {
 }
@@ -52,19 +55,27 @@ PedDevice* Device::get_c_device() {
  * public *
  * ------ */
 
-Device::Device() throw (std::runtime_error) {
+Device* Device::probe_all() throw (std::runtime_error) {
+    Device *device = new Device();
+
     ped_device_probe_all();
-    device_ = ped_device_get_next(NULL);
-    if (!device_) {
+    device->device_ = ped_device_get_next(NULL);
+    if (!device->device_) {
+        delete device;
         throw std::runtime_error("No further devices found!");
     }
+    return device;
 }
 
-Device::Device(const std::string& name) throw (std::runtime_error) {
-    device_ = ped_device_get(name.c_str());
-    if (!device_) {
+Device* Device::get(const std::string& name) throw (std::runtime_error) {
+    Device *device = new Device ();
+
+    device->device_ = ped_device_get(name.c_str());
+    if (!device->device_) {
+        delete device;
         throw std::runtime_error("");
     }
+    return device;
 }
 
 Device& Device::get_next() throw (std::runtime_error) {
diff --git a/cpp/tests/test1.cc b/cpp/tests/test1.cc
index c50102c..bd7a3a5 100644
--- a/cpp/tests/test1.cc
+++ b/cpp/tests/test1.cc
@@ -7,7 +7,15 @@ using namespace std;
 
 int main(int argc, char **argv) {
 
-    Ped::Device *test = new Ped::Device("/dev/hda");
+    Ped::Device *test;
+    try {
+        test = Ped::Device::get("/dev/hda");
+    }
+    catch (runtime_error &e) {
+        cerr << e.what() << endl;
+        return 1;
+    }
+
     Ped::Disk disk(*test);
     cout << "Disk type is: " << disk.get_disktype().get_name() << endl;
     delete test;
diff --git a/cpp/tests/test2.cc b/cpp/tests/test2.cc
index 3825ff5..20c6e87 100644
--- a/cpp/tests/test2.cc
+++ b/cpp/tests/test2.cc
@@ -11,14 +11,16 @@ int main(int argc, char **argv) {
     string s;
     cout << "Enter device path: ";
     cin >> s;
+
+    Ped::Device *test;
     try {
-      Ped::Device test(s);
-      Ped::Disk disk(test);
-      cout << "Disk type is: " << disk.get_disktype().get_name() << endl;
+        test = Ped::Device::get(s);
+        Ped::Disk disk(*test);
+        cout << "Disk type is: " << disk.get_disktype().get_name() << endl;
     }
     catch (runtime_error &e) {
-      cout << e.what() << endl;
+        cout << e.what() << endl;
     }
+    delete test;
     return 0;
 }
-
diff --git a/cpp/tests/test3.cc b/cpp/tests/test3.cc
index 00cf8cb..86851fe 100644
--- a/cpp/tests/test3.cc
+++ b/cpp/tests/test3.cc
@@ -8,9 +8,19 @@ using namespace std;
 
 int main(int argc, char **argv) {
 
-    Ped::Device *device = new Ped::Device();
+    Ped::Device *device;
+    try {
+        device = Ped::Device::probe_all();
+    }
+    catch (runtime_error &e) {
+        cerr << e.what() << endl;
+        return 1;
+    }
+
     Ped::Disk *disk1    = new Ped::Disk(*device);
     disk1->print();
+    delete disk1;
+
     try {
         Ped::Disk *disk2 = new Ped::Disk(device->get_next());
         disk2->print();
@@ -18,12 +28,9 @@ int main(int argc, char **argv) {
     }
     catch (std::runtime_error &e) {
         cout << e.what() << endl;
-        Ped::Device *dev = new Ped::Device("/dev/hda");
-        Ped::Disk disk(*dev);
-        cout << "Disk type is: " << disk.get_disktype().get_name() << endl;
+        delete device;
+        return 1;
     }
-    delete disk1;
     delete device;
     return 0;
 }
-
diff --git a/cpp/tests/test4.cc b/cpp/tests/test4.cc
index c0ca554..485caaa 100644
--- a/cpp/tests/test4.cc
+++ b/cpp/tests/test4.cc
@@ -7,7 +7,15 @@ using namespace std;
 
 int main(int argc, char **argv) {
 
-    Ped::Device *device = new Ped::Device("/dev/hda");
+    Ped::Device *device;
+    try {
+        device = Ped::Device::get("/dev/hda");
+    }
+    catch (runtime_error &e) {
+        cerr << e.what() << endl;
+        return 1;
+    }
+
     Ped::Disk *disk = new Ped::Disk(*device);
     int i;
     cout << "# of primary partitions: " << disk->get_primary_partition_count() << endl;
diff --git a/cpp/tests/test5.cc b/cpp/tests/test5.cc
index 1f9c98e..ae2a315 100644
--- a/cpp/tests/test5.cc
+++ b/cpp/tests/test5.cc
@@ -7,7 +7,15 @@ using namespace std;
 
 int main(int argc, char **argv) {
 
-    Ped::Device *device = new Ped::Device("/dev/hda");
+    Ped::Device *device;
+    try {
+        device = Ped::Device::get("/dev/hda");
+    }
+    catch (runtime_error &e) {
+        cerr << e.what() << endl;
+        return 1;
+    }
+
     cout << "Model: " << device->get_model() << endl;
     cout << "Device path: " << device->get_path() << endl;
     cout << "Type: " << device->get_type() << endl;

-- 
GNU Parted Library - Language Bindings



More information about the Parted-commits mailing list