]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAObjectInstance.hxx
d0f47b083e84ee2d041c1fb60f5fd65c945cfc3b
[simgear.git] / simgear / hla / HLAObjectInstance.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 HLAObjectInstance_hxx
19 #define HLAObjectInstance_hxx
20
21 #include <vector>
22
23 #include <simgear/structure/SGWeakPtr.hxx>
24
25 #include "HLADataElement.hxx"
26 #include "HLATypes.hxx"
27
28 class SGTimeStamp;
29
30 namespace simgear {
31
32 class RTIObjectInstance;
33 class HLAFederate;
34 class HLAObjectClass;
35
36 class HLAObjectInstance : public SGWeakReferenced {
37 public:
38     HLAObjectInstance(HLAObjectClass* objectClass);
39     virtual ~HLAObjectInstance();
40
41     /// Return the name of this object instance
42     const std::string& getName() const;
43
44     /// return the federate this instance belongs to
45     const SGWeakPtr<HLAFederate>& getFederate() const;
46
47     /// Return the object class of this instance.
48     /// Should always return a valid object class.
49     const SGSharedPtr<HLAObjectClass>& getObjectClass() const;
50
51     /// Return the number of attributes
52     unsigned getNumAttributes() const;
53
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;
58
59     /// Return true if the attribute with the given index is owned by
60     /// this federate
61     bool getAttributeOwned(unsigned index) const;
62
63     /// Return the data type of the attribute with the given index
64     const HLADataType* getAttributeDataType(unsigned index) const;
65
66     /// Return the data element of the attribute with the given index
67     HLADataElement* getAttributeDataElement(unsigned index);
68     const HLADataElement* getAttributeDataElement(unsigned index) const;
69
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;
73
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);
79
80     void registerInstance();
81     void deleteInstance(const RTIData& tag);
82
83     // Push the current values into the RTI
84     virtual void updateAttributeValues(const RTIData& tag);
85     virtual void updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
86     // encode periodic and dirty attribute values for the next sendAttributeValues
87     void encodeAttributeValues();
88     // encode the attribute value at index i for the next sendAttributeValues
89     void encodeAttributeValue(unsigned index);
90
91     // Really sends the prepared attribute update values into the RTI
92     void sendAttributeValues(const RTIData& tag);
93     void sendAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag);
94
95     class UpdateCallback : public SGReferenced {
96     public:
97         virtual ~UpdateCallback();
98
99         virtual void updateAttributeValues(HLAObjectInstance&, const RTIData&) = 0;
100         virtual void updateAttributeValues(HLAObjectInstance&, const SGTimeStamp&, const RTIData&) = 0;
101     };
102
103     void setUpdateCallback(const SGSharedPtr<UpdateCallback>& updateCallback)
104     { _updateCallback = updateCallback; }
105     const SGSharedPtr<UpdateCallback>& getUpdateCallback() const
106     { return _updateCallback; }
107
108
109     // Reflects the indices given in the index vector into the attributes HLADataElements.
110     virtual void reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
111     virtual void reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
112     // Reflect a single attribute value at the given index into the attributes HLADataELement.
113     virtual void reflectAttributeValue(unsigned index, const RTIData& tag);
114     virtual void reflectAttributeValue(unsigned index, const SGTimeStamp& timeStamp, const RTIData& tag);
115
116     class ReflectCallback : public SGReferenced {
117     public:
118         virtual ~ReflectCallback();
119
120         virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const RTIData&) = 0;
121         virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const SGTimeStamp&, const RTIData&) = 0;
122     };
123
124     void setReflectCallback(const SGSharedPtr<ReflectCallback>& reflectCallback)
125     { _reflectCallback = reflectCallback; }
126     const SGSharedPtr<ReflectCallback>& getReflectCallback() const
127     { return _reflectCallback; }
128
129     // deprecated.
130     class AttributeCallback : public SGReferenced {
131     public:
132         virtual ~AttributeCallback() {}
133         virtual void updateAttributeValues(HLAObjectInstance& objectInstance, const RTIData& tag)
134         { }
135         virtual void reflectAttributeValues(HLAObjectInstance& objectInstance,
136                                             const RTIIndexDataPairList& dataPairList, const RTIData& tag)
137         { }
138         virtual void reflectAttributeValues(HLAObjectInstance& objectInstance, const RTIIndexDataPairList& dataPairList,
139                                             const SGTimeStamp& timeStamp, const RTIData& tag)
140         { reflectAttributeValues(objectInstance, dataPairList, tag); }
141     };
142
143     void setAttributeCallback(const SGSharedPtr<AttributeCallback>& attributeCallback)
144     { _attributeCallback = attributeCallback; }
145     const SGSharedPtr<AttributeCallback>& getAttributeCallback() const
146     { return _attributeCallback; }
147
148 private:
149     void _setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance);
150     void _clearRTIObjectInstance();
151
152     // The callback entry points from the RTI interface classes.
153     void _removeInstance(const RTIData& tag);
154     void _reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag);
155     void _reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag);
156
157     class DataElementFactoryVisitor;
158
159     struct Attribute {
160         Attribute() : _enabledUpdate(false), _unconditionalUpdate(false) {}
161         SGSharedPtr<HLADataElement> _dataElement;
162         // SGSharedPtr<HLADataElement::TimeStamp> _timeStamp;
163         bool _enabledUpdate;
164         bool _unconditionalUpdate;
165         // HLAIndexList::iterator _unconditionalUpdateAttributeIndexListIterator;
166         // HLAIndexList::iterator _conditionalUpdateAttributeIndexListIterator;
167     };
168     typedef std::vector<Attribute> AttributeVector;
169
170     // At some time we want these: Until then, use the _enabledUpdate and _unconditionalUpdate flags in the Attribute struct.
171     // HLAIndexList _unconditionalUpdateAttributeIndexList;
172     // HLAIndexList _conditionalUpdateAttributeIndexList;
173
174     /// The parent Federate
175     SGWeakPtr<HLAFederate> _federate;
176
177     /// The ObjectClass
178     SGSharedPtr<HLAObjectClass> _objectClass;
179
180     /// The name as known in the RTI
181     std::string _name;
182
183     // /// The name as given by the local created instance
184     // std::string _givenName;
185
186     /// The underlying rti dispatcher class
187     SGSharedPtr<RTIObjectInstance> _rtiObjectInstance;
188
189     /// The attribute data
190     AttributeVector _attributeVector;
191
192     // Callback classes
193     SGSharedPtr<UpdateCallback> _updateCallback;
194     SGSharedPtr<ReflectCallback> _reflectCallback;
195     SGSharedPtr<AttributeCallback> _attributeCallback;
196
197     friend class HLAFederate;
198     friend class HLAObjectClass;
199     friend class RTIObjectInstance;
200 };
201
202 } // namespace simgear
203
204 #endif