Skip to content

Make cpp2::args usable as a std::ranges::bidirectional_range #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cpp2/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ As always, `main` is the entry point of the program. For example:

- One parameter of implicit type named `args`:   **`#!cpp main: (args) /*etc.*/`**

- The type of `args` cannot be explicitly specified. It is always `cpp2::args_t`, which behaves similarly to a `#!cpp const std::array<std::string_view>`.
- The type of `args` cannot be explicitly specified. It is always `cpp2::args`, which behaves similarly to a `#!cpp const std::array<std::string_view>`.

- Using `args` performs zero heap allocations. Every `string_view` is directly bound to the string storage provided by host environment.

Expand Down
10 changes: 7 additions & 3 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,10 @@ struct args
class iterator {
public:
constexpr iterator(int c, char** v, int start) : argc{c}, argv{v}, curr{start} {}
constexpr iterator() = default;

using difference_type = std::ptrdiff_t;
using value_type = std::string_view;

constexpr auto operator*() const {
if (curr < argc) { return std::string_view{ argv[curr] }; }
Expand All @@ -2342,9 +2346,9 @@ struct args
constexpr auto operator<=>(iterator const&) const = default;

private:
int argc;
char** argv;
int curr;
int argc = 0;
char** argv = nullptr;
int curr = 0;
};

constexpr auto begin() const -> iterator { return iterator{ argc, argv, 0 }; }
Expand Down
12 changes: 12 additions & 0 deletions regression-tests/pure2-main-args-range.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
main: (args) = {
static_assert(std::bidirectional_iterator<cpp2::args::iterator>);
static_assert(std::ranges::bidirectional_range<cpp2::args>);
static_assert(std::ranges::viewable_range<cpp2::args>);

// Output command line arguments, dropping the program name argc[0].
for args | std::ranges::views::drop(1) do (arg)
{
std::cout
<< arg << std::endl;
}
}
34 changes: 34 additions & 0 deletions regression-tests/test-results/pure2-main-args-range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#define CPP2_IMPORT_STD Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "pure2-main-args-range.cpp2"


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-main-args-range.cpp2"
auto main(int const argc_, char** argv_) -> int;

//=== Cpp2 function definitions =================================================

#line 1 "pure2-main-args-range.cpp2"
auto main(int const argc_, char** argv_) -> int{
auto const args = cpp2::make_args(argc_, argv_);
#line 2 "pure2-main-args-range.cpp2"
static_assert(std::bidirectional_iterator<cpp2::args::iterator>);
static_assert(std::ranges::bidirectional_range<cpp2::args>);
static_assert(std::ranges::viewable_range<cpp2::args>);

// Output command line arguments, dropping the program name argc[0].
for ( auto const& arg : args | std::ranges::views::drop(1) )
{
std::cout
<< arg << std::endl;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-main-args-range.cpp2... ok (all Cpp2, passes safety checks)

Loading