typeinfo.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef dap_typeinfo_h
  15. #define dap_typeinfo_h
  16. #include <functional>
  17. #include <string>
  18. namespace dap {
  19. class any;
  20. class Deserializer;
  21. class Serializer;
  22. // The TypeInfo interface provides basic runtime type information about DAP
  23. // types. TypeInfo is used by the serialization system to encode and decode DAP
  24. // requests, responses, events and structs.
  25. struct TypeInfo {
  26. virtual ~TypeInfo();
  27. virtual std::string name() const = 0;
  28. virtual size_t size() const = 0;
  29. virtual size_t alignment() const = 0;
  30. virtual void construct(void*) const = 0;
  31. virtual void copyConstruct(void* dst, const void* src) const = 0;
  32. virtual void destruct(void*) const = 0;
  33. virtual bool deserialize(const Deserializer*, void*) const = 0;
  34. virtual bool serialize(Serializer*, const void*) const = 0;
  35. // create() allocates and constructs the TypeInfo of type T, registers the
  36. // pointer for deletion on cppdap library termination, and returns the pointer
  37. // to T.
  38. template <typename T, typename... ARGS>
  39. static T* create(ARGS&&... args) {
  40. auto typeinfo = new T(std::forward<ARGS>(args)...);
  41. deleteOnExit(typeinfo);
  42. return typeinfo;
  43. }
  44. private:
  45. // deleteOnExit() ensures that the TypeInfo is destructed and deleted on
  46. // library termination.
  47. static void deleteOnExit(TypeInfo*);
  48. };
  49. } // namespace dap
  50. #endif // dap_typeinfo_h