]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAEnumeratedDataType.hxx
hla: Use raw pointers for HLAFederate::_insert methods.
[simgear.git] / simgear / hla / HLAEnumeratedDataType.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 HLAEnumeratedDataType_hxx
19 #define HLAEnumeratedDataType_hxx
20
21 #include <string>
22 #include <simgear/structure/SGSharedPtr.hxx>
23 #include "HLADataType.hxx"
24
25 namespace simgear {
26
27 class HLAEnumeratedDataType : public HLADataType {
28 public:
29     HLAEnumeratedDataType(const std::string& name = "HLAEnumeratedDataType");
30     virtual ~HLAEnumeratedDataType();
31
32     virtual void accept(HLADataTypeVisitor& visitor) const;
33
34     virtual const HLAEnumeratedDataType* toEnumeratedDataType() const;
35
36     virtual bool decode(HLADecodeStream& stream, unsigned& index) const;
37     virtual bool encode(HLAEncodeStream& stream, unsigned index) const;
38
39     std::string getString(unsigned index) const
40     {
41         if (!_map.valid())
42             return std::string();
43         return _map->getString(index);
44     }
45     unsigned getIndex(const std::string& name) const
46     {
47         if (!_map.valid())
48             return ~unsigned(0);
49         return _map->getIndex(name);
50     }
51     unsigned getNumEnumerators() const
52     {
53         if (!_map.valid())
54             return 0;
55         return _map->getNumEnumerators();
56     }
57
58     bool addEnumerator(const std::string& name, const std::string& number)
59     {
60         if (!_map.valid())
61             return false;
62         return _map->add(name, number);
63     }
64
65     void setRepresentation(HLABasicDataType* representation);
66     const HLABasicDataType* getRepresentation() const
67     {
68         if (!_map.valid())
69             return 0;
70         return _map->getDataType();
71     }
72
73 protected:
74     virtual void _recomputeAlignmentImplementation();
75
76 private:
77     class AbstractMap : public SGReferenced {
78     public:
79         virtual ~AbstractMap() {}
80         virtual bool encode(HLAEncodeStream& stream, unsigned index) const = 0;
81         virtual bool decode(HLADecodeStream& stream, unsigned& index) const = 0;
82         virtual const HLABasicDataType* getDataType() const = 0;
83         virtual bool add(const std::string& name, const std::string& number) = 0;
84         virtual std::string getString(unsigned index) const = 0;
85         virtual unsigned getIndex(const std::string& name) const = 0;
86         virtual unsigned getNumEnumerators() const = 0;
87     };
88
89     template<typename DataType, typename T>
90     class Map;
91     class RepresentationVisitor;
92
93     SGSharedPtr<AbstractMap> _map;
94 };
95
96 } // namespace simgear
97
98 #endif