Atlas 0.7.0
Networking protocol for the Worldforge system.
BaseObject.cpp
1// This file may be redistributed and modified only under the terms of
2// the GNU Lesser General Public License (See COPYING for details).
3// Copyright (C) 2000-2001 Stefanus Du Toit and Aloril
4
5// $Id$
6
7#include <Atlas/Objects/BaseObject.h>
8
10using Atlas::Message::MapType;
11
12namespace Atlas { namespace Objects {
13
15 m_class_no(BASE_OBJECT_NO),
16 m_refCount(0),
17 m_defaults(defaults),
18 m_attrFlags(0)
19{
20 if(defaults == nullptr) {
21 m_attrFlags = -1; //this is default object: all attributes here
22 }
23}
24
25BaseObjectData::~BaseObjectData()
26{
27 assert( m_refCount==0 );
28}
29
30bool BaseObjectData::instanceOf(int classNo) const
31{
32 return BASE_OBJECT_NO == classNo;
33}
34
35bool BaseObjectData::hasAttr(const std::string& name) const
36{
37 uint32_t flag;
38
39 //Use std::optional when we're using C++17
40 if (getAttrFlag(name, flag)) {
41 return m_attrFlags & flag;
42 } else {
43 return (m_attributes.find(name) != m_attributes.end());
44 }
45}
46
47bool BaseObjectData::hasAttrFlag(uint32_t flag) const
48{
49 return m_attrFlags & flag;
50}
51
52Element BaseObjectData::getAttr(const std::string& name) const
53
54{
55 Element attr;
56 if (copyAttr(name, attr) != 0) {
57 throw NoSuchAttrException(name);
58 }
59 return attr;
60}
61
62int BaseObjectData::copyAttr(const std::string& name, Element & attr) const
63{
64 auto I = m_attributes.find(name);
65 if (I == m_attributes.end()) {
66 return -1;
67 }
68 attr = I->second;
69 return 0;
70}
71
72void BaseObjectData::setAttr(std::string name, Element attr, const Atlas::Objects::Factories* factories)
73{
74 m_attributes[std::move(name)] = std::move(attr);
75}
76
77void BaseObjectData::removeAttr(const std::string& name)
78{
79 uint32_t flag;
80 if (getAttrFlag(name, flag)) {
81 removeAttrFlag(flag);
82 } else {
83 m_attributes.erase(name);
84 }
85}
86
88{
89 m_attrFlags &= ~flag;
90}
91
93{
94 MapType m;
95 addToMessage(m);
96 return m;
97}
98
99void BaseObjectData::addToMessage(MapType & m) const
100{
101 for (const auto & attribute : m_attributes) {
102 m[attribute.first] = attribute.second;
103 }
104}
105
107{
108 Message::Encoder e(b);
109 for (const auto & attribute : m_attributes) {
110 e.mapElementItem(attribute.first, attribute.second);
111 }
112}
113
114int BaseObjectData::getAttrClass(const std::string& name) const
115{
116 return -1;
117}
118
119bool BaseObjectData::getAttrFlag(const std::string& name, uint32_t& flag) const
120{
121 return false;
122}
123
124} } // namespace Atlas::Objects
virtual void addToMessage(Atlas::Message::MapType &) const
Write this object to an existing Element.
Definition: BaseObject.cpp:99
virtual void removeAttr(const std::string &name)
Remove the attribute "name".
Definition: BaseObject.cpp:77
virtual int getAttrClass(const std::string &name) const
Find the class which contains the attribute "name".
Definition: BaseObject.cpp:114
Atlas::Message::MapType asMessage() const
Definition: BaseObject.cpp:92
bool hasAttr(const std::string &name) const
Check whether the attribute "name" exists.
Definition: BaseObject.cpp:35
virtual void removeAttrFlag(uint32_t flag)
Remove the attribute "name".
Definition: BaseObject.cpp:87
virtual bool getAttrFlag(const std::string &name, uint32_t &flag) const
Find the flag for the attribute "name".
Definition: BaseObject.cpp:119
Atlas::Message::Element getAttr(const std::string &name) const
Definition: BaseObject.cpp:52
virtual int copyAttr(const std::string &name, Atlas::Message::Element &attr) const
Definition: BaseObject.cpp:62
virtual void setAttr(std::string name, Atlas::Message::Element attr, const Atlas::Objects::Factories *factories=nullptr)
Set the attribute "name" to the value given by "attr".
Definition: BaseObject.cpp:72
virtual void sendContents(Atlas::Bridge &b) const
Send the contents of this object to a Bridge.
Definition: BaseObject.cpp:106
BaseObjectData(BaseObjectData *defaults)
Definition: BaseObject.cpp:14
bool hasAttrFlag(uint32_t flag) const
Check whether the attribute "name" exists.
Definition: BaseObject.cpp:47
virtual bool instanceOf(int classNo) const
Is this instance of some class?
Definition: BaseObject.cpp:30
Definition: Bridge.h:20