Add dap::initialize() and terminate() functions

Can be used to explicitly control when the TypeInfo static initializers / destructors are called.
Usually not needed.

Issue: #40
This commit is contained in:
Ben Clayton
2020-06-15 17:00:50 +01:00
parent c9630a9aee
commit 7b02b9f73a
5 changed files with 221 additions and 57 deletions

35
include/dap/dap.h Normal file
View File

@@ -0,0 +1,35 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef dap_dap_h
#define dap_dap_h
namespace dap {
// Explicit library initialization and termination functions.
//
// cppdap automatically initializes and terminates its internal state using lazy
// static initialization, and so will usually work fine without explicit calls
// to these functions.
// However, if you use cppdap types in global state, you may need to call these
// functions to ensure that cppdap is not uninitialized before the last usage.
//
// Each call to initialize() must have a corresponding call to terminate().
// It is undefined behaviour to call initialize() after terminate().
void initialize();
void terminate();
} // namespace dap
#endif // dap_dap_h

View File

@@ -37,6 +37,21 @@ struct TypeInfo {
virtual void destruct(void*) const = 0;
virtual bool deserialize(const Deserializer*, void*) const = 0;
virtual bool serialize(Serializer*, const void*) const = 0;
// create() allocates and constructs the TypeInfo of type T, registers the
// pointer for deletion on cppdap library termination, and returns the pointer
// to T.
template <typename T, typename... ARGS>
static T* create(ARGS&&... args) {
auto typeinfo = new T(std::forward<ARGS>(args)...);
deleteOnExit(typeinfo);
return typeinfo;
}
private:
// deleteOnExit() ensures that the TypeInfo is destructed and deleted on
// library termination.
static void deleteOnExit(TypeInfo*);
};
} // namespace dap

View File

@@ -29,18 +29,20 @@ struct BasicTypeInfo : public TypeInfo {
constexpr BasicTypeInfo(std::string&& name) : name_(std::move(name)) {}
// TypeInfo compliance
inline std::string name() const { return name_; }
inline size_t size() const { return sizeof(T); }
inline size_t alignment() const { return alignof(T); }
inline void construct(void* ptr) const { new (ptr) T(); }
inline void copyConstruct(void* dst, const void* src) const {
inline std::string name() const override { return name_; }
inline size_t size() const override { return sizeof(T); }
inline size_t alignment() const override { return alignof(T); }
inline void construct(void* ptr) const override { new (ptr) T(); }
inline void copyConstruct(void* dst, const void* src) const override {
new (dst) T(*reinterpret_cast<const T*>(src));
}
inline void destruct(void* ptr) const { reinterpret_cast<T*>(ptr)->~T(); }
inline bool deserialize(const Deserializer* d, void* ptr) const {
inline void destruct(void* ptr) const override {
reinterpret_cast<T*>(ptr)->~T();
}
inline bool deserialize(const Deserializer* d, void* ptr) const override {
return d->deserialize(reinterpret_cast<T*>(ptr));
}
inline bool serialize(Serializer* s, const void* ptr) const {
inline bool serialize(Serializer* s, const void* ptr) const override {
return s->serialize(*reinterpret_cast<const T*>(ptr));
}
@@ -88,45 +90,33 @@ struct TypeOf<null> {
static const TypeInfo* type();
};
// TypeOf for template types requires dynamic generation of type information,
// triggering the clang -Wexit-time-destructors warning.
// TODO(bclayton): See if there's a way to avoid this, without requiring manual
// instantiation of each type.
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif // __clang__
template <typename T>
struct TypeOf<array<T>> {
static inline const TypeInfo* type() {
static BasicTypeInfo<array<T>> typeinfo("array<" +
TypeOf<T>::type()->name() + ">");
return &typeinfo;
static auto typeinfo = TypeInfo::create<BasicTypeInfo<array<T>>>(
"array<" + TypeOf<T>::type()->name() + ">");
return typeinfo;
}
};
template <typename T0, typename... Types>
struct TypeOf<variant<T0, Types...>> {
static inline const TypeInfo* type() {
static BasicTypeInfo<variant<T0, Types...>> typeinfo("variant");
return &typeinfo;
static auto typeinfo =
TypeInfo::create<BasicTypeInfo<variant<T0, Types...>>>("variant");
return typeinfo;
}
};
template <typename T>
struct TypeOf<optional<T>> {
static inline const TypeInfo* type() {
static BasicTypeInfo<optional<T>> typeinfo("optional<" +
TypeOf<T>::type()->name() + ">");
return &typeinfo;
static auto typeinfo = TypeInfo::create<BasicTypeInfo<optional<T>>>(
"optional<" + TypeOf<T>::type()->name() + ">");
return typeinfo;
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
// DAP_OFFSETOF() macro is a generalization of the offsetof() macro defined in
// <cstddef>. It evaluates to the offset of the given field, with fewer
// restrictions than offsetof(). We cast the address '32' and subtract it again,