1 // Copyright 2017-2017 the openage authors. See copying.md for legal info.
2 
3 #pragma once
4 
5 #include <map>
6 
7 #include "attribute.h"
8 
9 namespace openage {
10 
11 /**
12  * Contains a group of attributes.
13  * Can contain only one attribute of each type.
14  */
15 class Attributes {
16 public:
Attributes()17 	Attributes() {}
18 
19 	/**
20 	 * Add an attribute or replace any attribute of the same type.
21 	 */
22 	void add(const std::shared_ptr<AttributeContainer> attr);
23 
24 	/**
25 	 * Add copies of all the attributes from the given Attributes.
26 	 */
27 	void add_copies(const Attributes &attrs);
28 
29 	/**
30 	 * Add copies of all the attributes from the given Attributes.
31 	 * If shared is false, shared attributes are ignored.
32 	 * If unshared is false, unshared attributes are ignored.
33 	 */
34 	void add_copies(const Attributes &attrs, bool shared, bool unshared);
35 
36 	/**
37 	 * Remove an attribute based on the type.
38 	 */
39 	bool remove(const attr_type type);
40 
41 	/**
42 	 * Check if the attribute of the given type exists.
43 	 */
44 	bool has(const attr_type type) const;
45 
46 	/**
47 	 * Get the attribute based on the type.
48 	 */
49 	std::shared_ptr<AttributeContainer> get(const attr_type type) const;
50 
51 	/**
52 	 * Get the attribute
53 	 */
54 	template<attr_type T>
get()55 	Attribute<T> &get() const {
56 		return *reinterpret_cast<Attribute<T> *>(this->attrs.at(T).get());
57 	}
58 
59 private:
60 
61 	std::map<attr_type, std::shared_ptr<AttributeContainer>> attrs;
62 };
63 
64 } // namespace openage
65