smutnyjoe

Untitled

Dec 15th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1.     // Creates node representing virus of given id by constructing it from one parent.
  2.     // Throws VirusAlreadyCreated if virus of given id already exists.
  3.     // Throws VirusNotFound if given parent doesn't exist.
  4.     void create(typename Virus::id_type const &id, typename Virus::id_type const &parent_id)
  5.     {
  6.         if (exists(id)) {
  7.             throw VirusAlreadyCreated();
  8.         }
  9.  
  10.         if (!exists(parent_id)) {
  11.             throw VirusNotFound();
  12.         }
  13.  
  14.         // Creates new virus as a pair of id and smart pointer to its wrapper to be inserted to virus_map:
  15.         std::pair<typename Virus::id_type, std::shared_ptr<VirusGenealogyWrapper<Virus>>>
  16.             n(id, std::make_shared<VirusGenealogyWrapper<Virus> >(id));
  17.         // Inserts new pair into virus_map;
  18.         virus_map.insert(n); // strong guarantee, if fails, nothing changes
  19.  
  20.         // Inserts new parent into the copy of id's parents set
  21.         std::set<std::shared_ptr<VirusGenealogyWrapper<Virus>>> parents(n.second->parents);
  22.         parents.insert(virus_map.find(parent_id)->second);
  23.  
  24.         // Inserts new child into the copy of id's parent's children set:
  25.         std::set<std::shared_ptr<VirusGenealogyWrapper<Virus>>> parent_children(virus_map.find(parent_id)->second->children);
  26.         parent_children.insert(virus_map.find(id)->second);
  27.  
  28.         // If none of above threw the exception, it means real sets can now be updated by no-throw swap:
  29.         n.second->parents.swap(parents);
  30.         virus_map.find(parent_id)->second->children.swap(parent_children);
  31.  
  32.     }
Add Comment
Please, Sign In to add comment