1 // Copyright (C) 2009 - 2011 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
18 #ifndef HLADataElement_hxx
19 #define HLADataElement_hxx
23 #include <simgear/structure/SGReferenced.hxx>
24 #include <simgear/structure/SGSharedPtr.hxx>
25 #include <simgear/timing/timestamp.hxx>
26 #include "RTIData.hxx"
27 #include "HLADataType.hxx"
33 class HLADataElementVisitor;
34 class HLAConstDataElementVisitor;
36 class HLADataElement : public SGReferenced {
38 virtual ~HLADataElement();
40 virtual void accept(HLADataElementVisitor& visitor) = 0;
41 virtual void accept(HLAConstDataElementVisitor& visitor) const = 0;
43 virtual bool encode(HLAEncodeStream& stream) const = 0;
44 virtual bool decode(HLADecodeStream& stream) = 0;
46 virtual const HLADataType* getDataType() const = 0;
47 virtual bool setDataType(const HLADataType* dataType) = 0;
49 // Container for the timestamp the originating attribute was last updated for
50 // class TimeStamp : public SGReferenced {
52 // const SGTimeStamp& getTimeStamp() const
53 // { return _timeStamp; }
54 // void setTimeStamp(const SGTimeStamp& timeStamp)
55 // { _timeStamp = timeStamp; }
57 // SGTimeStamp _timeStamp;
60 // const TimeStamp* getTimeStamp() const
61 // { return _timeStamp.get(); }
62 // void setTimeStamp(const TimeStamp* timeStamp)
63 // { _timeStamp = timeStamp; }
65 // struct ChangeCount : public SGReferenced {
66 // ChangeCount() : _value(0) {}
69 // SGSharedPtr<ChangeCount> _changeCount;
70 // unsigned getChangeCount() const
72 // // If we don't have return allways the same
73 // if (!_changeCount.valid())
75 // return _changeCount->_value;
78 /// HLADataElements could be identified by path
79 /// These paths are composed of structure field names and array indices in the
80 /// order they appear while walking to the data element.
81 /// So provide here some tool functions to access these elements
82 /// Note that these functions are relatively expensive in execution time.
83 /// So only use them once at object creation time and store direct references to the values
87 PathElement(unsigned index) : _data(new IndexData(index)) {}
88 PathElement(const std::string& name) : _data(new FieldData(name)) {}
90 bool isFieldValue() const
91 { return _data->toFieldData(); }
92 bool isIndexValue() const
93 { return _data->toIndexData(); }
95 unsigned getIndexValue() const
97 const IndexData* indexData = _data->toIndexData();
100 return indexData->_index;
103 std::string getFieldValue() const
105 const FieldData* fieldData = _data->toFieldData();
107 return std::string();
108 return fieldData->_name;
111 // Want to be able to use that in std::map and std::set
112 bool operator<(const PathElement& pathElement) const
113 { return _data->less(pathElement._data.get()); }
114 bool operator==(const PathElement& pathElement) const
115 { return _data->equal(pathElement._data.get()); }
117 void append(std::string& s) const
118 { _data->append(s); }
123 struct Data : public SGReferenced {
125 virtual const FieldData* toFieldData() const;
126 virtual const IndexData* toIndexData() const;
127 virtual bool less(const Data*) const = 0;
128 virtual bool equal(const Data*) const = 0;
129 virtual void append(std::string&) const = 0;
131 struct FieldData : public Data {
132 FieldData(const std::string& name);
133 virtual ~FieldData();
134 virtual const FieldData* toFieldData() const;
135 virtual bool less(const Data* data) const;
136 virtual bool equal(const Data* data) const;
137 virtual void append(std::string& s) const;
140 struct IndexData : public Data {
141 IndexData(unsigned index);
142 virtual ~IndexData();
143 virtual const IndexData* toIndexData() const;
144 virtual bool less(const Data* data) const;
145 virtual bool equal(const Data* data) const;
146 virtual void append(std::string& s) const;
150 SGSharedPtr<Data> _data;
152 typedef std::list<PathElement> Path;
153 typedef std::pair<std::string, Path> AttributePathPair;
154 typedef std::pair<unsigned, Path> IndexPathPair;
156 static std::string toString(const Path& path);
157 static std::string toString(const AttributePathPair& path)
158 { return path.first + toString(path.second); }
159 static AttributePathPair toAttributePathPair(const std::string& s);
160 static Path toPath(const std::string& s)
161 { return toAttributePathPair(s).second; }
164 // SGSharedPtr<const TimeStamp> _timeStamp;
167 class HLADataElementProvider {
169 class AbstractProvider : public SGReferenced {
171 virtual ~AbstractProvider() { }
172 virtual HLADataElement* getDataElement(const HLADataElement::Path& path) = 0;
173 // virtual HLADataElement* getDataElement(const HLADataElement::Path& path, const HLADataType* dataType)
175 // SGSharedPtr<HLADataElement> dataElement = getDataElement(path);
176 // if (!dataElement.valid())
178 // if (!dataElement->setDataType(dataType))
180 // return dataElement.release();
184 HLADataElementProvider()
186 HLADataElementProvider(HLADataElement* dataElement) :
187 _provider(new ConcreteProvider(dataElement))
189 HLADataElementProvider(const SGSharedPtr<HLADataElement>& dataElement) :
190 _provider(new ConcreteProvider(dataElement))
192 HLADataElementProvider(AbstractProvider* provider) :
196 HLADataElement* getDataElement(const HLADataElement::Path& path) const
198 if (!_provider.valid())
200 return _provider->getDataElement(path);
204 class ConcreteProvider : public AbstractProvider {
206 ConcreteProvider(const SGSharedPtr<HLADataElement>& dataElement) :
207 _dataElement(dataElement)
209 virtual HLADataElement* getDataElement(const HLADataElement::Path&)
210 { return _dataElement.get(); }
212 SGSharedPtr<HLADataElement> _dataElement;
215 SGSharedPtr<AbstractProvider> _provider;
218 typedef std::map<HLADataElement::Path, HLADataElementProvider> HLAPathElementMap;
219 typedef std::map<unsigned, HLAPathElementMap> HLAAttributePathElementMap;