reflect: insert xo/ subdir into include path

This commit is contained in:
Roland Conybeare 2023-10-06 16:53:47 -04:00
commit 6be9037f10
20 changed files with 179 additions and 184 deletions

View file

@ -5,8 +5,8 @@
#pragma once
#include "reflect/TypeDescr.hpp"
#include "reflect/TaggedPtr.hpp"
#include "TypeDescr.hpp"
#include "TaggedPtr.hpp"
namespace xo {
namespace reflect {
@ -26,28 +26,28 @@ namespace xo {
template<typename T>
static TypeDescrW establish() {
TypeDescrW td = TypeDescrBase::require(&typeid(T),
type_name<T>(),
nullptr);
TypeDescrW td = TypeDescrBase::require(&typeid(T),
type_name<T>(),
nullptr);
#ifdef NOT_USING
std::function<TaggedPtr (void *)> to_self_tp;
std::function<TaggedPtr (void *)> to_self_tp;
if (std::is_base_of_v<SelfTagging, T>) {
/* T is a descendant of SelfTagging (or T = SelfTagging);
* use SelfTagging.self_tp()
*/
to_self_tp = [](void * x) { return reinterpret_cast<T *>(x)->self_tp(); };
} else {
/* T is not a descendant of SelfTagging.
* want to return
*/
to_self_tp = [td](void * x) { return TaggedPtr(td, x); };
}
if (std::is_base_of_v<SelfTagging, T>) {
/* T is a descendant of SelfTagging (or T = SelfTagging);
* use SelfTagging.self_tp()
*/
to_self_tp = [](void * x) { return reinterpret_cast<T *>(x)->self_tp(); };
} else {
/* T is not a descendant of SelfTagging.
* want to return
*/
to_self_tp = [td](void * x) { return TaggedPtr(td, x); };
}
td->assign_to_self_tp(to_self_tp);
td->assign_to_self_tp(to_self_tp);
#endif
return td;
return td;
}
}; /*EstablishTypeDescr*/

View file

@ -5,12 +5,12 @@
#pragma once
#include "reflect/SelfTagging.hpp"
#include "reflect/EstablishTypeDescr.hpp"
#include "reflect/atomic/AtomicTdx.hpp"
#include "reflect/pointer/PointerTdx.hpp"
#include "reflect/vector/VectorTdx.hpp"
#include "reflect/struct/StructTdx.hpp"
#include "SelfTagging.hpp"
#include "EstablishTypeDescr.hpp"
#include "atomic/AtomicTdx.hpp"
#include "pointer/PointerTdx.hpp"
#include "vector/VectorTdx.hpp"
#include "struct/StructTdx.hpp"
#include "refcnt/Refcounted.hpp"
#include <vector>
#include <array>
@ -22,7 +22,7 @@ namespace xo {
class EstablishTdx {
public:
static std::unique_ptr<TypeDescrExtra> make() { return AtomicTdx::make(); }
}; /*EstablishTdx*/
}; /*EstablishTdx*/
// ----- xo::ref::rp<Object> -----
@ -34,7 +34,7 @@ namespace xo {
}; /*EstablishTdx*/
// ----- std::array<Element, N> -----
/* definition provide after decl for Reflect {} below */
template<typename Element, std::size_t N>
class EstablishTdx<std::array<Element, N>> {
@ -73,11 +73,11 @@ namespace xo {
class TaggedPtrMaker<SelfTagging> {
public:
static TaggedPtr make_tp(SelfTagging * x) {
return x->self_tp();
return x->self_tp();
} /*make_tp*/
static TaggedRcptr make_rctp(SelfTagging * x) {
return x->self_tp();
return x->self_tp();
} /*make_rctp*/
}; /*TaggedPtrMaker*/
@ -113,35 +113,35 @@ namespace xo {
* implemented in specialized header (like [reflect/struct/VectorTdx.hpp]) to
* refer to reflection info for T without having to pull in all the
* headers needed to properly reflect T (like this [reflect/Reflect.hpp])
*
*
*/
template<typename T>
static TypeDescrW require() {
TypeDescrW retval_td = EstablishTypeDescr::establish<T>();
TypeDescrW retval_td = EstablishTypeDescr::establish<T>();
/* mark TypeDescr for T as complete (even though it isn't quite yet),
* so that when we encounter recursive types, reflection terminates.
* For example consider type resulting from code like
*
* typename T;
* using T = std::vector<T *>;
*
*/
if (retval_td->mark_complete()) {
/* control here on 2nd+later calls to require<T>().
* in principle can immediately short-circuit.
*/
} else {
/* control comes here the first time require<T>() runs */
/* mark TypeDescr for T as complete (even though it isn't quite yet),
* so that when we encounter recursive types, reflection terminates.
* For example consider type resulting from code like
*
* typename T;
* using T = std::vector<T *>;
*
*/
if (retval_td->mark_complete()) {
/* control here on 2nd+later calls to require<T>().
* in principle can immediately short-circuit.
*/
} else {
/* control comes here the first time require<T>() runs */
auto final_tdx = EstablishTdx<T>::make();
auto final_tdx = EstablishTdx<T>::make();
retval_td->assign_tdextra(std::move(final_tdx));
retval_td->assign_tdextra(std::move(final_tdx));
/* also need to require for each child */
}
/* also need to require for each child */
}
return retval_td;
return retval_td;
} /*require*/
/* Use:

View file

@ -5,9 +5,9 @@
#pragma once
#include "refcnt/Refcounted.hpp"
#include "reflect/TypeDescr.hpp"
#include "reflect/TaggedRcptr.hpp"
#include "Refcounted.hpp"
#include "TypeDescr.hpp"
#include "TaggedRcptr.hpp"
namespace xo {
namespace reflect {

View file

@ -2,10 +2,10 @@
#pragma once
#include "reflect/Reflect.hpp"
#include "reflect/TypeDescr.hpp"
#include "reflect/struct/StructMember.hpp"
#include "reflect/struct/StructTdx.hpp"
#include "Reflect.hpp"
#include "TypeDescr.hpp"
#include "struct/StructMember.hpp"
#include "struct/StructTdx.hpp"
#include <vector>
namespace xo {
@ -16,7 +16,7 @@ namespace xo {
template<typename StructT>
struct SelfTagger<StructT, true> {
static TaggedPtr self_tp(void * object) {
return (reinterpret_cast<StructT *>(object))->self_tp();
return (reinterpret_cast<StructT *>(object))->self_tp();
}
};
@ -35,7 +35,7 @@ namespace xo {
* REFLECT_LITERAL_MEMBER(sr, y_);
*
* // optional: regardless, reflection will be completed when sr goes out of scope
* sr.require_complete();
* sr.require_complete();
*/
template<typename StructT>
class StructReflector {
@ -45,68 +45,68 @@ namespace xo {
public:
StructReflector() : td_{EstablishTypeDescr::establish<StructT>()} {}
~StructReflector() {
this->require_complete();
this->require_complete();
}
bool is_complete() const { return s_reflected_flag; }
bool is_incomplete() const { return !s_reflected_flag; }
template<typename OwnerT, typename MemberT>
void reflect_member(std::string const & member_name,
MemberT OwnerT::* member_addr) {
MemberT OwnerT::* member_addr) {
auto accessor
(GeneralStructMemberAccessor<StructT, OwnerT, MemberT>::make(member_addr));
auto accessor
(GeneralStructMemberAccessor<StructT, OwnerT, MemberT>::make(member_addr));
/* used to do this in GeneralStructMemberAccessor<> ctor,
* but that introduces #include cycle
*/
Reflect::require<MemberT>();
/* used to do this in GeneralStructMemberAccessor<> ctor,
* but that introduces #include cycle
*/
Reflect::require<MemberT>();
this->member_v_.emplace_back(member_name, std::move(accessor));
this->member_v_.emplace_back(member_name, std::move(accessor));
} /*reflect_member*/
void require_complete() {
if(!s_reflected_flag) {
s_reflected_flag = true;
if(!s_reflected_flag) {
s_reflected_flag = true;
constexpr bool have_to_self_tp = std::is_base_of_v<SelfTagging, StructT>;
constexpr bool have_to_self_tp = std::is_base_of_v<SelfTagging, StructT>;
/* if self-tagging, can use .self_tp() to get most-derived tagged pointer */
auto to_self_tp_fn
= ([](void * object)
{
return SelfTagger<StructT, have_to_self_tp>::self_tp(object);
});
/* if self-tagging, can use .self_tp() to get most-derived tagged pointer */
auto to_self_tp_fn
= ([](void * object)
{
return SelfTagger<StructT, have_to_self_tp>::self_tp(object);
});
auto tdx = StructTdx::make(std::move(this->member_v_),
have_to_self_tp,
to_self_tp_fn);
auto tdx = StructTdx::make(std::move(this->member_v_),
have_to_self_tp,
to_self_tp_fn);
this->td_->assign_tdextra(std::move(tdx));
}
this->td_->assign_tdextra(std::move(tdx));
}
} /*complete*/
template<typename AncestorT>
void adopt_ancestors() {
assert(Reflect::is_reflected<AncestorT>());
assert(Reflect::is_reflected<AncestorT>());
TypeDescr ancestor_td = Reflect::require<AncestorT>();
TypeDescr ancestor_td = Reflect::require<AncestorT>();
/* requires that reflection of AncestorT has completed */
{
assert(ancestor_td->is_struct());
assert(ancestor_td->complete_flag());
}
/* requires that reflection of AncestorT has completed */
{
assert(ancestor_td->is_struct());
assert(ancestor_td->complete_flag());
}
/* for structs,
* we know that object argument to TypeDescr::n_child() is unused
*/
for (uint32_t i = 0, n = ancestor_td->n_child(nullptr); i < n; ++i) {
StructMember const & member = ancestor_td->struct_member(i);
/* for structs,
* we know that object argument to TypeDescr::n_child() is unused
*/
for (uint32_t i = 0, n = ancestor_td->n_child(nullptr); i < n; ++i) {
StructMember const & member = ancestor_td->struct_member(i);
this->member_v_.push_back(member.for_descendant<StructT, AncestorT>());
}
this->member_v_.push_back(member.for_descendant<StructT, AncestorT>());
}
} /*adopt_ancestors*/
private:

View file

@ -2,8 +2,7 @@
#pragma once
#include "reflect/TypeDescr.hpp"
//#include "reflect/EstablishTypeDescr.hpp"
#include "TypeDescr.hpp"
#include <unordered_set>
namespace xo {

View file

@ -5,7 +5,7 @@
#pragma once
#include "reflect/TaggedPtr.hpp"
#include "TaggedPtr.hpp"
// causes #include cycle, reflect/Reflect.hpp includes this header
//#include "reflect/Reflect.hpp"
#include "refcnt/Refcounted.hpp"
@ -20,22 +20,22 @@ namespace xo {
class TaggedRcptr : public TaggedPtr {
public:
using Refcount = ref::Refcount;
public:
TaggedRcptr(TypeDescr td, Refcount * x) : TaggedPtr(td, x) {
ref::intrusive_ptr_add_ref(x);
ref::intrusive_ptr_add_ref(x);
}
TaggedRcptr(TaggedRcptr const & x) : TaggedPtr(x) {
ref::intrusive_ptr_add_ref(x.rc_address());
ref::intrusive_ptr_add_ref(x.rc_address());
}
TaggedRcptr(TaggedRcptr && x) : TaggedPtr(std::move(x)) {
/* since we're moving from x, need to make sure x.dtor
* doesn't decrement refcount
*/
x.assign_address(nullptr);
/* since we're moving from x, need to make sure x.dtor
* doesn't decrement refcount
*/
x.assign_address(nullptr);
}
~TaggedRcptr() {
ref::intrusive_ptr_release(this->rc_address());
ref::intrusive_ptr_release(this->rc_address());
}
/* causes #include cycle, see [reflect/Reflect.hpp] */
@ -46,37 +46,37 @@ namespace xo {
#endif
Refcount * rc_address() const {
return reinterpret_cast<Refcount *>(this->address());
return reinterpret_cast<Refcount *>(this->address());
} /*rc_address*/
TaggedRcptr & operator=(TaggedRcptr const & rhs) {
Refcount * x = rhs.rc_address();
Refcount * old = this->rc_address();
Refcount * x = rhs.rc_address();
Refcount * old = this->rc_address();
TaggedPtr::operator=(rhs);
TaggedPtr::operator=(rhs);
if (x != old) {
intrusive_ptr_release(old);
intrusive_ptr_add_ref(x);
}
if (x != old) {
intrusive_ptr_release(old);
intrusive_ptr_add_ref(x);
}
return *this;
return *this;
} /*operator=*/
TaggedRcptr & operator=(TaggedRcptr && rhs) {
/* swap pointers + type descriptions;
* then don't need to touch refcounts
*/
std::swap(this->td_, rhs.td_);
std::swap(this->address_, rhs.address_);
/* swap pointers + type descriptions;
* then don't need to touch refcounts
*/
std::swap(this->td_, rhs.td_);
std::swap(this->address_, rhs.address_);
return *this;
return *this;
} /*operator=*/
void display(std::ostream & os) const;
std::string display_string() const;
}; /*TaggedRcptr*/
inline std::ostream & operator<<(std::ostream & os, TaggedRcptr const & x) {
x.display(os);
return os;

View file

@ -3,7 +3,7 @@
#pragma once
//#include "reflect/atomic/AtomicTdx.hpp"
#include "reflect/TypeDescrExtra.hpp"
#include "TypeDescrExtra.hpp"
#include "cxxutil/demangle.hpp"
#include <iostream>
#include <typeinfo>

View file

@ -2,7 +2,7 @@
#pragma once
#include "reflect/Metatype.hpp"
#include "Metatype.hpp"
#include <string>
/* note: this file #include'd into TypeDescr.hpp */
#include <cstdint>

View file

@ -2,7 +2,7 @@
#pragma once
#include "reflect/TypeDescrExtra.hpp"
#include "xo/reflect/TypeDescrExtra.hpp"
//#include "reflect/TaggedPtr.hpp"
#include <memory>

View file

@ -5,8 +5,8 @@
#pragma once
#include "reflect/TypeDescrExtra.hpp"
#include "reflect/EstablishTypeDescr.hpp"
#include "xo/reflect/TypeDescrExtra.hpp"
#include "xo/reflect/EstablishTypeDescr.hpp"
#include "indentlog/scope.hpp"
namespace xo {

View file

@ -2,10 +2,9 @@
#pragma once
#include "reflect/TypeDescr.hpp"
#include "reflect/EstablishTypeDescr.hpp"
//#include "reflect/Reflect.hpp"
#include "reflect/TaggedPtr.hpp"
#include "xo/reflect/TypeDescr.hpp"
#include "xo/reflect/EstablishTypeDescr.hpp"
#include "xo/reflect/TaggedPtr.hpp"
#include <string>
#include <memory>
@ -14,7 +13,7 @@ namespace reflect {
class AbstractStructMemberAccessor {
public:
virtual ~AbstractStructMemberAccessor() = default;
/* get tagged pointer referring to this member of the object at *struct_addr */
TaggedPtr member_tp(void * struct_addr) const;
@ -30,7 +29,7 @@ namespace reflect {
* .member_td() => Reflect::require<int>();
*/
virtual TypeDescr member_td() const = 0;
/* get address of a particular member, given parent address */
virtual void * address(void * struct_addr) const = 0;
@ -63,7 +62,7 @@ namespace reflect {
public:
GeneralStructMemberAccessor(Memptr memptr) : member_td_{EstablishTypeDescr::establish<MemberT>()},
memptr_{memptr} {}
memptr_{memptr} {}
GeneralStructMemberAccessor(GeneralStructMemberAccessor const & x) = default;
virtual ~GeneralStructMemberAccessor() = default;
@ -78,7 +77,7 @@ namespace reflect {
return &(owner_addr->*memptr_);
} /*address_impl*/
// ----- Inherited from AbstractStructMemberAccessor -----
#ifdef OBSOLETE
@ -102,7 +101,7 @@ namespace reflect {
virtual std::unique_ptr<AbstractStructMemberAccessor> clone() const override {
return std::unique_ptr<AbstractStructMemberAccessor>
(new GeneralStructMemberAccessor(*this));
(new GeneralStructMemberAccessor(*this));
} /*clone*/
private:
@ -142,7 +141,7 @@ namespace reflect {
static std::unique_ptr<AncestorStructMemberAccessor>
adopt(std::unique_ptr<AbstractStructMemberAccessor> ancestor_accessor) {
return std::unique_ptr<AncestorStructMemberAccessor>
(new AncestorStructMemberAccessor(std::move(ancestor_accessor)));
(new AncestorStructMemberAccessor(std::move(ancestor_accessor)));
} /*adopt*/
void * address_impl(StructT * self_addr) const {
@ -171,7 +170,7 @@ namespace reflect {
virtual std::unique_ptr<AbstractStructMemberAccessor> clone() const override {
return std::unique_ptr<AbstractStructMemberAccessor>
(new AncestorStructMemberAccessor(std::move(this->ancestor_accessor_->clone())));
(new AncestorStructMemberAccessor(std::move(this->ancestor_accessor_->clone())));
} /*clone*/
private:
@ -186,11 +185,11 @@ namespace reflect {
public:
StructMember() = default;
StructMember(std::string const & name,
std::unique_ptr<AbstractStructMemberAccessor> accessor)
std::unique_ptr<AbstractStructMemberAccessor> accessor)
: member_name_{name}, accessor_{std::move(accessor)} {}
StructMember(StructMember && x)
: member_name_{std::move(x.member_name_)},
accessor_{std::move(x.accessor_)} {}
accessor_{std::move(x.accessor_)} {}
static StructMember null();
@ -210,8 +209,8 @@ namespace reflect {
assert(EstablishTypeDescr::establish<StructT>() == this->get_struct_td());
return StructMember(this->member_name(),
std::move(AncestorStructMemberAccessor<DescendantT, StructT>::adopt
(std::move(this->accessor_->clone()))));
std::move(AncestorStructMemberAccessor<DescendantT, StructT>::adopt
(std::move(this->accessor_->clone()))));
} /*for_descendant*/
StructMember & operator=(StructMember && x) {

View file

@ -2,9 +2,10 @@
#pragma once
#include "reflect/TypeDescrExtra.hpp"
#include "reflect/TaggedPtr.hpp"
#include "reflect/struct/StructMember.hpp"
#include "xo/reflect/TypeDescrExtra.hpp"
#include "xo/reflect/TaggedPtr.hpp"
#include "StructMember.hpp"
//#include "xo/reflect/struct/StructMember.hpp"
#include <vector>
#include <functional>
#include <memory>

View file

@ -5,12 +5,8 @@
#pragma once
#include "reflect/TypeDescrExtra.hpp"
//#include "reflect/TaggedPtr.hpp"
#include "reflect/EstablishTypeDescr.hpp"
//#include "reflect/TaggedPtr.hpp"
//#include <vector>
//#include <memory>
#include "xo/reflect/TypeDescrExtra.hpp"
#include "xo/reflect/EstablishTypeDescr.hpp"
namespace xo {
namespace reflect {
@ -42,19 +38,19 @@ namespace xo {
using target_t = VectorT;
static std::unique_ptr<StlVectorTdx> make() {
return std::unique_ptr<StlVectorTdx>(new StlVectorTdx());
return std::unique_ptr<StlVectorTdx>(new StlVectorTdx());
} /*make*/
virtual uint32_t n_child(void * object) const override {
target_t * vec = reinterpret_cast<target_t *>(object);
target_t * vec = reinterpret_cast<target_t *>(object);
return vec->size();
return vec->size();
} /*n_child*/
virtual TaggedPtr child_tp(uint32_t i, void * object) const override {
target_t * vec = reinterpret_cast<target_t *>(object);
target_t * vec = reinterpret_cast<target_t *>(object);
return establish_most_derived_tp(&((*vec)[i]));
return establish_most_derived_tp(&((*vec)[i]));
} /*child_tp*/
}; /*StlVectorTdx*/
@ -66,9 +62,9 @@ namespace xo {
template<typename Element, std::size_t N>
using StdArrayTdx = StlVectorTdx<std::array<Element, N>>;
// ----- std::vector<Element> -----
/* coordinates with EstablishTdx<std::vector<Element>>::make()
* see [reflect/Reflect.hpp]
*/
@ -76,21 +72,21 @@ namespace xo {
class StdVectorTdx : public VectorTdx {
public:
using target_t = std::vector<Element>;
static std::unique_ptr<StdVectorTdx> make() {
return std::unique_ptr<StdVectorTdx>(new StdVectorTdx());
return std::unique_ptr<StdVectorTdx>(new StdVectorTdx());
} /*make*/
virtual uint32_t n_child(void * object) const override {
target_t * vec = reinterpret_cast<target_t *>(object);
target_t * vec = reinterpret_cast<target_t *>(object);
return vec->size();
return vec->size();
} /*n_child*/
virtual TaggedPtr child_tp(uint32_t i, void * object) const override {
target_t * vec = reinterpret_cast<target_t *>(object);
target_t * vec = reinterpret_cast<target_t *>(object);
return establish_most_derived_tp(&((*vec)[i]));
return establish_most_derived_tp(&((*vec)[i]));
}
}; /*StdVectorTdx*/
} /*namespace reflect*/