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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* Copyright (C) 2015 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. 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.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AtlasObject.h"
#include "AtlasObjectImpl.h"
#include <assert.h>
#include <sstream>
#include <wx/string.h>
#define ATSMARTPTR_IMPL(T) \
template<> void AtSmartPtr<T>::inc_ref() \
{ \
if (ptr) ++ptr->refcount; \
} \
\
template<> void AtSmartPtr<T>::dec_ref() \
{ \
if (ptr && --ptr->refcount == 0) \
delete ptr; \
} // (Don't set ptr=NULL, since it should never be possible for an
// unreferenced pointer to exist; I would rather see it die in debug
// mode if that ever happens, instead of just silently ignoring the error.)
ATSMARTPTR_IMPL(AtNode)
ATSMARTPTR_IMPL(const AtNode)
ATSMARTPTR_IMPL(AtIterImpl)
//////////////////////////////////////////////////////////////////////////
const AtIter AtIter::operator [] (const char* key) const
{
if (p)
return p->iter->second->getChild(key);
else
return AtIter();
}
AtIter::operator const wchar_t* () const
{
if (p)
return p->iter->second->value.c_str();
else
return L"";
}
//AtIter::operator const AtObj () const
const AtObj AtIter::operator * () const
{
if (p)
{
AtObj ret;
ret.p = p->iter->second;
return ret;
}
else
return AtObj();
}
AtIter& AtIter::operator ++ ()
{
assert(p);
// Increment the internal iterator, and stop if we've run out children
// to iterate over.
if (p && ++p->iter == p->iter_upperbound)
p = NULL;
return *this;
}
bool AtIter::defined() const
{
return (bool)p;
}
bool AtIter::hasContent() const
{
if (!p)
return false;
if (! p->iter->second)
return false;
return p->iter->second->hasContent();
}
size_t AtIter::count() const
{
if (!p)
return 0;
return std::distance(p->iter, p->iter_upperbound);
}
//////////////////////////////////////////////////////////////////////////
const AtIter AtObj::operator [] (const char* key) const
{
if (p)
return p->getChild(key);
else
// This object doesn't exist, so return another object that doesn't exist
return AtIter();
}
AtObj::operator const wchar_t* () const
{
if (p)
return p->value.c_str();
else
return L"";
}
double AtObj::getDouble() const
{
double val = 0;
if (p)
{
std::wstringstream s;
s << p->value;
s >> val;
}
return val;
}
int AtObj::getInt() const
{
int val = 0;
if (p)
{
std::wstringstream s;
s << p->value;
s >> val;
}
return val;
}
void AtObj::add(const char* key, AtObj& data)
{
if (!p)
p = new AtNode();
p = p->addChild(key, data.p);
}
void AtObj::add(const char* key, const wxString& value)
{
add(key, value.wc_str());
}
void AtObj::add(const char* key, const wchar_t* value)
{
const AtNode* o = new AtNode(value);
if (!p)
p = new AtNode();
p = p->addChild(key, AtNode::Ptr(o));
}
void AtObj::set(const char* key, AtObj& data)
{
if (!p)
p = new AtNode();
p = p->setChild(key, data.p);
}
void AtObj::set(const char* key, const wxString& value)
{
set(key, value.wc_str());
}
void AtObj::set(const char* key, const wchar_t* value)
{
const AtNode* o = new AtNode(value);
if (!p)
p = new AtNode();
p = p->setChild(key, AtNode::Ptr(o));
}
void AtObj::setBool(const char* key, bool value)
{
AtNode* o = new AtNode(value ? L"true" : L"false");
o->children.insert(AtNode::child_pairtype("@boolean", AtNode::Ptr(new AtNode())));
if (!p)
p = new AtNode();
p = p->setChild(key, AtNode::Ptr(o));
}
void AtObj::setDouble(const char* key, double value)
{
std::wstringstream str;
str << value;
AtNode* o = new AtNode(str.str().c_str());
o->children.insert(AtNode::child_pairtype("@number", AtNode::Ptr(new AtNode())));
if (!p)
p = new AtNode();
p = p->setChild(key, AtNode::Ptr(o));
}
void AtObj::setInt(const char* key, int value)
{
std::wstringstream str;
str << value;
AtNode* o = new AtNode(str.str().c_str());
o->children.insert(AtNode::child_pairtype("@number", AtNode::Ptr(new AtNode())));
if (!p)
p = new AtNode();
p = p->setChild(key, AtNode::Ptr(o));
}
void AtObj::setString(const wchar_t* value)
{
if (!p)
p = new AtNode();
p = p->setValue(value);
}
void AtObj::addOverlay(AtObj& data)
{
if (!p)
p = new AtNode();
p = p->addOverlay(data.p);
}
bool AtObj::hasContent() const
{
if (!p)
return false;
return p->hasContent();
}
//////////////////////////////////////////////////////////////////////////
const AtIter AtNode::getChild(const char* key) const
{
// Find the range of matching children
AtNode::child_maptype::const_iterator it = children.lower_bound(key);
AtNode::child_maptype::const_iterator it_upper = children.upper_bound(key);
if (it == it_upper) // No match found
return AtIter();
AtIter obj;
obj.p = new AtIterImpl(it, it_upper);
return obj;
}
bool AtNode::hasContent() const
{
if (value.length())
return true;
for (child_maptype::const_iterator it = children.begin(); it != children.end(); ++it)
if (it->second && it->second->hasContent())
return true;
return false;
}
const AtNode::Ptr AtNode::setValue(const wchar_t* value) const
{
AtNode* newNode = new AtNode();
newNode->children = children;
newNode->value = value;
return AtNode::Ptr(newNode);
}
const AtNode::Ptr AtNode::setChild(const char* key, const AtNode::Ptr &data) const
{
AtNode* newNode = new AtNode(this);
newNode->children.erase(key);
newNode->children.insert(AtNode::child_pairtype(key, data));
return AtNode::Ptr(newNode);
}
const AtNode::Ptr AtNode::addChild(const char* key, const AtNode::Ptr &data) const
{
AtNode* newNode = new AtNode(this);
newNode->children.insert(AtNode::child_pairtype(key, data));
return AtNode::Ptr(newNode);
}
const AtNode::Ptr AtNode::addOverlay(const AtNode::Ptr &data) const
{
AtNode* newNode = new AtNode(this);
// Delete old childs that are also in the overlay
for (AtNode::child_maptype::const_iterator it = data->children.begin(); it != data->children.end(); ++it)
newNode->children.erase(it->first);
// Add the overlay childs back in
for (AtNode::child_maptype::const_iterator it = data->children.begin(); it != data->children.end(); ++it)
newNode->children.insert(*it);
return AtNode::Ptr(newNode);
}
//////////////////////////////////////////////////////////////////////////
AtObj AtlasObject::TrimEmptyChildren(AtObj& obj)
{
AtObj ret;
for (AtNode::child_maptype::const_iterator it = obj.p->children.begin();
it != obj.p->children.end(); ++it)
{
if (it->second && it->second->hasContent())
{
AtObj node;
node.p = it->second;
ret.add(it->first.c_str(), node);
}
}
return ret;
}
|