NCBI C++ Toolkit Cross Reference

  C++/src/objects/general/User_object.cpp


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
/* $Id: User_object.cpp 68019 2015-07-02 12:58:35Z bollin $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: ....... * * File Description: * ....... * * Remark: * This code was originally generated by application DATATOOL * using specifications from the ASN data definition file * 'general.asn'. */ // standard includes // generated includes #include <ncbi_pch.hpp> #include <objects/general/User_object.hpp> #include <objects/general/User_field.hpp> #include <objects/general/Object_id.hpp> // generated classes BEGIN_NCBI_SCOPE BEGIN_objects_SCOPE // namespace ncbi::objects:: // destructor CUser_object::~CUser_object(void) { } // // retrieve a named field. The named field can recurse, depending // on a set of user-defined delimiters // const CUser_field& CUser_object::GetField(const string& str, const string& delim) const { CConstRef<CUser_field> ref(GetFieldRef(str, delim)); if (ref.Empty()) { NCBI_THROW(CCoreException, eNullPtr, "Unable to find User-field " + str); } return *ref; } CConstRef<CUser_field> CUser_object::GetFieldRef(const string& str, const string& delim) const { list<string> toks; NStr::Split(str, delim, toks); if ( !toks.size() ) { return CConstRef<CUser_field>(); } /// /// step 1: scan one level deep for a nested object /// we scan only with the first token /// string sub; string first; {{ list<string>::iterator iter = toks.begin(); first = *iter; for (++iter; iter != toks.end(); ++iter) { if ( !sub.empty() ) { sub += delim; } sub += *iter; } }} ITERATE(TData, field_iter, GetData()) { bool found = false; const CUser_field& field = **field_iter; if (field.IsSetLabel() && field.GetLabel().IsStr()) { const string& this_label = field.GetLabel().GetStr(); if (this_label == first) { found = true; } } if ( !found ) { continue; } if ( !sub.empty() ) { CConstRef<CUser_field> field_ref = (*field_iter)->GetFieldRef(sub, delim); if (field_ref) { return field_ref; } } else { return CConstRef<CUser_field>(&field); } } return CConstRef<CUser_field>(); } bool CUser_object::HasField(const string& str, const string& delim) const { return GetFieldRef(str, delim).GetPointer() ? true : false; } // // retrieve a named field. The named field can recurse, depending // on a set of user-defined delimiters // CUser_field& CUser_object::SetField(const string& str, const string& delim, const string& obj_subtype) { return *SetFieldRef(str, delim, obj_subtype); } CRef<CUser_field> CUser_object::SetFieldRef(const string& str, const string& delim, const string& /* obj_subtype */) { list<string> toks; NStr::Split(str, delim, toks); CRef<CUser_field> field_ref; /// pass 1: see if we have a field that starts with this label already NON_CONST_ITERATE(TData, field_iter, SetData()) { CUser_field& field = **field_iter; if (field.GetLabel().IsStr() && field.GetLabel().GetStr() == toks.front()) { field_ref = *field_iter; break; } } if ( !field_ref ) { field_ref.Reset(new CUser_field()); field_ref->SetLabel().SetStr(toks.front()); SetData().push_back(field_ref); } toks.pop_front(); if (toks.size()) { string s = NStr::Join(toks, delim); CRef<CUser_field> f = field_ref->SetFieldRef(s, delim); field_ref = f; } return field_ref; } // add a data field to the user object that holds a given value CUser_object& CUser_object::AddField(const string& label, const string& value, EParseField parse) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); switch (parse) { case eParse_Number: try { int i = NStr::StringToInt(value); field->SetData().SetInt(i); break; } catch (...) { } try { double d = NStr::StringToDouble(value); field->SetData().SetReal(d); break; } catch (...) { } field->SetData().SetStr(CUtf8::AsUTF8(value,eEncoding_UTF8)); break; default: case eParse_String: field->SetData().SetStr(CUtf8::AsUTF8(value,eEncoding_UTF8)); break; } SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, int value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetData().SetInt(value); SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, double value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetData().SetReal(value); SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, bool value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetData().SetBool(value); SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, CUser_object& object) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetData().SetObject(object); SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, const vector<string>& value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetNum(value.size()); CUser_field_Base::C_Data::TStrs& dest = field->SetData().SetStrs(); dest.clear(); ITERATE(vector<string>, v, value) { dest.push_back( CUtf8::AsUTF8( *v,eEncoding_UTF8)); } SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, const vector<int>& value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetNum(value.size()); field->SetData().SetInts() = value; SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, const vector<double>& value) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetNum(value.size()); field->SetData().SetReals() = value; SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, const vector< CRef<CUser_object> >& objects) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetNum(objects.size()); field->SetData().SetObjects() = objects; SetData().push_back(field); return *this; } CUser_object& CUser_object::AddField(const string& label, const vector<CRef<CUser_field> >& objects) { CRef<CUser_field> field(new CUser_field()); field->SetLabel().SetStr(label); field->SetNum(objects.size()); field->SetData().SetFields() = objects; SetData().push_back(field); return *this; } // static consts here allow us to use reference counting static const char* s_ncbi("NCBI"); static const char* s_expres("experimental_results"); static const char* s_exp("experiment"); static const char* s_sage("SAGE"); static const char* s_tag("tag"); static const char* s_count("count"); // accessors: classify a given user object CUser_object::ECategory CUser_object::GetCategory(void) const { if (!IsSetClass() || GetClass() != s_ncbi) { // we fail to recognize non-NCBI classes of user-objects return eCategory_Unknown; } // // experimental results // if (GetType().IsStr() && NStr::CompareNocase(GetType().GetStr(), s_expres) == 0 && GetData().size() == 1) { ITERATE (CUser_object::TData, iter, GetData()) { const CUser_field& field = **iter; const CUser_field::TData& data = field.GetData(); if (data.Which() != CUser_field::TData::e_Object || !field.IsSetLabel() || !field.GetLabel().IsStr() || NStr::CompareNocase(field.GetLabel().GetStr(), s_exp) != 0) { // poorly formed experiment spec return eCategory_Unknown; } } return eCategory_Experiment; } // // unrecognized - catch-all // return eCategory_Unknown; } // sub-category accessors: CUser_object::EExperiment CUser_object::GetExperimentType(void) const { // check to see if we have an experiment if ( GetCategory() != eCategory_Experiment) { return eExperiment_Unknown; } // we do - so we have one nested user object that contains the // specification of the experimental data const CUser_field& field = *GetData().front(); const CUser_object& obj = field.GetData().GetObject(); if (obj.GetType().IsStr() && NStr::CompareNocase(obj.GetType().GetStr(), s_sage) == 0) { return eExperiment_Sage; } // // catch-all // return eExperiment_Unknown; } // sub-category accessors: const CUser_object& CUser_object::GetExperiment(void) const { switch (GetExperimentType()) { case eExperiment_Sage: // we have one nested user object that contains the // specification of the experimental data return GetData().front()->GetData().GetObject(); case eExperiment_Unknown: default: return *this; } } // // format the type specification fo a given user object // static string s_GetUserObjectType(const CUser_object& obj) { switch (obj.GetCategory()) { case CUser_object::eCategory_Experiment: switch (obj.GetExperimentType()) { case CUser_object::eExperiment_Sage: return "SAGE"; case CUser_object::eExperiment_Unknown: default: return "Experiment"; } break; case CUser_object::eCategory_Unknown: default: break; } return "User"; } static string s_GetUserObjectContent(const CUser_object& obj) { switch (obj.GetCategory()) { case CUser_object::eCategory_Experiment: switch (obj.GetExperimentType()) { case CUser_object::eExperiment_Sage: {{ string label; const CUser_object& nested_obj = obj.GetData().front()->GetData().GetObject(); // grab the tag and count fields const CUser_field* tag = NULL; const CUser_field* count = NULL; ITERATE (CUser_object::TData, iter, nested_obj.GetData()) { const CUser_field& field = **iter; if (!field.GetLabel().IsStr()) { continue; } const string& lbl = field.GetLabel().GetStr(); if (NStr::CompareNocase(lbl, s_tag) == 0) { tag = &field; } else if (NStr::CompareNocase(lbl, s_count) == 0) { count = &field; } } if (tag && tag->GetData().IsStr()) { if ( !label.empty() ) { label += " "; } label += string(s_tag) + "=" + tag->GetData().GetStr(); } if (count && count->GetData().IsInt()) { if ( !label.empty() ) { label += " "; } label += string(s_count) + "=" + NStr::IntToString(count->GetData().GetInt()); } return label; }} case CUser_object::eExperiment_Unknown: default: break; } return "[experiment]"; case CUser_object::eCategory_Unknown: default: break; } return "[User]"; } // // append a formatted string to a label describing this object // void CUser_object::GetLabel(string* label, ELabelContent mode) const { // Check the label is not null if (!label) { return; } switch (mode) { case eType: *label += s_GetUserObjectType(*this); break; case eContent: *label += s_GetUserObjectContent(*this); break; case eBoth: *label += s_GetUserObjectType(*this) + ": " + s_GetUserObjectContent(*this); break; } } CUser_object& CUser_object::SetCategory(ECategory category) { Reset(); SetClass(s_ncbi); switch (category) { case eCategory_Experiment: SetType().SetStr(s_expres); {{ CRef<CUser_object> subobj(new CUser_object()); AddField(s_exp, *subobj); SetClass(s_ncbi); return *subobj; }} case eCategory_Unknown: default: break; } return *this; } CUser_object& CUser_object::SetExperiment(EExperiment category) { Reset(); SetClass(s_ncbi); switch (category) { case eExperiment_Sage: SetType().SetStr(s_sage); break; case eExperiment_Unknown: default: break; } return *this; } const string kDBLink = "DBLink"; const string kStructuredComment = "StructuredComment"; const string kOriginalId = "OriginalID"; const string kOrigIdAltSpell = "OrginalID"; const string kUnverified = "Unverified"; const string kValidationSuppression = "ValidationSuppression"; const string kNcbiCleanup = "NcbiCleanup"; CUser_object::EObjectType CUser_object::GetObjectType() const { if (!IsSetType() || !GetType().IsStr()) { return eObjectType_Unknown; } EObjectType rval = eObjectType_Unknown; string label = GetType().GetStr(); if (NStr::Equal(label, kDBLink)) { rval = eObjectType_DBLink; } else if (NStr::Equal(label, kStructuredComment)) { rval = eObjectType_StructuredComment; } else if (NStr::Equal(label, kOriginalId)) { rval = eObjectType_OriginalId; } else if (NStr::Equal(label, kOrigIdAltSpell)) { rval = eObjectType_OriginalId; } else if (NStr::Equal(label, kUnverified)) { rval = eObjectType_Unverified; } else if (NStr::Equal(label, kValidationSuppression)) { rval = eObjectType_ValidationSuppression; } else if (NStr::Equal(label, kNcbiCleanup)) { rval = eObjectType_Cleanup; } return rval; } void CUser_object::SetObjectType(EObjectType obj_type) { switch (obj_type) { case eObjectType_DBLink: SetType().SetStr(kDBLink); break; case eObjectType_StructuredComment: SetType().SetStr(kStructuredComment); break; case eObjectType_OriginalId: SetType().SetStr(kOriginalId); break; case eObjectType_Unverified: SetType().SetStr(kUnverified); break; case eObjectType_ValidationSuppression: SetType().SetStr(kValidationSuppression); break; case eObjectType_Cleanup: SetType().SetStr(kNcbiCleanup); break; case eObjectType_Unknown: ResetType(); break; } } bool CUser_object::x_IsUnverifiedType(const string& val, const CUser_field& field) const { if (field.IsSetLabel() && field.GetLabel().IsStr() && NStr::Equal(field.GetLabel().GetStr(), "Type") && field.IsSetData() && field.GetData().IsStr() && NStr::Equal(field.GetData().GetStr(), val)) { return true; } else { return false; } } bool CUser_object::x_IsUnverifiedType(const string& val) const { if (GetObjectType() != eObjectType_Unverified) { return false; } if (!IsSetData()) { return false; } bool found = false; ITERATE(CUser_object::TData, it, GetData()) { if (x_IsUnverifiedType(val, **it)) { found = true; } } return found; } void CUser_object::x_AddUnverifiedType(const string& val) { SetObjectType(eObjectType_Unverified); if (x_IsUnverifiedType(val)) { // value already set, nothing to do return; } CRef<CUser_field> f(new CUser_field()); f->SetLabel().SetStr("Type"); f->SetData().SetStr(val); SetData().push_back(f); } void CUser_object::x_RemoveUnverifiedType(const string& val) { if (GetObjectType() != eObjectType_Unverified) { return; } if (!IsSetData()) { return; } CUser_object::TData::iterator it = SetData().begin(); while (it != SetData().end()) { if (x_IsUnverifiedType(val, **it)) { it = SetData().erase(it); } else { it++; } } if (GetData().empty()) { ResetData(); } } static const string kUnverifiedOrganism = "Organism"; bool CUser_object::IsUnverifiedOrganism() const { return x_IsUnverifiedType(kUnverifiedOrganism); } void CUser_object::AddUnverifiedOrganism() { x_AddUnverifiedType(kUnverifiedOrganism); } void CUser_object::RemoveUnverifiedOrganism() { x_RemoveUnverifiedType(kUnverifiedOrganism); } static const string kUnverifiedFeature = "Feature"; bool CUser_object::IsUnverifiedFeature() const { return x_IsUnverifiedType(kUnverifiedFeature); } void CUser_object::AddUnverifiedFeature() { x_AddUnverifiedType(kUnverifiedFeature); } void CUser_object::RemoveUnverifiedFeature() { x_RemoveUnverifiedType(kUnverifiedFeature); } void CUser_object::UpdateNcbiCleanup(int version) { SetObjectType(eObjectType_Cleanup); CRef<CUser_field> method = SetFieldRef("method"); method->SetData().SetStr(CUtf8::AsUTF8("ExtendedSeqEntryCleanup", eEncoding_Ascii)); CRef<CUser_field> version_field = SetFieldRef("version"); version_field->SetData().SetInt(version); // get current time CTime curr_time(CTime::eCurrent); CRef<CUser_field> month = SetFieldRef("month"); month->SetData().SetInt(curr_time.Month()); CRef<CUser_field> day = SetFieldRef("day"); day->SetData().SetInt(curr_time.Day()); CRef<CUser_field> year = SetFieldRef("year"); year->SetData().SetInt(curr_time.Year()); } END_objects_SCOPE // namespace ncbi::objects:: END_NCBI_SCOPE /* Original file checksum: lines: 64, chars: 1893, CRC32: f7a70d1d */

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.