]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAObjectClass.hxx
math: Move lerp function into SGMisc.
[simgear.git] / simgear / hla / HLAObjectClass.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 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 number of attributes in this object class
42     unsigned getNumAttributes() const;
43
44     /// Adds a new attribute to this object class, return the index
45     unsigned addAttribute(const std::string& name);
46
47     /// Return the attribute index for the attribute with the given name
48     unsigned getAttributeIndex(const std::string& name) const;
49     /// Return the attribute name for the attribute with the given index
50     std::string getAttributeName(unsigned index) const;
51
52     /// Return the data type of the attribute with the given index
53     const HLADataType* getAttributeDataType(unsigned index) const;
54     /// Sets the data type of the attribute with the given index to dataType
55     void setAttributeDataType(unsigned index, const HLADataType* dataType);
56
57     /// Return the update type of the attribute with the given index
58     HLAUpdateType getAttributeUpdateType(unsigned index) const;
59     /// Sets the update type of the attribute with the given index to updateType
60     void setAttributeUpdateType(unsigned index, HLAUpdateType updateType);
61
62     /// Return the subscription type of the attribute with the given index
63     HLASubscriptionType getAttributeSubscriptionType(unsigned index) const;
64     /// Sets the subscription type of the attribute with the given index to subscriptionType
65     void setAttributeSubscriptionType(unsigned index, HLASubscriptionType subscriptionType);
66
67     /// Return the publication type of the attribute with the given index
68     HLAPublicationType getAttributePublicationType(unsigned index) const;
69     /// Sets the publication type of the attribute with the given index to publicationType
70     void setAttributePublicationType(unsigned index, HLAPublicationType publicationType);
71
72     /// Return the index, path pair for the given string path pair
73     HLADataElement::IndexPathPair getIndexPathPair(const HLADataElement::StringPathPair&) const;
74     /// Return the index, path pair for the given string path
75     HLADataElement::IndexPathPair getIndexPathPair(const std::string& path) const;
76
77     virtual bool subscribe();
78     virtual bool unsubscribe();
79
80     virtual bool publish();
81     virtual bool unpublish();
82
83     // Object instance creation and destruction
84     class InstanceCallback : public SGReferenced {
85     public:
86         virtual ~InstanceCallback();
87
88         virtual void discoverInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
89         virtual void removeInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance, const RTIData& tag);
90
91         virtual void registerInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
92         virtual void deleteInstance(const HLAObjectClass& objectClass, HLAObjectInstance& objectInstance);
93     };
94
95     void setInstanceCallback(const SGSharedPtr<InstanceCallback>& instanceCallback)
96     { _instanceCallback = instanceCallback; }
97     const SGSharedPtr<InstanceCallback>& getInstanceCallback() const
98     { return _instanceCallback; }
99
100     // Is called by the default registration callback if installed
101     // Should register the already known object instances of this class.
102     virtual void startRegistration() const;
103     virtual void stopRegistration() const;
104
105     // Handles startRegistrationForObjectClass and stopRegistrationForObjectClass events
106     class RegistrationCallback : public SGReferenced {
107     public:
108         virtual ~RegistrationCallback();
109         virtual void startRegistration(HLAObjectClass& objectClass) = 0;
110         virtual void stopRegistration(HLAObjectClass& objectClass) = 0;
111     };
112
113     void setRegistrationCallback(const SGSharedPtr<RegistrationCallback>& registrationCallback)
114     { _registrationCallback = registrationCallback; }
115     const SGSharedPtr<RegistrationCallback>& getRegistrationCallback() const
116     { return _registrationCallback; }
117
118     /// Create a new instance of this class.
119     virtual HLAObjectInstance* createObjectInstance(const std::string& name);
120     virtual HLAObjectInstance* createObjectInstance(); // deprecated
121
122 private:
123     HLAObjectClass(const HLAObjectClass&);
124     HLAObjectClass& operator=(const HLAObjectClass&);
125
126     void _setRTIObjectClass(RTIObjectClass* objectClass);
127     void _resolveAttributeIndex(const std::string& name, unsigned index);
128     void _clearRTIObjectClass();
129
130     // The internal entry points from the RTILObjectClass callback functions
131     void _discoverInstance(RTIObjectInstance* objectInstance, const RTIData& tag);
132     void _removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag);
133     void _registerInstance(HLAObjectInstance* objectInstance);
134     void _deleteInstance(HLAObjectInstance& objectInstance);
135
136     void _startRegistration();
137     void _stopRegistration();
138
139     friend class HLAObjectInstance;
140     friend class RTIObjectClass;
141
142     struct Attribute {
143         Attribute() : _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
144         Attribute(const std::string& name) : _name(name), _subscriptionType(HLAUnsubscribed), _publicationType(HLAUnpublished), _updateType(HLAUndefinedUpdate) {}
145         std::string _name;
146         SGSharedPtr<const HLADataType> _dataType;
147         HLASubscriptionType _subscriptionType;
148         HLAPublicationType _publicationType;
149         HLAUpdateType _updateType;
150     };
151     typedef std::vector<Attribute> AttributeVector;
152     typedef std::map<std::string,unsigned> NameIndexMap;
153
154     /// The parent federate.
155     SGWeakPtr<HLAFederate> _federate;
156
157     /// The object class name
158     std::string _name;
159
160     /// The underlying rti dispatcher class
161     SGSharedPtr<RTIObjectClass> _rtiObjectClass;
162
163     /// The attribute data
164     AttributeVector _attributeVector;
165     /// The mapping from attribute names to attribute indices
166     NameIndexMap _nameIndexMap;
167
168     // Callback classes
169     SGSharedPtr<InstanceCallback> _instanceCallback;
170     SGSharedPtr<RegistrationCallback> _registrationCallback;
171
172     friend class HLAFederate;
173 };
174
175 } // namespace simgear
176
177 #endif