]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAVariantRecordDataType.cxx
hla: Remove deprecated methods from HLAObjectClass
[simgear.git] / simgear / hla / HLAVariantRecordDataType.cxx
1 // Copyright (C) 2009 - 2012  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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <simgear/compiler.h>
23
24 #include "HLAVariantRecordDataType.hxx"
25
26 #include "HLADataTypeVisitor.hxx"
27 #include "HLAVariantRecordDataElement.hxx"
28
29 namespace simgear {
30
31 HLAVariantRecordDataType::HLAVariantRecordDataType(const std::string& name) :
32     HLADataType(name)
33 {
34 }
35
36 HLAVariantRecordDataType::~HLAVariantRecordDataType()
37 {
38 }
39
40 void
41 HLAVariantRecordDataType::accept(HLADataTypeVisitor& visitor) const
42 {
43     visitor.apply(*this);
44 }
45
46 const HLAVariantRecordDataType*
47 HLAVariantRecordDataType::toVariantRecordDataType() const
48 {
49     return this;
50 }
51
52 void
53 HLAVariantRecordDataType::releaseDataTypeReferences()
54 {
55     for (AlternativeList::iterator i = _alternativeList.begin(); i != _alternativeList.end(); ++i)
56         i->_dataType = 0;
57 }
58
59 bool
60 HLAVariantRecordDataType::decode(HLADecodeStream& stream, HLAAbstractVariantRecordDataElement& value) const
61 {
62     if (!stream.alignOffsetForSize(getAlignment()))
63         return false;
64     if (!_enumeratedDataType.valid())
65         return false;
66     unsigned index = ~0u;
67     if (!_enumeratedDataType->decode(stream, index))
68         return false;
69     if (!value.setAlternativeIndex(index))
70         return false;
71     if (!value.decodeAlternative(stream))
72         return false;
73     return true;
74 }
75
76 bool
77 HLAVariantRecordDataType::encode(HLAEncodeStream& stream, const HLAAbstractVariantRecordDataElement& value) const
78 {
79     if (!stream.alignOffsetForSize(getAlignment()))
80         return false;
81     if (!_enumeratedDataType.valid())
82         return false;
83     unsigned index = value.getAlternativeIndex();
84     if (!_enumeratedDataType->encode(stream, index))
85         return false;
86     if (!value.encodeAlternative(stream))
87         return false;
88     return true;
89 }
90
91 void
92 HLAVariantRecordDataType::setEnumeratedDataType(HLAEnumeratedDataType* dataType)
93 {
94     _enumeratedDataType = dataType;
95 }
96
97 bool
98 HLAVariantRecordDataType::addAlternative(const std::string& name, const std::string& enumerator,
99                                          const HLADataType* dataType, const std::string& semantics)
100 {
101     if (!_enumeratedDataType.valid())
102         return false;
103     unsigned index = _enumeratedDataType->getIndex(enumerator);
104     if (_enumeratedDataType->getNumEnumerators() <= index)
105         return false;
106     _alternativeList.resize(_enumeratedDataType->getNumEnumerators());
107     _alternativeList[index]._name = name;
108     _alternativeList[index]._dataType = dataType;
109     _alternativeList[index]._semantics = semantics;
110     return true;
111 }
112
113 void
114 HLAVariantRecordDataType::_recomputeAlignmentImplementation()
115 {
116     unsigned alignment = 1;
117     if (const HLADataType* dataType = getEnumeratedDataType())
118         alignment = std::max(alignment, dataType->getAlignment());
119     for (unsigned i = 0; i < getNumAlternatives(); ++i) {
120         if (const HLADataType* dataType = getAlternativeDataType(i))
121             alignment = std::max(alignment, dataType->getAlignment());
122     }
123     setAlignment(alignment);
124 }
125
126 } // namespace simgear