https://github.com/rnburn/bbai-kernel
C++ components used in the project https://github.com/rnburn/bbai
https://github.com/rnburn/bbai-kernel
cpp cpp20 memory-allocation performance polymorphic-allocators
Last synced: 2 months ago
JSON representation
C++ components used in the project https://github.com/rnburn/bbai
- Host: GitHub
- URL: https://github.com/rnburn/bbai-kernel
- Owner: rnburn
- License: apache-2.0
- Created: 2019-04-30T21:48:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-04T05:31:40.000Z (over 2 years ago)
- Last Synced: 2025-04-02T16:25:16.615Z (9 months ago)
- Topics: cpp, cpp20, memory-allocation, performance, polymorphic-allocators
- Language: C++
- Homepage: https://buildingblock.ai/
- Size: 176 KB
- Stars: 51
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bbai-kernel
 [](https://opensource.org/licenses/Apache-2.0)
bbai-kernel is a modern C++ library with components for reflection and building allocator aware types.
It is a small piece of the project [bbai](https://github.com/rnburn/bbai) that provides algorithms
for deterministic objective Bayesian analysis.
## Documentation
* [How to Implement Reflection With an Inline Macro](https://buildingblock.ai/reflection)
* [How to Build an Allocator-aware Smart Pointer](https://buildingblock.ai/allocator-aware-smart-ptr)
* [How to Make Your C++ Code Faster With Extended Stack Space](https://buildingblock.ai/extended-stack)
* [How to Compose Allocator-aware Types](https://buildingblock.ai/allocator-aware-composition)
## Quick start
```cpp
#include
#include
#include
#include
#include "bbai/memory/management/managed_ptr.h"
#include "bbai/memory/management/managed_ptr_utility.h"
struct A {
virtual ~A() noexcept = default;
virtual int f() const noexcept = 0;
};
struct B final : public A {
explicit B(int xx) : x{xx} {}
int x;
int f() const noexcept override { return x; }
};
int main() {
std::array buffer;
std::pmr::monotonic_buffer_resource resource{static_cast(buffer.data()),
buffer.size()};
bbai::memmg::managed_ptr ptr1{&resource};
// we configure ptr1 to allocate into buffer
std::pmr::polymorphic_allocator<> alloc;
bbai::memmg::managed_ptr ptr2 =
bbai::memmg::allocate_managed(alloc, 123);
// ptr2 manages memory from the heap
ptr1 = std::move(ptr2);
// because the two pointers have unequal allocators, the assignment will
// reallocate memory and move construct an instance of B into buffer
std::cout << (reinterpret_cast(ptr1.get()) == buffer.data()) << "\n";
// prints 1
std::cout << ptr1->f() << std::endl; // prints 123
return 0;
}
```