summaryrefslogtreecommitdiffstats
path: root/src/base/PropertyName.cpp
blob: 03b007f59cfd692bd8ce91699af37941f4b506c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
    Rosegarden
    A sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include <iostream>
#include <string>

#include "PropertyName.h"
#include "Exception.h"


namespace Rosegarden 
{
using std::string;

PropertyName::intern_map *PropertyName::m_interns = 0;
PropertyName::intern_reverse_map *PropertyName::m_internsReversed = 0;
int PropertyName::m_nextValue = 0;

int PropertyName::intern(const string &s)
{
    if (!m_interns) {
        m_interns = new intern_map;
        m_internsReversed = new intern_reverse_map;
    }

    intern_map::iterator i(m_interns->find(s));
    
    if (i != m_interns->end()) {
        return i->second;
    } else {
        int nv = ++m_nextValue;
        m_interns->insert(intern_pair(s, nv));
        m_internsReversed->insert(intern_reverse_pair(nv, s));
        return nv;
    }
}

string PropertyName::getName() const
{
    intern_reverse_map::iterator i(m_internsReversed->find(m_value));
    if (i != m_internsReversed->end()) return i->second;

    // dump some informative data, even if we aren't in debug mode,
    // because this really shouldn't be happening
    std::cerr << "ERROR: PropertyName::getName: value corrupted!\n";
    std::cerr << "PropertyName's internal value is " << m_value << std::endl;
    std::cerr << "Reverse interns are ";
    i = m_internsReversed->begin();
    if (i == m_internsReversed->end()) std::cerr << "(none)";
    else while (i != m_internsReversed->end()) {
	if (i != m_internsReversed->begin()) {
	    std::cerr << ", ";
	}
	std::cerr << i->first << "=" << i->second;
	++i;
    }
    std::cerr << std::endl;

    throw Exception
	("Serious problem in PropertyName::getName(): property "
	 "name's internal value is corrupted -- see stderr for details");
}

const PropertyName PropertyName::EmptyPropertyName = "";

}