| 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef dap_typeinfo_h |
| 16 | #define dap_typeinfo_h |
| 17 | |
| 18 | #include <functional> |
| 19 | #include <string> |
| 20 | |
| 21 | namespace dap { |
| 22 | |
| 23 | class any; |
| 24 | class Deserializer; |
| 25 | class Serializer; |
| 26 | |
| 27 | // The TypeInfo interface provides basic runtime type information about DAP |
| 28 | // types. TypeInfo is used by the serialization system to encode and decode DAP |
| 29 | // requests, responses, events and structs. |
| 30 | struct TypeInfo { |
| 31 | virtual ~TypeInfo(); |
| 32 | virtual std::string name() const = 0; |
| 33 | virtual size_t size() const = 0; |
| 34 | virtual size_t alignment() const = 0; |
| 35 | virtual void construct(void*) const = 0; |
| 36 | virtual void copyConstruct(void* dst, const void* src) const = 0; |
| 37 | virtual void destruct(void*) const = 0; |
| 38 | virtual bool deserialize(const Deserializer*, void*) const = 0; |
| 39 | virtual bool serialize(Serializer*, const void*) const = 0; |
| 40 | |
| 41 | // create() allocates and constructs the TypeInfo of type T, registers the |
| 42 | // pointer for deletion on cppdap library termination, and returns the pointer |
| 43 | // to T. |
| 44 | template <typename T, typename... ARGS> |
| 45 | static T* create(ARGS&&... args) { |
| 46 | auto typeinfo = new T(std::forward<ARGS>(args)...); |
| 47 | deleteOnExit(typeinfo); |
| 48 | return typeinfo; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | // deleteOnExit() ensures that the TypeInfo is destructed and deleted on |
| 53 | // library termination. |
| 54 | static void deleteOnExit(TypeInfo*); |
| 55 | }; |
| 56 | |
| 57 | } // namespace dap |
| 58 | |
| 59 | #endif // dap_typeinfo_h |
| 60 | |