};
+/**
+ * Managed pointer for components.
+ */
template <typename T>
class ComponentPtr {
public:
ComponentPtr() : id_(0), manager_(nullptr) {}
T *operator -> ();
-
const T *operator -> () const;
- operator bool () const;
+ operator bool () const { return valid(); }
+ bool valid() const;
bool operator == (const ComponentPtr<T> &other) const {
return other.id_ == id_ && other.manager_ == manager_;
ComponentPtr(Entity::Id id, EntityManager *manager) : id_(id), manager_(manager) {}
+ T *get_component_ptr();
+ const T *get_component_ptr() const;
+
template <typename R>
friend inline std::ostream &operator << (std::ostream &out, const ComponentPtr<R> &ptr);
};
+
/**
* Emitted when an entity is added to the system.
*/
};
-template <typename T>
-inline std::ostream &operator << (std::ostream &out, const ComponentPtr<T> &ptr) {
- return out << *(ptr.manager_->template get_component_ptr<T>(ptr.id_));
-}
-
-
/**
* Manages Entity::Id creation and component assignment.
*/
std::list<uint32_t> free_list_;
};
+
template <typename C>
BaseComponent::Family Component<C>::family() {
static Family family = family_counter_++;
return family;
}
+
template <typename C, typename ... Args>
ComponentPtr<C> Entity::assign(Args && ... args) {
assert(valid());
return !manager_.expired() && manager_.lock()->valid(id_);
}
+
template <typename T>
inline T *ComponentPtr<T>::operator -> () {
return manager_->get_component_ptr<T>(id_);
}
template <typename T>
-inline ComponentPtr<T>::operator bool () const {
+inline bool ComponentPtr<T>::valid() const {
return manager_ && manager_->valid(id_);
}
+template <typename T>
+inline T *ComponentPtr<T>::get_component_ptr() {
+ return manager_->get_component_ptr<T>(id_);
+}
+
+template <typename T>
+inline const T *ComponentPtr<T>::get_component_ptr() const {
+ return manager_->get_component_ptr<T>(id_);
+}
+
+template <typename T>
+inline std::ostream &operator << (std::ostream &out, const ComponentPtr<T> &ptr) {
+ return out << *(ptr.get_component_ptr());
+}
} // namespace entityx