|
12 | 12 |
|
13 | 13 | namespace cb
|
14 | 14 | {
|
15 |
| - /// Contains one or more commands packed together in a specific order. |
16 |
| - struct CommandPacket |
17 |
| - { |
18 |
| - CB_DECLARE_COMMAND_DEBUG(); |
19 |
| - |
20 |
| - template <class CommandClass> |
21 |
| - struct is_valid_type |
22 |
| - { |
23 |
| - enum |
24 |
| - { |
25 |
| - // must not be CommandPacket and must be pod, not void |
26 |
| - value = std::is_same<CommandPacket, CommandClass>::value == 0 && // |
27 |
| - std::is_void<CommandClass>::value == 0 && cb::detail::is_pod<CommandClass>::value |
28 |
| - }; |
29 |
| - }; |
30 |
| - |
31 |
| - template <class CommandClass, class AllocatorClass> |
32 |
| - static CommandPacket* create(AllocatorClass& allocator, uint32_t auxilarySize); |
33 |
| - |
34 |
| - template <class CommandClass> |
35 |
| - static CommandPacket* getCommandPacket(CommandClass* commandData); |
36 |
| - template <class CommandClass> |
37 |
| - static const CommandPacket* getCommandPacket(const CommandClass* commandData); |
38 |
| - template <class CommandClass> |
39 |
| - static CommandClass* getCommandData(CommandPacket* packet); |
40 |
| - template <class CommandClass, typename CastType> |
41 |
| - static CastType* getAuxilaryData(CommandClass* commandData); |
42 |
| - |
43 |
| - static void dispatch(const cb::CommandPacket* packet, cb::RenderContext* rc); |
| 15 | + /// Contains one or more commands packed together in a specific order. |
| 16 | + struct CommandPacket |
| 17 | + { |
| 18 | + CB_DECLARE_COMMAND_DEBUG(); |
| 19 | + |
| 20 | + template <class CommandClass> |
| 21 | + struct is_valid_type |
| 22 | + { |
| 23 | + enum |
| 24 | + { |
| 25 | + // must not be CommandPacket and must be pod, not void |
| 26 | + value = std::is_same<CommandPacket, CommandClass>::value == 0 && // |
| 27 | + std::is_void<CommandClass>::value == 0 && cb::detail::is_pod<CommandClass>::value |
| 28 | + }; |
| 29 | + }; |
| 30 | + |
| 31 | + template <class CommandClass, class AllocatorClass> |
| 32 | + static CommandPacket* create(AllocatorClass& allocator, uint32_t auxilarySize); |
| 33 | + |
| 34 | + template <class CommandClass> |
| 35 | + static CommandPacket* getCommandPacket(CommandClass* commandData); |
| 36 | + template <class CommandClass> |
| 37 | + static const CommandPacket* getCommandPacket(const CommandClass* commandData); |
| 38 | + template <class CommandClass> |
| 39 | + static CommandClass* getCommandData(CommandPacket* packet); |
| 40 | + template <class CommandClass, typename CastType> |
| 41 | + static CastType* getAuxilaryData(CommandClass* commandData); |
| 42 | + |
| 43 | + static void dispatch(const cb::CommandPacket* packet, cb::RenderContext* rc); |
44 | 44 |
|
45 | 45 | #if CB_DEBUG_TAG_COMMANDS
|
46 |
| - template <class CommandClass> |
47 |
| - static CommandPacket& command(CommandClass& cmd); |
48 |
| - template <class CommandClass> |
49 |
| - static CommandPacket& command(CommandClass* cmd); |
50 |
| - template <class CommandClass> |
51 |
| - static std::string commandName(CommandClass& cmd); |
52 |
| - template <class CommandClass> |
53 |
| - static std::string commandName(CommandClass* cmd); |
54 |
| - |
55 |
| - typedef int(*log_function_t)(const char* fmt, ...); |
56 |
| - static void log(const cb::CommandPacket* packet, log_function_t logger); |
| 46 | + template <class CommandClass> |
| 47 | + static CommandPacket& command(CommandClass& cmd); |
| 48 | + template <class CommandClass> |
| 49 | + static CommandPacket& command(CommandClass* cmd); |
| 50 | + template <class CommandClass> |
| 51 | + static std::string commandName(CommandClass& cmd); |
| 52 | + template <class CommandClass> |
| 53 | + static std::string commandName(CommandClass* cmd); |
| 54 | + |
| 55 | + typedef int(*log_function_t)(const char* fmt, ...); |
| 56 | + static void log(const cb::CommandPacket* packet, log_function_t logger); |
57 | 57 | #endif // #if CB_DEBUG_TAG_COMMANDS
|
58 | 58 |
|
59 |
| - private: |
60 |
| - // not allowed |
61 |
| - template <class CommandClass, typename CastType> |
62 |
| - static CastType* getAuxilaryData(CommandClass**); |
63 |
| - template <class CommandClass> |
64 |
| - static CommandPacket* getCommandPacket(CommandClass**); |
65 |
| - template <class CommandClass> |
66 |
| - static const CommandPacket* getCommandPacket(const CommandClass**); |
67 |
| - |
68 |
| - public: |
69 |
| - cb::RenderContext::function_t dispatchFunction; |
70 |
| - void* commandData; |
71 |
| - void* auxilaryData; // optional |
72 |
| - CommandPacket* nextCommand; |
73 |
| - }; |
74 |
| - |
75 |
| - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
76 |
| - |
77 |
| - template <class CommandClass, class AllocatorClass> |
78 |
| - CB_FORCE_INLINE CommandPacket* CommandPacket::create(AllocatorClass& allocator, uint32_t auxilarySize) |
79 |
| - { |
80 |
| - static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
| 59 | + private: |
| 60 | + // not allowed |
| 61 | + template <class CommandClass, typename CastType> |
| 62 | + static CastType* getAuxilaryData(CommandClass**); |
| 63 | + template <class CommandClass> |
| 64 | + static CommandPacket* getCommandPacket(CommandClass**); |
| 65 | + template <class CommandClass> |
| 66 | + static const CommandPacket* getCommandPacket(const CommandClass**); |
| 67 | + |
| 68 | + public: |
| 69 | + cb::RenderContext::function_t dispatchFunction; |
| 70 | + void* commandData; |
| 71 | + void* auxilaryData; // optional |
| 72 | + CommandPacket* nextCommand; |
| 73 | + }; |
| 74 | + |
| 75 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 76 | + |
| 77 | + template <class CommandClass, class AllocatorClass> |
| 78 | + CB_FORCE_INLINE CommandPacket* CommandPacket::create(AllocatorClass& allocator, uint32_t auxilarySize) |
| 79 | + { |
| 80 | + static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
81 | 81 |
|
82 | 82 | #if CB_COMMAND_PACKET_ALIGNED
|
83 |
| - static_assert(alignof(CommandPacket) == alignof(CommandClass), "COMMAND_MUST_BE_ALIGNED_WITH_PACKET"); |
84 |
| - const uint32_t alignment = alignof(CommandClass); |
| 83 | + static_assert(alignof(CommandPacket) == alignof(CommandClass), "COMMAND_MUST_BE_ALIGNED_WITH_PACKET"); |
| 84 | + const uint32_t alignment = alignof(CommandClass); |
85 | 85 | #else
|
86 |
| - const uint32_t a1 = alignof(CommandPacket); |
87 |
| - const uint32_t a2 = alignof(CommandClass); |
88 |
| - const uint32_t alignment = std::max(a1, a2); |
| 86 | + const uint32_t a1 = alignof(CommandPacket); |
| 87 | + const uint32_t a2 = alignof(CommandClass); |
| 88 | + const uint32_t alignment = std::max(a1, a2); |
89 | 89 | #endif
|
90 | 90 |
|
91 |
| - uint8_t* data = allocator.alloc(sizeof(CommandPacket) + sizeof(CommandClass) + auxilarySize, alignment); |
| 91 | + uint8_t* data = allocator.alloc(sizeof(CommandPacket) + sizeof(CommandClass) + auxilarySize, alignment); |
92 | 92 |
|
93 |
| - CommandPacket* packet = reinterpret_cast<CommandPacket*>(data); |
94 |
| - packet->commandData = data + sizeof(CommandPacket); |
95 |
| - packet->auxilaryData = data + sizeof(CommandPacket) + sizeof(CommandClass); |
| 93 | + CommandPacket* packet = reinterpret_cast<CommandPacket*>(data); |
| 94 | + packet->commandData = data + sizeof(CommandPacket); |
| 95 | + packet->auxilaryData = data + sizeof(CommandPacket) + sizeof(CommandClass); |
96 | 96 | #ifndef NDEBUG
|
97 |
| - if (auxilarySize == 0) |
98 |
| - packet->auxilaryData = NULL; |
| 97 | + if (auxilarySize == 0) |
| 98 | + packet->auxilaryData = NULL; |
99 | 99 | #endif
|
100 |
| - packet->nextCommand = NULL; |
101 |
| - |
102 |
| - return packet; |
103 |
| - } |
104 |
| - |
105 |
| - template <class CommandClass> |
106 |
| - CB_FORCE_INLINE CommandPacket* CommandPacket::getCommandPacket(CommandClass* commandData) |
107 |
| - { |
108 |
| - static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
109 |
| - return reinterpret_cast<CommandPacket*>(reinterpret_cast<uint8_t*>(commandData) - sizeof(CommandPacket)); |
110 |
| - } |
111 |
| - |
112 |
| - template <class CommandClass> |
113 |
| - CB_FORCE_INLINE const CommandPacket* CommandPacket::getCommandPacket(const CommandClass* commandData) |
114 |
| - { |
115 |
| - static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
116 |
| - return reinterpret_cast<const CommandPacket*>(reinterpret_cast<const uint8_t*>(commandData) - |
117 |
| - sizeof(CommandPacket)); |
118 |
| - } |
119 |
| - |
120 |
| - template <class CommandClass> |
121 |
| - CB_FORCE_INLINE CommandClass* CommandPacket::getCommandData(CommandPacket* packet) |
122 |
| - { |
123 |
| - static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
124 |
| - return reinterpret_cast<CommandClass*>(packet->commandData); |
125 |
| - } |
126 |
| - |
127 |
| - template <class CommandClass, typename CastType> |
128 |
| - CB_FORCE_INLINE CastType* CommandPacket::getAuxilaryData(CommandClass* commandData) |
129 |
| - { |
130 |
| - static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
131 |
| - CommandPacket* packet = CommandPacket::getCommandPacket<CommandClass>(commandData); |
132 |
| - return reinterpret_cast<CastType*>(packet->auxilaryData); |
133 |
| - } |
134 |
| - |
135 |
| - CB_FORCE_INLINE void CommandPacket::dispatch(const cb::CommandPacket* packet, cb::RenderContext* rc) |
136 |
| - { |
137 |
| - do |
138 |
| - { |
139 |
| - (*packet->dispatchFunction)(packet->commandData, rc); |
140 |
| - packet = packet->nextCommand; |
141 |
| - } while (packet != NULL); |
142 |
| - } |
| 100 | + packet->nextCommand = NULL; |
| 101 | + |
| 102 | + return packet; |
| 103 | + } |
| 104 | + |
| 105 | + template <class CommandClass> |
| 106 | + CB_FORCE_INLINE CommandPacket* CommandPacket::getCommandPacket(CommandClass* commandData) |
| 107 | + { |
| 108 | + static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
| 109 | + return reinterpret_cast<CommandPacket*>(reinterpret_cast<uint8_t*>(commandData) - sizeof(CommandPacket)); |
| 110 | + } |
| 111 | + |
| 112 | + template <class CommandClass> |
| 113 | + CB_FORCE_INLINE const CommandPacket* CommandPacket::getCommandPacket(const CommandClass* commandData) |
| 114 | + { |
| 115 | + static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
| 116 | + return reinterpret_cast<const CommandPacket*>(reinterpret_cast<const uint8_t*>(commandData) - |
| 117 | + sizeof(CommandPacket)); |
| 118 | + } |
| 119 | + |
| 120 | + template <class CommandClass> |
| 121 | + CB_FORCE_INLINE CommandClass* CommandPacket::getCommandData(CommandPacket* packet) |
| 122 | + { |
| 123 | + static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
| 124 | + return reinterpret_cast<CommandClass*>(packet->commandData); |
| 125 | + } |
| 126 | + |
| 127 | + template <class CommandClass, typename CastType> |
| 128 | + CB_FORCE_INLINE CastType* CommandPacket::getAuxilaryData(CommandClass* commandData) |
| 129 | + { |
| 130 | + static_assert(is_valid_type<CommandClass>::value, "COMMAND_INVALID_TYPE"); |
| 131 | + CommandPacket* packet = CommandPacket::getCommandPacket<CommandClass>(commandData); |
| 132 | + return reinterpret_cast<CastType*>(packet->auxilaryData); |
| 133 | + } |
| 134 | + |
| 135 | + CB_FORCE_INLINE void CommandPacket::dispatch(const cb::CommandPacket* packet, cb::RenderContext* rc) |
| 136 | + { |
| 137 | + do |
| 138 | + { |
| 139 | + (*packet->dispatchFunction)(packet->commandData, rc); |
| 140 | + packet = packet->nextCommand; |
| 141 | + } while (packet != NULL); |
| 142 | + } |
143 | 143 |
|
144 | 144 | #if CB_DEBUG_TAG_COMMANDS
|
145 |
| - template <class CommandClass> |
146 |
| - CB_FORCE_INLINE CommandPacket& CommandPacket::command(CommandClass& cmd) |
147 |
| - { |
148 |
| - return *CommandPacket::getCommandPacket<CommandClass>(&cmd); |
149 |
| - } |
150 |
| - |
151 |
| - template <class CommandClass> |
152 |
| - CB_FORCE_INLINE CommandPacket& CommandPacket::command(CommandClass* cmd) |
153 |
| - { |
154 |
| - return *CommandPacket::getCommandPacket<CommandClass>(cmd); |
155 |
| - } |
156 |
| - |
157 |
| - template <class CommandClass> |
158 |
| - CB_FORCE_INLINE std::string CommandPacket::commandName(CommandClass& cmd) |
159 |
| - { |
160 |
| - return typeid(&cmd).name(); |
161 |
| - } |
162 |
| - |
163 |
| - template <class CommandClass> |
164 |
| - CB_FORCE_INLINE std::string CommandPacket::commandName(CommandClass* cmd) |
165 |
| - { |
166 |
| - return typeid(cmd).name(); |
167 |
| - } |
168 |
| - |
169 |
| - CB_FORCE_INLINE void CommandPacket::log(const cb::CommandPacket* packet, log_function_t logger) |
170 |
| - { |
171 |
| - do |
172 |
| - { |
173 |
| - if (packet->nextCommand != NULL) |
174 |
| - (*logger)("%s ->", packet->debug.tag); |
175 |
| - else |
176 |
| - (*logger)("%s", packet->debug.tag); |
177 |
| - packet = packet->nextCommand; |
178 |
| - } while (packet != NULL); |
179 |
| - (*logger)("\n"); |
180 |
| - } |
| 145 | + template <class CommandClass> |
| 146 | + CB_FORCE_INLINE CommandPacket& CommandPacket::command(CommandClass& cmd) |
| 147 | + { |
| 148 | + return *CommandPacket::getCommandPacket<CommandClass>(&cmd); |
| 149 | + } |
| 150 | + |
| 151 | + template <class CommandClass> |
| 152 | + CB_FORCE_INLINE CommandPacket& CommandPacket::command(CommandClass* cmd) |
| 153 | + { |
| 154 | + return *CommandPacket::getCommandPacket<CommandClass>(cmd); |
| 155 | + } |
| 156 | + |
| 157 | + template <class CommandClass> |
| 158 | + CB_FORCE_INLINE std::string CommandPacket::commandName(CommandClass& cmd) |
| 159 | + { |
| 160 | + return typeid(&cmd).name(); |
| 161 | + } |
| 162 | + |
| 163 | + template <class CommandClass> |
| 164 | + CB_FORCE_INLINE std::string CommandPacket::commandName(CommandClass* cmd) |
| 165 | + { |
| 166 | + return typeid(cmd).name(); |
| 167 | + } |
| 168 | + |
| 169 | + CB_FORCE_INLINE void CommandPacket::log(const cb::CommandPacket* packet, log_function_t logger) |
| 170 | + { |
| 171 | + do |
| 172 | + { |
| 173 | + if (packet->nextCommand != NULL) |
| 174 | + (*logger)("%s ->", packet->debug.tag); |
| 175 | + else |
| 176 | + (*logger)("%s", packet->debug.tag); |
| 177 | + packet = packet->nextCommand; |
| 178 | + } while (packet != NULL); |
| 179 | + (*logger)("\n"); |
| 180 | + } |
181 | 181 | #endif // #if CB_DEBUG_TAG_COMMANDS
|
182 | 182 |
|
183 | 183 | } // namespace cb
|
0 commit comments