[beast: 55/73] BSE: SF2: BseStorage::Blob: replace C style API with C++ style API



commit e0e3c61c855ad03da6838f977f303479c05ccd0d
Author: Stefan Westerfeld <stefan space twc de>
Date:   Thu Dec 8 18:40:43 2016 +0100

    BSE: SF2: BseStorage::Blob: replace C style API with C++ style API
    
    Signed-off-by: Stefan Westerfeld <stefan space twc de>

 bse/bsesoundfont.cc     |   12 +++++-----
 bse/bsesoundfontrepo.cc |    2 +-
 bse/bsestorage.cc       |   54 ++++++++++------------------------------------
 bse/bsestorage.hh       |   19 ++++++++-------
 4 files changed, 29 insertions(+), 58 deletions(-)
---
diff --git a/bse/bsesoundfont.cc b/bse/bsesoundfont.cc
index 2926173..2442687 100644
--- a/bse/bsesoundfont.cc
+++ b/bse/bsesoundfont.cc
@@ -60,7 +60,7 @@ bse_sound_font_get_property (GObject    *object,
     {
     case PARAM_FILE_NAME:
       if (sound_font_impl->blob)
-        sfi_value_set_string (value, bse_storage_blob_file_name (sound_font_impl->blob));
+        sfi_value_set_string (value, sound_font_impl->blob->file_name().c_str());
       else
         sfi_value_set_string (value, NULL);
       break;
@@ -116,7 +116,7 @@ bse_sound_font_load_blob (BseSoundFont       *self,
 
   std::lock_guard<Bse::Mutex> guard (bse_sound_font_repo_mutex (self->sfrepo));
   fluid_synth_t *fluid_synth = bse_sound_font_repo_fluid_synth (self->sfrepo);
-  int sfont_id = fluid_synth_sfload (fluid_synth, bse_storage_blob_file_name (blob), 0);
+  int sfont_id = fluid_synth_sfload (fluid_synth, blob->file_name().c_str(), 0);
   Bse::Error error;
   if (sfont_id != -1)
     {
@@ -182,10 +182,10 @@ bse_sound_font_store_private (BseObject  *object,
   /* chain parent class' handler */
   BSE_OBJECT_CLASS (parent_class)->store_private (object, storage);
 
-  if (!BSE_STORAGE_SELF_CONTAINED (storage) && !bse_storage_blob_is_temp_file (sound_font_impl->blob))
+  if (!BSE_STORAGE_SELF_CONTAINED (storage) && !sound_font_impl->blob->is_temp_file())
     {
       bse_storage_break (storage);
-      bse_storage_printf (storage, "(load-sound-font \"%s\")", bse_storage_blob_file_name 
(sound_font_impl->blob));
+      bse_storage_printf (storage, "(load-sound-font \"%s\")", sound_font_impl->blob->file_name().c_str());
     }
   else
     {
@@ -220,7 +220,7 @@ bse_sound_font_restore_private (BseObject  *object,
       if (g_scanner_peek_next_token (scanner) == G_TOKEN_STRING)
        {
          parse_or_return (scanner, G_TOKEN_STRING);
-         blob = bse_storage_blob_new_from_file (scanner->value.v_string, FALSE);
+         blob = std::make_shared<BseStorage::Blob> (scanner->value.v_string, false);
        }
       else
        {
@@ -236,7 +236,7 @@ bse_sound_font_restore_private (BseObject  *object,
       error = bse_sound_font_load_blob (sound_font, blob, FALSE);
       if (error != 0)
        bse_storage_warn (storage, "failed to load sound font \"%s\": %s",
-                                   bse_storage_blob_file_name (blob), bse_error_blurb (error));
+                                   blob->file_name().c_str(), bse_error_blurb (error));
       expected_token = G_TOKEN_NONE; /* got ')' */
     }
   else /* chain parent class' handler */
diff --git a/bse/bsesoundfontrepo.cc b/bse/bsesoundfontrepo.cc
index 2a204e8..b54c260 100644
--- a/bse/bsesoundfontrepo.cc
+++ b/bse/bsesoundfontrepo.cc
@@ -374,7 +374,7 @@ repo_load_file (BseSoundFontRepo *sfrepo, const String &file_name, BseSoundFont
   BseSoundFont *sound_font = (BseSoundFont*) bse_object_new (BSE_TYPE_SOUND_FONT, "uname", fname.c_str(), 
NULL);
   bse_container_add_item (BSE_CONTAINER (sfrepo), BSE_ITEM (sound_font));
 
-  BseStorage::BlobP blob = bse_storage_blob_new_from_file (file_name.c_str(), FALSE);
+  BseStorage::BlobP blob = std::make_shared<BseStorage::Blob> (file_name, false);
   Error error = bse_sound_font_load_blob (sound_font, blob, TRUE);
 
   if (error == Bse::Error::NONE)
diff --git a/bse/bsestorage.cc b/bse/bsestorage.cc
index b3ec778..a3110d6 100644
--- a/bse/bsestorage.cc
+++ b/bse/bsestorage.cc
@@ -268,7 +268,7 @@ bse_storage_add_blob (BseStorage       *self,
                       BseStorage::BlobP blob)
 {
   self->data.blobs.push_back (blob);
-  return self->data.blobs.back()->id;
+  return self->data.blobs.back()->id();
 }
 
 static BseStorageDBlock*
@@ -1800,28 +1800,21 @@ bse_storage_parse_data_handle_rest (BseStorage     *self,
 
 // == blobs ==
 
-const gchar *
-bse_storage_blob_file_name (BseStorage::BlobP blob)
+BseStorage::Blob::Blob (const std::string& file_name, bool is_temp_file) :
+  file_name_ (file_name),
+  is_temp_file_ (is_temp_file)
 {
-  g_return_val_if_fail (blob != NULL, NULL);
-
-  blob->mutex.lock();
-  const gchar *file_name = blob->file_name;
-  blob->mutex.unlock();
-
-  return file_name;
+  id_ = bse_id_alloc();
 }
 
 BseStorage::Blob::~Blob()
 {
-  if (is_temp_file)
+  if (is_temp_file_)
     {
-      unlink (file_name);
+      unlink (file_name_.c_str());
       /* FIXME: check error code and do what? */
     }
-  g_free (file_name);
-  file_name = NULL;
-  bse_id_free (id);
+  bse_id_free (id_);
 }
 
 /* search in /tmp for files called "bse-<user>-<pid>*"
@@ -1856,29 +1849,6 @@ bse_storage_blob_clean_files()
     }
 }
 
-BseStorage::BlobP
-bse_storage_blob_new_from_file (const char *file_name,
-                                gboolean    is_temp_file)
-{
-  BseStorage::BlobP blob = std::make_shared<BseStorage::Blob>(); // FIXME: constructor
-  blob->file_name = g_strdup (file_name);
-  blob->is_temp_file = is_temp_file;
-  blob->id = bse_id_alloc();
-  return blob;
-}
-
-gboolean
-bse_storage_blob_is_temp_file (BseStorage::BlobP blob)
-{
-  g_return_val_if_fail (blob != NULL, FALSE);
-
-  blob->mutex.lock();
-  gboolean is_temp_file = blob->is_temp_file;
-  blob->mutex.unlock();
-
-  return is_temp_file;
-}
-
 struct WStoreBlob
 {
   BseStorage::BlobP blob;
@@ -1906,11 +1876,11 @@ wstore_blob_reader (gpointer data,
   if (wsb->fd == -1)
     {
       do
-       wsb->fd = open (bse_storage_blob_file_name (wsb->blob), O_RDONLY);
+       wsb->fd = open (wsb->blob->file_name().c_str(), O_RDONLY);
       while (wsb->fd == -1 && errno == EINTR);
       if (wsb->fd == -1)
        {
-         bse_storage_error (wsb->storage, "file %s could not be opened: %s", bse_storage_blob_file_name 
(wsb->blob), strerror (errno));
+         bse_storage_error (wsb->storage, "file %s could not be opened: %s", wsb->blob->file_name().c_str(), 
strerror (errno));
          return -errno;
        }
     }
@@ -2032,7 +2002,7 @@ bse_storage_parse_blob (BseStorage             *self,
        }
       close (bse_fd);
       close (tmp_fd);
-      blob_out = bse_storage_blob_new_from_file (file_name, TRUE);
+      blob_out = std::make_shared<BseStorage::Blob> (file_name, true);
       g_free (file_name);
     }
   else if (g_quark_try_string (scanner->value.v_identifier) == quark_blob_id)
@@ -2043,7 +2013,7 @@ bse_storage_parse_blob (BseStorage             *self,
       blob_out = NULL;
       for (auto blob : self->data.blobs)
        {
-         if (blob->id == id)
+         if (blob->id() == id)
            blob_out = blob;
 ;
        }
diff --git a/bse/bsestorage.hh b/bse/bsestorage.hh
index db7ace5..05efde9 100644
--- a/bse/bsestorage.hh
+++ b/bse/bsestorage.hh
@@ -65,12 +65,17 @@ struct BseStorage : BseObject {
   guint                  n_channels;
 
   /* storage blob */
-  struct Blob {
-    Bse::Mutex  mutex;
-    char       *file_name;
-    gboolean    is_temp_file;
-    gulong      id;
+  class Blob {
+    std::string file_name_;
+    bool        is_temp_file_;
+    gulong      id_;
 
+  public:
+    bool        is_temp_file() const  { return is_temp_file_; }
+    std::string file_name() const     { return file_name_; }
+    gulong      id() const            { return id_; }
+
+    Blob (const std::string& file_name, bool is_temp_file);
     ~Blob();
   };
 
@@ -182,10 +187,6 @@ gboolean     bse_storage_check_parse_negate     (BseStorage             *self);
 
 /* --- bse storage blob --- */
 
-const gchar      *bse_storage_blob_file_name         (BseStorage::BlobP      self);
-gboolean          bse_storage_blob_is_temp_file      (BseStorage::BlobP      self);
-BseStorage::BlobP bse_storage_blob_new_from_file     (const gchar           *file_name,
-                                                     gboolean                is_temp_file);
 void              bse_storage_blob_clean_files       (void);
 
 /* --- short-hands --- */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]