1 // Copyright (C) 2009 - 2010 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 HLADataElement : public SGReferenced {
35 virtual ~HLADataElement();
37 virtual bool encode(HLAEncodeStream& stream) const = 0;
38 virtual bool decode(HLADecodeStream& stream) = 0;
40 virtual const HLADataType* getDataType() const = 0;
41 virtual bool setDataType(const HLADataType* dataType) = 0;
43 // Container for the timestamp the originating attribute was last updated for
44 // class TimeStamp : public SGReferenced {
46 // const SGTimeStamp& getTimeStamp() const
47 // { return _timeStamp; }
48 // void setTimeStamp(const SGTimeStamp& timeStamp)
49 // { _timeStamp = timeStamp; }
51 // SGTimeStamp _timeStamp;
54 // const TimeStamp* getTimeStamp() const
55 // { return _timeStamp.get(); }
56 // void setTimeStamp(const TimeStamp* timeStamp)
57 // { _timeStamp = timeStamp; }
59 // struct ChangeCount : public SGReferenced {
60 // ChangeCount() : _value(0) {}
63 // SGSharedPtr<ChangeCount> _changeCount;
64 // unsigned getChangeCount() const
66 // // If we don't have return allways the same
67 // if (!_changeCount.valid())
69 // return _changeCount->_value;
72 /// HLADataElements could be identified by path
73 /// These paths are composed of structure field names and array indices in the
74 /// order they appear while walking to the data element.
75 /// So provide here some tool functions to access these elements
76 /// Note that these functions are relatively expensive in execution time.
77 /// So only use them once at object creation time and store direct references to the values
81 PathElement(unsigned index) : _data(new IndexData(index)) {}
82 PathElement(const std::string& name) : _data(new FieldData(name)) {}
84 bool isFieldValue() const
85 { return _data->toFieldData(); }
86 bool isIndexValue() const
87 { return _data->toIndexData(); }
89 unsigned getIndexValue() const
91 const IndexData* indexData = _data->toIndexData();
94 return indexData->_index;
97 std::string getFieldValue() const
99 const FieldData* fieldData = _data->toFieldData();
101 return std::string();
102 return fieldData->_name;
105 // Want to be able to use that in std::map and std::set
106 bool operator<(const PathElement& pathElement) const
107 { return _data->less(pathElement._data.get()); }
108 bool operator==(const PathElement& pathElement) const
109 { return _data->equal(pathElement._data.get()); }
111 void append(std::string& s) const
112 { _data->append(s); }
117 struct Data : public SGReferenced {
119 virtual const FieldData* toFieldData() const;
120 virtual const IndexData* toIndexData() const;
121 virtual bool less(const Data*) const = 0;
122 virtual bool equal(const Data*) const = 0;
123 virtual void append(std::string&) const = 0;
125 struct FieldData : public Data {
126 FieldData(const std::string& name);
127 virtual ~FieldData();
128 virtual const FieldData* toFieldData() const;
129 virtual bool less(const Data* data) const;
130 virtual bool equal(const Data* data) const;
131 virtual void append(std::string& s) const;
134 struct IndexData : public Data {
135 IndexData(unsigned index);
136 virtual ~IndexData();
137 virtual const IndexData* toIndexData() const;
138 virtual bool less(const Data* data) const;
139 virtual bool equal(const Data* data) const;
140 virtual void append(std::string& s) const;
144 SGSharedPtr<Data> _data;
146 typedef std::list<PathElement> Path;
147 typedef std::pair<std::string, Path> AttributePathPair;
148 typedef std::pair<unsigned, Path> IndexPathPair;
150 static std::string toString(const Path& path);
151 static std::string toString(const AttributePathPair& path)
152 { return path.first + toString(path.second); }
153 static AttributePathPair toAttributePathPair(const std::string& s);
154 static Path toPath(const std::string& s)
155 { return toAttributePathPair(s).second; }
158 // SGSharedPtr<const TimeStamp> _timeStamp;
161 class HLADataElementProvider {
163 class AbstractProvider : public SGReferenced {
165 virtual ~AbstractProvider() { }
166 virtual HLADataElement* getDataElement(const HLADataElement::Path& path) = 0;
167 // virtual HLADataElement* getDataElement(const HLADataElement::Path& path, const HLADataType* dataType)
169 // SGSharedPtr<HLADataElement> dataElement = getDataElement(path);
170 // if (!dataElement.valid())
172 // if (!dataElement->setDataType(dataType))
174 // return dataElement.release();
178 HLADataElementProvider()
180 HLADataElementProvider(HLADataElement* dataElement) :
181 _provider(new ConcreteProvider(dataElement))
183 HLADataElementProvider(const SGSharedPtr<HLADataElement>& dataElement) :
184 _provider(new ConcreteProvider(dataElement))
186 HLADataElementProvider(AbstractProvider* provider) :
190 HLADataElement* getDataElement(const HLADataElement::Path& path) const
192 if (!_provider.valid())
194 return _provider->getDataElement(path);
198 class ConcreteProvider : public AbstractProvider {
200 ConcreteProvider(const SGSharedPtr<HLADataElement>& dataElement) :
201 _dataElement(dataElement)
203 virtual HLADataElement* getDataElement(const HLADataElement::Path&)
204 { return _dataElement.get(); }
206 SGSharedPtr<HLADataElement> _dataElement;
209 SGSharedPtr<AbstractProvider> _provider;
212 typedef std::map<HLADataElement::Path, HLADataElementProvider> HLAPathElementMap;
213 typedef std::map<unsigned, HLAPathElementMap> HLAAttributePathElementMap;