VR-Forces 5.0.1 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
registerExtendedAttributeClassesHLA.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2017 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
9 
10 #pragma once
11 
12 #ifdef DtHLA
13 
18 #include <vl/stateEncoderFactory.h>
19 
20 #define DtALMOST(x, y, e) (((x)<=((y)+(e))) && ((x)>=((y)-(e))))
21 
22 namespace makVrfVrlinkExtToolkit
23 {
26  template <typename StateRepType>
27  void setAttributeNameToHandleMap(DtObjClassDesc* desc)
28  {
29  std::map<DtString, RTI::AttributeHandle> attrNamesToHandles;
30 
31  const RTI::AttributeHandleSet& ahs = desc->attrHandleSet();
32 
33  DtObjClassDesc* baseClass = desc->baseClass();
34 
35  // Initialize the attribute name->handle map with the map from the base class
36  if (baseClass)
37  {
38  attrNamesToHandles = StateRepType::attributeNameToHandleMap(baseClass->handle());
39  }
40 
41  // Loop through all attribute handles. Add any attributes that do not
42  // belong to the base class.
43  #if DtHLA_1516
44 
45  RTI::AttributeHandleSet::const_iterator itr = ahs.begin();
46  const RTI::AttributeHandleSet::const_iterator end = ahs.end();
47  for (; itr != end; itr++)
48  {
49  DtString attrName = desc->attributeNameByHandle(*itr);
50  if(!baseClass->hasAttribute(attrName.c_str()))
51  {
52  attrNamesToHandles.insert(std::make_pair(attrName, *itr));
53  }
54  }
55 
56  #else
57 
58  for (RTI::ULong i = 0; i < ahs.size(); i++)
59  {
60  RTI::AttributeHandle hdl = ahs.getHandle(i);
61  DtString attrName = desc->attributeNameByHandle(hdl);
62  if(!baseClass->hasAttribute(attrName.c_str()))
63  {
64  attrNamesToHandles.insert(std::make_pair(attrName, hdl));
65  }
66  }
67 
68  #endif
69 
70  StateRepType::setAttributeNameToHandleMap(desc->handle(), attrNamesToHandles);
71  }
72 
75  inline void setExtendedAttributesNameToHandleMap(DtObjClassDesc* desc)
76  {
77  std::map<DtString, RTI::AttributeHandle> attrNamesToHandles;
78 
79  const RTI::AttributeHandleSet& ahs = desc->attrHandleSet();
80 
81  // Loop through all attribute handles and add all attribute mappings.
82 #if DtHLA_1516
83 
84  RTI::AttributeHandleSet::const_iterator itr = ahs.begin();
85  const RTI::AttributeHandleSet::const_iterator end = ahs.end();
86  for (; itr != end; itr++)
87  {
88  DtString attrName = desc->attributeNameByHandle(*itr);
89  attrNamesToHandles.insert(std::make_pair(attrName, *itr));
90  }
91 
92 #else
93 
94  for (RTI::ULong i = 0; i < ahs.size(); i++)
95  {
96  RTI::AttributeHandle hdl = ahs.getHandle(i);
97  DtString attrName = desc->attributeNameByHandle(hdl);
98  attrNamesToHandles.insert(std::make_pair(attrName, hdl));
99  }
100 
101 #endif
102 
104  }
105 
106  inline bool areClassNamesEquivalent(const DtString& name1, const DtString& name2)
107  {
108  const DtString hla13RootName("ObjectRoot.");
109  const DtString hla1516RootName("HLAobjectRoot.");
110 
111  const char* name1Ptr = name1.c_str();
112  const char* name2Ptr = name2.c_str();
113 
114  if (name1.findString(hla13RootName) == 0)
115  {
116  name1Ptr += hla13RootName.size();
117  }
118  else if (name1.findString(hla1516RootName) == 0)
119  {
120  name1Ptr += hla1516RootName.size();
121  }
122 
123  if (name2.findString(hla13RootName) == 0)
124  {
125  name2Ptr += hla13RootName.size();
126  }
127  else if (name2.findString(hla1516RootName) == 0)
128  {
129  name2Ptr += hla1516RootName.size();
130  }
131 
132  return (strcmp(name1Ptr, name2Ptr) == 0);
133  }
134 
150  template <typename StateRepType, typename PublisherType, typename ReflectedObjType>
151  void registerExtendedAttributeClasses(const DtString& fullBaseClassName,
152  const DtString& publisherName,
153  const DtString& reflListName,
154  const DtFomMapper::DtObjectClassChooserWithUsrData& classChooser,
155  void* classChooserUsr,
156  DtExerciseConn* exConn)
157  {
158  static DtFomMapper::DtObjectClassChooserWithUsrData prevClassChooser = 0;
159  static void* prevClassChooserUsr = 0;
160 
161  static DtString thePublisherName;
162  static DtString theReflListName;
163  thePublisherName = publisherName;
164  theReflListName = reflListName;
165 
166  // Update class chooser if it has changed or is uninitialized
167  if (classChooser &&
168  thePublisherName.size() > 0 &&
169  (prevClassChooser != classChooser ||
170  prevClassChooserUsr != classChooserUsr))
171  {
172  // Set the object class to publish chooser function
173  exConn->fomMapper()->setObjectClassChooser(thePublisherName.c_str(), classChooser, classChooserUsr);
174  }
175 
176  // Do not reregister if already done. Need to check if this is the same exConn as last time
177  // to handle the case of disconnect/reconnect. Unfortunately sometimes the memory address is
178  // reused, so we can't simply check that. Instead use the exConn's clock to check the time
179  // the exConn was initialized.
180  static DtTime prevExConnInitTime = -1.0;
181  DtTime exConnInitTime = exConn->clock()->absRealTime() - exConn->clock()->elapsedRealTime();
182  if (DtALMOST(prevExConnInitTime, exConnInitTime, 0.0001))
183  {
184  // Already registered
185  return;
186  }
187 
188  static DtString theExtendedAttributesClassName = "VrfExtendedAttributes";
189  static std::vector<DtString> theFullClassNames;
190 
191  std::set<DtString> objClassNames;
192 
193  // Get the list of all subclasses of the specified base class
194  DtObjClassDesc* baseEntDesc = exConn->fom()->objClassByName(fullBaseClassName);
195  DtObjClassDesc* classDesc = NULL;
196 
197 #if DtHLA_1516
198  for (std::list<DtObjClassDesc*>::const_iterator item = exConn->fom()->objClassList().begin();
199  item != exConn->fom()->objClassList().end();
200  ++item)
201  {
202  classDesc = const_cast<DtObjClassDesc*>(*item);
203 #else
204  for (classDesc = exConn->fom()->firstObjClass();
205  classDesc;
206  classDesc = exConn->fom()->nextObjClass(classDesc))
207  {
208 #endif
209  if (baseEntDesc && classDesc->isOfClass(baseEntDesc->handle()))
210  {
211  theFullClassNames.push_back(classDesc->name());
212  }
213  }
214 
215  // Get the current list of object classes supported by the reflected list
216  const std::set<DtString>* currObjClassNames = exConn->fomMapper()->objectClassNames(theReflListName.c_str());
217  if(currObjClassNames)
218  {
219  objClassNames = *currObjClassNames;
220  }
221 
222  for(int i = 0; i < theFullClassNames.size(); ++i)
223  {
224  classDesc = exConn->fom()->objClassByName(theFullClassNames[i].c_str());
225 
226  if(classDesc)
227  {
228  // Set the unknown attribute checker
229  classDesc->setUnknownAttributeChecker((DtHlaStateEncoder::DtAttributeCheckerWithHandle)
231 
232  // Set the unknown attribute encoder
233  classDesc->setUnknownAttributeEncoder((DtHlaStateEncoder::DtAttributeEncoder)
235 
236  // Set the unknown attribute decoder
237  classDesc->setUnknownAttributeDecoder((DtHlaStateDecoder::DtAttributeDecoder)
239 
240  // Add class to list of class names. Check to make sure class isn't already present in
241  // another acceptable form (i.e. with or without the root class specified.
242  bool alreadyPresent = false;
243  std::set<DtString>::const_iterator objClassIter = objClassNames.begin();
244  for (; objClassIter != objClassNames.end() && !alreadyPresent; ++objClassIter)
245  {
246  if (areClassNamesEquivalent(theFullClassNames[i], *objClassIter))
247  {
248  alreadyPresent = true;
249  }
250  }
251 
252  if (!alreadyPresent)
253  {
254  objClassNames.insert(theFullClassNames[i]);
255  exConn->fomMapper()->setObjectClassNames(theReflListName.c_str(), objClassNames);
256  }
257 
258  // Set the attribute name-handle map in the state rep class
259  setAttributeNameToHandleMap<StateRepType>(classDesc);
260  }
261 
262  }
263 
264  // Set the state repository creators
265  PublisherType::setStateRepCreator((typename PublisherType::DtStateRepCreator)StateRepType::create);
266  ReflectedObjType::setStateRepCreator((typename ReflectedObjType::DtStateRepCreator)StateRepType::create);
267 
268  // Set the reflected list object class names
269  exConn->fomMapper()->setObjectClassNames(theReflListName.c_str(), objClassNames);
270 
271  // Now setup the Extended Attributes object attributes
272  classDesc = exConn->fom()->objClassByName(theExtendedAttributesClassName.c_str());
273 
274  if (classDesc)
275  {
277  }
278  else
279  {
280  DtWarn << "Could not initialize use of extended attributes class: " <<
281  theExtendedAttributesClassName << " not found in FOM." << std::endl;
282  }
283 
284  prevExConnInitTime = exConnInitTime;
285  }
286 }
287 
288 #endif
Contains the DtSensorFieldOfViewPublisher class declaration.
Contains the DtExtendedAttributesObjectStateRepository class declaration.
#define DtALMOST(x, y, e)
Definition: registerExtendedAttributeClassesHLA.h:20

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)