Honeycomb  0.1
Component-Model Framework
Property.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Misc/Enum.h"
5 #include "Honey/Math/Real.h"
6 
7 namespace honey
8 {
9 
12 
14 class PropertyBase : public Object
15 {
16 public:
19 
20  PropertyBase(const String& name) : _name(name) {}
21 
23  const String& name() const { return _name.name(); }
25  const Id& id() const { return _name; }
27  virtual const NameId& type() const = 0;
29  virtual PropertyBase& clone() const = 0;
30 
31 protected:
32  const NameId _name;
33 };
34 
36 template<class T>
37 class Property : public PropertyBase
38 {
39 public:
42 
43  Property(const String& name) : PropertyBase(name) {}
44  Property(const String& name, const T& val) : PropertyBase(name), _val(val) {}
45  Property(const String& name, T&& val) : PropertyBase(name), _val(move(val)) {}
46  Property(const Property& rhs) : PropertyBase(rhs._name), _val(rhs._val) {}
47 
49  static const NameId& s_type();
50  virtual const NameId& type() const { return s_type(); }
51 
52  virtual Property& clone() const { return *new Property(*this); }
53 
54  Property& operator=(const Property& rhs) { _val = rhs._val; return *this; }
55  Property& operator=(const T& rhs) { _val = rhs; return *this; }
56  Property& operator=(T&& rhs) { _val = move(rhs); return *this; }
57 
58  T& operator*() { return _val; }
59  const T& operator*() const { return _val; }
60  T* operator->() { return &_val; }
61  const T* operator->() const { return &_val; }
62  operator T&() { return _val; }
63  operator const T&() const { return _val; }
64 
65 private:
66  T _val;
67 };
68 
70 template<> inline auto Property<int>::s_type() -> const NameId& { static NameId _("int"); return _; }
72 template<> inline auto Property<Real>::s_type() -> const NameId& { static NameId _("Real"); return _; }
74 template<> inline auto Property<String>::s_type() -> const NameId& { static NameId _("String"); return _; }
75 
77 
78 }
79 
SharedPtr< const Property > ConstPtr
Definition: Property.h:41
Property & operator=(const Property &rhs)
Definition: Property.h:54
Property(const String &name)
Definition: Property.h:43
Holds both a name string and its hashed value, and unlike Id the name is never compiled out...
Definition: Id.h:144
const NameId _name
Definition: Property.h:32
const T * operator->() const
Definition: Property.h:61
Combined intrusive/non-intrusive smart pointer. Can reference and share any object automatically...
Definition: SharedPtr.h:175
Property(const String &name, const T &val)
Definition: Property.h:44
SharedPtr< Property > Ptr
Definition: Property.h:40
Property(const Property &rhs)
Definition: Property.h:46
virtual const NameId & type() const
Get property type info.
Definition: Property.h:50
static auto _
Definition: Module.cpp:8
T & operator*()
Definition: Property.h:58
Property & operator=(T &&rhs)
Definition: Property.h:56
const String & name() const
Get property name.
Definition: Property.h:23
SharedPtr< PropertyBase > Ptr
Definition: Property.h:17
virtual PropertyBase & clone() const =0
Create a clone of this property.
virtual Property & clone() const
Create a clone of this property.
Definition: Property.h:52
Base class for all properties.
Definition: Property.h:14
PropertyBase(const String &name)
Definition: Property.h:20
T * operator->()
Definition: Property.h:60
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
virtual const NameId & type() const =0
Get property type info.
const Id & id() const
Get property id.
Definition: Property.h:25
const T & operator*() const
Definition: Property.h:59
Property(const String &name, T &&val)
Definition: Property.h:45
static const NameId & s_type()
Static function to get property type info.
Property & operator=(const T &rhs)
Definition: Property.h:55
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
SharedPtr< const PropertyBase > ConstPtr
Definition: Property.h:18
const String & name() const
Definition: Id.h:155
Base class for objects.
Definition: Object.h:12
Global Honeycomb namespace.
Generic property.
Definition: Property.h:37