Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. Class Foo
  2. {
  3. public:
  4.  
  5. static unsigned std::set<unsigned long> s_usedID;
  6.  
  7. static unsigned long generateID() // Generate a valid ID
  8. {
  9. static unsigned long id = 0;
  10.  
  11. while (Foo::isIDUsed(id)) // If all ID are taken, create an infinite loop!
  12. ++id;
  13.  
  14. return id;
  15. }
  16.  
  17. static void addID(unsigned long id) // Add a given ID to the set of used ID
  18. {
  19. s_usedID.insert(id);
  20. }
  21.  
  22. static void removeID(unsigned long id) // Remove a given ID from the set of used ID
  23. {
  24. s_usedID.erase(id);
  25. }
  26.  
  27. static bool isIDUsed(unsigned long id)
  28. {
  29. return s_usedID.count(id) == 1 ? true:false;
  30. }
  31.  
  32. explicit Foo() : m_id(Foo::generateID())
  33. {
  34. Foo::addID(m_id); // ID is now taken
  35. }
  36.  
  37. virtual ~Foo()
  38. {
  39. Foo::removeID(m_id); // Free the ID
  40. }
  41.  
  42. private:
  43.  
  44. unsigned long m_id;
  45.  
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement