Merged with trunk R1719.
[ardour.git] / libs / pbd / id.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <ostream>
21 #include <iostream>
22 #include <stdio.h>
23
24 #ifndef __STDC_FORMAT_MACROS
25 #define __STDC_FORMAT_MACROS
26 #endif
27 #include <inttypes.h>
28
29 #include <pbd/id.h>
30 #include <string>
31
32 using namespace std;
33 using namespace PBD;
34
35 Glib::Mutex* ID::counter_lock = 0;
36 uint64_t ID::_counter = 0;
37
38 void
39 ID::init ()
40 {
41         counter_lock = new Glib::Mutex;
42 }
43
44 ID::ID ()
45 {
46         Glib::Mutex::Lock lm (*counter_lock);
47         _id = _counter++;
48 }
49
50 ID::ID (string str)
51 {
52         string_assign (str);
53 }
54
55 int
56 ID::string_assign (string str)
57 {
58         return sscanf (str.c_str(), "%" PRIu64, &_id) != 0;
59 }
60
61 void
62 ID::print (char* buf, uint32_t bufsize) const
63 {
64         snprintf (buf, bufsize, "%" PRIu64, _id);
65 }
66
67 string ID::to_s() const
68 {
69     char buf[32]; // see print()
70     print(buf, sizeof (buf));
71     return string(buf);
72 }
73
74 ID&
75 ID::operator= (string str)
76 {
77         string_assign (str);
78         return *this;
79 }
80
81 ostream&
82 operator<< (ostream& ostr, const ID& _id)
83 {
84         char buf[32];
85         _id.print (buf, sizeof (buf));
86         ostr << buf;
87         return ostr;
88 }
89