0de0d052c3d7a5e2d6e5c84697f8d553adba61df
[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 #include <string>
12
13 using namespace std;
14 using namespace PBD;
15
16 Glib::Mutex* ID::counter_lock = 0;
17 uint64_t ID::_counter = 0;
18
19 void
20 ID::init ()
21 {
22         counter_lock = new Glib::Mutex;
23 }
24
25 ID::ID ()
26 {
27         Glib::Mutex::Lock lm (*counter_lock);
28         id = _counter++;
29 }
30
31 ID::ID (string str)
32 {
33         string_assign (str);
34 }
35
36 int
37 ID::string_assign (string str)
38 {
39         return sscanf (str.c_str(), "%" PRIu64, &id) != 0;
40 }
41
42 void
43 ID::print (char* buf) const
44 {
45         /* XXX sizeof buf is unknown. bad API design */
46         snprintf (buf, 16, "%" PRIu64, id);
47 }
48
49 string ID::to_s() const
50 {
51     char buf[16]; // see print()
52     print(buf);
53     return string(buf);
54 }
55
56 ID&
57 ID::operator= (string str)
58 {
59         string_assign (str);
60         return *this;
61 }
62
63 ostream&
64 operator<< (ostream& ostr, const ID& id)
65 {
66         char buf[32];
67         id.print (buf);
68         ostr << buf;
69         return ostr;
70 }
71