Flow 1.0.2
Flow project: Full implementation reference.
op.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#include "flow/async/op.hpp"
20
21namespace flow::async
22{
23
24// Implementations.
25
26Op_list::Op_list(log::Logger* logger_ptr, size_t n_ops, const Create_op_func& create_op_func) :
27 log::Log_context(logger_ptr, Flow_log_component::S_ASYNC),
28 m_ops(n_ops), // This many empty boost::any objects. Must still set each one.
29 m_rnd_op_idx(0, n_ops - 1) // Random indices generated in [0, n_ops). Seed from time; it's fine.
30{
31 assert(n_ops >= 1);
32
33 FLOW_LOG_INFO("Op_list [" << static_cast<const void*>(this) << "]: will store [" << n_ops << "] ops.");
34
35 for (size_t idx = 0; idx != n_ops; ++idx)
36 {
37 m_ops[idx] = create_op_func(idx);
38 }
39}
40
41size_t Op_list::size() const
42{
43 return m_ops.size();
44}
45
46const Op& Op_list::operator[](size_t idx) const
47{
48 return m_ops[idx];
49}
50
51const Op& Op_list::random_op(size_t* chosen_idx) const
52{
53 const auto idx = random_idx();
54 if (chosen_idx)
55 {
56 *chosen_idx = idx;
57 }
58
59 FLOW_LOG_TRACE("Op_list [" << this << "]: Randomly selected "
60 "op index [" << idx << "] from [0, " << size() << ").");
61
62 return operator[](idx);
63}
64
65size_t Op_list::random_idx() const
66{
67 return m_rnd_op_idx();
68}
69
70const std::vector<Op>& Op_list::ops_sequence() const
71{
72 return m_ops;
73}
74
75} // namespace flow::async
const Op & random_op(size_t *chosen_idx=0) const
Returns (*this)[R], where we randomly select R as if by random_idx() and communicate it to the caller...
Definition: op.cpp:51
Op_list(log::Logger *logger_ptr, size_t n_ops, const Create_op_func &create_op_func)
Populates the async::Op list, which is immutable after this returns.
Definition: op.cpp:26
util::Rnd_gen_uniform_range_mt< size_t > m_rnd_op_idx
Random number facility for generating random indices in [0, m_ops.size()).
Definition: op.hpp:148
const std::vector< Op > & ops_sequence() const
Returns reference to immutable internally stored sequence of async::Op in order consistent with other...
Definition: op.cpp:70
const Op & operator[](size_t idx) const
Returns reference to immutable async::Op at the given index in [0, size()).
Definition: op.cpp:46
std::vector< Op > m_ops
The payload of async::Op objects.
Definition: op.hpp:138
size_t random_idx() const
Returns a randomly selected index from range [O, size()).
Definition: op.cpp:65
size_t size() const
Returns the number of async::Op stored in this Op_list.
Definition: op.cpp:41
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
#define FLOW_LOG_INFO(ARG_stream_fragment)
Logs an INFO message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:197
#define FLOW_LOG_TRACE(ARG_stream_fragment)
Logs a TRACE message into flow::log::Logger *get_logger() with flow::log::Component get_log_component...
Definition: log.hpp:227
Flow module containing tools enabling multi-threaded event loops operating under the asynchronous-tas...
Definition: async_fwd.hpp:75
boost::any Op
An object of this opaque type represents a collection of 1 or more async::Task, past or future,...
Definition: async_fwd.hpp:153
@ S_ASYNC
Simply post the given task to execute asynchronously in some execution context – as soon as the conte...
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...
Definition: common.hpp:633