]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAVariantDataType.cxx
Add an initial implementation of a rti/hla dispatcher.
[simgear.git] / simgear / hla / HLAVariantDataType.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 "HLAVariantDataType.hxx"
19
20 #include "HLADataTypeVisitor.hxx"
21 #include "HLAVariantDataElement.hxx"
22
23 namespace simgear {
24
25 HLAVariantDataType::HLAVariantDataType(const std::string& name) :
26     HLADataType(name)
27 {
28 }
29
30 HLAVariantDataType::~HLAVariantDataType()
31 {
32 }
33
34 void
35 HLAVariantDataType::accept(HLADataTypeVisitor& visitor) const
36 {
37     visitor.apply(*this);
38 }
39
40 const HLAVariantDataType*
41 HLAVariantDataType::toVariantDataType() const
42 {
43     return this;
44 }
45
46 bool
47 HLAVariantDataType::decode(HLADecodeStream& stream, HLAAbstractVariantDataElement& value) const
48 {
49     if (!stream.alignOffsetForSize(getAlignment()))
50         return false;
51     if (!_enumeratedDataType.valid())
52         return false;
53     unsigned index = ~0u;
54     if (!_enumeratedDataType->decode(stream, index))
55         return false;
56     if (!value.setAlternativeIndex(index))
57         return false;
58     if (!value.decodeAlternative(stream))
59         return false;
60     return true;
61 }
62
63 bool
64 HLAVariantDataType::encode(HLAEncodeStream& stream, const HLAAbstractVariantDataElement& value) const
65 {
66     if (!stream.alignOffsetForSize(getAlignment()))
67         return false;
68     if (!_enumeratedDataType.valid())
69         return false;
70     unsigned index = value.getAlternativeIndex();
71     if (!_enumeratedDataType->encode(stream, index))
72         return false;
73     if (!value.encodeAlternative(stream))
74         return false;
75     return true;
76 }
77
78 void
79 HLAVariantDataType::setEnumeratedDataType(HLAEnumeratedDataType* dataType)
80 {
81     _enumeratedDataType = dataType;
82 }
83
84 bool
85 HLAVariantDataType::addAlternative(const std::string& name, const std::string& enumerator,
86                                    const HLADataType* dataType, const std::string& semantics)
87 {
88     if (!_enumeratedDataType.valid())
89         return false;
90     unsigned index = _enumeratedDataType->getIndex(enumerator);
91     if (_enumeratedDataType->getNumEnumerators() <= index)
92         return false;
93     _alternativeList.resize(_enumeratedDataType->getNumEnumerators());
94     _alternativeList[index]._name = name;
95     _alternativeList[index]._dataType = dataType;
96     _alternativeList[index]._semantics = semantics;
97     setAlignment(SGMisc<unsigned>::max(getAlignment(), dataType->getAlignment()));
98     return true;
99 }
100
101 } // namespace simgear