1 // Copyright (C) 2009 - 2012 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 HLAObjectInstance_hxx
19 #define HLAObjectInstance_hxx
23 #include <simgear/structure/SGWeakPtr.hxx>
25 #include "HLADataElement.hxx"
26 #include "HLATypes.hxx"
32 class RTIObjectInstance;
36 class HLAObjectInstance : public SGWeakReferenced {
38 HLAObjectInstance(HLAObjectClass* objectClass);
39 virtual ~HLAObjectInstance();
41 /// Return the name of this object instance
42 const std::string& getName() const
45 /// Return the object class of this instance.
46 /// Should always return a valid object class.
47 const SGSharedPtr<HLAObjectClass>& getObjectClass() const
48 { return _objectClass; }
50 /// Return the number of attributes
51 unsigned getNumAttributes() const;
53 /// Return the attribute index for the attribute with the given name
54 unsigned getAttributeIndex(const std::string& name) const;
55 /// Return the attribute name for the attribute with the given index
56 std::string getAttributeName(unsigned index) const;
58 /// Return true if the attribute with the given index is owned by this federate
59 bool getAttributeOwned(unsigned index) const;
61 /// Return the data type of the attribute with the given index
62 const HLADataType* getAttributeDataType(unsigned index) const;
64 /// Return the data element of the attribute with the given index
65 HLADataElement* getAttributeDataElement(unsigned index);
66 const HLADataElement* getAttributeDataElement(unsigned index) const;
68 /// Write the raw attribute data value into data, works only of the object is backed up with an rti object instance
69 bool getAttributeData(unsigned index, RTIData& data) const;
71 /// Sets the data element of the attribute with the given index to dataElement
72 void setAttributeDataElement(unsigned index, const SGSharedPtr<HLADataElement>& dataElement);
73 /// Sets the data element of the attribute with the given index to the content of pathElementMap
74 void setAttribute(unsigned index, const HLAPathElementMap& pathElementMap);
75 void setAttributes(const HLAAttributePathElementMap& attributePathElementMap);
77 void registerInstance();
78 void deleteInstance(const RTIData& tag);
80 // Push the current values into the RTI
81 virtual void updateAttributeValues(const RTIData& tag);
82 virtual void updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
83 // encode periodic and dirty attribute values for the next sendAttributeValues
84 void encodeAttributeValues();
85 // encode the attribute value at index i for the next sendAttributeValues
86 void encodeAttributeValue(unsigned index);
88 // Really sends the prepared attribute update values into the RTI
89 void sendAttributeValues(const RTIData& tag);
90 void sendAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
92 class UpdateCallback : public SGReferenced {
94 virtual ~UpdateCallback();
96 virtual void updateAttributeValues(HLAObjectInstance&, const RTIData&) = 0;
97 virtual void updateAttributeValues(HLAObjectInstance&, const SGTimeStamp&, const RTIData&) = 0;
100 void setUpdateCallback(const SGSharedPtr<UpdateCallback>& updateCallback)
101 { _updateCallback = updateCallback; }
102 const SGSharedPtr<UpdateCallback>& getUpdateCallback() const
103 { return _updateCallback; }
106 // Reflects the indices given in the index vector into the attributes HLADataElements.
107 virtual void reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
108 virtual void reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
109 // Reflect a single attribute value at the given index into the attributes HLADataELement.
110 virtual void reflectAttributeValue(unsigned index, const RTIData& tag);
111 virtual void reflectAttributeValue(unsigned index, const SGTimeStamp& timeStamp, const RTIData& tag);
113 class ReflectCallback : public SGReferenced {
115 virtual ~ReflectCallback();
117 virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const RTIData&) = 0;
118 virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const SGTimeStamp&, const RTIData&) = 0;
121 void setReflectCallback(const SGSharedPtr<ReflectCallback>& reflectCallback)
122 { _reflectCallback = reflectCallback; }
123 const SGSharedPtr<ReflectCallback>& getReflectCallback() const
124 { return _reflectCallback; }
127 class AttributeCallback : public SGReferenced {
129 virtual ~AttributeCallback() {}
130 virtual void updateAttributeValues(HLAObjectInstance& objectInstance, const RTIData& tag)
132 virtual void reflectAttributeValues(HLAObjectInstance& objectInstance,
133 const RTIIndexDataPairList& dataPairList, const RTIData& tag)
135 virtual void reflectAttributeValues(HLAObjectInstance& objectInstance, const RTIIndexDataPairList& dataPairList,
136 const SGTimeStamp& timeStamp, const RTIData& tag)
137 { reflectAttributeValues(objectInstance, dataPairList, tag); }
140 void setAttributeCallback(const SGSharedPtr<AttributeCallback>& attributeCallback)
141 { _attributeCallback = attributeCallback; }
142 const SGSharedPtr<AttributeCallback>& getAttributeCallback() const
143 { return _attributeCallback; }
146 void _setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance);
147 void _clearRTIObjectInstance();
149 // The callback entry points from the RTI interface classes.
150 void _removeInstance(const RTIData& tag);
151 void _reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
152 void _reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
154 class DataElementFactoryVisitor;
157 Attribute() : _enabledUpdate(false), _unconditionalUpdate(false) {}
158 SGSharedPtr<HLADataElement> _dataElement;
159 // SGSharedPtr<HLADataElement::TimeStamp> _timeStamp;
161 bool _unconditionalUpdate;
162 // HLAIndexList::iterator _unconditionalUpdateAttributeIndexListIterator;
163 // HLAIndexList::iterator _conditionalUpdateAttributeIndexListIterator;
165 typedef std::vector<Attribute> AttributeVector;
167 // At some time we want these: Until then, use the _enabledUpdate and _unconditionalUpdate flags in the Attribute struct.
168 // HLAIndexList _unconditionalUpdateAttributeIndexList;
169 // HLAIndexList _conditionalUpdateAttributeIndexList;
171 /// The parent Federate
172 SGWeakPtr<HLAFederate> _federate;
175 SGSharedPtr<HLAObjectClass> _objectClass;
177 /// The name as known in the RTI
180 // /// The name as given by the local created instance
181 // std::string _givenName;
183 /// The underlying rti dispatcher class
184 SGSharedPtr<RTIObjectInstance> _rtiObjectInstance;
186 /// The attribute data
187 AttributeVector _attributeVector;
190 SGSharedPtr<UpdateCallback> _updateCallback;
191 SGSharedPtr<ReflectCallback> _reflectCallback;
192 SGSharedPtr<AttributeCallback> _attributeCallback;
194 friend class HLAFederate;
195 friend class HLAObjectClass;
196 friend class RTIObjectInstance;
199 } // namespace simgear