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 Alistair Riddoch
4
5 #include <Atlas/Message/Encoder.h>
6 #include <Atlas/Objects/Root.h>
7
8 using Atlas::Message::Element;
9
10 namespace Atlas { namespace Objects {
11
Root()12 Root::Root() : attr_parents(1, std::string("root")), attr_objtype("instance")
13 {
14 // SetParents(Element::ListType(1,std::string("root")));
15 // SetId(id);
16 // SetObjtype("instance");
17 }
18
Root(const char * id)19 Root::Root(const char * id) : attr_id(id), attr_objtype("meta")
20 {
21 // SetId("root");
22 // SetObjtype("meta");
23 }
24
Root(const char * id,const char * parent)25 Root::Root(const char * id, const char * parent) : attr_parents(1,parent),
26 attr_id(id)
27 {
28 // SetId(id);
29 // SetParents(Element::ListType(1,parent));
30 }
31
~Root()32 Root::~Root()
33 {
34 }
35
Class()36 Root Root::Class()
37 {
38 Root root("root");
39 return root;
40 }
41
hasAttr(const std::string & name) const42 bool Root::hasAttr(const std::string& name) const
43 {
44 if (name == "parents") return true;
45 if (name == "id") return true;
46 if (name == "objtype") return true;
47 if (name == "name") return true;
48 return (attributes.find(name) != attributes.end());
49 }
50
getAttr(const std::string & name) const51 Element Root::getAttr(const std::string& name) const
52 throw (NoSuchAttrException)
53 {
54 if (name == "parents") return attr_parents;
55 if (name == "id") return attr_id;
56 if (name == "objtype") return attr_objtype;
57 if (name == "name") return attr_name;
58 Element::MapType::const_iterator I = attributes.find(name);
59 if (I == attributes.end())
60 throw NoSuchAttrException(name);
61 return ((*I).second);
62 }
63
setAttr(const std::string & name,const Element & attr)64 void Root::setAttr(const std::string& name, const Element& attr)
65 {
66 if (name == "parents") { setParents(attr.asList()); return; }
67 if (name == "id") { setId(attr.asString()); return; }
68 if (name == "objtype") { setObjtype(attr.asString()); return; }
69 if (name == "name") { setName(attr.asString()); return; }
70 attributes[name] = attr;
71 }
72
removeAttr(const std::string & name)73 void Root::removeAttr(const std::string& name)
74 {
75 if (name == "parents") return;
76 if (name == "id") return;
77 if (name == "objtype") return;
78 if (name == "name") return;
79 attributes.erase(name);
80 }
81
asObject() const82 Element Root::asObject() const
83 {
84 Element::MapType allattrs = attributes;
85 allattrs["parents"] = attr_parents;
86 allattrs["id"] = attr_id;
87 allattrs["objtype"] = attr_objtype;
88 allattrs["name"] = attr_name;
89 return Element(allattrs);
90 }
91
asMap() const92 Element::MapType Root::asMap() const
93 {
94 Element::MapType allattrs = attributes;
95 allattrs["parents"] = attr_parents;
96 allattrs["id"] = attr_id;
97 allattrs["objtype"] = attr_objtype;
98 allattrs["name"] = attr_name;
99 return allattrs;
100 }
101
sendParents(Atlas::Bridge * b) const102 void Root::sendParents(Atlas::Bridge* b) const
103 {
104 Atlas::Message::Encoder e(b);
105 e.mapItem("parents", attr_parents);
106 }
107
sendId(Atlas::Bridge * b) const108 void Root::sendId(Atlas::Bridge* b) const
109 {
110 b->mapItem("id", attr_id);
111 }
112
sendObjtype(Atlas::Bridge * b) const113 void Root::sendObjtype(Atlas::Bridge* b) const
114 {
115 b->mapItem("objtype", attr_objtype);
116 }
117
sendName(Atlas::Bridge * b) const118 void Root::sendName(Atlas::Bridge* b) const
119 {
120 b->mapItem("name", attr_name);
121 }
122
sendContents(Bridge * b) const123 void Root::sendContents(Bridge* b) const
124 {
125 sendParents(b);
126 sendId(b);
127 sendObjtype(b);
128 sendName(b);
129
130 Message::Encoder e(b);
131 typedef std::map<std::string, Element>::const_iterator Iter;
132 for (Iter I = attributes.begin(); I != attributes.end(); I++)
133 e.mapItem((*I).first, (*I).second);
134 }
135
136 } } // namespace Atlas::Objects
137