Atlas-C++
BaseObject.h
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-2004 Stefanus Du Toit, Aloril and Al Riddoch
4 
5 // $Id$
6 
7 #ifndef ATLAS_OBJECTS_BASEOBJECT_H
8 #define ATLAS_OBJECTS_BASEOBJECT_H
9 
10 #include <Atlas/Message/MEncoder.h>
11 #include <Atlas/Message/Element.h>
12 #include <Atlas/Bridge.h>
13 #include <Atlas/Exception.h>
14 
15 #include <map>
16 #include <list>
17 #include <string>
18 
19 #include <assert.h>
20 
21 namespace Atlas {
22 
26 namespace Objects {
27 
33 {
35  std::string m_name;
36  public:
37  NoSuchAttrException(const std::string& name) :
38  Atlas::Exception("No such attribute"), m_name(name) {}
39  virtual ~NoSuchAttrException();
41  const std::string & getName() const {
42  return m_name;
43  }
44 };
45 
46 static const int BASE_OBJECT_NO = 0;
47 
68 {
69 public:
75 
76  virtual ~BaseObjectData();
77 
79  int getClassNo() const
80  {
81  return m_class_no;
82  }
83 
84  int getAttrFlags() const
85  {
86  return m_attrFlags;
87  }
88 
89  virtual BaseObjectData * copy() const = 0;
90 
92  virtual bool instanceOf(int classNo) const;
93 
95  bool hasAttr(const std::string& name) const;
97  bool hasAttrFlag(int flag) const;
100  const Atlas::Message::Element getAttr(const std::string& name)
101  const;
104  virtual int copyAttr(const std::string& name,
105  Atlas::Message::Element & attr) const;
107  virtual void setAttr(const std::string& name,
108  const Atlas::Message::Element& attr);
110  virtual void removeAttr(const std::string& name);
112  virtual void removeAttrFlag(int flag);
113 
116  const Atlas::Message::MapType asMessage() const;
117 
119  virtual void addToMessage(Atlas::Message::MapType &) const;
120 
122  virtual void sendContents(Atlas::Bridge & b) const;
123 
124  //move to protected once SmartPtr <-> BaseObject order established
125  inline void incRef();
126  inline void decRef();
127 
133  static BaseObjectData *alloc() {assert(0); return NULL;} //not callable
138  virtual void free() = 0;
139 
140  class const_iterator;
141 
142  // FIXME should this hold a reference to the object it's
143  // iterating over?
144 
159  class iterator
160  {
161  public:
162  friend class BaseObjectData;
163  friend class const_iterator;
164 
165  iterator() : m_obj(0), m_val("", *this) {}
166  iterator(const iterator& I) : m_obj(I.m_obj),
167  m_current_class(I.m_current_class),
168  m_I(I.m_I), m_val(I.m_val.first, *this) {}
169  iterator(BaseObjectData& obj, int current_class);
170 
171  // default destructor is fine unless we hold a reference to m_obj
172 
173  iterator& operator=(const iterator& I);
174 
175  iterator& operator++(); // preincrement
176 
177  inline iterator operator++(int); // postincrement
178 
179  bool operator==(const iterator& I) const;
180 
181  bool operator!=(const iterator& I) const {return !operator==(I);}
182 
184  {
185  public:
186  PsuedoElement(const iterator& I) : m_I(I) {}
187 
188  operator Message::Element() const;
189  // this acts on const PsuedoElement instead of PsuedoElement
190  // so that we can assign to attributes refered to by
191  // a const iterator& (as opposed to a const_iterator, where
192  // we can't, but that's done later)
193  const PsuedoElement& operator=(const Message::Element& val) const;
194 
195  private:
196  const iterator& m_I;
197  };
198 
199  friend class PsuedoElement;
200 
201  typedef std::pair<std::string,PsuedoElement> value_type;
202 
203  const value_type& operator*() const {return m_val;}
204  const value_type* operator->() const {return &m_val;}
205 
206  private:
207  BaseObjectData *m_obj; // pointer to object whose args we're iterating
208  int m_current_class; // m_class_no for current class in the iteration
209  Message::MapType::iterator m_I; // iterator in m_obj->m_attributes
210  value_type m_val;
211  };
212  friend class iterator;
213 
214  // FIXME should this hold a reference to the object it's
215  // iterating over?
217  {
218  public:
219  friend class BaseObjectData;
220 
221  const_iterator() : m_obj(0), m_val("", *this) {}
222  const_iterator(const const_iterator& I) : m_obj(I.m_obj),
223  m_current_class(I.m_current_class),
224  m_I(I.m_I), m_val(I.m_val.first, *this) {}
225  const_iterator(const iterator& I) : m_obj(I.m_obj),
226  m_current_class(I.m_current_class),
227  m_I(I.m_I), m_val(I.m_val.first, *this) {}
228  const_iterator(const BaseObjectData& obj, int current_class);
229 
230  // default destructor is fine unless we hold a reference to m_obj
231 
232  const_iterator& operator=(const const_iterator& I);
233 
234  const_iterator& operator++(); // preincrement
235 
236  inline const_iterator operator++(int); // postincrement
237 
238  bool operator==(const const_iterator& I) const;
239 
240  bool operator!=(const const_iterator& I) const {return !operator==(I);}
241 
243  {
244  public:
245  PsuedoElement(const const_iterator& I) : m_I(I) {}
246 
247  operator Message::Element() const;
248 
249  private:
250  const const_iterator& m_I;
251  };
252 
253  friend class PsuedoElement;
254 
255  typedef std::pair<std::string,PsuedoElement> value_type;
256 
257  const value_type& operator*() const {return m_val;}
258  const value_type* operator->() const {return &m_val;}
259 
260  private:
261  const BaseObjectData *m_obj; // pointer to object whose args we're iterating
262  int m_current_class; // m_class_no for current class in the iteration
263  Message::MapType::const_iterator m_I; // const_iterator in m_obj->m_attributes
264  value_type m_val;
265  };
266 
267  friend class const_iterator;
268 
269  iterator begin() {return iterator(*this, -1);}
270  iterator end() {return iterator(*this, BASE_OBJECT_NO);}
271  iterator find(const std::string&);
272 
273  const_iterator begin() const {return const_iterator(*this, -1);}
274  const_iterator end() const {return const_iterator(*this, BASE_OBJECT_NO);}
275  const_iterator find(const std::string&) const;
276 
277 protected:
278 
280  virtual int getAttrClass(const std::string& name) const;
281 
283  virtual int getAttrFlag(const std::string& name) const;
284 
286  virtual void iterate(int& current_class, std::string& attr) const;
287 
288  int m_class_no; //each class has different enum
289  int m_refCount; //how many instances
290  BaseObjectData *m_defaults;
291  //this will be defined in each subclass separately, so no need here for it
292  //static BaseObjectData *begin;
293  BaseObjectData *m_next;
294  std::map<std::string, Atlas::Message::Element> m_attributes;
295  // is attribute in this object or in default object?
296  int m_attrFlags;
297 };
298 
299 void BaseObjectData::incRef() {
300  m_refCount++;
301 }
302 
303 void BaseObjectData::decRef() {
304  //why zero based refCount? avoids one m_refCount-- ;-)
305  assert( m_refCount >= 0 );
306  if(!m_refCount) {
307  free();
308  return;
309  }
310  m_refCount--;
311 }
312 
313 BaseObjectData::iterator BaseObjectData::iterator::operator++(int) // postincrement
314 {
315  iterator tmp = *this;
316  operator++();
317  return tmp;
318 }
319 
320 BaseObjectData::const_iterator BaseObjectData::const_iterator::operator++(int) // postincrement
321 {
322  const_iterator tmp = *this;
323  operator++();
324  return tmp;
325 }
326 
327 
328 } } // namespace Atlas::Objects
329 
330 #endif
Atlas::Objects::BaseObjectData
Atlas base object class.
Definition: BaseObject.h:68
Atlas::Objects::BaseObjectData::setAttr
virtual void setAttr(const std::string &name, const Atlas::Message::Element &attr)
Set the attribute "name" to the value given by "attr".
Atlas::Bridge
Atlas stream bridge.
Definition: Bridge.h:36
Atlas::Objects::BaseObjectData::BaseObjectData
BaseObjectData(BaseObjectData *defaults)
Construct a new BaseObjectData from a subclass.
Atlas::Objects::BaseObjectData::removeAttr
virtual void removeAttr(const std::string &name)
Remove the attribute "name".
Atlas::Objects::BaseObjectData::iterator::PsuedoElement
Definition: BaseObject.h:184
Atlas::Objects::NoSuchAttrException
An exception indicating the requested attribute does not exist.
Definition: BaseObject.h:33
Atlas::Objects::BaseObjectData::getAttrFlag
virtual int getAttrFlag(const std::string &name) const
Find the flag for the attribute "name".
Atlas::Objects::BaseObjectData::getClassNo
int getClassNo() const
Get class number:
Definition: BaseObject.h:79
Atlas::Objects::BaseObjectData::getAttr
const Atlas::Message::Element getAttr(const std::string &name) const
Retrieve the attribute "name".
Atlas::Message::Element
Multi-type container.
Definition: Element.h:61
Atlas::Objects::BaseObjectData::copyAttr
virtual int copyAttr(const std::string &name, Atlas::Message::Element &attr) const
Retrieve the attribute "name".
Atlas::Objects::BaseObjectData::hasAttr
bool hasAttr(const std::string &name) const
Check whether the attribute "name" exists.
Atlas::Exception
Base class for all exceptions thrown by Atlas-C++.
Definition: Exception.h:18
Atlas::Objects::BaseObjectData::addToMessage
virtual void addToMessage(Atlas::Message::MapType &) const
Write this object to an existing Element.
Atlas::Objects::BaseObjectData::const_iterator
Definition: BaseObject.h:217
Atlas::Objects::BaseObjectData::sendContents
virtual void sendContents(Atlas::Bridge &b) const
Send the contents of this object to a Bridge.
Atlas::Objects::BaseObjectData::iterator
The iterator first iterates over the contents of m_obj->m_attributes, holding an iterator to the attr...
Definition: BaseObject.h:160
Atlas::Objects::BaseObjectData::removeAttrFlag
virtual void removeAttrFlag(int flag)
Remove the attribute "name".
Atlas
The Atlas namespace.
Definition: Bridge.h:20
Atlas::Objects::BaseObjectData::asMessage
const Atlas::Message::MapType asMessage() const
Convert this object to a Object.
Atlas::Objects::BaseObjectData::getAttrClass
virtual int getAttrClass(const std::string &name) const
Find the class which contains the attribute "name".
Atlas::Objects::BaseObjectData::hasAttrFlag
bool hasAttrFlag(int flag) const
Check whether the attribute "name" exists.
Atlas::Objects::BaseObjectData::instanceOf
virtual bool instanceOf(int classNo) const
Is this instance of some class?
Atlas::Objects::BaseObjectData::iterate
virtual void iterate(int &current_class, std::string &attr) const
Iterate over the attributes of this instance.
Atlas::Objects::BaseObjectData::const_iterator::PsuedoElement
Definition: BaseObject.h:243
Atlas::Objects::NoSuchAttrException::getName
const std::string & getName() const
Get the name of the attribute which does not exist.
Definition: BaseObject.h:41
Atlas::Objects::BaseObjectData::alloc
static BaseObjectData * alloc()
Allocate a new instance of this class, using an existing instance if available.
Definition: BaseObject.h:133
Atlas::Objects::BaseObjectData::free
virtual void free()=0
Free an instance of this class, returning it to the memory pool.

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.