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 = 0);
39 virtual ~HLAObjectInstance();
41 /// Return the name of this object instance
42 const std::string& getName() const;
44 /// return the federate this instance belongs to
45 const SGWeakPtr<HLAFederate>& getFederate() const;
47 /// Return the object class of this instance.
48 /// Should always return a valid object class.
49 const SGSharedPtr<HLAObjectClass>& getObjectClass() const;
51 /// Return the number of attributes
52 unsigned getNumAttributes() const;
54 /// Return the attribute index for the attribute with the given name
55 unsigned getAttributeIndex(const std::string& name) const;
56 /// Return the attribute name for the attribute with the given index
57 std::string getAttributeName(unsigned index) const;
59 /// Return true if the attribute with the given index is owned by
61 bool getAttributeOwned(unsigned index) const;
63 /// Return the data type of the attribute with the given index
64 const HLADataType* getAttributeDataType(unsigned index) const;
66 /// Return the data element of the attribute with the given index
67 HLADataElement* getAttributeDataElement(unsigned index);
68 const HLADataElement* getAttributeDataElement(unsigned index) const;
70 /// Write the raw attribute data value into data, works only of the object
71 /// is backed up with an rti object instance
72 bool getAttributeData(unsigned index, RTIData& data) const;
74 /// Sets the data element of the attribute with the given index to dataElement
75 void setAttributeDataElement(unsigned index, const SGSharedPtr<HLADataElement>& dataElement);
76 /// Sets the data element of the attribute with the given index to the content of pathElementMap
77 void setAttribute(unsigned index, const HLAPathElementMap& pathElementMap);
78 void setAttributes(const HLAAttributePathElementMap& attributePathElementMap);
80 /// Retrieve the data element index for the given path.
81 bool getDataElementIndex(HLADataElementIndex& index, const std::string& path) const;
82 HLADataElementIndex getDataElementIndex(const std::string& path) const;
84 /// Return the data element of the attribute with the given index
85 HLADataElement* getAttributeDataElement(const HLADataElementIndex& index);
86 const HLADataElement* getAttributeDataElement(const HLADataElementIndex& index) const;
87 /// Set the data element of the attribute with the given index
88 void setAttributeDataElement(const HLADataElementIndex& index, const SGSharedPtr<HLADataElement>& dataElement);
90 /// Return the data element of the attribute with the given path.
91 /// The method is only for convenience as parsing the path is expensive.
92 /// Precompute the index and use the index instead if you use this method more often.
93 HLADataElement* getAttributeDataElement(const std::string& path);
94 const HLADataElement* getAttributeDataElement(const std::string& path) const;
95 /// Set the data element of the attribute with the given path
96 /// The method is only for convenience as parsing the path is expensive.
97 /// Precompute the index and use the index instead if you use this method more often.
98 void setAttributeDataElement(const std::string& path, const SGSharedPtr<HLADataElement>& dataElement);
100 /// Gets called on discovering this object instance.
101 virtual void discoverInstance(const RTIData& tag);
102 /// Gets called on remove this object instance.
103 virtual void removeInstance(const RTIData& tag);
105 /// Call this to register the object instance at the rti and assign the object class to it.
106 void registerInstance();
107 virtual void registerInstance(HLAObjectClass* objectClass);
108 /// Call this to delete the object instance from the rti.
109 virtual void deleteInstance(const RTIData& tag);
111 /// Is called when the instance is either registered or discovered.
112 /// It creates data elements for each element that is not yet set and that has a data type attached.
113 /// the default calls back into the object class createAttributeDataElements method.
114 virtual void createAttributeDataElements();
115 /// Create and set the data element with index. Called somewhere in the above callchain.
116 void createAndSetAttributeDataElement(unsigned index);
117 /// Create an individual data element, the default calls back into the object class
118 /// createAttributeDataElement method.
119 virtual HLADataElement* createAttributeDataElement(unsigned index);
121 // Push the current values into the RTI
122 virtual void updateAttributeValues(const RTIData& tag);
123 virtual void updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
124 // encode periodic and dirty attribute values for the next sendAttributeValues
125 void encodeAttributeValues();
126 // encode the attribute value at index i for the next sendAttributeValues
127 void encodeAttributeValue(unsigned index);
129 // Really sends the prepared attribute update values into the RTI
130 void sendAttributeValues(const RTIData& tag);
131 void sendAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
133 class UpdateCallback : public SGReferenced {
135 virtual ~UpdateCallback();
137 virtual void updateAttributeValues(HLAObjectInstance&, const RTIData&) = 0;
138 virtual void updateAttributeValues(HLAObjectInstance&, const SGTimeStamp&, const RTIData&) = 0;
141 void setUpdateCallback(const SGSharedPtr<UpdateCallback>& updateCallback)
142 { _updateCallback = updateCallback; }
143 const SGSharedPtr<UpdateCallback>& getUpdateCallback() const
144 { return _updateCallback; }
147 // Reflects the indices given in the index vector into the attributes HLADataElements.
148 virtual void reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
149 virtual void reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
150 // Reflect a single attribute value at the given index into the attributes HLADataELement.
151 virtual void reflectAttributeValue(unsigned index, const RTIData& tag);
152 virtual void reflectAttributeValue(unsigned index, const SGTimeStamp& timeStamp, const RTIData& tag);
154 class ReflectCallback : public SGReferenced {
156 virtual ~ReflectCallback();
158 virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const RTIData&) = 0;
159 virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const SGTimeStamp&, const RTIData&) = 0;
162 void setReflectCallback(const SGSharedPtr<ReflectCallback>& reflectCallback)
163 { _reflectCallback = reflectCallback; }
164 const SGSharedPtr<ReflectCallback>& getReflectCallback() const
165 { return _reflectCallback; }
168 void _setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance);
169 void _clearRTIObjectInstance();
171 // The callback entry points from the RTI interface classes.
172 void _removeInstance(const RTIData& tag);
173 void _reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
174 void _reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
176 class DataElementFactoryVisitor;
179 Attribute() : _enabledUpdate(false), _unconditionalUpdate(false) {}
180 SGSharedPtr<HLADataElement> _dataElement;
182 bool _unconditionalUpdate;
183 // HLAIndexList::iterator _unconditionalUpdateAttributeIndexListIterator;
184 // HLAIndexList::iterator _conditionalUpdateAttributeIndexListIterator;
186 typedef std::vector<Attribute> AttributeVector;
188 // At some time we want these: Until then, use the _enabledUpdate and _unconditionalUpdate flags in the Attribute struct.
189 // HLAIndexList _unconditionalUpdateAttributeIndexList;
190 // HLAIndexList _conditionalUpdateAttributeIndexList;
192 /// The parent Federate
193 SGWeakPtr<HLAFederate> _federate;
196 SGSharedPtr<HLAObjectClass> _objectClass;
198 /// The name as known in the RTI
201 // /// The name as given by the local created instance
202 // std::string _givenName;
204 /// The underlying rti dispatcher class
205 SGSharedPtr<RTIObjectInstance> _rtiObjectInstance;
207 /// The attribute data
208 AttributeVector _attributeVector;
211 SGSharedPtr<UpdateCallback> _updateCallback;
212 SGSharedPtr<ReflectCallback> _reflectCallback;
214 friend class HLAFederate;
215 friend class HLAObjectClass;
216 friend class RTIObjectInstance;
219 } // namespace simgear