PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
feed_reader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pineforge/bar.hpp>
4
5#include <cstddef>
6#include <cstdint>
7#include <stdexcept>
8
9namespace pineforge {
10
11// A feed is addressed by its logical bar index, independently of residency.
12// read() copies exact records into caller storage. A valid cold index must
13// read from the provider; only an invalid range or an actual provider failure
14// may fail. No pointer into a provider's transfer/cache window escapes.
15//
16// A reader installed in a projection is an immutable snapshot: its size and
17// records stay fixed for that snapshot's lifetime. A changed feed uses a new
18// reader. Implementations preserve their typed backing I/O failures.
19template<class Row>
21public:
22 virtual ~FeedReader() = default;
23 virtual std::size_t size() const noexcept = 0;
24 virtual void read(std::size_t first, Row* destination,
25 std::size_t count) const = 0;
26
27 Row at(std::size_t index) const {
28 Row row{};
29 read(index, &row, 1);
30 return row;
31 }
32};
33
34struct FeedBar {
36 std::int64_t close_ms = 0;
37};
38
39// An opt-in bridge for an existing caller-owned pair of arrays. It neither
40// copies nor takes ownership; the caller retains both arrays, unchanged, for
41// the reader's lifetime. Existing raw-array engine entry points keep their
42// existing ownership contracts.
43class BorrowedBarFeedReader final : public FeedReader<FeedBar> {
44public:
45 BorrowedBarFeedReader(const Bar* bars, const std::int64_t* close_ms,
46 std::size_t count)
47 : bars_(bars), close_ms_(close_ms), count_(count) {
48 if (count && (!bars || !close_ms))
49 throw std::invalid_argument("feed reader arrays");
50 }
51 std::size_t size() const noexcept override { return count_; }
52 void read(std::size_t first, FeedBar* destination,
53 std::size_t count) const override {
54 if (first > count_ || count > count_ - first)
55 throw std::out_of_range("feed reader range");
56 if (count && !destination)
57 throw std::invalid_argument("feed reader destination");
58 for (std::size_t i = 0; i < count; ++i)
59 destination[i] = FeedBar{bars_[first + i], close_ms_[first + i]};
60 }
61private:
62 const Bar* bars_;
63 const std::int64_t* close_ms_;
64 std::size_t count_;
65};
66
67} // namespace pineforge
std::size_t size() const noexcept override
BorrowedBarFeedReader(const Bar *bars, const std::int64_t *close_ms, std::size_t count)
void read(std::size_t first, FeedBar *destination, std::size_t count) const override
virtual void read(std::size_t first, Row *destination, std::size_t count) const =0
Row at(std::size_t index) const
virtual ~FeedReader()=default
virtual std::size_t size() const noexcept=0
std::int64_t close_ms