Skip to content

Zoo/auditable #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions benchmark/cm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class square : public shape_base
square(f32 SideInit) : Side(SideInit) {}
f32 AreaNP() {return Side*Side;}
virtual f32 Area() {return Side*Side;}

private:
f32 Side;
};
Expand All @@ -39,7 +39,7 @@ class rectangle : public shape_base
rectangle(f32 WidthInit, f32 HeightInit) : Width(WidthInit), Height(HeightInit) {}
f32 AreaNP() {return Width*Height;}
virtual f32 Area() {return Width*Height;}

private:
f32 Width, Height;
};
Expand All @@ -50,7 +50,7 @@ class triangle : public shape_base
triangle(f32 BaseInit, f32 HeightInit) : Base(BaseInit), Height(HeightInit) {}
f32 AreaNP() {return 0.5f*Base*Height;}
virtual f32 Area() {return 0.5f*Base*Height;}

private:
f32 Base, Height;
};
Expand All @@ -61,7 +61,7 @@ class circle : public shape_base
circle(f32 RadiusInit) : Radius(RadiusInit) {}
f32 AreaNP() {return Pi32*Radius*Radius;}
virtual f32 Area() {return Pi32*Radius*Radius;}

private:
f32 Radius;
};
Expand All @@ -77,7 +77,7 @@ f32 TotalAreaVTBL(u32 ShapeCount, shape_base **Shapes)
{
Accum += Shapes[ShapeIndex]->Area();
}

return Accum;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ enum shape_type : u32
Shape_Rectangle,
Shape_Triangle,
Shape_Circle,

Shape_Count,
};

Expand All @@ -223,17 +223,17 @@ struct shape_union
f32 GetAreaSwitch(shape_union Shape)
{
f32 Result = 0.0f;

switch(Shape.Type)
{
case Shape_Square: {Result = Shape.Width*Shape.Width;} break;
case Shape_Rectangle: {Result = Shape.Width*Shape.Height;} break;
case Shape_Triangle: {Result = 0.5f*Shape.Width*Shape.Height;} break;
case Shape_Circle: {Result = Pi32*Shape.Width*Shape.Width;} break;

case Shape_Count: {} break;
}

return Result;
}

