C++ Joueur Client
 All Classes Namespaces Functions Variables
base_object.hpp
1 #ifndef BASE_OBJECT_HPP
2 #define BASE_OBJECT_HPP
3 
4 #include <vector>
5 #include <string>
6 #include <unordered_map>
7 #include <initializer_list>
8 #include <utility>
9 #include <iostream>
10 #include <typeindex>
11 
12 #include "sgr.hpp"
13 #include "delta_mergable.hpp"
14 #include "base_game.hpp"
15 
16 namespace cpp_client
17 {
18 
19 class Base_object : public Delta_mergable
20 {
21 public:
22  Base_object(std::initializer_list<std::pair<std::string, Any&&>> init);
23  virtual ~Base_object();
24  Base_object() noexcept;
25 
31  template<typename T>
32  std::shared_ptr<typename T::element_type> as()
33  {
34  auto& self = get_game()->get_objects()[get_id()];
35  return std::dynamic_pointer_cast<typename T::element_type>(self);
36  }
37 
42  template<typename T>
43  bool is()
44  {
45  return (this->as<T>() != nullptr);
46  }
47 
48  //Don't really like doing this, but I can't think of a better way
50  virtual void resize(const std::string& name, std::size_t size) override {}
51  virtual void change_vec_values(const std::string& name,
52  std::vector<std::pair<std::size_t, Any>>& values) override {}
53  virtual void remove_key(const std::string& name, Any& key) override {}
54  virtual std::unique_ptr<Any>
55  add_key_value(const std::string& name, Any& key, Any& value) override;
56  virtual bool is_map(const std::string& name) override { return false; }
57  virtual Base_game* get_game() { return nullptr; }
58  virtual void rebind_by_name(Any* to_change, const std::string& member, std::shared_ptr<Base_object> ref) override {}
60 
61 private:
62 
63  const std::string& get_id() const noexcept;
64 };
65 
66 } // cpp_client
67 
68 #endif // BASE_OBJECT_HPP
Definition: base_object.hpp:19
bool is()
Determines if this object is of the specified object type
Definition: base_object.hpp:43
std::shared_ptr< typename T::element_type > as()
Dynamically casts this object to another type. Returns nullptr if it cannot be converted to the type...
Definition: base_object.hpp:32