]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAFixedRecordDataElement.cxx
Merge branch 'next' of git://gitorious.org/fg/simgear into next
[simgear.git] / simgear / hla / HLAFixedRecordDataElement.cxx
1 // Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #include "HLAFixedRecordDataElement.hxx"
19
20 #include <string>
21 #include <vector>
22 #include <simgear/debug/logstream.hxx>
23 #include "HLADataTypeVisitor.hxx"
24
25 namespace simgear {
26
27 HLAAbstractFixedRecordDataElement::HLAAbstractFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
28     _dataType(dataType)
29 {
30 }
31
32 HLAAbstractFixedRecordDataElement::~HLAAbstractFixedRecordDataElement()
33 {
34 }
35
36 bool
37 HLAAbstractFixedRecordDataElement::decode(HLADecodeStream& stream)
38 {
39     if (!_dataType.valid())
40         return false;
41     return _dataType->decode(stream, *this);
42 }
43
44 bool
45 HLAAbstractFixedRecordDataElement::encode(HLAEncodeStream& stream) const
46 {
47     if (!_dataType.valid())
48         return false;
49     return _dataType->encode(stream, *this);
50 }
51
52 const HLAFixedRecordDataType*
53 HLAAbstractFixedRecordDataElement::getDataType() const
54 {
55     return _dataType.get();
56 }
57
58 bool
59 HLAAbstractFixedRecordDataElement::setDataType(const HLADataType* dataType)
60 {
61     const HLAFixedRecordDataType* fixedRecordDataType = dataType->toFixedRecordDataType();
62     if (!fixedRecordDataType) {
63         SG_LOG(SG_NETWORK, SG_WARN, "HLAFixedRecordDataType: unable to set data type!");
64         return false;
65     }
66     setDataType(fixedRecordDataType);
67     return true;
68 }
69
70 void
71 HLAAbstractFixedRecordDataElement::setDataType(const HLAFixedRecordDataType* dataType)
72 {
73     _dataType = dataType;
74 }
75
76 unsigned
77 HLAAbstractFixedRecordDataElement::getNumFields() const
78 {
79     return _dataType->getNumFields();
80 }
81
82 unsigned
83 HLAAbstractFixedRecordDataElement::getFieldNumber(const std::string& name) const
84 {
85     return _dataType->getFieldNumber(name);
86 }
87
88 const HLADataType*
89 HLAAbstractFixedRecordDataElement::getFieldDataType(unsigned i) const
90 {
91     return _dataType->getFieldDataType(i);
92 }
93
94 const HLADataType*
95 HLAAbstractFixedRecordDataElement::getFieldDataType(const std::string& name) const
96 {
97     return getFieldDataType(getFieldNumber(name));
98 }
99
100
101 HLAFixedRecordDataElement::HLAFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
102     HLAAbstractFixedRecordDataElement(dataType)
103 {
104     _fieldVector.resize(getNumFields());
105 }
106
107 HLAFixedRecordDataElement::~HLAFixedRecordDataElement()
108 {
109 }
110
111 bool
112 HLAFixedRecordDataElement::decodeField(HLADecodeStream& stream, unsigned i)
113 {
114     HLADataElement* dataElement = getField(i);
115     if (dataElement) {
116         return dataElement->decode(stream);
117     } else {
118         HLADataTypeDecodeVisitor visitor(stream);
119         getDataType()->getFieldDataType(i)->accept(visitor);
120         return true;
121     }
122 }
123
124 bool
125 HLAFixedRecordDataElement::encodeField(HLAEncodeStream& stream, unsigned i) const
126 {
127     const HLADataElement* dataElement = getField(i);
128     if (dataElement) {
129         return dataElement->encode(stream);
130     } else {
131         HLADataTypeEncodeVisitor visitor(stream);
132         getDataType()->getFieldDataType(i)->accept(visitor);
133         return true;
134     }
135 }
136
137 HLADataElement*
138 HLAFixedRecordDataElement::getField(const std::string& name)
139 {
140     return getField(getFieldNumber(name));
141 }
142
143 const HLADataElement*
144 HLAFixedRecordDataElement::getField(const std::string& name) const
145 {
146     return getField(getFieldNumber(name));
147 }
148
149 HLADataElement*
150 HLAFixedRecordDataElement::getField(unsigned i)
151 {
152     if (_fieldVector.size() <= i)
153         return 0;
154     return _fieldVector[i].get();
155 }
156
157 const HLADataElement*
158 HLAFixedRecordDataElement::getField(unsigned i) const
159 {
160     if (_fieldVector.size() <= i)
161         return 0;
162     return _fieldVector[i].get();
163 }
164
165 void
166 HLAFixedRecordDataElement::setField(unsigned index, HLADataElement* value)
167 {
168     if (getNumFields() <= index)
169         return;
170     _fieldVector[index] = value;
171 }
172
173 void
174 HLAFixedRecordDataElement::setField(const std::string& name, HLADataElement* value)
175 {
176     setField(getFieldNumber(name), value);
177 }
178
179 }