Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Generator.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Traversal_Generator_h_
5#define _Stroika_Foundation_Traversal_Generator_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
11
12/**
13 * \file
14 *
15 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
16 *
17 * TODO:
18 * @todo Complete redesign in light of C++23 coroutines (when we do Stroika v3.1).
19 *
20 * @todo See if/how DiscreteRange<> can be made simpler using this generator code.
21 *
22 * @todo I have old docs comment:
23 *
24 * "Writeup details on how todo generator � as lambda-from-iteator � like I did for
25 * revision with queie and lambdas � except we have we construct new object with
26 * updstream-get-lambda and pass it to downstream one. I think that works. Try draft�."
27 *
28 * I'm now not quite sure what that means. This may have been more for the FunctionApplicaiton module?
29 * But keep for a little bit to see if it makes sense when I review this code later...
30 *
31 * -- LGP 2013-10-14
32 *
33 */
34
35namespace Stroika::Foundation::Traversal {
36
37 /**
38 * Note - if you need to maintain context for the iterator (typically yes) - bind it into the
39 * std::function lambda closure (with smart pointers).
40 */
41 template <typename T>
42 Iterator<T> CreateGeneratorIterator (const function<optional<T> ()>& getNext);
43
44 /**
45 * \brief Create an Iterable<T> from a function that returns optional<T> - treating nullopt as meaning the END of iteration.
46 *
47 * Note - if you need to maintain context for the iterator (typically yes) - bind it into the
48 * std::function lambda closure (with smart pointers).
49 *
50 * \par Example Usage
51 * \code
52 * constexpr int kMin = 1;
53 * constexpr int kMax = 10;
54 * auto myContext = make_shared<int> (kMin - 1);
55 * auto getNext = [myContext] () -> optional<int> {
56 * ++(*myContext);
57 * if (*myContext > 10)
58 * {
59 * return nullopt;
60 * }
61 * return *myContext;
62 * };
63 *
64 * int sum = 0;
65 * for (auto i : CreateGenerator<int> (getNext)) {
66 * EXPECT_TRUE (1 <= i and i <= 10);
67 * sum += i;
68 * }
69 * EXPECT_TRUE (sum == (kMax - kMin + 1) * (kMax + kMin) / 2);
70 * \endcode
71 */
72 template <typename T>
73 Iterable<T> CreateGenerator (const function<optional<T> ()>& getNext);
74
75}
76
77/*
78 ********************************************************************************
79 ******************************* Implementation Details *************************
80 ********************************************************************************
81 */
82#include "Generator.inl"
83
84#endif /*_Stroika_Foundation_Traversal_Generator_h_ */
Iterator< T > CreateGeneratorIterator(const function< optional< T >()> &getNext)
Definition Generator.inl:14
Iterable< T > CreateGenerator(const function< optional< T >()> &getNext)
Create an Iterable<T> from a function that returns optional<T> - treating nullopt as meaning the END ...
Definition Generator.inl:55