a551a5b5a451fbef5ee20af4a6a73d1a9a4bd414
[ardour.git] / libs / ardour / uri_map.cc
1 /*
2     Copyright (C) 2008-2010 Paul Davis
3     Author: David Robillard
4     
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <cassert>
22 #include <iostream>
23 #include <stdint.h>
24 #include "ardour/uri_map.h"
25
26 using namespace std;
27
28 namespace ARDOUR {
29
30
31 URIMap::URIMap()
32         : next_uri_id(1)
33 {
34         uri_map_feature_data.uri_to_id = &URIMap::uri_map_uri_to_id;
35         uri_map_feature_data.callback_data = this;
36         uri_map_feature.URI = LV2_URI_MAP_URI;
37         uri_map_feature.data = &uri_map_feature_data;
38 }
39
40
41 uint32_t
42 URIMap::uri_to_id(const char* map,
43                   const char* uri)
44 {
45         return uri_map_uri_to_id(this, map, uri);
46 }
47
48
49 uint32_t
50 URIMap::uri_map_uri_to_id(LV2_URI_Map_Callback_Data callback_data,
51                           const char*               /*map*/,
52                           const char*               uri)
53 {
54         // TODO: map ignored, < UINT16_MAX assumed
55
56         URIMap* me = (URIMap*)callback_data;
57         uint32_t ret = 0;
58
59         Map::iterator i = me->uri_map.find(uri);
60         if (i != me->uri_map.end()) {
61                 ret = i->second;
62         } else {
63                 ret = me->next_uri_id++;
64                 me->uri_map.insert(make_pair(string(uri), ret));
65         }
66
67         /*cout << "URI MAP (" << (map ? (void*)map : NULL)
68                 << "): " << uri << " -> " << ret << endl;*/
69
70         assert(ret <= UINT16_MAX);
71         return ret;
72 }
73
74
75 } // namespace ARDOUR
76