]> 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
24 #include "HLADataElementVisitor.hxx"
25 #include "HLADataTypeVisitor.hxx"
26
27 namespace simgear {
28
29 HLAAbstractFixedRecordDataElement::HLAAbstractFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
30     _dataType(dataType)
31 {
32 }
33
34 HLAAbstractFixedRecordDataElement::~HLAAbstractFixedRecordDataElement()
35 {
36 }
37
38 void
39 HLAAbstractFixedRecordDataElement::accept(HLADataElementVisitor& visitor)
40 {
41     visitor.apply(*this);
42 }
43
44 void
45 HLAAbstractFixedRecordDataElement::accept(HLAConstDataElementVisitor& visitor) const
46 {
47     visitor.apply(*this);
48 }
49
50 bool
51 HLAAbstractFixedRecordDataElement::decode(HLADecodeStream& stream)
52 {
53     if (!_dataType.valid())
54         return false;
55     return _dataType->decode(stream, *this);
56 }
57
58 bool
59 HLAAbstractFixedRecordDataElement::encode(HLAEncodeStream& stream) const
60 {
61     if (!_dataType.valid())
62         return false;
63     return _dataType->encode(stream, *this);
64 }
65
66 const HLAFixedRecordDataType*
67 HLAAbstractFixedRecordDataElement::getDataType() const
68 {
69     return _dataType.get();
70 }
71
72 bool
73 HLAAbstractFixedRecordDataElement::setDataType(const HLADataType* dataType)
74 {
75     const HLAFixedRecordDataType* fixedRecordDataType = dataType->toFixedRecordDataType();
76     if (!fixedRecordDataType) {
77         SG_LOG(SG_NETWORK, SG_WARN, "HLAFixedRecordDataType: unable to set data type!");
78         return false;
79     }
80     setDataType(fixedRecordDataType);
81     return true;
82 }
83
84 void
85 HLAAbstractFixedRecordDataElement::setDataType(const HLAFixedRecordDataType* dataType)
86 {
87     _dataType = dataType;
88 }
89
90 unsigned
91 HLAAbstractFixedRecordDataElement::getNumFields() const
92 {
93     return _dataType->getNumFields();
94 }
95
96 unsigned
97 HLAAbstractFixedRecordDataElement::getFieldNumber(const std::string& name) const
98 {
99     return _dataType->getFieldNumber(name);
100 }
101
102 const HLADataType*
103 HLAAbstractFixedRecordDataElement::getFieldDataType(unsigned i) const
104 {
105     return _dataType->getFieldDataType(i);
106 }
107
108 const HLADataType*
109 HLAAbstractFixedRecordDataElement::getFieldDataType(const std::string& name) const
110 {
111     return getFieldDataType(getFieldNumber(name));
112 }
113
114
115 HLAFixedRecordDataElement::HLAFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
116     HLAAbstractFixedRecordDataElement(dataType)
117 {
118     _fieldVector.resize(getNumFields());
119 }
120
121 HLAFixedRecordDataElement::~HLAFixedRecordDataElement()
122 {
123 }
124
125 bool
126 HLAFixedRecordDataElement::decodeField(HLADecodeStream& stream, unsigned i)
127 {
128     HLADataElement* dataElement = getField(i);
129     if (dataElement) {
130         return dataElement->decode(stream);
131     } else {
132         HLADataTypeDecodeVisitor visitor(stream);
133         getDataType()->getFieldDataType(i)->accept(visitor);
134         return true;
135     }
136 }
137
138 bool
139 HLAFixedRecordDataElement::encodeField(HLAEncodeStream& stream, unsigned i) const
140 {
141     const HLADataElement* dataElement = getField(i);
142     if (dataElement) {
143         return dataElement->encode(stream);
144     } else {
145         HLADataTypeEncodeVisitor visitor(stream);
146         getDataType()->getFieldDataType(i)->accept(visitor);
147         return true;
148     }
149 }
150
151 HLADataElement*
152 HLAFixedRecordDataElement::getField(const std::string& name)
153 {
154     return getField(getFieldNumber(name));
155 }
156
157 const HLADataElement*
158 HLAFixedRecordDataElement::getField(const std::string& name) const
159 {
160     return getField(getFieldNumber(name));
161 }
162
163 HLADataElement*
164 HLAFixedRecordDataElement::getField(unsigned i)
165 {
166     if (_fieldVector.size() <= i)
167         return 0;
168     return _fieldVector[i].get();
169 }
170
171 const HLADataElement*
172 HLAFixedRecordDataElement::getField(unsigned i) const
173 {
174     if (_fieldVector.size() <= i)
175         return 0;
176     return _fieldVector[i].get();
177 }
178
179 void
180 HLAFixedRecordDataElement::setField(unsigned index, HLADataElement* value)
181 {
182     if (getNumFields() <= index)
183         return;
184     _fieldVector[index] = value;
185 }
186
187 void
188 HLAFixedRecordDataElement::setField(const std::string& name, HLADataElement* value)
189 {
190     setField(getFieldNumber(name), value);
191 }
192
193 }