extend PBD::ID API to allow construction and operator== using uint64_t
authorPaul Davis <paul@linuxaudiosystems.com>
Fri, 5 May 2017 11:18:06 +0000 (12:18 +0100)
committerPaul Davis <paul@linuxaudiosystems.com>
Fri, 5 May 2017 17:56:25 +0000 (18:56 +0100)
This is theoretically dangerous, because a PBD::ID is supposed to be unique, and this new constructor
cannot guarantee that. However, the same danger already exists with the std::string-based constructor

libs/pbd/id.cc
libs/pbd/pbd/id.h

index d50782434931ea37fd38fbf2d9ffb05157c60f58..bc119b13dcb88e2db6583cdf37488bff410db9ef 100644 (file)
@@ -34,8 +34,9 @@ uint64_t ID::_counter = 0;
 void
 ID::init ()
 {
-       if (!counter_lock)
+       if (!counter_lock) {
                counter_lock = new Glib::Threads::Mutex;
+       }
 }
 
 ID::ID ()
@@ -50,14 +51,21 @@ ID::ID (const ID& other)
 
 ID::ID (string str)
 {
+       /* danger, will robinson: could result in non-unique ID */
        string_assign (str);
 }
 
+ID::ID (uint64_t n)
+{
+       /* danger, will robinson: could result in non-unique ID */
+       _id = n;
+}
+
 void
 ID::reset ()
 {
        Glib::Threads::Mutex::Lock lm (*counter_lock);
-       _id = _counter++;
+       _id = ++_counter;
 }
 
 bool
index 6c5fcb873e07b37cd30a003cfa96a56440aefb11..a597b6512b471397973225cb3132c41fbb3fb9c7 100644 (file)
@@ -35,6 +35,7 @@ class LIBPBD_API ID {
        ID ();
        ID (std::string);
        ID (const ID&);
+       ID (uint64_t);
 
        void reset ();
 
@@ -47,6 +48,9 @@ class LIBPBD_API ID {
        }
 
        bool operator== (const std::string&) const;
+       bool operator== (uint64_t n) const {
+               return _id == n;
+       }
 
        ID& operator= (std::string);
        ID& operator= (const ID&);