f9afa72c985a359f667e985c5666544759c5c64e
[ardour.git] / libs / pbd / id.cc
1 #include <ostream>
2 #include <iostream>
3 #include <stdio.h>
4
5 #ifndef __STDC_FORMAT_MACROS
6 #define __STDC_FORMAT_MACROS
7 #endif
8 #include <inttypes.h>
9
10 #include <pbd/id.h>
11
12 using namespace std;
13 using namespace PBD;
14
15 Glib::Mutex* ID::counter_lock = 0;
16 uint64_t ID::_counter = 0;
17
18 void
19 ID::init ()
20 {
21         counter_lock = new Glib::Mutex;
22 }
23
24 ID::ID ()
25 {
26         Glib::Mutex::Lock lm (*counter_lock);
27         id = _counter++;
28 }
29
30 ID::ID (string str)
31 {
32         string_assign (str);
33 }
34
35 int
36 ID::string_assign (string str)
37 {
38         return sscanf (str.c_str(), "%" PRIu64, &id) != 0;
39 }
40
41 void
42 ID::print (char* buf) const
43 {
44         /* XXX sizeof buf is unknown. bad API design */
45         snprintf (buf, 16, "%" PRIu64, id);
46 }
47
48 ID&
49 ID::operator= (string str)
50 {
51         string_assign (str);
52         return *this;
53 }
54
55 ostream&
56 operator<< (ostream& ostr, const ID& id)
57 {
58         char buf[32];
59         id.print (buf);
60         ostr << buf;
61         return ostr;
62 }
63