PageRenderTime 526ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/branches/bullet/src/other/step/src/clstepcore/instmgr.cc

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C++ | 413 lines | 270 code | 58 blank | 85 comment | 49 complexity | b86d08be27a98afc33a939f4db8c768a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /*
  2. * NIST STEP Editor Class Library
  3. * cleditor/instmgr.cc
  4. * April 1997
  5. * David Sauder
  6. * K. C. Morris
  7. * Development of this software was funded by the United States Government,
  8. * and is not subject to copyright.
  9. */
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. // InstMgr member functions
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include <sdai.h>
  16. #include <instmgr.h>
  17. #include "scl_memmgr.h"
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // debug_level >= 2 => tells when a command is chosen
  20. // debug_level >= 3 => prints values within functions:
  21. ///////////////////////////////////////////////////////////////////////////////
  22. static int debug_level = 3;
  23. // if debug_level is greater than this number then
  24. // function names get printed out when executed
  25. //static int PrintFunctionTrace = 2;
  26. // if debug_level is greater than this number then
  27. // values within functions get printed out
  28. //static int PrintValues = 3;
  29. ///////////////////////////////////////////////////////////////////////////////
  30. void
  31. InstMgr::PrintSortedFileIds() {
  32. MgrNode * mn = 0;
  33. int count = InstanceCount();
  34. int i = 0;
  35. for( i = 0; i < count; i++ ) {
  36. mn = ( MgrNode * )( ( *sortedMaster )[i] );
  37. cout << i << " " << mn->GetFileId() << endl;
  38. }
  39. }
  40. InstMgr::InstMgr( int ownsInstances )
  41. : maxFileId( -1 ), _ownsInstances( ownsInstances ) {
  42. master = new MgrNodeArray();
  43. sortedMaster = new MgrNodeArraySorted();
  44. }
  45. InstMgr::~InstMgr() {
  46. if( _ownsInstances ) {
  47. master->DeleteEntries();
  48. } else {
  49. master->ClearEntries();
  50. }
  51. sortedMaster->ClearEntries();
  52. delete master;
  53. delete sortedMaster;
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////
  56. void InstMgr::ClearInstances() {
  57. master->ClearEntries();
  58. sortedMaster->ClearEntries();
  59. maxFileId = -1;
  60. }
  61. void InstMgr::DeleteInstances() {
  62. master->DeleteEntries();
  63. sortedMaster->ClearEntries();
  64. maxFileId = -1;
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////
  67. /**************************************************
  68. description:
  69. returns:
  70. SEVERITY_NULL: if all instances are complete
  71. SEVERITY_INCOMPLETE: if at least one instance is missing a required attribute
  72. SEVERITY_WARNING:
  73. SEVERITY_INPUT_ERROR: if at least one instance can not be fetched
  74. from the instance list.
  75. **************************************************/
  76. enum Severity
  77. InstMgr::VerifyInstances( ErrorDescriptor & err ) {
  78. int errorCount = 0;
  79. char errbuf[BUFSIZ];
  80. int n = InstanceCount();
  81. MgrNode * mn;
  82. SDAI_Application_instance * se;
  83. enum Severity rval = SEVERITY_NULL;
  84. //for each instance on the list,
  85. //check the MgrNode state.
  86. //if the state is complete then do nothing
  87. //if the state is not complete,
  88. // then check if it is valid
  89. // if it is valid then increment the warning count,
  90. // and set the rval to SEVERITY_INCOMPLETE
  91. // if it is not valid, then increment the error count
  92. // and set the rval to
  93. for( int i = 0; i < n; ++i ) {
  94. mn = GetMgrNode( i );
  95. if( !mn ) {
  96. ++errorCount;
  97. if( errorCount == 1 )
  98. sprintf( errbuf,
  99. "VerifyInstances: Unable to verify the following instances: node %d",
  100. i );
  101. else {
  102. sprintf( errbuf, ", node %d", i );
  103. }
  104. err.AppendToDetailMsg( errbuf );
  105. rval = SEVERITY_INPUT_ERROR;
  106. err.GreaterSeverity( SEVERITY_INPUT_ERROR );
  107. continue;
  108. }
  109. if( debug_level > 3 )
  110. cerr << "In VerifyInstances: "
  111. << "new MgrNode for " << mn->GetFileId() << " with state "
  112. << mn->CurrState() << endl;
  113. if( !mn->MgrNodeListMember( completeSE ) ) {
  114. se = mn->GetApplication_instance();
  115. if( se->ValidLevel( &err, this, 0 ) < SEVERITY_USERMSG ) {
  116. if( rval > SEVERITY_INCOMPLETE ) {
  117. rval = SEVERITY_INCOMPLETE;
  118. }
  119. ++errorCount;
  120. if( errorCount == 1 )
  121. sprintf( errbuf,
  122. "VerifyInstances: Unable to verify the following instances: #%d",
  123. se->StepFileId() );
  124. else {
  125. sprintf( errbuf, ", #%d", se->StepFileId() );
  126. }
  127. err.AppendToDetailMsg( errbuf );
  128. }
  129. }
  130. }
  131. if( errorCount ) {
  132. sprintf( errbuf,
  133. "VerifyInstances: %d invalid instances in list.\n",
  134. errorCount );
  135. err.AppendToUserMsg( errbuf );
  136. err.AppendToDetailMsg( ".\n" );
  137. err.GreaterSeverity( SEVERITY_INCOMPLETE );
  138. }
  139. return rval;
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////
  142. MgrNode * InstMgr::FindFileId( int fileId ) {
  143. int index = sortedMaster->MgrNodeIndex( fileId );
  144. if( index >= 0 ) {
  145. return ( MgrNode * )( *sortedMaster )[index];
  146. } else {
  147. return ( MgrNode * )0;
  148. }
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////
  151. // get the index into display list given a SDAI_Application_instance
  152. // called by see initiated functions
  153. int InstMgr::GetIndex( MgrNode * mn ) {
  154. return mn->ArrayIndex();
  155. }
  156. ///////////////////////////////////////////////////////////////////////////////
  157. int InstMgr::GetIndex( SDAI_Application_instance * se ) {
  158. int fileId = se->StepFileId();
  159. return sortedMaster->MgrNodeIndex( fileId );
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////
  162. int InstMgr::VerifyEntity( int fileId, const char * expectedType ) {
  163. MgrNode * mn = FindFileId( fileId );
  164. if( mn ) {
  165. if( !strcmp( expectedType, mn->GetApplication_instance()->EntityName() ) ) {
  166. return 2; // types match
  167. } else {
  168. return 1; // possible mismatch depending on descendants
  169. }
  170. } else {
  171. return 0;
  172. }
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////
  175. // Append instance to the list of instances. Checks the file id and
  176. // sets it if 1) it is not set already or 2) it already exists in the list.
  177. MgrNode * InstMgr::Append( SDAI_Application_instance * se, stateEnum listState ) {
  178. if( debug_level > 3 ) {
  179. cout << "#" << se->StepFileId() << " append node to InstMgr" << endl;
  180. }
  181. MgrNode * mn = 0;
  182. if( se->StepFileId() == 0 ) { // no id assigned
  183. se->StepFileId( NextFileId() ); // assign a file id
  184. }
  185. mn = FindFileId( se->StepFileId() );
  186. if( mn ) { // if id already in list
  187. // and it's because instance is already in list
  188. if( GetApplication_instance( mn ) == se ) {
  189. return 0; // return 0 or mn?
  190. } else {
  191. se->StepFileId( NextFileId() ); // otherwise assign a new file id
  192. }
  193. }
  194. // update the maxFileId if needed
  195. if( se->StepFileId() > MaxFileId() ) {
  196. maxFileId = se->StepFileId();
  197. }
  198. mn = new MgrNode( se, listState );
  199. if( debug_level > 3 )
  200. cerr << "new MgrNode for " << mn->GetFileId() << " with state "
  201. << mn->CurrState() << endl;
  202. if( listState == noStateSE )
  203. cout << "append to InstMgr **ERROR ** node #" << se->StepFileId() <<
  204. " doesn't have state information" << endl;
  205. master->Append( mn );
  206. sortedMaster->Insert( mn );
  207. //PrintSortedFileIds();
  208. return mn;
  209. }
  210. ///////////////////////////////////////////////////////////////////////////////
  211. void InstMgr::Delete( MgrNode * node ) {
  212. // delete the node from its current state list
  213. node->Remove();
  214. int index;
  215. // get the index of the node in the sorted master array
  216. index = sortedMaster->MgrNodeIndex( node->GetFileId() );
  217. // remove the node from the sorted master array
  218. sortedMaster->Remove( index );
  219. // get the index into the master array by ptr arithmetic
  220. index = node->ArrayIndex();
  221. master->Remove( index );
  222. delete node;
  223. }
  224. ///////////////////////////////////////////////////////////////////////////////
  225. void InstMgr::Delete( SDAI_Application_instance * se ) {
  226. Delete( FindFileId( se->StepFileId() ) );
  227. }
  228. ///////////////////////////////////////////////////////////////////////////////
  229. void InstMgr::ChangeState( MgrNode * node, stateEnum listState ) {
  230. switch( listState ) {
  231. case completeSE:
  232. if( debug_level > 3 )
  233. cout << "#" << node->GetApplication_instance()->StepFileId() <<
  234. " change node to InstMgr's complete list\n";
  235. node->ChangeState( listState );
  236. break;
  237. case incompleteSE:
  238. if( debug_level > 3 )
  239. cout << "#" << node->GetApplication_instance()->StepFileId() <<
  240. " change node to InstMgr's incomplete list\n";
  241. node->ChangeState( listState );
  242. break;
  243. case newSE:
  244. if( debug_level > 3 )
  245. cout << "#" << node->GetApplication_instance()->StepFileId() <<
  246. " change node to InstMgr's new list\n";
  247. node->ChangeState( listState );
  248. break;
  249. case deleteSE:
  250. if( debug_level > 3 )
  251. cout << "#" << node->GetApplication_instance()->StepFileId() <<
  252. " change node to InstMgr's delete list\n";
  253. node->ChangeState( listState );
  254. break;
  255. case noStateSE:
  256. cout << "#" << node->GetApplication_instance()->StepFileId() <<
  257. "ERROR can't change this node state\n";
  258. node->Remove();
  259. break;
  260. }
  261. }
  262. ///////////////////////////////////////////////////////////////////////////////
  263. /**************************************************
  264. description:
  265. This function returns an integer value indicating
  266. the number of instances with the given name appearing
  267. on the instance manager.
  268. **************************************************/
  269. int
  270. InstMgr::EntityKeywordCount( const char * name ) {
  271. int count = 0;
  272. MgrNode * node;
  273. SDAI_Application_instance * se;
  274. int n = InstanceCount();
  275. for( int j = 0; j < n; ++j ) {
  276. node = GetMgrNode( j );
  277. se = node->GetApplication_instance();
  278. if( !strcmp( se->EntityName(),
  279. PrettyTmpName( name ) ) ) {
  280. ++count;
  281. }
  282. }
  283. return count;
  284. }
  285. ///////////////////////////////////////////////////////////////////////////////
  286. SDAI_Application_instance *
  287. InstMgr::GetApplication_instance( int index ) {
  288. MgrNode * mn = ( MgrNode * )( *master )[index];
  289. if( mn ) {
  290. return mn->GetApplication_instance();
  291. } else {
  292. return 0;
  293. }
  294. }
  295. SDAI_Application_instance *
  296. InstMgr::GetSTEPentity( int index ) {
  297. MgrNode * mn = ( MgrNode * )( *master )[index];
  298. if( mn ) {
  299. return mn->GetApplication_instance();
  300. } else {
  301. return 0;
  302. }
  303. }
  304. ///////////////////////////////////////////////////////////////////////////////
  305. /**************************************************
  306. description:
  307. This function returns the first entity (by index)
  308. on the instance manager, which has the given
  309. entity name. It starts its search at starting_index,
  310. and returns ENTITY_NULL if a match does not occur
  311. before the last index is reached. This function
  312. does not wrap around to search indices before the
  313. starting_index.
  314. **************************************************/
  315. SDAI_Application_instance *
  316. InstMgr::GetApplication_instance( const char * entityKeyword, int starting_index ) {
  317. MgrNode * node;
  318. SDAI_Application_instance * se;
  319. int count = InstanceCount();
  320. for( int j = starting_index; j < count; ++j ) {
  321. node = GetMgrNode( j );
  322. se = node->GetApplication_instance();
  323. if( !strcmp( se->EntityName(),
  324. PrettyTmpName( entityKeyword ) ) ) {
  325. return se;
  326. }
  327. }
  328. return ENTITY_NULL;
  329. }
  330. SDAI_Application_instance *
  331. InstMgr::GetSTEPentity( const char * entityKeyword, int starting_index ) {
  332. MgrNode * node;
  333. SDAI_Application_instance * se;
  334. int count = InstanceCount();
  335. for( int j = starting_index; j < count; ++j ) {
  336. node = GetMgrNode( j );
  337. se = node->GetApplication_instance();
  338. if( !strcmp( se->EntityName(),
  339. PrettyTmpName( entityKeyword ) ) ) {
  340. return se;
  341. }
  342. }
  343. return ENTITY_NULL;
  344. }
  345. ///////////////////////////////////////////////////////////////////////////////
  346. void *
  347. InstMgr::GetSEE( int index ) {
  348. MgrNode * mn = ( MgrNode * )( *master )[index];
  349. if( mn ) {
  350. return mn->SEE();
  351. } else {
  352. return 0;
  353. }
  354. }