Containers
This page documents the container and smart pointer utilities in Capy.
Code snippets assume using namespace boost::capy; is in effect.
|
polystore
A container of type-erased objects indexed by their type:
polystore ps;
// Insert and retrieve by type
A& a = ps.emplace<A>();
B& b = ps.insert(B{});
A& found = ps.get<A>(); // Throws if not found
A* maybe = ps.find<A>(); // Returns nullptr if not found
datastore
A polystore with an explicit clear() method:
datastore ctx;
ctx.emplace<my_service>();
// ... use services ...
ctx.clear(); // Destroy all stored objects
Commonly used as a service container for compression services.
application
A polystore with lifecycle management for application components:
application app;
// Construct parts
app.emplace<http_server>();
app.emplace<database_pool>();
// Start all parts (calls start() on each)
app.start();
// ... run until shutdown ...
// Stop all parts (calls stop() on each in reverse order)
app.stop();
// Wait for completion
app.join();
Parts should implement start() and stop() methods.
intrusive_list
A doubly-linked list where elements derive from intrusive_list<T>::node:
struct my_item : intrusive_list<my_item>::node
{
int value;
};
intrusive_list<my_item> list;
my_item item;
list.push_back(&item);
my_item* front = list.pop_front();
list.remove(&item);
intrusive_queue
A FIFO queue where elements derive from intrusive_queue<T>::node:
struct work_item : intrusive_queue<work_item>::node
{
std::function<void()> fn;
};
intrusive_queue<work_item> queue;
work_item item;
queue.push(&item);
work_item* next = queue.pop();
small_unique_ptr
A smart pointer with small buffer optimization:
// Uses SBO if Derived fits in 32-byte buffer
auto p = make_small_unique<Base, 32, Derived>(constructor_args...);
// Access like unique_ptr
p->method();
Base& ref = *p;
embed
A utility for embedding string literals:
embed text(R"(
Hello "world"
This has quotes and )
)");
std::string_view sv = text; // Implicit conversion
The first character (typically newline) is removed, enabling clean formatting in source code.
Summary
| Class | Purpose |
|---|---|
|
Type-erased container indexed by type |
|
Polystore with clear() |
|
Lifecycle management for app components |
|
Doubly-linked list without node allocation |
|
FIFO queue without node allocation |
|
Unique pointer with small buffer optimization |
|
String literal embedding helper |
Next Steps
-
File I/O — Platform-independent file operations
-
Compression — Brotli and ZLib support