STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
HoldParse.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <charconv>
4#include <cstdint>
5#include <string>
6
7namespace sequencer {
8
9/**
10 * Hard ceiling on any client-supplied hold, independent of per-state config.
11 *
12 * This is not the real limit — each holdable state carries its own, usually far smaller, max. This
13 * one exists so a typo or a garbage frame is rejected at the wire before it reaches anything that
14 * has to reason about it.
15 */
16inline constexpr uint32_t kMaxHoldMs = 3600000; // 1 hour
17
18/**
19 * Parse a hold duration off the wire, strictly.
20 *
21 * Strictly, because the lenient version of this is a hazard: atoi("1e3") returns 1, so a frame
22 * asking for a thousand seconds would have quietly run a one-millisecond pulse and the operator
23 * would have weighed a mass against a window that never happened. The whole string must be digits.
24 *
25 * Rejects: empty, non-digits, a leading sign, trailing junk, anything over kMaxHoldMs, and zero —
26 * zero is the "no duration supplied" sentinel everywhere else, so it must never arrive as one.
27 *
28 * @param out written only on success.
29 * @return true if `text` is a usable duration.
30 */
31inline bool parseHoldMs(const std::string& text, uint32_t& out) {
32 const size_t b = text.find_first_not_of(" \t\r\n");
33 if (b == std::string::npos)
34 return false;
35 const size_t e = text.find_last_not_of(" \t\r\n");
36
37 const char* first = text.data() + b;
38 const char* last = text.data() + e + 1;
39
40 uint64_t v = 0;
41 const auto res = std::from_chars(first, last, v);
42 if (res.ec != std::errc() || res.ptr != last)
43 return false; // not a number, or only a prefix of one ("1e3", "1000x")
44 if (v == 0 || v > kMaxHoldMs)
45 return false;
46
47 out = static_cast<uint32_t>(v);
48 return true;
49}
50
51} // namespace sequencer
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
json_pointer res
Definition json.hpp:13957
constexpr uint32_t kMaxHoldMs
Hard ceiling on any client-supplied hold, independent of per-state config.
Definition HoldParse.hpp:16
bool parseHoldMs(const std::string &text, uint32_t &out)
Parse a hold duration off the wire, strictly.
Definition HoldParse.hpp:31