]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAVariantRecordDataType.hxx
hla: Fixes for the data element index implementation.
[simgear.git] / simgear / hla / HLAVariantRecordDataType.hxx
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 #ifndef HLAVariantRecordDataType_hxx
19 #define HLAVariantRecordDataType_hxx
20
21 #include <string>
22 #include <vector>
23 #include "simgear/structure/SGSharedPtr.hxx"
24 #include "HLADataType.hxx"
25 #include "HLAEnumeratedDataType.hxx"
26
27 namespace simgear {
28
29 class HLAAbstractVariantRecordDataElement;
30
31 class HLAVariantRecordDataType : public HLADataType {
32 public:
33     HLAVariantRecordDataType(const std::string& name = "HLAVariantRecordDataType");
34     virtual ~HLAVariantRecordDataType();
35
36     virtual void accept(HLADataTypeVisitor& visitor) const;
37
38     virtual const HLAVariantRecordDataType* toVariantRecordDataType() const;
39
40     virtual void releaseDataTypeReferences();
41     
42     virtual bool decode(HLADecodeStream& stream, HLAAbstractVariantRecordDataElement& value) const;
43     virtual bool encode(HLAEncodeStream& stream, const HLAAbstractVariantRecordDataElement& value) const;
44
45     const HLAEnumeratedDataType* getEnumeratedDataType() const
46     { return _enumeratedDataType.get(); }
47     void setEnumeratedDataType(HLAEnumeratedDataType* dataType);
48
49     bool addAlternative(const std::string& name, const std::string& enumerator,
50                         const HLADataType* dataType, const std::string& semantics);
51
52     unsigned getNumAlternatives() const
53     { return _alternativeList.size(); }
54
55     unsigned getAlternativeIndex(const std::string& enumerator) const
56     {
57         if (!_enumeratedDataType.valid())
58             return ~unsigned(0);
59         return _enumeratedDataType->getIndex(enumerator);
60     }
61
62     const HLADataType* getAlternativeDataType(unsigned index) const
63     {
64         if (_alternativeList.size() <= index)
65             return 0;
66         return _alternativeList[index]._dataType.get();
67     }
68     const HLADataType* getAlternativeDataType(const std::string& enumerator) const
69     { return getAlternativeDataType(getAlternativeIndex(enumerator)); }
70
71     std::string getAlternativeName(unsigned index) const
72     {
73         if (_alternativeList.size() <= index)
74             return std::string();
75         return _alternativeList[index]._name;
76     }
77     std::string getAlternativeName(const std::string& enumerator) const
78     { return getAlternativeName(getAlternativeIndex(enumerator)); }
79
80     std::string getAlternativeSemantics(unsigned index) const
81     {
82         if (_alternativeList.size() <= index)
83             return std::string();
84         return _alternativeList[index]._semantics;
85     }
86     std::string getAlternativeSemantics(const std::string& enumerator) const
87     { return getAlternativeSemantics(getAlternativeIndex(enumerator)); }
88
89 protected:
90     virtual void _recomputeAlignmentImplementation();
91
92 private:
93     SGSharedPtr<HLAEnumeratedDataType> _enumeratedDataType;
94
95     struct Alternative {
96         std::string _name;
97         SGSharedPtr<const HLADataType> _dataType;
98         std::string _semantics;
99     };
100
101     typedef std::vector<Alternative> AlternativeList;
102     AlternativeList _alternativeList;
103 };
104
105 } // namespace simgear
106
107 #endif