Expand All @@ -244,7 +244,7 @@ f32 GetAreaSwitch(shape_union Shape)
f32 TotalAreaSwitch(u32 ShapeCount, shape_union *Shapes)
{
f32 Accum = 0.0f;

for(u32 ShapeIndex = 0; ShapeIndex < ShapeCount; ++ShapeIndex)
{
Accum += GetAreaSwitch(Shapes[ShapeIndex]);
Expand All @@ -259,18 +259,18 @@ f32 TotalAreaSwitch4(u32 ShapeCount, shape_union *Shapes)
f32 Accum1 = 0.0f;
f32 Accum2 = 0.0f;
f32 Accum3 = 0.0f;

ShapeCount /= 4;
while(ShapeCount--)
{
Accum0 += GetAreaSwitch(Shapes[0]);
Accum1 += GetAreaSwitch(Shapes[1]);
Accum2 += GetAreaSwitch(Shapes[2]);
Accum3 += GetAreaSwitch(Shapes[3]);

Shapes += 4;
}

f32 Result = (Accum0 + Accum1 + Accum2 + Accum3);
return Result;
}
Expand Down
4 changes: 2 additions & 2 deletions design/generic-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Description of how to add polymorphic interfaces to `AnyContainer` by assembling

The great software engineer Louis Dionne explains very well what normal C++ polymorphism, based on `virtual` overrides, leaves to be desired in his project [Dyno](https://github.com/ldionne/dyno).

Additionally, in this repository there are a few extra pointers with regards to more subtle needs for polymorphism, in particular sub-typing relationships as in the original meaning of [Barbara Liskov's substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) that should not force their representation to be sub-classing in C++. This is better explained in the foundational work by Kevlin Henney, in the [ACCU September 2000 article "From Mechanism to Method: Substitutability"](https://accu.org/index.php/journals/475) and the [C++ Report magazine article with the original implementation of the `any` component](http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf), which led to `boost::function` and `std::function` and the "mainstreaming" of type erasure as an alternative for polymorphism, this has been a 20 year journey that keeps going.
Additionally, in this repository there are a few extra pointers with regards to more subtle needs for polymorphism, in particular sub-typing relationships as in the original meaning of [Barbara Liskov's substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) that should not force their representation to be sub-classing in C++. This is better explained in the foundational work by Kevlin Henney, in the [ACCU September 2000 article "From Mechanism to Method: Substitutability"](https://accu.org/index.php/journals/475) and the [C++ Report magazine article with the original implementation of the `any` component](http://www.two-sdg.demon.co.uk/curbralan/papers/ValuedConversions.pdf), which led to `boost::function` and `std::function` and the "mainstreaming" of type erasure as an alternative for polymorphism, this has been a 20 year journey that keeps going.

One particularly complete way of doing polymorphism (implementing sub-typing relationships) without inheritance and `virtual` overrides of member functions is "type erasure". Within "type erasure", educator Arthur O'Dwyer has coined the concept of "affordance" to refer to the polymorphic operations a "type erased" object is capable of doing, described in his excellent blog article ["What is Type Erasure?"](https://quuxplusone.github.io/blog/2019/03/18/what-is-type-erasure/).

Expand Down Expand Up @@ -96,7 +96,7 @@ In this example, the container implementations and the affordance of copyability
template<typename... Affordances>
struct GenericPolicy {
struct VTable: Affordances::VTableEntry... {};
using VTHolder = VTableHolder<VTable>;
using VTHolder = VTablePointerWrapper<VTable>;
using HoldingModel = void *;

struct Container: VTHolder, Affordances::template Mixin<Container>... {
Expand Down
18 changes: 11 additions & 7 deletions inc/zoo/Any/VTable.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef Zoo_Any_VTable_h
#define Zoo_Any_VTable_h

#include "zoo/Any/Traits.h"

#include "zoo/AlignedStorage.h"
#include <new>
#include <typeinfo>
#include <utility>
Expand All @@ -16,7 +15,7 @@ struct TypeErasureOperations {

template<int Size, int Alignment>
struct TypeErasedContainer {
alignas(Alignment) char space_[Size];
AlignedStorage<Size, Alignment> space_;
const TypeErasureOperations *vTable_ = &Empty;

template<typename T>
Expand All @@ -28,7 +27,12 @@ struct TypeErasedContainer {
}

// note: the policy operations do not assume an "Empty" to allow inheritance
// contravariance of the AnyContainer
// contravariance of a type erasure container.
// What this means is that we might want to override, for example,
// the destructor in a derived class, to replicate the behaviour of virtual
// inheritance or the normal or contravariant of destructors in conditions
// of inheritance. Therefore we can not asssume that we're working directly
// with an `Empty`.
inline const static TypeErasureOperations Empty = {
[](void *) noexcept {},
reinterpret_cast<void (*)(void *, void *) noexcept>(copyVTable)
Expand Down Expand Up @@ -73,9 +77,9 @@ struct SmallBufferTypeEraser:
template<int Size, int Alignment, typename V>
struct ReferentialTypeEraser: TypeErasedContainer<Size, Alignment> {
using Me = ReferentialTypeEraser;

V *&value() noexcept { return *this->template as<V *>(); }

inline const static TypeErasureOperations VTable = {
[](void *who) noexcept { delete static_cast<Me *>(who)->value(); },
[](void *from, void *to) noexcept {
Expand All @@ -86,7 +90,7 @@ struct ReferentialTypeEraser: TypeErasedContainer<Size, Alignment> {
source->vTable = &TypeErasedContainer<Size, Alignment>::Empty;
}
};

template<typename... Args>
ReferentialTypeEraser(Args &&...args) {
this->vTable_ = &VTable;
Expand Down
23 changes: 3 additions & 20 deletions inc/zoo/Any/VTablePolicy.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef ZOO_VTABLE_POLICY_H
#define ZOO_VTABLE_POLICY_H

#include "zoo/Any/Traits.h"
#include "zoo/tea/Traits.h"

#include "zoo/tea/VTablePointerWrapper.h"
#include "zoo/AlignedStorage.h"

#include "zoo/pp/platform.h"
Expand Down Expand Up @@ -210,24 +211,6 @@ struct CallableViaVTable<R(As...)> {
};
};

template<typename VirtualTable>
struct VTableHolder {
const VirtualTable *pointer_;

/// \brief from the vtable returns the entry corresponding to the affordance
template<typename Affordance>
const typename Affordance::VTableEntry *vTable() const noexcept {
return //static_cast<const typename Affordance::VTableEntry *>(pointer_);
pointer_->template upcast<Affordance>();
}

VTableHolder(const VirtualTable *p): pointer_(p) {}

auto pointer() const noexcept { return pointer_; }

void change(const VirtualTable *p) noexcept { pointer_ = p; }
};

template<std::size_t S, std::size_t A, typename V>
struct BuilderDecider {
constexpr static auto NoThrowMovable =
Expand All @@ -251,7 +234,7 @@ struct GenericPolicy {
}
};

using VTHolder = VTableHolder<VTable>;
using VTHolder = VTablePointerWrapper<VTable>;

struct MSVC_EMPTY_BASES Container:
VTHolder,
Expand Down
43 changes: 10 additions & 33 deletions inc/zoo/AnyContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define ZOO_ANYCONTAINER_H

#include "zoo/pp/platform.h"
#include "zoo/Any/Traits.h"
#include "zoo/tea/Traits.h"
#include "zoo/tea/AffordsCopying.hpp"
#include "zoo/utility.h"

#include "zoo/meta/NotBasedOn.h"
Expand All @@ -24,32 +25,7 @@ struct PolicyDefaultBuilder<P, std::void_t<typename P::DefaultImplementation>> {
using type = typename P::DefaultImplementation;
};

template<typename, typename = void>
struct MemoryLayoutHasCopy: std::false_type {};
template<typename Policy>
struct MemoryLayoutHasCopy<
Policy,
std::void_t<decltype(&Policy::MemoryLayout::copy)>
>: std::true_type {};

template<typename, typename = void>
struct ExtraAffordanceOfCopying: std::false_type {};
template<typename Policy>
struct ExtraAffordanceOfCopying<
Policy,
std::void_t<decltype(&Policy::ExtraAffordances::copy)>
>: std::true_type {};

/// Copy constructibility and assignability are fundamental operations that
/// can not be enabled/disabled with SFINAE, this trait is to detect copyability
/// in a policy
template<typename Policy>
using AffordsCopying =
std::disjunction<
MemoryLayoutHasCopy<Policy>, ExtraAffordanceOfCopying<Policy>
>;

}
} // detail

template<typename Policy, typename = void>
struct CompositionChain {
Expand Down Expand Up @@ -90,7 +66,7 @@ struct MSVC_EMPTY_BASES AnyContainerBase:
using Policy = Policy_;
using SuperContainer = typename CompositionChain<Policy>::Base;
using Container = typename Policy::MemoryLayout;
constexpr static auto Copyable = detail::AffordsCopying<Policy>::value;
constexpr static auto Copyable = tea::detail::AffordsCopying<Policy>::value;

AnyContainerBase() noexcept:
SuperContainer(SuperContainer::Token, nullptr)
Expand Down Expand Up @@ -316,10 +292,10 @@ struct AnyCopyable: AnyContainerBase<Policy> {
Base(Base::Token, model)
{
auto source = model.container();
if constexpr(detail::MemoryLayoutHasCopy<Policy>::value) {
if constexpr(tea::detail::MemoryLayoutHasCopy<Policy>::value) {
source->copy(this->container());
} else {
static_assert(detail::ExtraAffordanceOfCopying<Policy>::value);
static_assert(tea::detail::ExtraAffordanceOfCopying<Policy>::value);
Policy::ExtraAffordances::copy(this->container(), source);
}
}
Expand All @@ -334,10 +310,10 @@ struct AnyCopyable: AnyContainerBase<Policy> {
myself->destroy();
try {
auto source = model.container();
if constexpr(detail::MemoryLayoutHasCopy<Policy>::value) {
if constexpr(tea::detail::MemoryLayoutHasCopy<Policy>::value) {
source->copy(this->container());
} else {
static_assert(detail::ExtraAffordanceOfCopying<Policy>::value);
static_assert(tea::detail::ExtraAffordanceOfCopying<Policy>::value);
Policy::ExtraAffordances::copy(this->container(), source);
}
} catch(...) {
Expand All @@ -354,7 +330,7 @@ struct AnyCopyable: AnyContainerBase<Policy> {
template<typename Policy>
#define PP_BASE_TYPE \
std::conditional_t< \
detail::AffordsCopying<Policy>::value, \
tea::detail::AffordsCopying<Policy>::value, \
AnyCopyable<Policy>, \
AnyContainerBase<Policy> \
>
Expand All @@ -378,6 +354,7 @@ T *anyContainerCast(const AnyContainer<Policy> *ptr) noexcept {
return const_cast<T *>(ptr->template state<T>());
}


}

#endif
10 changes: 10 additions & 0 deletions inc/zoo/tea/AffordsCopying.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
AnyContainerBase(AnyContainerBase &&moveable) noexcept:
SuperContainer(
SuperContainer::Token,
static_cast<SuperContainer &&>(moveable)
)
{
auto source = moveable.container();
source->move(container());
}

38 changes: 38 additions & 0 deletions inc/zoo/tea/AffordsCopying.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef ZOO_TEA_AFFORDSCOPYING_H
#define ZOO_TEA_AFFORDSCOPYING_H
#include <type_traits>

namespace zoo::tea {

namespace detail {

template<typename, typename = void>
struct MemoryLayoutHasCopy: std::false_type {};
template<typename Policy>
struct MemoryLayoutHasCopy<
Policy,
std::void_t<decltype(&Policy::MemoryLayout::copy)>
>: std::true_type {};

template<typename, typename = void>
struct ExtraAffordanceOfCopying: std::false_type {};
template<typename Policy>
struct ExtraAffordanceOfCopying<
Policy,
std::void_t<decltype(&Policy::ExtraAffordances::copy)>
>: std::true_type {};

/// Copy constructibility and assignability are fundamental operations that
/// can not be enabled/disabled with SFINAE, this trait is to detect copyability
/// in a policy
template<typename Policy>
using AffordsCopying =
std::disjunction<
detail::MemoryLayoutHasCopy<Policy>, detail::ExtraAffordanceOfCopying<Policy>
>;

} // detail

} // zoo::tea

#endif
Loading