]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAObjectInstance.hxx
hla: Rework toplevel HLA classes.
[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     { return _name; }
44
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; }
49
50     /// Return the number of attributes
51     unsigned getNumAttributes() const;
52
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;
57
58     /// Return true if the attribute with the given index is owned by this federate
59     bool getAttributeOwned(unsigned index) const;
60
61     /// Return the data type of the attribute with the given index
62     const HLADataType* getAttributeDataType(unsigned index) const;
63
64     /// Return the data element of the attribute with the given index
65     HLADataElement* getAttributeDataElement(unsigned index);
66     const HLADataElement* getAttributeDataElement(unsigned index) const;
67
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;
70
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);
76
77     void registerInstance();
78     void deleteInstance(const RTIData& tag);
79
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);
87
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);
91
92     class UpdateCallback : public SGReferenced {
93     public:
94         virtual ~UpdateCallback();
95
96         virtual void updateAttributeValues(HLAObjectInstance&, const RTIData&) = 0;
97         virtual void updateAttributeValues(HLAObjectInstance&, const SGTimeStamp&, const RTIData&) = 0;
98     };
99
100     void setUpdateCallback(const SGSharedPtr<UpdateCallback>& updateCallback)
101     { _updateCallback = updateCallback; }
102     const SGSharedPtr<UpdateCallback>& getUpdateCallback() const
103     { return _updateCallback; }
104
105
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);
112
113     class ReflectCallback : public SGReferenced {
114     public:
115         virtual ~ReflectCallback();
116
117         virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const RTIData&) = 0;
118         virtual void reflectAttributeValues(HLAObjectInstance&, const HLAIndexList&, const SGTimeStamp&, const RTIData&) = 0;
119     };
120
121     void setReflectCallback(const SGSharedPtr<ReflectCallback>& reflectCallback)
122     { _reflectCallback = reflectCallback; }
123     const SGSharedPtr<ReflectCallback>& getReflectCallback() const
124     { return _reflectCallback; }
125
126     // deprecated.
127     class AttributeCallback : public SGReferenced {
128     public:
129         virtual ~AttributeCallback() {}
130         virtual void updateAttributeValues(HLAObjectInstance& objectInstance, const RTIData& tag)
131         { }
132         virtual void reflectAttributeValues(HLAObjectInstance& objectInstance,
133                                             const RTIIndexDataPairList& dataPairList, const RTIData& tag)
134         { }
135         virtual void reflectAttributeValues(HLAObjectInstance& objectInstance, const RTIIndexDataPairList& dataPairList,
136                                             const SGTimeStamp& timeStamp, const RTIData& tag)
137         { reflectAttributeValues(objectInstance, dataPairList, tag); }
138     };
139
140     void setAttributeCallback(const SGSharedPtr<AttributeCallback>& attributeCallback)
141     { _attributeCallback = attributeCallback; }
142     const SGSharedPtr<AttributeCallback>& getAttributeCallback() const
143     { return _attributeCallback; }
144
145 private:
146     void _setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance);
147     void _clearRTIObjectInstance();
148
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);
153
154     class DataElementFactoryVisitor;
155
156     struct Attribute {
157         Attribute() : _enabledUpdate(false), _unconditionalUpdate(false) {}
158         SGSharedPtr<HLADataElement> _dataElement;
159         // SGSharedPtr<HLADataElement::TimeStamp> _timeStamp;
160         bool _enabledUpdate;
161         bool _unconditionalUpdate;
162         // HLAIndexList::iterator _unconditionalUpdateAttributeIndexListIterator;
163         // HLAIndexList::iterator _conditionalUpdateAttributeIndexListIterator;
164     };
165     typedef std::vector<Attribute> AttributeVector;
166
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;
170
171     /// The parent Federate
172     SGWeakPtr<HLAFederate> _federate;
173
174     /// The ObjectClass
175     SGSharedPtr<HLAObjectClass> _objectClass;
176
177     /// The name as known in the RTI
178     std::string _name;
179
180     // /// The name as given by the local created instance
181     // std::string _givenName;
182
183     /// The underlying rti dispatcher class
184     SGSharedPtr<RTIObjectInstance> _rtiObjectInstance;
185
186     /// The attribute data
187     AttributeVector _attributeVector;
188
189     // Callback classes
190     SGSharedPtr<UpdateCallback> _updateCallback;
191     SGSharedPtr<ReflectCallback> _reflectCallback;
192     SGSharedPtr<AttributeCallback> _attributeCallback;
193
194     friend class HLAFederate;
195     friend class HLAObjectClass;
196     friend class RTIObjectInstance;
197 };
198
199 } // namespace simgear
200
201 #endif