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 HLAObjectClass_hxx
19 #define HLAObjectClass_hxx
20 
21 #include <string>
22 #include <vector>
23 
24 #include "HLADataType.hxx"
25 #include "HLAObjectInstance.hxx"
26 #include "HLATypes.hxx"
27 
28 namespace simgear {
29 
30 class RTIObjectClass;
31 class HLAFederate;
32 
33 class HLAObjectClass : public SGWeakReferenced {
34 public:
35     HLAObjectClass(const std::string& name, HLAFederate* federate);
36     virtual ~HLAObjectClass();
37 
38     /// Return the name of this object class
39     const std::string& getName() const;
40 
41     /// return the federate this class belongs to
42     const SGWeakPtr<HLAFederate>& getFederate() const;
43 
44     /// Return the number of attributes in this object class
45     unsigned getNumAttributes() const;
46 
47     /// Adds a new attribute to this object class, return the index
48     unsigned addAttribute(const std::string& name);
49 
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;
54 
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);
59 
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);
64 
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);
69 
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);
74 
75     /// Get the attribute data element index for the given path, return true if successful
76     bool getDataElementIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const;
77     HLADataElementIndex getDataElementIndex(const std::string& path) const;
78 
79     virtual bool subscribe();
80     virtual bool unsubscribe();
81 
82     virtual bool publish();
83     virtual bool unpublish();
84 
85     // Object instance creation and destruction
86     class InstanceCallback : public SGReferenced {
87     public:
88         virtual ~InstanceCallback();
89 
90         virtual void discoverInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
91         virtual void removeInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
92 
93         virtual void registerInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
94         virtual void deleteInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
95     };
96 
setInstanceCallback(const SGSharedPtr<InstanceCallback> & instanceCallback)97     void setInstanceCallback(const SGSharedPtr<InstanceCallback>& instanceCallback)
98     { _instanceCallback = instanceCallback; }
getInstanceCallback() const99     const SGSharedPtr<InstanceCallback>& getInstanceCallback() const
100     { return _instanceCallback; }
101 
102     virtual void discoverInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
103     virtual void removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
104     virtual void registerInstance(HLAObjectInstance& objectInstance);
105     virtual void deleteInstance(HLAObjectInstance& objectInstance);
106 
107     // Is called by the default registration callback if installed
108     // Should register the already known object instances of this class.
109     virtual void startRegistration() const;
110     virtual void stopRegistration() const;
111 
112     // Handles startRegistrationForObjectClass and stopRegistrationForObjectClass events
113     class RegistrationCallback : public SGReferenced {
114     public:
115         virtual ~RegistrationCallback();
116         virtual void startRegistration(HLAObjectClass& objectClass) = 0;
117         virtual void stopRegistration(HLAObjectClass& objectClass) = 0;
118     };
119 
setRegistrationCallback(const SGSharedPtr<RegistrationCallback> & registrationCallback)120     void setRegistrationCallback(const SGSharedPtr<RegistrationCallback>& registrationCallback)
121     { _registrationCallback = registrationCallback; }
getRegistrationCallback() const122     const SGSharedPtr<RegistrationCallback>& getRegistrationCallback() const
123     { return _registrationCallback; }
124 
125     /// Create a new instance of this class.
126     virtual HLAObjectInstance* createObjectInstance(const std::string& name);
127 
128     virtual void createAttributeDataElements(HLAObjectInstance& objectInstance);
129     virtual HLADataElement* createAttributeDataElement(HLAObjectInstance& objectInstance, unsigned index);
130 
131 private:
132     HLAObjectClass(const HLAObjectClass&);
133     HLAObjectClass& operator=(const HLAObjectClass&);
134 
135     void _setRTIObjectClass(RTIObjectClass* objectClass);
136     void _resolveAttributeIndex(const std::string& name, unsigned index);
137     void _clearRTIObjectClass();
138 
139     // The internal entry points from the RTILObjectClass callback functions
140     void _discoverInstance(RTIObjectInstance* objectInstance, const RTIData& tag);
141     void _removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
142     void _registerInstance(HLAObjectInstance* objectInstance);
143     void _deleteInstance(HLAObjectInstance& objectInstance);
144 
145     void _startRegistration();
146     void _stopRegistration();
147 
148     friend class HLAObjectInstance;
149     friend class RTIObjectClass;
150 
151     struct Attribute {
Attributesimgear::HLAObjectClass::Attribute152         Attribute() : _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
Attributesimgear::HLAObjectClass::Attribute153         Attribute(const std::string& name) : _name(name), _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
154         std::string _name;
155         SGSharedPtr<const HLADataType> _dataType;
156         HLASubscriptionType _subscriptionType;
157         HLAPublicationType _publicationType;
158         HLAUpdateType _updateType;
159     };
160     typedef std::vector<Attribute> AttributeVector;
161     typedef std::map<std::string,unsigned> NameIndexMap;
162 
163     /// The parent federate.
164     SGWeakPtr<HLAFederate> _federate;
165 
166     /// The object class name
167     std::string _name;
168 
169     /// The underlying rti dispatcher class
170     SGSharedPtr<RTIObjectClass> _rtiObjectClass;
171 
172     /// The attribute data
173     AttributeVector _attributeVector;
174     /// The mapping from attribute names to attribute indices
175     NameIndexMap _nameIndexMap;
176 
177     // Callback classes
178     SGSharedPtr<InstanceCallback> _instanceCallback;
179     SGSharedPtr<RegistrationCallback> _registrationCallback;
180 
181     friend class HLAFederate;
182 };
183 
184 } // namespace simgear
185 
186 #endif
187