has_id.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* has_id.h - template for entities with Id
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef CORE_NODE_HAS_ID_H_85A247EB_F007_5E20_80F3_8F1BA2F3FB36
  18. #define CORE_NODE_HAS_ID_H_85A247EB_F007_5E20_80F3_8F1BA2F3FB36
  19. #include <core/std/string.h>
  20. namespace rainynite::core {
  21. template <typename Id, typename Generator>
  22. class HasId {
  23. public:
  24. Id get_id() const {
  25. return id;
  26. }
  27. void set_id(Id id_) {
  28. id = id_;
  29. }
  30. void new_id() {
  31. id = Generator()();
  32. }
  33. private:
  34. Id id;
  35. };
  36. template <typename Id, typename G>
  37. string id_to_string(HasId<Id, G> const& has_id) {
  38. return to_string(has_id.get_id());
  39. }
  40. } // namespace rainynite::core
  41. #endif