SHOW:
|
|
- or go back to the newest paste.
| 1 | #pragma once | |
| 2 | ||
| 3 | namespace Nio | |
| 4 | {
| |
| 5 | namespace Managers | |
| 6 | {
| |
| 7 | template<class Type> class Manager | |
| 8 | {
| |
| 9 | protected: | |
| 10 | std::vector<Type> objects; | |
| 11 | std::function<Type(const std::string&)> construct; | |
| 12 | ||
| 13 | public: | |
| 14 | inline Manager(const std::function<Type(const std::string&)> &construct = [](const std::string &name) -> Type { return Type(name); }) :
| |
| 15 | construct(construct) | |
| 16 | {
| |
| 17 | } | |
| 18 | ||
| 19 | inline ~Manager() | |
| 20 | {
| |
| 21 | } | |
| 22 | ||
| 23 | - | inline Type* add(const std::string &name) |
| 23 | + | inline Type& add(const std::string &name) |
| 24 | {
| |
| 25 | this->objects.push_back(this->construct(name)); | |
| 26 | - | return this->objects.back(); |
| 26 | + | return *(this->objects.back()); |
| 27 | } | |
| 28 | ||
| 29 | - | inline Type* get(const std::string &name) |
| 29 | + | inline Type& get(const std::string &name) |
| 30 | {
| |
| 31 | typename std::vector<Type>::iterator index = std::find_if(this->objects.begin(), this->objects.end(), [name](const Type &object) | |
| 32 | {
| |
| 33 | if (object.getName() == name) return true; | |
| 34 | return false; | |
| 35 | }); | |
| 36 | if (index == this->objects.end()) return this->add(name); | |
| 37 | - | return index; |
| 37 | + | return *index; |
| 38 | } | |
| 39 | }; | |
| 40 | } | |
| 41 | } |