Ocular Engine
Object.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_OBJECT__H__
19 #define __H__OCULAR_OBJECT__H__
20 
21 #include "ObjectIO/ObjectIO.hpp"
22 #include "UUID.hpp"
23 
24 #include <string>
25 
26 //------------------------------------------------------------------------------------------
27 
32 namespace Ocular
33 {
38  namespace Core
39  {
44  class Object : public ObjectIO
45  {
46  public:
47 
53  Object(std::string name, std::string className);
54 
59  Object(std::string name);
60 
64  Object();
65 
69  virtual ~Object();
70 
74  std::string const& getName() const;
75 
80  void setName(std::string name);
81 
85  std::string const& getClass() const;
86 
91  void setUUID(std::string const& uuid);
92 
96  UUID const& getUUID() const;
97 
101  int64_t getCreationTime() const;
102 
106  virtual std::string toString() const;
107 
111  template<class T> bool isType()
112  {
113  /* Unfortunately, this dynamic_cast seems to be the best solution.
114  * Using std::is_base_of, std::is_convertible, typeid() ==, etc. just aren't sufficient.
115  *
116  * The issue is that a common use-case for this method is checking if an event is a
117  * certain type. For example, we get an Ocular::Core::AEvent* and we want to see if it
118  * is an instance of Ocular::Core::MouseScrollInputEvent.
119  *
120  * The other approaches such as:
121  *
122  * std::is_base_of<T, std::remove_pointer<decltype(this)>::type>::value
123  * typeid(T*) == typeid(*this)
124  *
125  * Work fine if checking making this comparison in a vaccuum. Ie:
126  *
127  * std::is_convertible<AEvent*, MouseScrollInputEvent*> -> true
128  * std::is_convertible<MouseScrollInputEvent*, AEvent*> -> true
129  *
130  * But since this a method of Object, when we call `this`, all of the types return
131  * the type of `Object` and not whatever the actual derived caller may be (in this
132  * example MouseScrollInputEvent).
133  */
134 
135  return (dynamic_cast<T*>(this) ? true : false);
136  }
137 
138  protected:
139 
140  std::string m_Name;
141  std::string m_Class;
143  UUID m_UUID;
144  const int64_t m_CreationTime;
145 
146  private:
147  };
148  }
152 }
157 //------------------------------------------------------------------------------------------
158 
159 #endif
std::string const & getName() const
Definition: Object.cpp:78
int64_t getCreationTime() const
Definition: Object.cpp:102
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
std::string m_Class
Definition: Object.hpp:141
virtual std::string toString() const
Definition: Object.cpp:107
void setUUID(std::string const &uuid)
Definition: Object.cpp:88
Definition: ObjectIO.hpp:38
Definition: UUID.hpp:45
std::string m_Name
Definition: Object.hpp:140
UUID const & getUUID() const
Definition: Object.cpp:97
void setName(std::string name)
Sets the name of the Object.
Definition: Object.cpp:73
Base class of all representable Ocular constructs.
Definition: Object.hpp:44
std::string const & getClass() const
Definition: Object.cpp:83
Object()
Creates a new Object with the default name ('Name').
Definition: Object.cpp:53