Cogs.Core
Name.h
1#pragma once
2
3#include "../StringView.h"
4
5#include <type_traits>
6
7
10 template<typename>
11 constexpr bool always_false = false;
12}
13
15template<typename T>
16Cogs::StringView getNameImpl(T *)
17{
18 static_assert(Cogs::Reflection::detail::always_false<T> && "No name specialization provided. Implement getName<Type>() or getNameImpl(Type *).");
19 return Cogs::StringView();
20}
21
22template<typename T>
23Cogs::StringView getName()
24{
25 return getNameImpl(static_cast<typename std::remove_extent_t<typename std::remove_pointer_t<T>>*>(nullptr));
26}
27
28template<> inline Cogs::StringView getName<void *>() { return "pointer"; }
29
30namespace Cogs
31{
33 namespace Reflection
34 {
36 using CreateInstance = void* (*)();
37
39 using ConstructInstance = void* (*)(void*);
40
42 using DestroyInstance = void (*)(void*);
43
45 using DestructInstance = void (*)(void *);
46
48 using TypeId = uint16_t;
49
51 constexpr TypeId NoType = 0xFFFF;
52
54 using FieldId = uint16_t;
55
57 using MethodId = uint16_t;
58
60 constexpr FieldId NoField = 0xFFFF;
61
63 constexpr MethodId NoMethod = 0xFFFF;
64
69 struct COGSFOUNDATION_API Name
70 {
72 Name() = default;
73
79 Name(const StringView & name) : name(name), id(Cogs::hash(name)) {}
80
84 Name(const char * name) : Name(StringView(name)) {}
85
89 Name(const std::string & name) : Name(StringView(name)) {}
90
92 Name(const Name & other) = default;
93
95 Name(Name && other) noexcept = default;
96
98 Name & operator=(const Name & other) = default;
99
101 Name & operator=(Name && other) noexcept = default;
102
104 [[nodiscard]]
105 bool operator==(const Name & other) const
106 {
107 return this->id == other.id;
108 }
109
111 [[nodiscard]]
112 const std::string & getName() const { return name; }
113
115 [[nodiscard]]
116 const char * c_str() const { return name.c_str(); }
117
119 [[nodiscard]]
120 size_t getId() const { return id; }
121
122 private:
124 std::string name;
125 size_t id = Cogs::hash();
127 };
128 }
129}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Resepy from: https://developercommunity.visualstudio.com/t/C2607-after-upgrading-to-Visual-Studio-1/1...
Definition: Name.h:9
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
void(*)(void *) DestroyInstance
Object destroy function type. Comparable to regular delete.
Definition: Name.h:42
void *(*)(void *) ConstructInstance
Object construction function type. Comparable to placement new.
Definition: Name.h:39
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
constexpr FieldId NoField
No field id.
Definition: Name.h:60
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
constexpr MethodId NoMethod
No method id.
Definition: Name.h:63
void *(*)() CreateInstance
Object creation function type. Comparable to regular new.
Definition: Name.h:36
void(*)(void *) DestructInstance
Object destruction function type. Comparable to calling the destructor.
Definition: Name.h:45
uint16_t MethodId
Type used to index methods.
Definition: Name.h:57
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
Definition: HashFunctions.h:62
Represents an unique name.
Definition: Name.h:70
Name(const char *name)
Construct a new unique name from the given null terminated C string.
Definition: Name.h:84
Name()=default
Default constructor.
const char * c_str() const
Gets the name as a null-terminated string.
Definition: Name.h:116
Name(const Name &other)=default
Defaulted copy constructor.
Name & operator=(Name &&other) noexcept=default
Default move assignment operator.
Name(const StringView &name)
Construct a new unique name instance from the given name string.
Definition: Name.h:79
size_t getId() const
Get the unique identifier of this name.
Definition: Name.h:120
Name & operator=(const Name &other)=default
Default copy assignment operator.
Name(Name &&other) noexcept=default
Default move constructor.
const std::string & getName() const
Get the string name. This can be empty, even in valid instances of Name.
Definition: Name.h:112
bool operator==(const Name &other) const
Compare to other. Considered equal if the identifier is the same.
Definition: Name.h:105
Name(const std::string &name)
Construct a new unique name from the given standard string.
Definition: Name.h:89