Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "AtlasObject.h" 19 : #include "AtlasObjectImpl.h" 20 : 21 : #include <cassert> 22 : #include <cstddef> 23 : #include <iterator> 24 : #include <sstream> 25 : 26 : #define ATSMARTPTR_IMPL(T) \ 27 : template<> void AtSmartPtr<T>::inc_ref() \ 28 : { \ 29 : if (ptr) ++ptr->m_Refcount; \ 30 : } \ 31 : \ 32 : template<> void AtSmartPtr<T>::dec_ref() \ 33 : { \ 34 : if (ptr && --ptr->m_Refcount == 0) \ 35 : delete ptr; \ 36 : } // (Don't set ptr=NULL, since it should never be possible for an 37 : // unreferenced pointer to exist; I would rather see it die in debug 38 : // mode if that ever happens, instead of just silently ignoring the error.) 39 : 40 : 41 42 : ATSMARTPTR_IMPL(AtNode) 42 402 : ATSMARTPTR_IMPL(const AtNode) 43 0 : ATSMARTPTR_IMPL(AtIterImpl) 44 : 45 : 46 : ////////////////////////////////////////////////////////////////////////// 47 : 48 0 : const AtIter AtIter::operator [] (const char* key) const 49 : { 50 0 : if (m_Impl) 51 0 : return m_Impl->iter->second->getChild(key); 52 : else 53 0 : return AtIter(); 54 : } 55 : 56 0 : AtIter::operator const char* () const 57 : { 58 0 : return m_Impl ? m_Impl->iter->second->m_Value.c_str() : ""; 59 : } 60 : 61 : //AtIter::operator const AtObj () const 62 0 : const AtObj AtIter::operator * () const 63 : { 64 0 : if (m_Impl) 65 : { 66 0 : AtObj ret; 67 0 : ret.m_Node = m_Impl->iter->second; 68 0 : return ret; 69 0 : } 70 : else 71 0 : return AtObj(); 72 : } 73 : 74 : 75 0 : AtIter& AtIter::operator ++ () 76 : { 77 0 : assert(m_Impl); 78 : 79 : // Increment the internal iterator, and stop if we've run out children 80 : // to iterate over. 81 0 : if (m_Impl && ++m_Impl->iter == m_Impl->iter_upperbound) 82 0 : m_Impl = nullptr; 83 : 84 0 : return *this; 85 : } 86 : 87 0 : bool AtIter::defined() const 88 : { 89 0 : return static_cast<bool>(m_Impl); 90 : } 91 : 92 0 : bool AtIter::hasContent() const 93 : { 94 0 : if (!m_Impl) 95 : return false; 96 : 97 0 : if (!m_Impl->iter->second) 98 : return false; 99 : 100 0 : return m_Impl->iter->second->hasContent(); 101 : } 102 : 103 0 : size_t AtIter::count() const 104 : { 105 0 : if (!m_Impl) 106 : return 0; 107 : 108 0 : return std::distance(m_Impl->iter, m_Impl->iter_upperbound); 109 : } 110 : 111 : ////////////////////////////////////////////////////////////////////////// 112 : 113 0 : const AtIter AtObj::operator [] (const char* key) const 114 : { 115 0 : if (m_Node) 116 0 : return m_Node->getChild(key); 117 : else 118 : // This object doesn't exist, so return another object that doesn't exist 119 0 : return AtIter(); 120 : } 121 : 122 0 : AtObj::operator const char* () const 123 : { 124 0 : return m_Node ? m_Node->m_Value.c_str() : ""; 125 : } 126 : 127 0 : double AtObj::getDouble() const 128 : { 129 0 : double val = 0; 130 0 : if (m_Node) 131 0 : static_cast<std::stringstream>(m_Node->m_Value) >> val; 132 0 : return val; 133 : } 134 : 135 0 : int AtObj::getInt() const 136 : { 137 0 : int val = 0; 138 0 : if (m_Node) 139 0 : static_cast<std::stringstream>(m_Node->m_Value) >> val; 140 0 : return val; 141 : } 142 : 143 0 : long AtObj::getLong() const 144 : { 145 0 : long val = 0; 146 0 : if (m_Node) 147 0 : static_cast<std::stringstream>(m_Node->m_Value) >> val; 148 0 : return val; 149 : } 150 : 151 0 : void AtObj::add(const char* key, AtObj& data) 152 : { 153 0 : if (!m_Node) 154 0 : m_Node = new AtNode(); 155 : 156 0 : m_Node = m_Node->addChild(key, data.m_Node); 157 0 : } 158 : 159 0 : void AtObj::add(const char* key, const char* value) 160 : { 161 0 : const AtNode* o = new AtNode(value); 162 : 163 0 : if (!m_Node) 164 0 : m_Node = new AtNode(); 165 : 166 0 : m_Node = m_Node->addChild(key, AtNode::Ptr(o)); 167 0 : } 168 : 169 13 : void AtObj::set(const char* key, AtObj& data) 170 : { 171 13 : if (!m_Node) 172 13 : m_Node = new AtNode(); 173 : 174 26 : m_Node = m_Node->setChild(key, data.m_Node); 175 13 : } 176 : 177 0 : void AtObj::set(const char* key, const char* value) 178 : { 179 0 : const AtNode* o = new AtNode(value); 180 : 181 0 : if (!m_Node) 182 0 : m_Node = new AtNode(); 183 : 184 0 : m_Node = m_Node->setChild(key, AtNode::Ptr(o)); 185 0 : } 186 : 187 0 : void AtObj::unset(const char* key) 188 : { 189 0 : if (!m_Node) 190 0 : m_Node = new AtNode(); 191 : 192 0 : m_Node = m_Node->unsetChild(key); 193 0 : } 194 : 195 0 : void AtObj::setBool(const char* key, bool value) 196 : { 197 0 : AtNode* o = new AtNode(value ? "true" : "false"); 198 0 : o->m_Children.insert(AtNode::child_pairtype("@boolean", AtNode::Ptr(new AtNode()))); 199 : 200 0 : if (!m_Node) 201 0 : m_Node = new AtNode(); 202 : 203 0 : m_Node = m_Node->setChild(key, AtNode::Ptr(o)); 204 0 : } 205 : 206 0 : void AtObj::setDouble(const char* key, double value) 207 : { 208 0 : std::stringstream str; 209 0 : str << value; 210 0 : AtNode* o = new AtNode(str.str().c_str()); 211 0 : o->m_Children.insert(AtNode::child_pairtype("@number", AtNode::Ptr(new AtNode()))); 212 : 213 0 : if (!m_Node) 214 0 : m_Node = new AtNode(); 215 : 216 0 : m_Node = m_Node->setChild(key, AtNode::Ptr(o)); 217 0 : } 218 : 219 0 : void AtObj::setInt(const char* key, int value) 220 : { 221 0 : std::stringstream str; 222 0 : str << value; 223 0 : AtNode* o = new AtNode(str.str().c_str()); 224 0 : o->m_Children.insert(AtNode::child_pairtype("@number", AtNode::Ptr(new AtNode()))); 225 : 226 0 : if (!m_Node) 227 0 : m_Node = new AtNode(); 228 : 229 0 : m_Node = m_Node->setChild(key, AtNode::Ptr(o)); 230 0 : } 231 : 232 0 : void AtObj::setString(const char* value) 233 : { 234 0 : if (!m_Node) 235 0 : m_Node = new AtNode(); 236 : 237 0 : m_Node = m_Node->setValue(value); 238 0 : } 239 : 240 0 : void AtObj::addOverlay(AtObj& data) 241 : { 242 0 : if (!m_Node) 243 0 : m_Node = new AtNode(); 244 : 245 0 : m_Node = m_Node->addOverlay(data.m_Node); 246 0 : } 247 : 248 0 : bool AtObj::hasContent() const 249 : { 250 0 : if (!m_Node) 251 : return false; 252 : 253 0 : return m_Node->hasContent(); 254 : } 255 : 256 : ////////////////////////////////////////////////////////////////////////// 257 : 258 0 : const AtIter AtNode::getChild(const char* key) const 259 : { 260 : // Find the range of matching children 261 0 : AtNode::child_maptype::const_iterator it = m_Children.lower_bound(key); 262 0 : AtNode::child_maptype::const_iterator it_upper = m_Children.upper_bound(key); 263 : 264 0 : if (it == it_upper) // No match found 265 0 : return AtIter(); 266 : 267 0 : AtIter obj; 268 0 : obj.m_Impl = new AtIterImpl(it, it_upper); 269 0 : return obj; 270 0 : } 271 : 272 0 : bool AtNode::hasContent() const 273 : { 274 0 : if (m_Value.length()) 275 : return true; 276 : 277 0 : for (const child_maptype::value_type& child : m_Children) 278 0 : if (child.second && child.second->hasContent()) 279 0 : return true; 280 : 281 : return false; 282 : } 283 : 284 0 : const AtNode::Ptr AtNode::setValue(const char* value) const 285 : { 286 0 : AtNode* newNode = new AtNode(); 287 0 : newNode->m_Children = m_Children; 288 0 : newNode->m_Value = value; 289 0 : return AtNode::Ptr(newNode); 290 : } 291 : 292 13 : const AtNode::Ptr AtNode::setChild(const char* key, const AtNode::Ptr &data) const 293 : { 294 13 : AtNode* newNode = new AtNode(this); 295 26 : newNode->m_Children.erase(key); 296 26 : newNode->m_Children.insert(AtNode::child_pairtype(key, data)); 297 13 : return AtNode::Ptr(newNode); 298 : } 299 : 300 0 : const AtNode::Ptr AtNode::unsetChild(const char* key) const 301 : { 302 0 : AtNode* newNode = new AtNode(this); 303 0 : newNode->m_Children.erase(key); 304 0 : return AtNode::Ptr(newNode); 305 : } 306 : 307 0 : const AtNode::Ptr AtNode::addChild(const char* key, const AtNode::Ptr &data) const 308 : { 309 0 : AtNode* newNode = new AtNode(this); 310 0 : newNode->m_Children.insert(AtNode::child_pairtype(key, data)); 311 0 : return AtNode::Ptr(newNode); 312 : } 313 : 314 0 : const AtNode::Ptr AtNode::addOverlay(const AtNode::Ptr &data) const 315 : { 316 0 : AtNode* newNode = new AtNode(this); 317 : 318 : // Delete old childs that are also in the overlay 319 0 : for (AtNode::child_maptype::const_iterator it = data->m_Children.begin(); it != data->m_Children.end(); ++it) 320 0 : newNode->m_Children.erase(it->first); 321 : 322 : // Add the overlay childs back in 323 0 : for (AtNode::child_maptype::const_iterator it = data->m_Children.begin(); it != data->m_Children.end(); ++it) 324 0 : newNode->m_Children.insert(*it); 325 : 326 0 : return AtNode::Ptr(newNode); 327 : } 328 : ////////////////////////////////////////////////////////////////////////// 329 : 330 0 : AtObj AtlasObject::TrimEmptyChildren(AtObj& obj) 331 : { 332 0 : AtObj ret; 333 : 334 0 : for (const AtNode::child_maptype::value_type& child : obj.m_Node->m_Children) 335 : { 336 0 : if (child.second && child.second->hasContent()) 337 : { 338 0 : AtObj node; 339 0 : node.m_Node = child.second; 340 0 : ret.add(child.first.c_str(), node); 341 0 : } 342 : } 343 : 344 0 : return ret; 345 0 : }