Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# std.range.interfaces

This module is a submodule of std.range.
The main std.range module provides template-based tools for working with ranges, but sometimes an object-based interface for ranges is needed, such as when runtime polymorphism is required. For this purpose, this submodule provides a number of object and interface definitions that can be used to wrap around range objects created by the std.range templates.

InputRange | Wrapper for input ranges.
InputAssignable | Wrapper for input ranges with assignable elements.
ForwardRange | Wrapper for forward ranges.
ForwardAssignable | Wrapper for forward ranges with assignable elements.
BidirectionalRange | Wrapper for bidirectional ranges.
BidirectionalAssignable | Wrapper for bidirectional ranges with assignable elements.
RandomAccessFinite | Wrapper for finite random-access ranges.
RandomAccessAssignable | Wrapper for finite random-access ranges with assignable elements.
RandomAccessInfinite | Wrapper for infinite random-access ranges.
OutputRange | Wrapper for output ranges.
OutputRangeObject | Class that implements the OutputRange interface and wraps the put methods in virtual functions.
outputRangeObject | Convenience function for creating an OutputRangeObject with a base range of type R that accepts types E.
InputRangeObject | Class that implements the InputRange interface and wraps the input range methods in virtual functions.
inputRangeObject | Convenience function for creating an InputRangeObject of the proper type.
MostDerivedInputRange | Returns the interface type that best matches the range.

Source std/range/interfaces.d

License:
Boost License 1.0.
Authors:
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.

interface `InputRange`(E);
  These interfaces are intended to provide virtual function-based wrappers around input ranges with element type E. This is useful where a well-defined binary interface is required, such as when a DLL function or virtual function needs to accept a generic range as a parameter. Note that isInputRange and friends check for conformance to structural interfaces not for implementation of these interface types.

  Limitations These interfaces are not capable of forwarding ref access to elements.

  Infiniteness of the wrapped range is not propagated.
  Length is not propagated in the case of non-random access ranges.
  See Also:
  inputRangeObject
  Examples:

      import std.algorithm.iteration : map;
      import std.range : iota;

      void useRange(InputRange!int range) {
          // Function body.
      }

      // Create a range type.
      auto squares = map!"a * a"(iota(10));

      // Wrap it in an interface.
      auto squaresWrapped = inputRangeObject(squares);

      // Use it.
      useRange(squaresWrapped);

  @property E `front`();
  E `moveFront`();
    Calls std.range.primitives.moveFront on the wrapped range, if possible. Otherwise, throws an UnsupportedRangeMethod exception.
  void `popFront`();
  @property bool `empty`();
  int `opApply`(scope int delegate(E));
  int `opApply`(scope int delegate(size_t, E));
    foreach iteration uses opApply, since one delegate call per loop iteration is faster than three virtual function calls.
interface `ForwardRange`(E): InputRange!E;
  Interface for a forward range of type E.
  @property ForwardRange!E `save`();
interface `BidirectionalRange`(E): ForwardRange!E;
  Interface for a bidirectional range of type E.
  @property BidirectionalRange!E `save`();
  @property E `back`();
  E `moveBack`();
    Calls std.range.primitives.moveBack on the wrapped range, if possible. Otherwise, throws an UnsupportedRangeMethod exception
  void `popBack`();
interface `RandomAccessFinite`(E): BidirectionalRange!E;
  Interface for a finite random access range of type E.
  @property RandomAccessFinite!E `save`();
  E `opIndex`(size_t);
  E `moveAt`(size_t);
  @property size_t `length`();
  alias `opDollar` = length;
  RandomAccessFinite!E `opSlice`(size_t, size_t);
interface `RandomAccessInfinite`(E): ForwardRange!E;
  Interface for an infinite random access range of type E.
  enum bool `empty`;
  E `moveAt`(size_t);
    Calls std.range.primitives.moveAt on the wrapped range, if possible. Otherwise, throws an UnsupportedRangeMethod exception.
  @property RandomAccessInfinite!E `save`();
  E `opIndex`(size_t);
interface `InputAssignable`(E): InputRange!E;
  Adds assignable elements to InputRange.
  @property void `front`(E `newVal`);
interface `ForwardAssignable`(E): InputAssignable!E, ForwardRange!E;
  Adds assignable elements to ForwardRange.
  @property ForwardAssignable!E `save`();
interface `BidirectionalAssignable`(E): ForwardAssignable!E, BidirectionalRange!E;
  Adds assignable elements to BidirectionalRange.
  @property BidirectionalAssignable!E `save`();
  @property void `back`(E `newVal`);
interface `RandomFiniteAssignable`(E): RandomAccessFinite!E, BidirectionalAssignable!E;
  Adds assignable elements to RandomAccessFinite.
  @property RandomFiniteAssignable!E `save`();
  void `opIndexAssign`(E `val`, size_t `index`);
interface `OutputRange`(E);
  Interface for an output range of type E. Usage is similar to the InputRange interface and descendants.
  void `put`(E);
class `OutputRangeObject`(R, E...): staticMap!(OutputRange, E);
  Implements the OutputRange interface for all types E and wraps the put method for each type E in a virtual function.
  this(R `range`);
template `MostDerivedInputRange`(R) if (isInputRange!(Unqual!R))
  Returns the interface type that best matches R.
template `InputRangeObject`(R) if (isInputRange!(Unqual!R))
  Implements the most derived interface that R works with and wraps all relevant range primitives in virtual functions. If R is already derived from the InputRange interface, aliases itself away.
InputRangeObject!R `inputRangeObject`(R)(R `range`)
if (isInputRange!R);
  Convenience function for creating an InputRangeObject of the proper type. See InputRange for an example.
template `outputRangeObject`(E...)
  Convenience function for creating an OutputRangeObject with a base range of type R that accepts types E.
  Examples:

      import std.array;
      auto app = appender!(uint[])();
      auto appWrapped = outputRangeObject!(uint, uint[])(app);
      static assert(is(typeof(appWrapped) : OutputRange!(uint[])));
      static assert(is(typeof(appWrapped) : OutputRange!(uint)));

  OutputRangeObject!(R, E) `outputRangeObject`(R)(R `range`);
class `UnsupportedRangeMethod`: object.Exception;
  Thrown when an interface method is not supported by the wrapped range

Copyright © 1999-2022 by the D Language Foundation | Page generated by Ddoc on Thu Apr 7 14:47:36 2022