ultimatepp/uppsrc/Esc/EscMap.cpp
mdelfede d2b54f7989 changed svn layout
git-svn-id: svn://ultimatepp.org/upp/trunk@281 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2008-06-07 22:31:27 +00:00

73 lines
1.2 KiB
C++

#include "Esc.h"
NAMESPACE_UPP
#define LTIMING(x) // RTIMING(x)
VectorMap<EscValue, EscValue>& EscValue::CloneMap()
{
LTIMING("CloneMap");
ASSERT(IsMap());
if(AtomicRead(map->refcount) != 1) {
EscMap *c = new EscMap;
c->map <<= map->map;
map->Release();
map = c;
}
hash = 0;
return map->map;
}
const VectorMap<EscValue, EscValue>& EscValue::GetMap() const
{
ASSERT(IsMap());
return map->map;
}
void EscValue::SetEmptyMap()
{
Free();
type = ESC_MAP;
hash = 0;
map = new EscMap;
}
EscValue EscValue::MapGet(EscValue key) const
{
LTIMING("MapGet");
return GetMap().Get(key, EscValue());
}
void EscValue::MapSet(EscValue key, EscValue value)
{
LTIMING("MapSet");
if(IsVoid())
SetEmptyMap();
VectorMap<EscValue, EscValue>& m = CloneMap();
int q = m.Find(key);
if(q >= 0) {
if(value.IsVoid()) {
m.Unlink(q);
map->count--;
}
else
m[q] = value;
}
else
if(!value.IsVoid()) {
map->count++;
m.Add(key, value);
}
}
bool EscValue::HasNumberField(const char *id) const
{
return IsMap() && GetMap().Find(id) >= 0;
}
int EscValue::GetFieldInt(const char *id) const
{
return HasNumberField(id) ? MapGet(id).GetInt() : 0;
}
END_UPP_NAMESPACE