| 1 | #include "CATSTT.hpp" |
|---|
| 2 | #include "RefEntity.hpp" |
|---|
| 3 | #include "RefEntityName.hpp" |
|---|
| 4 | #include "RefEntityFactory.hpp" |
|---|
| 5 | #include "CubitAttribManager.hpp" |
|---|
| 6 | #include "RefGroup.hpp" |
|---|
| 7 | #include "TDUniqueId.hpp" |
|---|
| 8 | #include "CGMApp.hpp" |
|---|
| 9 | #include "TSTTB_SNL.h" |
|---|
| 10 | #include "TSTTG_CGM.h" |
|---|
| 11 | |
|---|
| 12 | #define CHECK_SIZE(array, type, this_size, retval) \ |
|---|
| 13 | if (0 == array ## _allocated || array ## _allocated < this_size) {\ |
|---|
| 14 | if (NULL != array) free(array); \ |
|---|
| 15 | array = (type*)malloc(this_size*sizeof(type));\ |
|---|
| 16 | array ## _allocated=this_size;\ |
|---|
| 17 | if (NULL == array) {TSTTG_processError(TSTTB_MEMORY_ALLOCATION_FAILED, \ |
|---|
| 18 | "Couldn't allocate array.");return retval; }\ |
|---|
| 19 | }; \ |
|---|
| 20 | array ## _size = this_size; |
|---|
| 21 | |
|---|
| 22 | #define TAG_CHECK_SIZE(array, allocated, size) \ |
|---|
| 23 | if (allocated < size) {\ |
|---|
| 24 | if (NULL != array) free(array); \ |
|---|
| 25 | allocated=size; \ |
|---|
| 26 | array = (char*)malloc(allocated); \ |
|---|
| 27 | if (NULL == array) {TSTTG_processError(TSTTB_MEMORY_ALLOCATION_FAILED, \ |
|---|
| 28 | "Couldn't allocate array.");return CUBIT_FAILURE; }\ |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | const char *CGMTagManager::CATSTT_NAME = "TSTTB_Tag"; |
|---|
| 32 | const char *CGMTagManager::CATSTT_NAME_INTERNAL = "TSTTB_TAG"; |
|---|
| 33 | |
|---|
| 34 | CGMTagManager *CGMTagManager::staticCGMTagManager = NULL; |
|---|
| 35 | |
|---|
| 36 | CubitAttrib *CGMTagManager::CATSTT_creator(RefEntity* entity, CubitSimpleAttrib *p_csa) |
|---|
| 37 | { |
|---|
| 38 | CATSTT *this_ca = new CATSTT(staticCGMTagManager, entity, p_csa); |
|---|
| 39 | return this_ca; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | CGMTagManager::CGMTagManager() |
|---|
| 43 | : interfaceGroup(NULL) |
|---|
| 44 | { |
|---|
| 45 | staticCGMTagManager = this; |
|---|
| 46 | |
|---|
| 47 | pcTag = 0; |
|---|
| 48 | |
|---|
| 49 | // get the tag number for CATSTT |
|---|
| 50 | DLIList<int> tag_types; |
|---|
| 51 | int max_type = 0; |
|---|
| 52 | CubitAttribManager *cam = CGMApp::instance()->attrib_manager(); |
|---|
| 53 | cam->get_registered_types(tag_types); |
|---|
| 54 | for (int i = 0; i < tag_types.size(); i++) { |
|---|
| 55 | int this_type = tag_types.get_and_step(); |
|---|
| 56 | max_type = (max_type < this_type ? this_type : max_type); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | max_type++; |
|---|
| 60 | CubitStatus status = cam->register_attrib_type(max_type, CATSTT_NAME, CATSTT_NAME_INTERNAL, |
|---|
| 61 | &CGMTagManager::CATSTT_creator, false, true, |
|---|
| 62 | true, true, true, false); |
|---|
| 63 | if (CUBIT_FAILURE == status) { |
|---|
| 64 | TSTTG_processError(TSTTB_FAILURE, "Couldn't create cgm attribute for tags."); |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | CATSTT_att_type = max_type; |
|---|
| 68 | |
|---|
| 69 | // create the 0th tag, since this index represents no tag |
|---|
| 70 | TagInfo tmp_info = {0, std::string("NULL tag"), TSTTG_BYTES, NULL, false}; |
|---|
| 71 | tagInfo.push_back(tmp_info); |
|---|
| 72 | presetTagInfo.push_back(tmp_info); |
|---|
| 73 | |
|---|
| 74 | // create preset tags, for CGM attributes we want to be visible as tags |
|---|
| 75 | // name |
|---|
| 76 | tmp_info.tagLength = sizeof(std::string); |
|---|
| 77 | tmp_info.tagName = std::string("ENTITY_NAME"); |
|---|
| 78 | tmp_info.defaultValue = NULL; |
|---|
| 79 | tmp_info.tagType = TSTTG_BYTES; |
|---|
| 80 | tmp_info.isActive = true; |
|---|
| 81 | presetTagInfo.push_back(tmp_info); |
|---|
| 82 | tagNameMap[std::string("ENTITY_NAME")] = -1; |
|---|
| 83 | // id |
|---|
| 84 | tmp_info.tagLength = sizeof(int); |
|---|
| 85 | tmp_info.tagName = std::string("GLOBAL_ID"); |
|---|
| 86 | tmp_info.tagType = TSTTG_INTEGER; |
|---|
| 87 | presetTagInfo.push_back(tmp_info); |
|---|
| 88 | tagNameMap[std::string("GLOBAL_ID")] = -2; |
|---|
| 89 | // uid |
|---|
| 90 | tmp_info.tagName = std::string("UNIQUE_ID"); |
|---|
| 91 | tmp_info.tagType = TSTTG_INTEGER; |
|---|
| 92 | presetTagInfo.push_back(tmp_info); |
|---|
| 93 | tagNameMap[std::string("UNIQUE_ID")] = -3; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | CGMTagManager::~CGMTagManager() |
|---|
| 97 | { |
|---|
| 98 | staticCGMTagManager = NULL; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | int CGMTagManager::pc_tag(const bool create_if_missing) |
|---|
| 102 | { |
|---|
| 103 | if (0 == pcTag && create_if_missing) { |
|---|
| 104 | pcTag = getTagHandle("__cgm_parent_child_tag"); |
|---|
| 105 | if (0 == pcTag) { |
|---|
| 106 | // ok, one doesn't exist, create one |
|---|
| 107 | void *def_val[] = {NULL, NULL}; |
|---|
| 108 | |
|---|
| 109 | createTag("__cgm_parent_child_tag", 2*sizeof(std::vector<RefGroup*>*), |
|---|
| 110 | TSTTG_BYTES, (char*)def_val, &pcTag); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | return pcTag; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | CubitStatus CGMTagManager::createTag (/*in*/ const char *tag_name, |
|---|
| 118 | /*in*/ const int tag_length, |
|---|
| 119 | /*in*/ const int tag_type, |
|---|
| 120 | /*in*/ char* default_value, |
|---|
| 121 | /*out*/ int *tag_handle) |
|---|
| 122 | { |
|---|
| 123 | std::string tmp_name(tag_name); |
|---|
| 124 | std::map<std::string,int>::iterator mit = tagNameMap.find(tmp_name); |
|---|
| 125 | if (mit != tagNameMap.end()) { |
|---|
| 126 | *tag_handle = mit->second; |
|---|
| 127 | TSTTG_processError(TSTTB_TAG_ALREADY_EXISTS, "Tag already created."); |
|---|
| 128 | return CUBIT_SUCCESS; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | TagInfo tmp_info = {tag_length, std::string(tag_name), tag_type, NULL, true}; |
|---|
| 132 | tagInfo.push_back(tmp_info); |
|---|
| 133 | *tag_handle = tagInfo.size()-1; |
|---|
| 134 | if (default_value != NULL) { |
|---|
| 135 | tagInfo[*tag_handle].defaultValue = (char *) malloc(tag_length); |
|---|
| 136 | memcpy(tagInfo[*tag_handle].defaultValue, default_value, tag_length); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | // put the name and handle into the map too |
|---|
| 140 | tagNameMap[std::string(tag_name)] = *tag_handle; |
|---|
| 141 | |
|---|
| 142 | return CUBIT_SUCCESS; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | CubitStatus CGMTagManager::destroyTag (/*in*/ const int tag_handle, |
|---|
| 147 | /*in*/ const bool forced) |
|---|
| 148 | { |
|---|
| 149 | if (!forced) { |
|---|
| 150 | // see whether this tag is still assigned anywhere |
|---|
| 151 | // not implemented yet |
|---|
| 152 | return CUBIT_FAILURE; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | // if we got here, assume we can delete it |
|---|
| 156 | TagInfo *tinfo = (tag_handle >= 0 ? &tagInfo[tag_handle] : &presetTagInfo[-tag_handle]); |
|---|
| 157 | tinfo->isActive = false; |
|---|
| 158 | |
|---|
| 159 | return CUBIT_SUCCESS; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | const char *CGMTagManager::getTagName (/*in*/ const int tag_handle) |
|---|
| 163 | { |
|---|
| 164 | return (tag_handle >= 0 ? tagInfo[tag_handle].tagName.c_str() : |
|---|
| 165 | presetTagInfo[-tag_handle].tagName.c_str()); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | int CGMTagManager::getTagSize (/*in*/ const int tag_handle) |
|---|
| 169 | { |
|---|
| 170 | return (tag_handle >= 0 ? |
|---|
| 171 | tagInfo[tag_handle].tagLength : |
|---|
| 172 | presetTagInfo[-tag_handle].tagLength); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | int CGMTagManager::getTagHandle (/*in*/ const char *tag_name) |
|---|
| 176 | { |
|---|
| 177 | std::map<std::string,int>::iterator it = |
|---|
| 178 | tagNameMap.find(std::string(tag_name)); |
|---|
| 179 | if (it != tagNameMap.end()) |
|---|
| 180 | return (*it).second; |
|---|
| 181 | else return 0; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | int CGMTagManager::getTagType (/*in*/ const int tag_handle) |
|---|
| 185 | { |
|---|
| 186 | return (tag_handle >= 0 ? |
|---|
| 187 | tagInfo[tag_handle].tagType : |
|---|
| 188 | presetTagInfo[-tag_handle].tagType); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | CubitStatus CGMTagManager::getArrData (/*in*/ const RefEntity**entity_handles, |
|---|
| 192 | const int entity_handles_size, |
|---|
| 193 | /*in*/ const int tag_handle, |
|---|
| 194 | /*inout*/ ARRAY_INOUT_DECL(char, tag_value)) |
|---|
| 195 | { |
|---|
| 196 | TagInfo *tinfo = (tag_handle >= 0 ? &tagInfo[tag_handle] : &presetTagInfo[-tag_handle]); |
|---|
| 197 | int tag_size = tinfo->tagLength; |
|---|
| 198 | // either way, we have to have that many bytes when we leave this function |
|---|
| 199 | *tag_value_size = entity_handles_size*tinfo->tagLength; |
|---|
| 200 | TAG_CHECK_SIZE(*tag_value, *tag_value_allocated, *tag_value_size); |
|---|
| 201 | char *val_ptr = *tag_value; |
|---|
| 202 | if (tag_handle < 0) { |
|---|
| 203 | for (int i = 0; i < entity_handles_size; i++) { |
|---|
| 204 | if (NULL == entity_handles[i]) |
|---|
| 205 | getPresetTagData(interface_group(), tag_handle, val_ptr, tinfo->tagLength); |
|---|
| 206 | else |
|---|
| 207 | getPresetTagData(entity_handles[i], tag_handle, val_ptr, tinfo->tagLength); |
|---|
| 208 | val_ptr += tinfo->tagLength; |
|---|
| 209 | } |
|---|
| 210 | return CUBIT_SUCCESS; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | CubitStatus tmp_result = CUBIT_SUCCESS; |
|---|
| 214 | |
|---|
| 215 | for (int i = 0; i < entity_handles_size; i++) { |
|---|
| 216 | // ok to cast away const-ness because "false" passed in for create_if_missing |
|---|
| 217 | RefEntity *this_ent = (NULL == entity_handles[i] ? interface_group() : |
|---|
| 218 | const_cast<RefEntity*>(entity_handles[i])); |
|---|
| 219 | CATSTT *catstt = get_catstt(this_ent); |
|---|
| 220 | if (NULL != catstt) tmp_result = catstt->get_tag_data(tag_handle, val_ptr); |
|---|
| 221 | else if (NULL != tinfo->defaultValue) |
|---|
| 222 | memcpy(val_ptr, tinfo->defaultValue, tinfo->tagLength); |
|---|
| 223 | else |
|---|
| 224 | tmp_result = CUBIT_FAILURE; |
|---|
| 225 | |
|---|
| 226 | val_ptr += tag_size; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | return CUBIT_SUCCESS; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | CubitStatus CGMTagManager::setArrData (/*in*/ ARRAY_IN_DECL(RefEntity*, entity_handles), |
|---|
| 233 | /*in*/ const int tag_handle, |
|---|
| 234 | /*in*/ const char *tag_values, const int tag_values_size) |
|---|
| 235 | { |
|---|
| 236 | TagInfo *tinfo = (tag_handle >= 0 ? &tagInfo[tag_handle] : &presetTagInfo[-tag_handle]); |
|---|
| 237 | int tag_size = tinfo->tagLength; |
|---|
| 238 | |
|---|
| 239 | const char *val_ptr = tag_values; |
|---|
| 240 | |
|---|
| 241 | if (tag_handle < 0) { |
|---|
| 242 | for (int i = 0; i < entity_handles_size; i++) { |
|---|
| 243 | if (NULL == entity_handles[i]) |
|---|
| 244 | setPresetTagData(interface_group(), tag_handle, val_ptr, tag_size); |
|---|
| 245 | else |
|---|
| 246 | setPresetTagData(entity_handles[i], tag_handle, val_ptr, tag_size); |
|---|
| 247 | val_ptr += tag_size; |
|---|
| 248 | } |
|---|
| 249 | return CUBIT_SUCCESS; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | for (int i = 0; i < entity_handles_size; i++) { |
|---|
| 253 | RefEntity *this_ent = (NULL == entity_handles[i] ? interface_group() : |
|---|
| 254 | entity_handles[i]); |
|---|
| 255 | CATSTT *catstt = get_catstt(this_ent, true); |
|---|
| 256 | catstt->set_tag_data(tag_handle, val_ptr); |
|---|
| 257 | val_ptr += tag_size; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | return CUBIT_SUCCESS; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | CubitStatus CGMTagManager::rmvArrTag (/*in*/ ARRAY_IN_DECL(RefEntity*, entity_handles), |
|---|
| 264 | /*in*/ const int tag_handle) |
|---|
| 265 | { |
|---|
| 266 | for (int i = 0; i < entity_handles_size; i++) { |
|---|
| 267 | CATSTT *catstt = get_catstt((entity_handles[i] == NULL ? |
|---|
| 268 | interface_group() : entity_handles[i])); |
|---|
| 269 | if (NULL != catstt) catstt->remove_tag(tag_handle); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | return CUBIT_SUCCESS; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | CubitStatus CGMTagManager::getAllTags (/*in*/ const RefEntity* entity_handle, |
|---|
| 276 | /*inout*/ ARRAY_INOUT_DECL(int, tag_handles)) |
|---|
| 277 | { |
|---|
| 278 | int i = 0, uid = 0, tag_size; |
|---|
| 279 | char *uid_ptr = (char*) &uid; |
|---|
| 280 | bool has_uid = getPresetTagData(entity_handle, -3, uid_ptr, tag_size); |
|---|
| 281 | int num_tags = (has_uid ? 3 : 2); |
|---|
| 282 | |
|---|
| 283 | RefEntity *this_ent = (NULL == entity_handle ? interface_group() : |
|---|
| 284 | const_cast<RefEntity*>(entity_handle)); |
|---|
| 285 | |
|---|
| 286 | // const-cast because we're passing in false for create_if_missing |
|---|
| 287 | CATSTT *catstt = get_catstt(this_ent); |
|---|
| 288 | if (NULL != catstt) { |
|---|
| 289 | // need to check whether entity has a uid |
|---|
| 290 | num_tags += catstt->tagData.size(); |
|---|
| 291 | CHECK_SIZE(*tag_handles, int, num_tags, CUBIT_FAILURE); |
|---|
| 292 | for (std::map<int,void*>::iterator tag_it = catstt->tagData.begin(); |
|---|
| 293 | tag_it != catstt->tagData.end(); tag_it++) |
|---|
| 294 | (*tag_handles)[i++] = (*tag_it).first; |
|---|
| 295 | } |
|---|
| 296 | else { |
|---|
| 297 | CHECK_SIZE(*tag_handles, int, num_tags, CUBIT_FAILURE); |
|---|
| 298 | } |
|---|
| 299 | (*tag_handles)[i++] = -1; |
|---|
| 300 | (*tag_handles)[i++] = -2; |
|---|
| 301 | if (has_uid) (*tag_handles)[i++] = -3; |
|---|
| 302 | |
|---|
| 303 | return CUBIT_SUCCESS; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | CATSTT *CGMTagManager::get_catstt(RefEntity *ent, |
|---|
| 307 | const bool create_if_missing) |
|---|
| 308 | { |
|---|
| 309 | CubitAttrib *this_attrib = ent->get_cubit_attrib(CATSTT_att_type, create_if_missing); |
|---|
| 310 | if (NULL != this_attrib) |
|---|
| 311 | return dynamic_cast<CATSTT*>(this_attrib); |
|---|
| 312 | else |
|---|
| 313 | return NULL; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | RefGroup *CGMTagManager::interface_group(const bool create_if_missing) |
|---|
| 317 | { |
|---|
| 318 | if (NULL == interfaceGroup) |
|---|
| 319 | interfaceGroup = |
|---|
| 320 | dynamic_cast<RefGroup*>(RefEntityName::instance()->get_refentity("interface_group")); |
|---|
| 321 | |
|---|
| 322 | if (NULL == interfaceGroup && create_if_missing) |
|---|
| 323 | interfaceGroup = RefEntityFactory::instance()->construct_RefGroup("interface_group"); |
|---|
| 324 | |
|---|
| 325 | return interfaceGroup; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | void CGMTagManager::setPresetTagData(RefEntity *entity, |
|---|
| 329 | const int tag_handle, |
|---|
| 330 | const char *tag_value, |
|---|
| 331 | const int tag_size) |
|---|
| 332 | { |
|---|
| 333 | const std::string *this_name; |
|---|
| 334 | switch (-tag_handle) { |
|---|
| 335 | case 1: |
|---|
| 336 | // entity name |
|---|
| 337 | if (sizeof(std::string) != tag_size) { |
|---|
| 338 | TSTTG_processError(TSTTB_INVALID_ARGUMENT, "Tag of type 'name' is the wrong size."); |
|---|
| 339 | return; |
|---|
| 340 | } |
|---|
| 341 | this_name = reinterpret_cast<const std::string*>(tag_value); |
|---|
| 342 | entity->entity_name(CubitString(this_name->c_str())); |
|---|
| 343 | return; |
|---|
| 344 | case 2: |
|---|
| 345 | // entity id |
|---|
| 346 | TSTTG_processError(TSTTB_NOT_SUPPORTED, "Can't set id of entities with this implementation."); |
|---|
| 347 | return; |
|---|
| 348 | case 3: |
|---|
| 349 | // unique id |
|---|
| 350 | TSTTG_processError(TSTTB_NOT_SUPPORTED, "Can't set unique id of entities with this implementation."); |
|---|
| 351 | return; |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | bool CGMTagManager::getPresetTagData(const RefEntity *entity, |
|---|
| 356 | const int tag_handle, |
|---|
| 357 | char *tag_value, |
|---|
| 358 | int &tag_size) |
|---|
| 359 | { |
|---|
| 360 | std::string *this_name; |
|---|
| 361 | int *this_id; |
|---|
| 362 | int *this_uid; |
|---|
| 363 | |
|---|
| 364 | switch (-tag_handle) { |
|---|
| 365 | case 1: |
|---|
| 366 | // entity name |
|---|
| 367 | tag_size = sizeof(std::string); |
|---|
| 368 | this_name = reinterpret_cast<std::string*>(tag_value); |
|---|
| 369 | *this_name = std::string(entity->entity_name().c_str()); |
|---|
| 370 | return true; |
|---|
| 371 | case 2: |
|---|
| 372 | // entity id |
|---|
| 373 | tag_size = sizeof(int); |
|---|
| 374 | this_id = reinterpret_cast<int*>(tag_value); |
|---|
| 375 | *this_id = entity->id(); |
|---|
| 376 | return true; |
|---|
| 377 | case 3: |
|---|
| 378 | // unique id |
|---|
| 379 | tag_size = sizeof(int); |
|---|
| 380 | this_uid = reinterpret_cast<int*>(tag_value); |
|---|
| 381 | // const_cast because we're passing false for create_if_missing |
|---|
| 382 | *this_uid = TDUniqueId::get_unique_id(const_cast<RefEntity*>(entity), false); |
|---|
| 383 | return (*this_uid == 0 ? false : true); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | return false; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | std::vector<RefGroup*> *CGMTagManager::pc_list(RefEntity *gentity, int list_no, |
|---|
| 390 | const bool create_if_missing) |
|---|
| 391 | { |
|---|
| 392 | if (NULL == gentity) return NULL; |
|---|
| 393 | |
|---|
| 394 | int dum_tag_size = sizeof(std::vector<RefGroup*>*); |
|---|
| 395 | int dum = 2*dum_tag_size; |
|---|
| 396 | std::vector<RefGroup*> *pc_lists[2]; |
|---|
| 397 | char *dum_ptr = (char*) pc_lists; |
|---|
| 398 | CubitStatus result = |
|---|
| 399 | getArrData(const_cast<const RefEntity**>(&gentity), 1, pc_tag(create_if_missing), &dum_ptr, |
|---|
| 400 | &dum, &dum); |
|---|
| 401 | assert(CUBIT_SUCCESS == result); |
|---|
| 402 | |
|---|
| 403 | if (0 > list_no || 1 < list_no) return NULL; |
|---|
| 404 | |
|---|
| 405 | if (NULL == pc_lists[list_no] && create_if_missing) { |
|---|
| 406 | pc_lists[list_no] = new std::vector<RefGroup*>(); |
|---|
| 407 | result = |
|---|
| 408 | setArrData(&gentity, 1, pc_tag(), (char*)pc_lists, 2*dum_tag_size); |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | return pc_lists[list_no]; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | void CGMTagManager::pc_list(RefEntity *gentity, std::vector<RefGroup*> *&parents, |
|---|
| 415 | std::vector<RefGroup*> *&children, |
|---|
| 416 | const bool create_if_missing) |
|---|
| 417 | { |
|---|
| 418 | if (NULL == gentity) return; |
|---|
| 419 | |
|---|
| 420 | int dum_tag_size = sizeof(std::vector<RefGroup*>*); |
|---|
| 421 | int dum = 2*dum_tag_size; |
|---|
| 422 | std::vector<RefGroup*> *pc_lists[2]; |
|---|
| 423 | char *dum_ptr = (char*) pc_lists; |
|---|
| 424 | CubitStatus result = |
|---|
| 425 | getArrData(const_cast<const RefEntity**>(&gentity), 1, pc_tag(), |
|---|
| 426 | &dum_ptr, &dum, &dum); |
|---|
| 427 | assert(CUBIT_SUCCESS == result); |
|---|
| 428 | |
|---|
| 429 | if ((NULL == pc_lists[0] || NULL == pc_lists[1]) && create_if_missing) { |
|---|
| 430 | bool must_set = false; |
|---|
| 431 | for (int i = 0; i < 2; i++) { |
|---|
| 432 | if (NULL == pc_lists[i]) { |
|---|
| 433 | pc_lists[i] = new std::vector<RefGroup*>(); |
|---|
| 434 | must_set = true; |
|---|
| 435 | } |
|---|
| 436 | } |
|---|
| 437 | if (must_set) { |
|---|
| 438 | result = setArrData(&gentity, 1, pc_tag(), (char*)pc_lists, |
|---|
| 439 | 2*dum_tag_size); |
|---|
| 440 | assert(CUBIT_SUCCESS == result); |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | parents = pc_lists[0]; |
|---|
| 445 | children = pc_lists[1]; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | void CGMTagManager::get_pc_groups(RefGroup *this_grp, const int p_or_c, const int num_hops, |
|---|
| 449 | std::vector<RefGroup *> &group_ptrs) |
|---|
| 450 | { |
|---|
| 451 | if (NULL == this_grp) return; |
|---|
| 452 | |
|---|
| 453 | int next_hop = num_hops - 1; |
|---|
| 454 | std::vector<RefGroup*> tmp_groups; |
|---|
| 455 | |
|---|
| 456 | // get my children |
|---|
| 457 | std::vector<RefGroup*> *my_children = pc_list(this_grp, p_or_c, false); |
|---|
| 458 | if (NULL != my_children) |
|---|
| 459 | tmp_groups = *my_children; |
|---|
| 460 | |
|---|
| 461 | // get their children if we're not out of hops |
|---|
| 462 | std::vector<RefGroup*>::iterator vit; |
|---|
| 463 | if (0 < next_hop) { |
|---|
| 464 | for (vit = tmp_groups.begin(); vit != tmp_groups.end(); vit++) |
|---|
| 465 | get_pc_groups(*vit, p_or_c, next_hop, group_ptrs); |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | // now add mine to the list; make sure it isn't there already |
|---|
| 469 | for (vit = tmp_groups.begin(); vit != tmp_groups.end(); vit++) { |
|---|
| 470 | if (std::find(group_ptrs.begin(), group_ptrs.end(), *vit) == group_ptrs.end()) |
|---|
| 471 | group_ptrs.push_back(*vit); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | CATSTT::~CATSTT() |
|---|
| 476 | { |
|---|
| 477 | for (std::map<int, void*>::iterator |
|---|
| 478 | mit = tagData.begin(); mit != tagData.end(); mit++) |
|---|
| 479 | if (NULL != (*mit).second) free ((*mit).second); |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | CATSTT::CATSTT(CGMTagManager *manager, RefEntity *entity) |
|---|
| 484 | : CubitAttrib(entity), myManager(manager) |
|---|
| 485 | { |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | CATSTT::CATSTT(CGMTagManager *manager, RefEntity *owner, CubitSimpleAttrib *csa_ptr) |
|---|
| 489 | : CubitAttrib(owner), myManager(manager) |
|---|
| 490 | { |
|---|
| 491 | if (NULL != csa_ptr) add_csa_data(csa_ptr); |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | CubitStatus CATSTT::reset() |
|---|
| 495 | { |
|---|
| 496 | for (std::map<int, void*>::iterator |
|---|
| 497 | mit = tagData.begin(); mit != tagData.end(); mit++) |
|---|
| 498 | if (NULL != (*mit).second) free ((*mit).second); |
|---|
| 499 | |
|---|
| 500 | tagData.clear(); |
|---|
| 501 | |
|---|
| 502 | return CUBIT_SUCCESS; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | CubitSimpleAttrib* CATSTT::cubit_simple_attrib() |
|---|
| 506 | { |
|---|
| 507 | //if (tagData.size() == 0) return NULL; |
|---|
| 508 | |
|---|
| 509 | DLIList<int> int_data; |
|---|
| 510 | DLIList<CubitString*> str_data; |
|---|
| 511 | DLIList<double> dbl_data; |
|---|
| 512 | |
|---|
| 513 | str_data.append(new CubitString(myManager->CATSTT_NAME_INTERNAL)); |
|---|
| 514 | |
|---|
| 515 | // int data first gets the # tags on this entity |
|---|
| 516 | int_data.append(tagData.size()); |
|---|
| 517 | |
|---|
| 518 | // for each tag: |
|---|
| 519 | for (std::map<int, void*>::iterator |
|---|
| 520 | mit = tagData.begin(); mit != tagData.end(); mit++) { |
|---|
| 521 | int tag_handle = (*mit).first; |
|---|
| 522 | CGMTagManager::TagInfo *tinfo = (tag_handle > 0 ? |
|---|
| 523 | &(myManager->tagInfo[tag_handle]) : |
|---|
| 524 | &(myManager->presetTagInfo[-tag_handle])); |
|---|
| 525 | |
|---|
| 526 | // store the name |
|---|
| 527 | str_data.append(new CubitString(tinfo->tagName.c_str())); |
|---|
| 528 | |
|---|
| 529 | // store the length in bytes |
|---|
| 530 | int_data.append(tinfo->tagLength); |
|---|
| 531 | |
|---|
| 532 | // now the data |
|---|
| 533 | // store the raw memory interpreted as an array of ints, padded to a full int |
|---|
| 534 | int tag_ints = tinfo->tagLength/4; |
|---|
| 535 | if (tinfo->tagLength % 4 != 0) tag_ints++; |
|---|
| 536 | |
|---|
| 537 | int *tag_data = reinterpret_cast<int*>((*mit).second); |
|---|
| 538 | for (int i = 0; i < tag_ints; i++) |
|---|
| 539 | int_data.append(tag_data[i]); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | // store the data on the csa |
|---|
| 543 | CubitSimpleAttrib *csa = new CubitSimpleAttrib(&str_data, &dbl_data, &int_data); |
|---|
| 544 | |
|---|
| 545 | for (int i = 0; i < str_data.size(); i++) |
|---|
| 546 | delete str_data.get_and_step(); |
|---|
| 547 | |
|---|
| 548 | return csa; |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | void CATSTT::add_csa_data(CubitSimpleAttrib *csa_ptr) |
|---|
| 552 | { |
|---|
| 553 | // make sure it's a CATSTT |
|---|
| 554 | static CubitString my_type("CA_TSTT"); |
|---|
| 555 | if (csa_ptr->character_type() != my_type) |
|---|
| 556 | return; |
|---|
| 557 | |
|---|
| 558 | csa_ptr->int_data_list()->reset(); |
|---|
| 559 | csa_ptr->string_data_list()->reset(); |
|---|
| 560 | int num_attribs = *(csa_ptr->int_data_list()->get_and_step()); |
|---|
| 561 | |
|---|
| 562 | int *tmp_data; |
|---|
| 563 | |
|---|
| 564 | for (int i = 0; i < num_attribs; i++) { |
|---|
| 565 | |
|---|
| 566 | // map the attrib name to a tag |
|---|
| 567 | std::map<std::string,int>::iterator pos = |
|---|
| 568 | myManager->tagNameMap.find(std::string(csa_ptr->string_data_list()->get()->c_str())); |
|---|
| 569 | |
|---|
| 570 | int thandle = 0; |
|---|
| 571 | |
|---|
| 572 | if (pos == myManager->tagNameMap.end()) { |
|---|
| 573 | // tag doesn't exist - create one |
|---|
| 574 | myManager->createTag(csa_ptr->string_data_list()->get()->c_str(), |
|---|
| 575 | *(csa_ptr->int_data_list()->get()), TSTTG_BYTES, |
|---|
| 576 | NULL, &thandle); |
|---|
| 577 | } |
|---|
| 578 | else thandle = (*pos).second; |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | int tag_handle = reinterpret_cast<int>(thandle); |
|---|
| 582 | |
|---|
| 583 | // copy the ints to a temporary space we can get a ptr to... |
|---|
| 584 | int int_length = *(csa_ptr->int_data_list()->get_and_step()); |
|---|
| 585 | if (int_length % 4 != 0) int_length++; |
|---|
| 586 | tmp_data = (int*) malloc(int_length*sizeof(int)); |
|---|
| 587 | for (int j = 0; j < int_length; j++) |
|---|
| 588 | tmp_data[j] = *(csa_ptr->int_data_list()->get_and_step()); |
|---|
| 589 | |
|---|
| 590 | // now actually set the data |
|---|
| 591 | this->set_tag_data(tag_handle, tmp_data, true); |
|---|
| 592 | } |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | void CATSTT::print() |
|---|
| 596 | { |
|---|
| 597 | std::cout << "This entity has " << tagData.size() << " tags. Types are: " << std::endl; |
|---|
| 598 | for (std::map<int,void*>::iterator mit = tagData.begin(); mit != tagData.end(); mit++) |
|---|
| 599 | { |
|---|
| 600 | if ((*mit).first < 0) |
|---|
| 601 | std::cout << myManager->tagInfo[-(*mit).first].tagName << std::endl; |
|---|
| 602 | else |
|---|
| 603 | std::cout << myManager->tagInfo[(*mit).first].tagName << std::endl; |
|---|
| 604 | } |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | CubitStatus CATSTT::get_tag_data(int tag_handle, void *tag_data) |
|---|
| 608 | { |
|---|
| 609 | assert(NULL != tag_data); |
|---|
| 610 | |
|---|
| 611 | CGMTagManager::TagInfo *tinfo = (tag_handle >= 0 ? |
|---|
| 612 | &(myManager->tagInfo[tag_handle]) : |
|---|
| 613 | &(myManager->presetTagInfo[-tag_handle])); |
|---|
| 614 | |
|---|
| 615 | // check if this attribute has this tag |
|---|
| 616 | std::map<int, void*>::iterator tdpos = tagData.find(tag_handle); |
|---|
| 617 | if (tdpos == tagData.end()) { |
|---|
| 618 | if (NULL != tinfo->defaultValue) |
|---|
| 619 | memcpy(tag_data, tinfo->defaultValue, tinfo->tagLength); |
|---|
| 620 | else return CUBIT_FAILURE; |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | else |
|---|
| 624 | memcpy(tag_data, (*tdpos).second, tinfo->tagLength); |
|---|
| 625 | |
|---|
| 626 | return CUBIT_SUCCESS; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | CubitStatus CATSTT::set_tag_data(int tag_handle, const void *tag_data, |
|---|
| 630 | const bool can_shallow_copy) |
|---|
| 631 | { |
|---|
| 632 | CGMTagManager::TagInfo *tinfo = (tag_handle >= 0 ? |
|---|
| 633 | &(myManager->tagInfo[tag_handle]) : |
|---|
| 634 | &(myManager->presetTagInfo[-tag_handle])); |
|---|
| 635 | |
|---|
| 636 | // check if this attribute has this tag |
|---|
| 637 | std::map<int, void*>::iterator tdpos = tagData.find(tag_handle); |
|---|
| 638 | if (tdpos == tagData.end()) |
|---|
| 639 | tdpos = tagData.insert(tagData.end(), |
|---|
| 640 | std::pair<int,void*>(tag_handle, NULL)); |
|---|
| 641 | |
|---|
| 642 | if (!can_shallow_copy) { |
|---|
| 643 | // need to copy the data; might need to allocate first |
|---|
| 644 | if ((*tdpos).second == NULL) |
|---|
| 645 | (*tdpos).second = malloc(tinfo->tagLength); |
|---|
| 646 | |
|---|
| 647 | memcpy((*tdpos).second, tag_data, tinfo->tagLength); |
|---|
| 648 | } |
|---|
| 649 | else { |
|---|
| 650 | // should shallow copy; might have to delete what's there already |
|---|
| 651 | if ((*tdpos).second != NULL) free((*tdpos).second); |
|---|
| 652 | |
|---|
| 653 | // if shallow copying, caller is saying we can copy, so cast away const |
|---|
| 654 | (*tdpos).second = const_cast<void*>(tag_data); |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | return CUBIT_SUCCESS; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | void CATSTT::remove_tag(int tag_handle) |
|---|
| 661 | { |
|---|
| 662 | tagData.erase(tag_handle); |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | CubitStatus CATSTT::update() |
|---|
| 666 | { |
|---|
| 667 | if (tagData.empty()) |
|---|
| 668 | this->delete_attrib(true); |
|---|
| 669 | |
|---|
| 670 | return CUBIT_SUCCESS; |
|---|
| 671 | } |
|---|