clientobject: add null checks
[waspsaliva.git] / src / staticobject.h
blob6fb486193c9790fc2727c7283d959e808fe0107b
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 #include "irrlichttypes_bloated.h"
23 #include <string>
24 #include <sstream>
25 #include <vector>
26 #include <map>
27 #include "debug.h"
29 class ServerActiveObject;
31 struct StaticObject
33 u8 type = 0;
34 v3f pos;
35 std::string data;
37 StaticObject() = default;
38 StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
40 void serialize(std::ostream &os);
41 void deSerialize(std::istream &is, u8 version);
44 class StaticObjectList
46 public:
48 Inserts an object to the container.
49 Id must be unique (active) or 0 (stored).
51 void insert(u16 id, const StaticObject &obj)
53 if(id == 0)
55 m_stored.push_back(obj);
57 else
59 if(m_active.find(id) != m_active.end())
61 dstream<<"ERROR: StaticObjectList::insert(): "
62 <<"id already exists"<<std::endl;
63 FATAL_ERROR("StaticObjectList::insert()");
65 m_active[id] = obj;
69 void remove(u16 id)
71 assert(id != 0); // Pre-condition
72 if(m_active.find(id) == m_active.end())
74 warningstream<<"StaticObjectList::remove(): id="<<id
75 <<" not found"<<std::endl;
76 return;
78 m_active.erase(id);
81 void serialize(std::ostream &os);
82 void deSerialize(std::istream &is);
85 NOTE: When an object is transformed to active, it is removed
86 from m_stored and inserted to m_active.
87 The caller directly manipulates these containers.
89 std::vector<StaticObject> m_stored;
90 std::map<u16, StaticObject> m_active;
92 private: