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 HLAObjectClass_hxx
19 #define HLAObjectClass_hxx
24 #include "HLADataType.hxx"
25 #include "HLAObjectInstance.hxx"
26 #include "HLATypes.hxx"
33 class HLAObjectClass : public SGWeakReferenced {
35 HLAObjectClass(const std::string& name, HLAFederate* federate);
36 virtual ~HLAObjectClass();
38 /// Return the name of this object class
39 const std::string& getName() const;
41 /// return the federate this class belongs to
42 const SGWeakPtr<HLAFederate>& getFederate() const;
44 /// Return the number of attributes in this object class
45 unsigned getNumAttributes() const;
47 /// Adds a new attribute to this object class, return the index
48 unsigned addAttribute(const std::string& name);
50 /// Return the attribute index for the attribute with the given name
51 unsigned getAttributeIndex(const std::string& name) const;
52 /// Return the attribute name for the attribute with the given index
53 std::string getAttributeName(unsigned index) const;
55 /// Return the data type of the attribute with the given index
56 const HLADataType* getAttributeDataType(unsigned index) const;
57 /// Sets the data type of the attribute with the given index to dataType
58 void setAttributeDataType(unsigned index, const HLADataType* dataType);
60 /// Return the update type of the attribute with the given index
61 HLAUpdateType getAttributeUpdateType(unsigned index) const;
62 /// Sets the update type of the attribute with the given index to updateType
63 void setAttributeUpdateType(unsigned index, HLAUpdateType updateType);
65 /// Return the subscription type of the attribute with the given index
66 HLASubscriptionType getAttributeSubscriptionType(unsigned index) const;
67 /// Sets the subscription type of the attribute with the given index to subscriptionType
68 void setAttributeSubscriptionType(unsigned index, HLASubscriptionType subscriptionType);
70 /// Return the publication type of the attribute with the given index
71 HLAPublicationType getAttributePublicationType(unsigned index) const;
72 /// Sets the publication type of the attribute with the given index to publicationType
73 void setAttributePublicationType(unsigned index, HLAPublicationType publicationType);
75 /// Return the index, path pair for the given string path pair
76 HLADataElement::IndexPathPair getIndexPathPair(const HLADataElement::StringPathPair&) const;
77 /// Return the index, path pair for the given string path
78 HLADataElement::IndexPathPair getIndexPathPair(const std::string& path) const;
80 /// Get the attribute data element index for the given path, return true if successful
81 bool getAttributeIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const;
83 virtual bool subscribe();
84 virtual bool unsubscribe();
86 virtual bool publish();
87 virtual bool unpublish();
89 // Object instance creation and destruction
90 class InstanceCallback : public SGReferenced {
92 virtual ~InstanceCallback();
94 virtual void discoverInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
95 virtual void removeInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
97 virtual void registerInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
98 virtual void deleteInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
101 void setInstanceCallback(const SGSharedPtr<InstanceCallback>& instanceCallback)
102 { _instanceCallback = instanceCallback; }
103 const SGSharedPtr<InstanceCallback>& getInstanceCallback() const
104 { return _instanceCallback; }
106 virtual void discoverInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
107 virtual void removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
108 virtual void registerInstance(HLAObjectInstance& objectInstance);
109 virtual void deleteInstance(HLAObjectInstance& objectInstance);
111 // Is called by the default registration callback if installed
112 // Should register the already known object instances of this class.
113 virtual void startRegistration() const;
114 virtual void stopRegistration() const;
116 // Handles startRegistrationForObjectClass and stopRegistrationForObjectClass events
117 class RegistrationCallback : public SGReferenced {
119 virtual ~RegistrationCallback();
120 virtual void startRegistration(HLAObjectClass& objectClass) = 0;
121 virtual void stopRegistration(HLAObjectClass& objectClass) = 0;
124 void setRegistrationCallback(const SGSharedPtr<RegistrationCallback>& registrationCallback)
125 { _registrationCallback = registrationCallback; }
126 const SGSharedPtr<RegistrationCallback>& getRegistrationCallback() const
127 { return _registrationCallback; }
129 /// Create a new instance of this class.
130 virtual HLAObjectInstance* createObjectInstance(const std::string& name);
132 virtual void createAttributeDataElements(HLAObjectInstance& objectInstance);
133 virtual HLADataElement* createAttributeDataElement(HLAObjectInstance& objectInstance, unsigned index);
136 HLAObjectClass(const HLAObjectClass&);
137 HLAObjectClass& operator=(const HLAObjectClass&);
139 void _setRTIObjectClass(RTIObjectClass* objectClass);
140 void _resolveAttributeIndex(const std::string& name, unsigned index);
141 void _clearRTIObjectClass();
143 // The internal entry points from the RTILObjectClass callback functions
144 void _discoverInstance(RTIObjectInstance* objectInstance, const RTIData& tag);
145 void _removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
146 void _registerInstance(HLAObjectInstance* objectInstance);
147 void _deleteInstance(HLAObjectInstance& objectInstance);
149 void _startRegistration();
150 void _stopRegistration();
152 friend class HLAObjectInstance;
153 friend class RTIObjectClass;
156 Attribute() : _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
157 Attribute(const std::string& name) : _name(name), _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
159 SGSharedPtr<const HLADataType> _dataType;
160 HLASubscriptionType _subscriptionType;
161 HLAPublicationType _publicationType;
162 HLAUpdateType _updateType;
164 typedef std::vector<Attribute> AttributeVector;
165 typedef std::map<std::string,unsigned> NameIndexMap;
167 /// The parent federate.
168 SGWeakPtr<HLAFederate> _federate;
170 /// The object class name
173 /// The underlying rti dispatcher class
174 SGSharedPtr<RTIObjectClass> _rtiObjectClass;
176 /// The attribute data
177 AttributeVector _attributeVector;
178 /// The mapping from attribute names to attribute indices
179 NameIndexMap _nameIndexMap;
182 SGSharedPtr<InstanceCallback> _instanceCallback;
183 SGSharedPtr<RegistrationCallback> _registrationCallback;
185 friend class HLAFederate;
188 } // namespace simgear