OpenASIP  2.0
ObjectState.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ObjectState.hh
26  *
27  * Declaration of ObjectState class.
28  *
29  * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi)
30  * @note reviewed 8 Jun 2004 by tr, jm, am, ll
31  * @note rating: red
32  */
33 
34 #ifndef TTA_OBJECT_STATE_HH
35 #define TTA_OBJECT_STATE_HH
36 
37 #include <string>
38 #include <vector>
39 
40 #include "Exception.hh"
41 #include "BaseType.hh"
42 
43 /**
44  * Class ObjectState represents state of an object.
45  *
46  * It may contain attribute-value pairs of the object and a main
47  * value. It may be used as a node in tree structure or alone. If a
48  * class is going to be serialized to a file, it must be able to
49  * create an ObjectState object of itself. It must be able to create
50  * itself from an ObjectState object too. That is, it must implement
51  * the Serializable interface. ObjectState tree is a general
52  * structure which can be handled by many different kind of
53  * serializers, for example XMLSerializer. Serializers can read a file
54  * create an ObjectState tree according to it and they can write a
55  * file of a certain format according to a ObjectState
56  * tree. ObjectState enables an easy way to serialize an object model
57  * to different kinds of file formats.
58  */
59 class ObjectState {
60 public:
61  /// Struct for describing an attribute of the XML element.
62  struct Attribute {
63  std::string name; ///< Name of the attribute.
64  std::string value; ///< Value of the attribute.
65  };
66 
67  ObjectState(const std::string& name, ObjectState* parent=NULL);
68  ObjectState(const ObjectState& old);
69  ~ObjectState();
70 
71  ObjectState* parent() const;
72  std::string name() const;
73  void setName(const std::string& name);
74 
75  void setValue(const std::string& value);
76  void setValue(int value);
77  void setValue(double value);
78  void setValue(bool value);
79  void setValue(UIntWord value);
80 
81  std::string stringValue() const;
82  int intValue() const;
83  unsigned int unsignedIntValue() const;
84  double doubleValue() const;
85  bool boolValue() const;
86  int UIntWordValue() const;
87 
88  void setAttribute(const std::string& name, const std::string& value);
89  void setAttribute(const std::string& name, int value);
90  void setAttribute(const std::string& name, unsigned int value);
91  void setAttribute(const std::string& name, double value);
92  void setAttribute(const std::string& name, bool value);
93  void setAttribute(const std::string& name, ULongWord value);
94 
95  int attributeCount() const;
96  Attribute* attribute(int index) const;
97 
98  bool hasAttribute(const std::string& name) const;
99  std::string stringAttribute(const std::string& name) const;
100  int intAttribute(const std::string& name) const;
101  unsigned int unsignedIntAttribute(const std::string& name) const;
102  double doubleAttribute(const std::string& name) const;
103  bool boolAttribute(const std::string& name) const;
104  UIntWord UIntWordAttribute(const std::string& name) const;
105  ULongWord uLongAttribute(const std::string& name) const;
106 
107  bool hasChild(const std::string& name) const;
108  void addChild(ObjectState* child);
110  void replaceChild(ObjectState* old, ObjectState* newChild);
111 
112  int childCount() const;
113  ObjectState* childByName(const std::string& name) const;
114  ObjectState* child(int index) const;
115 
116  bool operator!=(const ObjectState& object);
117 
118  static void dumpObjectState(
119  const ObjectState& state,
120  std::ostream& output,
121  const std::string& identation = "");
122 
123 private:
124  /// Table of child ObjectState.
125  typedef std::vector<ObjectState*> ChildTable;
126  /// Table of attributes.
127  typedef std::vector<Attribute> AttributeTable;
128 
129  /// Assingment forbidden.
130  ObjectState& operator=(const ObjectState& old);
131  std::string commonErrorMessage() const;
132 
133  /// Name of the element.
134  std::string name_;
135 
136  /// The value of a leaf element.
137  std::string value_;
138 
139  /// The parent element.
141 
142  /// The child elements.
144 
145  /// Contains all the attributes of the element.
147 };
148 
149 #include "ObjectState.icc"
150 
151 #endif
ObjectState::attributes_
AttributeTable attributes_
Contains all the attributes of the element.
Definition: ObjectState.hh:146
UIntWord
Word UIntWord
Definition: BaseType.hh:144
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
ObjectState::attribute
Attribute * attribute(int index) const
Definition: ObjectState.cc:228
ObjectState::Attribute
Struct for describing an attribute of the XML element.
Definition: ObjectState.hh:62
BaseType.hh
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
Exception.hh
ObjectState::Attribute::name
std::string name
Name of the attribute.
Definition: ObjectState.hh:63
ObjectState::Attribute::value
std::string value
Value of the attribute.
Definition: ObjectState.hh:64
ObjectState::intValue
int intValue() const
ObjectState::~ObjectState
~ObjectState()
Definition: ObjectState.cc:81
ObjectState::uLongAttribute
ULongWord uLongAttribute(const std::string &name) const
Definition: ObjectState.cc:291
ObjectState::doubleValue
double doubleValue() const
ObjectState
Definition: ObjectState.hh:59
ObjectState::value_
std::string value_
The value of a leaf element.
Definition: ObjectState.hh:137
ObjectState::operator!=
bool operator!=(const ObjectState &object)
Definition: ObjectState.cc:491
ObjectState::children_
ChildTable children_
The child elements.
Definition: ObjectState.hh:143
ObjectState::setName
void setName(const std::string &name)
ObjectState::replaceChild
void replaceChild(ObjectState *old, ObjectState *newChild)
Definition: ObjectState.cc:408
ObjectState::childByName
ObjectState * childByName(const std::string &name) const
Definition: ObjectState.cc:443
ObjectState::dumpObjectState
static void dumpObjectState(const ObjectState &state, std::ostream &output, const std::string &identation="")
Definition: ObjectState.cc:540
ObjectState::ObjectState
ObjectState(const std::string &name, ObjectState *parent=NULL)
Definition: ObjectState.cc:51
ObjectState::ChildTable
std::vector< ObjectState * > ChildTable
Table of child ObjectState.
Definition: ObjectState.hh:125
ObjectState::commonErrorMessage
std::string commonErrorMessage() const
Definition: ObjectState.cc:529
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
ObjectState::childCount
int childCount() const
ObjectState::hasChild
bool hasChild(const std::string &name) const
Definition: ObjectState.cc:358
ObjectState::name
std::string name() const
ObjectState::UIntWordValue
int UIntWordValue() const
ObjectState.icc
ObjectState::parent
ObjectState * parent() const
ObjectState::doubleAttribute
double doubleAttribute(const std::string &name) const
Definition: ObjectState.cc:323
ObjectState::unsignedIntAttribute
unsigned int unsignedIntAttribute(const std::string &name) const
Definition: ObjectState.cc:308
ObjectState::removeChild
void removeChild(ObjectState *child)
Definition: ObjectState.cc:389
ObjectState::boolAttribute
bool boolAttribute(const std::string &name) const
Definition: ObjectState.cc:338
ObjectState::unsignedIntValue
unsigned int unsignedIntValue() const
ObjectState::boolValue
bool boolValue() const
ObjectState::parent_
ObjectState * parent_
The parent element.
Definition: ObjectState.hh:140
ObjectState::stringValue
std::string stringValue() const
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
ObjectState::UIntWordAttribute
UIntWord UIntWordAttribute(const std::string &name) const
ULongWord
unsigned long ULongWord
Definition: BaseType.hh:51
ObjectState::setValue
void setValue(const std::string &value)
ObjectState::AttributeTable
std::vector< Attribute > AttributeTable
Table of attributes.
Definition: ObjectState.hh:127
ObjectState::operator=
ObjectState & operator=(const ObjectState &old)
Assingment forbidden.
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
ObjectState::name_
std::string name_
Name of the element.
Definition: ObjectState.hh:134
ObjectState::attributeCount
int attributeCount() const