AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
TypeIndex.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TYPEINDEX_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TYPEINDEX_H_
18 
19 #include <sstream>
20 #include <string>
21 #include <typeindex>
22 #include <type_traits>
23 
27 
28 namespace alexaClientSDK {
29 namespace avsCommon {
30 namespace utils {
35 struct TypeIndex {
36 #ifdef ACSDK_USE_RTTI
37 
39  using Value = std::type_index;
40 
41 #else // ACSDK_USE_RTTI
42 
45  using Value = void*;
46 
51  template <typename Type>
52  struct TypeSpecific {
53  static char m_instance;
54  };
55 
56 #endif // ACSDK_USE_RTTI
57 
63  std::string getName() const;
64 
71  bool operator==(TypeIndex rhs) const;
72 
79  bool operator!=(TypeIndex rhs) const;
80 
87  bool operator<(TypeIndex rhs) const;
88 
89 private:
90  friend struct std::hash<TypeIndex>;
91 
92  template <typename Type>
93  friend TypeIndex getTypeIndex();
94 
100  explicit TypeIndex(Value value);
101 
103  Value m_value;
104 };
105 
106 #ifdef ACSDK_USE_RTTI
107 
113 template <typename Type>
114 inline TypeIndex getTypeIndex() {
115  return TypeIndex{typeid(Type)};
116 }
117 
118 inline std::string TypeIndex::getName() const {
119  return m_value.name();
120 }
121 
122 #else // ACSDK_USE_RTTI
123 
124 template <typename Type>
126 
132 template <typename Type>
136 }
137 
139  std::stringstream ss;
140  ss << '[' << m_value << ']';
141  return ss.str();
142 }
143 
144 #endif // ACSDK_USE_RTTI
145 
146 inline TypeIndex::TypeIndex(Value value) : m_value{value} {
147 }
148 
149 inline bool TypeIndex::operator==(TypeIndex x) const {
150  return m_value == x.m_value;
151 }
152 
153 inline bool TypeIndex::operator!=(TypeIndex x) const {
154  return m_value != x.m_value;
155 }
156 
157 inline bool TypeIndex::operator<(TypeIndex x) const {
158  return m_value < x.m_value;
159 }
160 
168 template <typename Type>
169 inline void logTypeIndex(const std::string& name) {
171  .d("name", name)
172  .d("TypeIndex", getTypeIndex<Type>().getName()));
173 }
174 
178 #define ACSDK_LOG_TYPE_INDEX(type) logTypeIndex<type>(#type)
179 
180 } // namespace utils
181 } // namespace avsCommon
182 } // namespace alexaClientSDK
183 
184 namespace std {
185 
186 template <>
187 struct hash<alexaClientSDK::avsCommon::utils::TypeIndex> {
188  std::size_t operator()(alexaClientSDK::avsCommon::utils::TypeIndex typeIndex) const;
189 };
190 
191 inline std::size_t hash<alexaClientSDK::avsCommon::utils::TypeIndex>::operator()(
193  return hash<alexaClientSDK::avsCommon::utils::TypeIndex::Value>()(typeIndex.m_value);
194 }
195 
196 } // namespace std
197 
198 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_TYPEINDEX_H_
static char m_instance
Definition: TypeIndex.h:53
friend TypeIndex getTypeIndex()
Definition: TypeIndex.h:133
::std::string string
Definition: gtest-port.h:1097
bool operator<(TypeIndex rhs) const
Definition: TypeIndex.h:157
void * Value
Definition: TypeIndex.h:45
Definition: CapabilityConfiguration.h:190
bool operator!=(TypeIndex rhs) const
Definition: TypeIndex.h:153
std::string getName() const
Definition: TypeIndex.h:138
void logTypeIndex(const std::string &name)
Definition: TypeIndex.h:169
void acsdkInfo(const LogEntry &entry)
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
type
Definition: upload.py:443
bool operator==(TypeIndex rhs) const
Definition: TypeIndex.h:149
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0