Add PBD API to hard-link files
[ardour.git] / libs / pbd / id.cc
1 /*
2  * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
4  * Copyright (C) 2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <ostream>
23 #include <stdio.h>
24
25 #include "pbd/id.h"
26 #include "pbd/string_convert.h"
27
28 #include <string>
29
30 using namespace std;
31 using namespace PBD;
32
33 Glib::Threads::Mutex* ID::counter_lock = 0;
34 uint64_t ID::_counter = 0;
35
36 void
37 ID::init ()
38 {
39         if (!counter_lock) {
40                 counter_lock = new Glib::Threads::Mutex;
41         }
42 }
43
44 ID::ID ()
45 {
46         reset ();
47 }
48
49 ID::ID (const ID& other)
50 {
51         _id = other._id;
52 }
53
54 ID::ID (string str)
55 {
56         /* danger, will robinson: could result in non-unique ID */
57         string_assign (str);
58 }
59
60 ID::ID (uint64_t n)
61 {
62         /* danger, will robinson: could result in non-unique ID */
63         _id = n;
64 }
65
66 void
67 ID::reset ()
68 {
69         Glib::Threads::Mutex::Lock lm (*counter_lock);
70         _id = ++_counter;
71 }
72
73 bool
74 ID::string_assign (string str)
75 {
76         return string_to_uint64 (str, _id);
77 }
78
79 std::string
80 ID::to_s () const
81 {
82         return to_string (_id);
83 }
84
85 bool
86 ID::operator== (const string& str) const
87 {
88         return to_string (_id) == str;
89 }
90
91 ID&
92 ID::operator= (string str)
93 {
94         string_assign (str);
95         return *this;
96 }
97
98 ID&
99 ID::operator= (const ID& other)
100 {
101         if (&other != this) {
102                 _id = other._id;
103         }
104         return *this;
105 }
106
107 ostream&
108 operator<< (ostream& ostr, const ID& id)
109 {
110         ostr << id.to_s();
111         return ostr;
112 }
113