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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/**
* @file lluicolortable.cpp
* @brief brief LLUIColorTable class implementation file
*
* $LicenseInfo:firstyear=2009&license=viewergpl$
* Copyright (c) 2009, Linden Research, Inc.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include <queue>
#include "lluicolortable.h"
LLUIColorTable::ColorParams::ColorParams()
: value("value"),
reference("reference")
{
}
LLUIColorTable::ColorEntryParams::ColorEntryParams()
: name("name"),
color("")
{
}
LLUIColorTable::Params::Params()
: color_entries("color_entries")
{
}
void LLUIColorTable::init(const Params& p)
{
// this map will contain all color references after the following loop
typedef std::map<std::string, std::string> string_string_map_t;
string_string_map_t unresolved_refs;
mColors.clear();
for(LLInitParam::ParamIterator<ColorEntryParams>::const_iterator it = p.color_entries().begin();
it != p.color_entries().end();
++it)
{
ColorEntryParams color_entry = *it;
if(color_entry.color.value.isChosen())
{
mColors.insert(string_color_map_t::value_type(color_entry.name, color_entry.color.value));
}
else
{
unresolved_refs.insert(string_string_map_t::value_type(color_entry.name, color_entry.color.reference));
}
}
// maintain an in order queue of visited references for better debugging of cycles
typedef std::queue<std::string> string_queue_t;
string_queue_t ref_chain;
// maintain a map of the previously visited references in the reference chain for detecting cycles
typedef std::map<std::string, string_string_map_t::iterator> string_color_ref_iter_map_t;
string_color_ref_iter_map_t visited_refs;
// loop through the unresolved color references until there are none left
while(!unresolved_refs.empty())
{
// we haven't visited any references yet
visited_refs.clear();
string_string_map_t::iterator it = unresolved_refs.begin();
while(true)
{
if(it != unresolved_refs.end())
{
// locate the current reference in the previously visited references...
string_color_ref_iter_map_t::iterator visited = visited_refs.lower_bound(it->first);
if(visited != visited_refs.end()
&& !(visited_refs.key_comp()(it->first, visited->first)))
{
// ...if we find the current reference in the previously visited references
// we know that there is a cycle
std::string ending_ref = it->first;
std::string warning("The following colors form a cycle: ");
// warn about the references in the chain and remove them from
// the unresolved references map because they cannot be resolved
for(string_color_ref_iter_map_t::iterator iter = visited_refs.begin();
iter != visited_refs.end();
++iter)
{
if(!ref_chain.empty())
{
warning += ref_chain.front() + "->";
ref_chain.pop();
}
unresolved_refs.erase(iter->second);
}
llwarns << warning + ending_ref << llendl;
break;
}
else
{
// ...continue along the reference chain
ref_chain.push(it->first);
visited_refs.insert(visited, string_color_ref_iter_map_t::value_type(it->first, it));
}
}
else
{
// since this reference does not refer to another reference it must refer to an
// actual color, lets find it...
string_color_map_t::iterator color_value = mColors.find(it->second);
if(color_value != mColors.end())
{
// ...we found the color, and we now add every reference in the reference chain
// to the color map
for(string_color_ref_iter_map_t::iterator iter = visited_refs.begin();
iter != visited_refs.end();
++iter)
{
mColors.insert(string_color_map_t::value_type(iter->first, color_value->second));
unresolved_refs.erase(iter->second);
}
break;
}
else
{
// ... we did not find the color which imples that the current reference
// references a non-existant color
for(string_color_ref_iter_map_t::iterator iter = visited_refs.begin();
iter != visited_refs.end();
++iter)
{
llwarns << iter->first << " references a non-existent color" << llendl;
unresolved_refs.erase(iter->second);
}
break;
}
}
// find the next color reference in the reference chain
it = unresolved_refs.find(it->second);
}
}
}
const LLColor4& LLUIColorTable::getColor(const std::string& name) const
{
string_color_map_t::const_iterator iter = mColors.find(name);
return (iter != mColors.end() ? iter->second : LLColor4::magenta);
}
|