### Example usage of make_proxy_inplace Source: https://github.com/microsoft/proxy/blob/main/docs/spec/make_proxy_inplace.md Demonstrates creating a proxy with an integer value. It also shows a commented-out example that would fail compilation due to size constraints. ```cpp #include #include // By default, the maximum pointer size defined by pro::facade_builder // is 2 * sizeof(void*). This value can be overridden by `restrict_layout`. struct Any : pro::facade_builder::build {}; int main() { // sizeof(int) is usually not greater than sizeof(void*) for modern // 32/64-bit compilers pro::proxy p1 = pro::make_proxy_inplace(123); // Won't compile because sizeof(std::array) is usually greater than // 2 * sizeof(void*) pro::proxy p2 = pro::make_proxy_inplace>(); } ``` -------------------------------- ### Example Usage of Proxy Swap Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxy/friend_swap.md Demonstrates how to use `std::ranges::swap` with `proxy` objects. This example shows the state of two stringable proxies before and after swapping. ```cpp #include #include #include #include PRO_DEF_FREE_DISPATCH(FreeToString, std::to_string, ToString); struct Stringable : pro::facade_builder // ::add_convention // ::build {}; int main() { pro::proxy p0 = pro::make_proxy(123); pro::proxy p1 = pro::make_proxy(std::numbers::pi); std::cout << ToString(*p0) << "\n"; // Prints "10" std::cout << ToString(*p1) << "\n"; // Prints "3.14..." std::ranges::swap(p0, p1); // finds the hidden friend std::cout << ToString(*p0) << "\n"; // Prints "3.14..." std::cout << ToString(*p1) << "\n"; // Prints "10" } ``` -------------------------------- ### Example Usage of Facade Builder Conventions Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/add_convention.md Demonstrates the usage of add_convention and add_direct_convention to build facades with specific dispatch and overload types. Includes examples of proxy creation, copying, moving, and calling a dispatched function. ```cpp #include #include #include #include PRO_DEF_FREE_DISPATCH(FreeToString, std::to_string, ToString); struct BasicStringable : pro::facade_builder // ::add_convention // ::build {}; struct Stringable : pro::facade_builder // ::add_facade // ::support_copy // ::add_direct_convention() &&> // ::build {}; int main() { pro::proxy p1 = std::make_shared(123); pro::proxy p2 = p1; pro::proxy p3 = static_cast>(std::move(p2)); pro::proxy p4 = std::move(p3); // pro::proxy p5 = p4; // Won't compile std::cout << ToString(*p4) << "\n"; // Prints "123" std::cout << std::boolalpha << p3.has_value() << "\n"; // Prints "false" } ``` -------------------------------- ### Example Usage Source: https://github.com/microsoft/proxy/blob/main/docs/spec/make_proxy_inplace.md Demonstrates how to use `make_proxy_inplace` to create proxy objects. ```APIDOC ## Example ```cpp #include #include // By default, the maximum pointer size defined by pro::facade_builder // is 2 * sizeof(void*). This value can be overridden by `restrict_layout`. struct Any : pro::facade_builder::build {}; int main() { // sizeof(int) is usually not greater than sizeof(void*) for modern // 32/64-bit compilers pro::proxy p1 = pro::make_proxy_inplace(123); // Won't compile because sizeof(std::array) is usually greater than // 2 * sizeof(void*) pro::proxy p2 = pro::make_proxy_inplace>(); } ``` ``` -------------------------------- ### Example: LayoutAware Facade with Reflection Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/add_reflection.md Demonstrates how to use `add_direct_reflection` and `add_indirect_reflection` to create a facade that can report layout information (size and alignment) of its contained types. This example shows how the reflection mechanism works with raw pointers, Small Buffer Optimization (SBO), and heap allocation. ```cpp #include #include #include struct LayoutReflector { public: template constexpr explicit LayoutReflector(std::in_place_type_t) : Size(sizeof(T)), Align(alignof(T)) {} template struct accessor { friend std::size_t SizeOf(const P& self) noexcept { const LayoutReflector& refl = pro::proxy_reflect(self); return refl.Size; } friend std::size_t AlignOf(const P& self) noexcept { const LayoutReflector& refl = pro::proxy_reflect(self); return refl.Align; } }; std::size_t Size, Align; }; struct LayoutAware : pro::facade_builder // ::add_direct_reflection // ::add_indirect_reflection // ::build {}; int main() { int a = 123; pro::proxy p = &a; std::cout << SizeOf(p) << "\n"; // Prints sizeof(raw pointer) std::cout << AlignOf(p) << "\n"; // Prints alignof(raw pointer) std::cout << SizeOf(*p) << "\n"; // Prints sizeof(int) std::cout << AlignOf(*p) << "\n"; // Prints alignof(int) p = pro::make_proxy(123); // SBO enabled std::cout << SizeOf(p) << "\n"; // Prints sizeof(int) std::cout << AlignOf(p) << "\n"; // Prints alignof(int) std::cout << SizeOf(*p) << "\n"; // Prints sizeof(int) std::cout << AlignOf(*p) << "\n"; // Prints alignof(int) p = pro::make_proxy>(); // SBO disabled std::cout << SizeOf(p) << "\n"; // Prints sizeof(raw pointer) std::cout << AlignOf(p) << "\n"; // Prints alignof(raw pointer) std::cout << SizeOf(*p) << "\n"; // Prints "100" std::cout << AlignOf(*p) << "\n"; // Prints "1" } ``` -------------------------------- ### Example Usage of proxy_invoke Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxy_invoke.md Demonstrates invoking a proxy using both an accessor and proxy_invoke directly. This example requires the proxy library and defines a Stringable facade. ```cpp #include #include #include PRO_DEF_FREE_DISPATCH(FreeToString, std::to_string, ToString); struct Stringable : pro::facade_builder // ::add_convention // ::build {}; int main() { int a = 123; pro::proxy p = &a; std::cout << ToString(*p) << "\n"; // Invokes with accessor, prints: "123" std::cout << pro::proxy_invoke(*p) << "\n"; // Invokes with proxy_invoke, also prints: "123" } ``` -------------------------------- ### Example: Shared Formattable Facade Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/add_skill.md Demonstrates creating a `SharedFormattable` struct by adding `SharedSlim` and `format` skills to a `facade_builder`. This example includes static assertions to check proxiability and shows how to use `proxy` and `weak_proxy` with shared semantics. ```cpp #include #include template using SharedSlim = typename FB // ::template restrict_layout // ::template support_copy // ::template add_skill; struct SharedFormattable : pro::facade_builder // ::add_skill // ::add_skill // ::build {}; int main() { // Raw pointer does not have shared semantics static_assert(!pro::proxiable); // The implementation of std::shared_ptr is usually larger than sizeof(void*) static_assert(!pro::proxiable, SharedFormattable>); // The built-in shared pointer is guaranteed to be "slim" pro::proxy p1 = pro::make_proxy_shared(123); pro::weak_proxy p2 = p1; pro::proxy p3 = p2.lock(); std::cout << std::format("{} ", *p3); // Prints "123" } ``` -------------------------------- ### Example Usage of facade_aware_overload_t with Addable Facade Source: https://github.com/microsoft/proxy/blob/main/docs/spec/facade_aware_overload_t.md This example demonstrates how to use facade_aware_overload_t to define an `Addable` facade. It includes a custom addition operator and shows how to create and use proxy objects with this facade. ```cpp #include #include template using BinaryOverload = pro::proxy(const pro::proxy_indirect_accessor& rhs) const; template pro::proxy operator+(const T& value, const pro::proxy_indirect_accessor& rhs) requires(!std::is_same_v>) { return pro::make_proxy(value + proxy_cast(rhs)); } struct Addable : pro::facade_builder // ::add_skill // ::add_skill // ::add_convention, pro::facade_aware_overload_t> // ::build {}; int main() { pro::proxy p1 = pro::make_proxy(1); pro::proxy p2 = pro::make_proxy(2); pro::proxy p3 = *p1 + *p2; std::cout << std::format("{} ", *p3); // Prints "3" } ``` -------------------------------- ### Example usage of `allocate_proxy_shared` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/allocate_proxy_shared.md Demonstrates creating and manipulating a proxy object using `allocate_proxy_shared` with a custom facade and a polymorphic allocator. The example shows incrementing the proxy's value and printing the result. ```cpp #include #include #include struct RttiAware : pro::facade_builder // ::support_copy // ::add_skill // ::build {}; int main() { std::pmr::unsynchronized_pool_resource pool; std::pmr::polymorphic_allocator<> alloc{&pool}; pro::proxy p1 = pro::allocate_proxy_shared(alloc, 1); pro::proxy p2 = p1; proxy_cast(*p1) += 2; std::cout << proxy_cast(*p2) << "\n"; // Prints "3" } ``` -------------------------------- ### Hello World Example with Proxy Source: https://github.com/microsoft/proxy/blob/main/docs/README.md This example demonstrates creating `pro::proxy` objects from a raw pointer, a `std::unique_ptr`, and directly using `pro::make_proxy`. It utilizes a custom facade `Formattable` that enables `std::format` for the proxied objects. Note that proxies created from raw pointers do not own the underlying object, while those created from smart pointers or `pro::make_proxy` do. ```cpp #include #include #include #include struct Formattable : pro::facade_builder ::add_skill ::build {}; int main() { static std::string str = "Hello World"; pro::proxy p1 = &str; std::cout << std::format("*p1 = {}\n", *p1); // Prints "*p1 = Hello World" pro::proxy p2 = std::make_unique(123); std::cout << std::format("*p2 = {}\n", *p2); // Prints "*p2 = 123" pro::proxy p3 = pro::make_proxy(3.14159); std::cout << std::format("*p3 = {:.2f}\n", *p3); // Prints "*p3 = 3.14" } ``` -------------------------------- ### make_proxy_shared usage example Source: https://github.com/microsoft/proxy/blob/main/docs/spec/make_proxy_shared.md This example demonstrates how to use `make_proxy_shared` to create a proxy object with a specific type and value. It also shows how to use `weak_proxy` and `proxy_cast`. ```cpp #include #include struct RttiAware : pro::facade_builder // ::support_copy // ::add_skill // ::add_skill // ::build {}; int main() { pro::proxy p1 = pro::make_proxy_shared(123); pro::weak_proxy p2 = p1; pro::proxy p3 = p2.lock(); std::cout << std::boolalpha << p3.has_value() << "\n"; // Prints "true" std::cout << proxy_cast(*p3) << "\n"; // Prints "123" p3.reset(); p1.reset(); p3 = p2.lock(); std::cout << std::boolalpha << p3.has_value() << "\n"; // Prints "false" } ``` -------------------------------- ### Example of using support_copy Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/support_copy.md Demonstrates how to use `support_copy` to create a facade that supports nontrivial copyability. It shows the difference between a facade that only supports moving and one that supports copying. ```cpp #include #include struct Movable : pro::facade_builder::build {}; struct Copyable : pro::facade_builder // ::support_copy // ::build {}; int main() { pro::proxy p1 = std::make_unique(123); // pro::proxy p2 = std::make_unique(123); // Won't compile pro::proxy p3 = std::make_shared(456); // auto p4 = p1; // Won't compile auto p5 = p3; } ``` -------------------------------- ### Example Facade Builder Usage Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/build.md Demonstrates how to use `basic_facade_builder::build` with different support configurations for copyability, relocation, and destruction. Static assertions verify the resulting proxy type's properties. ```cpp #include #include struct DefaultBase : pro::facade_builder // ::build {}; struct CopyableBase : pro::facade_builder // ::support_copy // ::build {}; struct TrivialBase : pro::facade_builder // ::support_copy // ::support_relocation // ::support_destruction // ::restrict_layout // ::build {}; int main() { static_assert(!std::is_copy_constructible_v>); static_assert(std::is_nothrow_move_constructible_v>); static_assert(std::is_nothrow_destructible_v>); static_assert(std::is_copy_constructible_v>); static_assert(std::is_nothrow-move_constructible_v>); static_assert(std::is_nothrow_destructible_v>); static_assert( std::is_trivially_copy_constructible_v>); static_assert( std::is_trivially_move_constructible_v>); static_assert(std::is_trivially_destructible_v>); } ``` -------------------------------- ### Example of `restrict_layout` usage Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/restrict_layout.md Demonstrates how to use `restrict_layout` to create a `SmallFacade` with a restricted layout based on `sizeof(void*)`. This example includes static assertions to verify the resulting size and proxiability constraints. ```cpp #include #include #include struct DefaultFacade : pro::facade_builder::build {}; struct SmallFacade : pro::facade_builder // ::restrict_layout // ::build {}; int main() { static_assert(sizeof(pro::proxy) > sizeof(pro::proxy)); static_assert(pro::proxiable, DefaultFacade>); static_assert(pro::proxiable, SmallFacade>); static_assert(pro::proxiable, DefaultFacade>); static_assert(!pro::proxiable, SmallFacade>); static_assert( pro::inplace_proxiable_target, DefaultFacade>); static_assert( !pro::inplace_proxiable_target, SmallFacade>); static_assert( !pro::inplace_proxiable_target, DefaultFacade>); static_assert( !pro::inplace_proxiable_target, SmallFacade>); pro::proxy p1 = std::make_shared(123); // pro::proxy p2 = std::make_shared(123); // Won't compile } ``` -------------------------------- ### Proxy Example with Various Constructions Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxy/constructor.md Demonstrates constructing a proxy with raw pointers, smart pointers, and showcases copy and move construction. Also illustrates checking value presence and accessing member functions through the proxy. ```cpp #include #include #include #include PRO_DEF_MEM_DISPATCH(MemSize, size); PRO_DEF_MEM_DISPATCH(MemClear, clear); struct BasicContainer : pro::facade_builder // ::add_convention // ::add_convention // ::support_copy // ::build {}; int main() { std::vector v{1, 2, 3}; pro::proxy p0; std::cout << std::boolalpha << p0.has_value() << "\n"; // Prints "false" // Construct a proxy with a raw pointer pro::proxy p1 = &v; std::cout << p1.has_value() << ", " << p1->size() << "\n"; // Prints "true,3" // Construct a proxy with a smart pointer pro::proxy p2 = std::make_shared>(10); std::cout << p2.has_value() << ", " << p2->size() << "\n"; // Prints "true,10" // Copy construction pro::proxy p3 = p2; std::cout << p3.has_value() << ", " << p3->size() << "\n"; // Prints "true,10" // Move construction pro::proxy p4 = std::move(p3); std::cout << p4.has_value() << ", " << p4->size() << "\n"; // Prints "true,10" // p3 no longer contains a value std::cout << p3.has_value() << "\n"; // Prints "false" // p2 and p4 shares the same object of std::deque p2->clear(); std::cout << p4.has_value() << ", " << p4->size() << "\n"; // Prints "true,0" } ``` -------------------------------- ### Example usage of `slim` facade Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_slim.md Demonstrates the creation and usage of `Default` and `Slim` facade builders, comparing their proxy sizes and proxiable types. It highlights that `std::shared_ptr` is too large for a slim facade. ```cpp #include #include struct Default : pro::facade_builder::build {}; struct Slim : pro::facade_builder // ::add_skill // ::build {}; int main() { static_assert(sizeof(pro::proxy) > sizeof(pro::proxy)); static_assert(pro::proxiable); static_assert(pro::proxiable, Default>); static_assert(pro::proxiable, Default>); static_assert(pro::proxiable); static_assert(pro::proxiable, Slim>); // std::shared_ptr is too large for a slim facade static_assert(!pro::proxiable, Slim>); // pro::make_proxy_shared works with a slim facade pro::proxy p = pro::make_proxy_shared(123); } ``` -------------------------------- ### Example Usage of `add_facade` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/add_facade.md Demonstrates the use of `add_facade` to build complex facade types like `StringDictionary` and `MutableStringDictionary`, including optional substitution. ```cpp #include #include #include PRO_DEF_MEM_DISPATCH(MemSize, size); PRO_DEF_MEM_DISPATCH(MemAt, at); PRO_DEF_MEM_DISPATCH(MemEmplace, emplace); struct Copyable : pro::facade_builder // ::support_copy // ::build {}; struct BasicContainer : pro::facade_builder // ::add_convention // ::build {}; struct StringDictionary : pro::facade_builder // ::add_facade // ::add_facade // ::add_convention // ::build {}; struct MutableStringDictionary : pro::facade_builder // ::add_facade // ::add_convention // ::build {}; int main() { pro::proxy p1 = pro::make_proxy>(); std::cout << p1->size() << "\n"; // Prints "0" try { std::cout << p1->at(123) << "\n"; // No output because the expression throws } catch (const std::out_of_range& e) { std::cerr << e.what() << "\n"; // Prints error message } p1->emplace(123, "lalala"); auto p2 = p1; // Performs a deep copy p2->emplace(456, "trivial"); // Performs a substitution from an rvalue reference pro::proxy p3 = std::move(p2); std::cout << p1->size() << "\n"; // Prints "1" std::cout << p1->at(123) << "\n"; // Prints "lalala" // Prints "false" because it is moved std::cout << std::boolalpha << p2.has_value() << "\n"; std::cout << p3->size() << "\n"; // Prints "2" std::cout << p3->at(123) << "\n"; // Prints "lalala" std::cout << p3->at(456) << "\n"; // Prints "trivial" } ``` -------------------------------- ### Example usage of `as_weak` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_as_weak.md Demonstrates how to use the `as_weak` skill to create a facade that supports implicit conversion from `proxy` to `weak_proxy`. Shows locking a weak proxy and checking for value presence. ```cpp #include #include struct RttiAware : pro::facade_builder // ::add_skill // ::add_skill // ::build {}; int main() { pro::proxy p1 = pro::make_proxy_shared(123); pro::weak_proxy p2 = p1; proxy_cast(*p1) = 456; // Modifies the contained object of p1 pro::proxy p3 = p2.lock(); p1.reset(); std::cout << proxy_cast(*p3) << "\n"; // Prints "456" p3.reset(); pro::proxy p4 = p2.lock(); std::cout << std::boolalpha << p4.has_value() << "\n"; // Prints "false" } ``` -------------------------------- ### Example usage of `as_view` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_as_view.md Demonstrates how to use the `as_view` skill to enable implicit conversion from `pro::proxy` to `pro::proxy_view`. This allows modification of the contained object through the view. ```cpp #include #include struct RttiAware : pro::facade_builder // ::add_skill // ::add_skill // ::build {}; int main() { pro::proxy p = pro::make_proxy(123); pro::proxy_view pv = p; proxy_cast(*pv) = 456; // Modifies the contained object of p std::cout << proxy_cast(*pv) << "\n"; // Prints "456" std::cout << proxy_cast(*p) << "\n"; // Prints "456" } ``` -------------------------------- ### Example Usage of proxy_reflect for Copyability Check Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxy_reflect.md Demonstrates how to use proxy_reflect within a custom facade to determine if the contained type is copy constructible. This example defines a CopyabilityReflector and a CopyabilityAware facade. ```cpp #include #include #include #include class CopyabilityReflector { public: template constexpr explicit CopyabilityReflector(std::in_place_type_t) : copyable_(std::is_copy_constructible_v) {} template struct accessor { bool IsCopyable() const noexcept { const CopyabilityReflector& self = pro::proxy_reflect(static_cast(*this)); return self.copyable_; } }; private: bool copyable_; }; struct CopyabilityAware : pro::facade_builder // ::add_direct_reflection // ::build {}; int main() { pro::proxy p1 = std::make_unique(); std::cout << std::boolalpha << p1.IsCopyable() << "\n"; // Prints "false" pro::proxy p2 = std::make_shared(); std::cout << p2.IsCopyable() << "\n"; // Prints "true" } ``` -------------------------------- ### Example of proxiable usage Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxiable.md Demonstrates how to use the `proxiable` concept with `static_assert` to verify type compatibility for proxying. ```cpp #include #include #include PRO_DEF_FREE_DISPATCH(FreeToString, std::to_string, ToString); struct Stringable : pro::facade_builder // ::add_convention // ::build {}; int main() { static_assert(pro::proxiable); static_assert(pro::proxiable, Stringable>); static_assert(!pro::proxiable*, Stringable>); } ``` -------------------------------- ### Example usage of proxy_typeid Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_rtti/proxy_typeid.md Demonstrates how to use proxy_typeid to get the runtime type information of a proxy object. Ensure the proxy is built with the 'rtti' skill. ```cpp #include #include struct RttiAware : pro::facade_builder // ::add_skill // ::build {}; int main() { pro::proxy p = pro::make_proxy(123); std::cout << proxy_typeid(*p).name() << "\n"; // Prints "i" (assuming GCC) } ``` -------------------------------- ### Example usage of `is_bitwise_trivially_relocatable` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/is_bitwise_trivially_relocatable.md Demonstrates how to use `is_bitwise_trivially_relocatable` by specializing it for a custom type `C` and then using `inplace_proxiable_target` to check compatibility. This example highlights how to opt-in types that meet the semantic requirements for bitwise relocation. ```cpp #include #include struct Any : pro::facade_builder // ::build {}; // Requires trivial relocatability by default struct A { int Val; }; struct B { B() = default; B(B&&) noexcept {} int Val; }; struct C { C() = default; C(B&&) noexcept {} int Val; }; namespace pro { template <> struct is_bitwise_trivially_relocatable : std::true_type {}; } // namespace pro int main() { static_assert(pro::inplace_proxiable_target); static_assert(!pro::inplace_proxiable_target); static_assert(pro::inplace_proxiable_target); } ``` -------------------------------- ### Example Usage of PRO_DEF_FREE_AS_MEM_DISPATCH Source: https://github.com/microsoft/proxy/blob/main/docs/spec/PRO_DEF_FREE_AS_MEM_DISPATCH.md This example demonstrates how to use PRO_DEF_FREE_AS_MEM_DISPATCH to create a proxy that can call std::to_string via a member function named ToString. It includes necessary headers and a simple main function to show the output. ```cpp #include #include #include PRO_DEF_FREE_AS_MEM_DISPATCH(FreeToString, std::to_string, ToString); struct Stringable : pro::facade_builder // ::add_convention // ::build {}; int main() { pro::proxy p = pro::make_proxy(123); std::cout << p->ToString() << "\n"; // Prints "123" } ``` -------------------------------- ### Example of using make_proxy_view with std::map Source: https://github.com/microsoft/proxy/blob/main/docs/spec/make_proxy_view.md This example demonstrates how to use make_proxy_view with a std::map to create a proxy_view for a ResourceDictionary facade. It includes static assertions to verify the correct overload of the 'at' method is selected for const and non-const proxy_view objects. ```cpp #include #include #include #include PRO_DEF_MEM_DISPATCH(MemAt, at); struct ResourceDictionary : pro::facade_builder // ::add_convention // ::build {}; int main() { std::map dict; dict[1] = "init"; pro::proxy_view pv = pro::make_proxy_view(dict); static_assert(std::is_same_vat(1)), std::string&>, "Non-const overload"); static_assert( std::is_same_vat(1)), const std::string&>, "Const overload"); // Invokes the const overload and prints "init" std::cout << std::as_const(pv)->at(1) << "\n"; pv->at(1) = "modified"; // Invokes the non-const overload // Invokes the const overload and prints "modified" std::cout << std::as_const(pv)->at(1) << "\n"; } ``` -------------------------------- ### Build and Run Tests with CMake Source: https://github.com/microsoft/proxy/blob/main/docs/README.md Clone the repository, navigate to the directory, and use CMake to build and run tests. Ensure you have CMake installed and configured correctly. ```bash git clone https://github.com/microsoft/proxy.git cd proxy cmake -B build cmake --build build -j ctest --test-dir build -j ``` -------------------------------- ### Example usage of `make_proxy` Source: https://github.com/microsoft/proxy/blob/main/docs/spec/make_proxy.md Demonstrates creating `proxy` objects from various types like bool, int, double, const char*, and in-place construction of a string. Ensure the `Printable` facade is correctly defined. ```cpp #include #include #include #include struct Printable : pro::facade_builder // ::add_convention, std::ostream&(std::ostream&) const> // ::build {}; int main() { // From bool pro::proxy p1 = pro::make_proxy(true); // Prints "true" std::cout << std::boolalpha << *p1 << "\n"; // From int pro::proxy p2 = pro::make_proxy(123); // Prints "123" std::cout << *p2 << "\n"; // From double pro::proxy p3 = pro::make_proxy(3.1415926); // Prints "3.1415926000" std::cout << std::fixed << std::setprecision(10) << *p3 << "\n"; // From const char* pro::proxy p4 = pro::make_proxy("lalala"); // Prints "lalala" std::cout << *p4 << "\n"; // From a in-place constructed string pro::proxy p5 = pro::make_proxy(5, 'x'); // Prints "xxxxx" std::cout << *p5 << "\n"; } ``` -------------------------------- ### Proxiable Target Example Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxiable_target.md Demonstrates the usage of the proxiable_target concept with a lambda function and an integer. Includes necessary headers and a static assert for compile-time verification. ```cpp #include struct Runnable : pro::facade_builder // ::add_convention, void()> // ::build {}; int main() { auto fun = [] {}; static_assert(pro::proxiable_target); static_assert(!pro::proxiable_target); } ``` -------------------------------- ### Example of using allocate_proxy with std::array Source: https://github.com/microsoft/proxy/blob/main/docs/spec/allocate_proxy.md Demonstrates allocating a proxy for a large std::array. The example highlights how allocate_proxy handles targets whose size exceeds default pointer size limitations by allocating additional storage if necessary. ```cpp #include #include // By default, the maximum pointer size defined by pro::facade_builder // is 2 * sizeof(void*). This value can be overridden by `restrict_layout`. struct Any : pro::facade_builder::build {}; int main() { // sizeof(std::array) is usually greater than 2 * sizeof(void*), // calling allocate_proxy has no limitation to the size and alignment of the // target using Target = std::array; pro::proxy p1 = pro::allocate_proxy(std::allocator{}); } ``` -------------------------------- ### Example Usage of PRO_DEF_MEM_DISPATCH Source: https://github.com/microsoft/proxy/blob/main/docs/spec/PRO_DEF_MEM_DISPATCH.md Demonstrates how to use PRO_DEF_MEM_DISPATCH to define a member dispatch for a vector's 'at' function and integrate it into a facade builder. ```cpp #include #include #include #include PRO_DEF_MEM_DISPATCH(MemAt, at); struct Dictionary : pro::facade_builder // ::add_convention // ::build {}; int main() { std::vector v{"hello", "world"}; pro::proxy p = &v; std::cout << p->at(1) << "\n"; // Prints "world" } ``` -------------------------------- ### Example Usage of RTTI Skills Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_rtti/README.md Demonstrates how to use `pro::skills::rtti` and `pro::skills::direct_rtti` to enable RTTI on a facade. It shows the usage of `proxy_typeid` and `proxy_cast` for inspecting and accessing the proxied object. ```cpp #include #include struct RttiAware : pro::facade_builder // ::add_skill // ::add_skill // ::build {}; int main() { int v = 123; pro::proxy p = &v; std::cout << proxy_typeid(p).name() << "\n"; // Prints "Pi" (assuming GCC) std::cout << proxy_cast(p) << "\n"; // Prints the address of v std::cout << proxy_typeid(*p).name() << "\n"; // Prints "i" (assuming GCC) std::cout << proxy_cast(*p) << "\n"; // Prints "123" } ``` -------------------------------- ### Example of support_relocation usage Source: https://github.com/microsoft/proxy/blob/main/docs/spec/basic_facade_builder/support_relocation.md Demonstrates the usage of `support_relocation` in conjunction with other support traits. Note that `Trivial` requires explicit support for relocation even if the underlying type is trivial. ```cpp #include #include struct Movable : pro::facade_builder::build {}; struct Trivial : pro::facade_builder // ::support_copy // ::support_relocation // ::support_destruction // ::build {}; int main() { pro::proxy p1 = std::make_unique(123); // pro::proxy p2 = std::make_unique(456); // Won't compile double v = 3.14; pro::proxy p3 = &v; // Compiles because double* is trivial } ``` -------------------------------- ### Example Usage of substitution_dispatch Source: https://github.com/microsoft/proxy/blob/main/docs/spec/substitution_dispatch/README.md Demonstrates the construction and implicit conversion of proxy objects using `substitution_dispatch`. It shows how proxies can be implicitly converted via const reference and rvalue reference, and how substitution from a null proxy is handled. ```cpp #include #include struct Runnable : pro::facade_builder // ::add_convention, void()> // ::build {}; struct CopyableRunnable : pro::facade_builder // ::support_copy // ::add_facade // ::add_direct_convention() const&, pro::proxy() &&> // ::build {}; int main() { pro::proxy p1 = pro::make_proxy( [] { std::cout << "Lambda expression invoked\n"; }); // Implicit conversion via const reference of pro::proxy pro::proxy p2 = p1; std::cout << std::boolalpha << p2.has_value() << "\n"; // Prints "true" // Implicit conversion via rvalue reference of pro::proxy pro::proxy p3 = std::move(p1); std::cout << p1.has_value() << "\n"; // Prints "false" (*p3)(); // Prints "Lambda expression invoked" // Different from implicit_conversion_dispatch, substitution from a null proxy // is well-formed pro::proxy p4 = p1; std::cout << p4.has_value() << "\n"; // Prints "false" } ``` -------------------------------- ### Example of explicit_conversion_dispatch Source: https://github.com/microsoft/proxy/blob/main/docs/spec/explicit_conversion_dispatch/README.md Demonstrates how to use explicit_conversion_dispatch to create a proxy that can be explicitly converted to an int. The proxy holds a short value which is then cast to an int. ```cpp #include #include struct IntConvertible : pro::facade_builder ::add_convention ::build {}; int main() { // p holds a short pro::proxy p = pro::make_proxy(123); std::cout << static_cast(*p) << "\n"; // Prints "123" } ``` -------------------------------- ### Example Usage of PRO_DEF_FREE_DISPATCH Source: https://github.com/microsoft/proxy/blob/main/docs/spec/PRO_DEF_FREE_DISPATCH.md Demonstrates how to use PRO_DEF_FREE_DISPATCH to create a free function dispatch for std::to_string with a custom accessibility function ToString. ```cpp #include #include #include PRO_DEF_FREE_DISPATCH(FreeToString, std::to_string, ToString); struct Stringable : pro::facade_builder // ::add_convention // ::build {}; int main() { pro::proxy p = pro::make_proxy(123); std::cout << ToString(*p) << "\n"; // Prints "123" } ``` -------------------------------- ### Weak Proxy Example Source: https://github.com/microsoft/proxy/blob/main/docs/spec/weak_proxy.md Demonstrates the usage of `weak_proxy` and its `lock()` method. It shows how to create a weak proxy from a `std::weak_ptr`, attempt to obtain a strong proxy, and check for value presence before and after the underlying object is destroyed. ```cpp #include #include struct Formattable : pro::facade_builder // ::add_skill // ::build {}; int main() { std::shared_ptr val = std::make_shared(123); pro::weak_proxy wp = std::weak_ptr{val}; pro::proxy p = wp.lock(); std::cout << std::boolalpha << p.has_value() << "\n"; // Prints "true" std::cout << std::format("{}\n", *p); // Prints "123" p.reset(); val.reset(); p = wp.lock(); std::cout << p.has_value() << "\n"; // Prints "false" } ``` -------------------------------- ### Example Usage of Proxy Equality Operators Source: https://github.com/microsoft/proxy/blob/main/docs/spec/proxy/friend_operator_equality.md Demonstrates how to use the `==` and `!=` operators with a proxy object to check for the presence of a value. The `!=` operator is synthesized from `operator==`. ```cpp #include #include struct AnyMovable : pro::facade_builder::build {}; int main() { pro::proxy p; std::cout << std::boolalpha << (p == nullptr) << "\n"; // Prints "true" std::cout << (p != nullptr) << "\n"; // Prints "false" p = std::make_unique(123); std::cout << (p == nullptr) << "\n"; // Prints "false" std::cout << (p != nullptr) << "\n"; // Prints "true" } ``` -------------------------------- ### Example Usage of fmt_format with a Formattable Struct Source: https://github.com/microsoft/proxy/blob/main/docs/spec/skills_fmt_format.md Demonstrates how to create a formattable struct using the fmt_format skill and then format a proxy object of that struct using fmt::format. Ensure {fmt} library is included and fmt/xchar.h is included for versions 8.0.0+. ```cpp #include #include #include #include #include struct Formattable : pro::facade_builder // ::add_skill // ::build {}; int main() { pro::proxy p = pro::make_proxy(3.14159); std::cout << fmt::format("*p = {:.2f}\n", *p); // Prints "*p = 3.14" } ```