Skip to content

Commit

Permalink
Update variant component documentation
Browse files Browse the repository at this point in the history
Remove some dead code from internals
  • Loading branch information
bruxisma committed Apr 22, 2015
1 parent bcdd47e commit e1d1dd7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
68 changes: 46 additions & 22 deletions docs/variant.rst
Expand Up @@ -3,6 +3,8 @@ Variant Component

.. namespace:: core

.. index:: variant

The :any:`variant` component is a generic type-safe implementation of a
discriminate union. It is equivalent to the Boost.Variant library, with several
small exceptions.
Expand All @@ -20,6 +22,8 @@ switch statement*

The variant component resides in :file:`<core/{variant}.hpp>`.

.. index:: variant; exceptions

.. class:: bad_variant_get

:inherits: :cxx:`std::logic_error`
Expand All @@ -36,6 +40,8 @@ The variant component resides in :file:`<core/{variant}.hpp>`.
default constructing a :any:`variant`, the first type in the
:any:`variant`'s typelist is initialized.

.. index:: variant; constructors

.. function:: variant (T&& value)

:requires: :samp:`{T}` be of a type from which any type in :samp:`{Ts}`
Expand Down Expand Up @@ -82,23 +88,14 @@ The variant component resides in :file:`<core/{variant}.hpp>`.
Assigns the contents of the other :any:`variant` to :cxx:`*this`. The
object contained within :cxx:`*this` is destructed first.

.. function:: bool operator == (variant const& that) const noexcept

If both :any:`variant`'s :any:`which` is the same value, the values
contained within are compared via :cxx:`operator ==`. Otherwise, the
result of comparing any:`which` is returned.

.. function:: bool operator < (variant const& that) const noexcept

If both :any:`variant`'s :any:`which` are equal, the values contained are
compared. Otherwise, the result of comparing :any:`which` is returned.

.. function:: void swap (variant&)

Swaps the contents of both variants.

.. function:: visit (Visitor&&, Args&&... args) const
visit (Visitor&&, Args&&... args)
.. index:: variant; operations

.. function:: auto visit (Visitor&&, Args&&... args) const
auto visit (Visitor&&, Args&&... args)

Visiting a :any:`variant` follows the following semantics. These semantics
require that, when given a callable type :samp:`{Visitor}`, and variadic
Expand All @@ -116,12 +113,23 @@ The variant component resides in :file:`<core/{variant}.hpp>`.

:returns: :samp:`common_type_t<invoke_of_t<{Visitor}, {Ts}, {Args}...>...>`

.. function:: match (Visitors&&) const
match (Visitors&&)
.. function:: auto match (Visitors&&... visitors) const
auto match (Visitors&&... visitors)

Takes a variadic number of :samp:`{visitors}` that are all callable
objects. These objects are combined into a single visitor and then
executed on the :any:`variant`.

:example:

Takes a variadic number of arguments that are all callable objects. These
objects are combined into a single visitor and then executed on the
:any:`variant`.
.. code-block:: cpp
variant<int, std::string> item { };
item.match(
[] (int v) { std::cout << v << std::endl; },
[] (std::string const& s) { std::cout << s << std::endl; });
.. index:: variant; observers

.. function:: std::type_info const& type () const noexcept

Expand All @@ -140,6 +148,22 @@ The variant component resides in :file:`<core/{variant}.hpp>`.

:returns: false

.. index:: variant; operators

.. function:: bool operator == (variant const& that) const noexcept

If both :any:`variant`'s :any:`which` is the same value, the values
contained within are compared via :cxx:`operator ==`. Otherwise, the
result of comparing any:`which` is returned.

.. function:: bool operator < (variant const& that) const noexcept

If both :any:`variant`'s :any:`which` are equal, the values contained are
compared. Otherwise, the result of comparing :any:`which` is returned.


.. index:: variant; functions

.. function:: auto const& get<N> (variant const& v)
auto&& get<N> (variant&& v)
auto& get<N> (variant& v)
Expand Down Expand Up @@ -180,7 +204,7 @@ The variant component resides in :file:`<core/{variant}.hpp>`.

.. function:: void swap (variant& lhs, variant& rhs)

Calls :any:`variant\<Ts...>::swap`
Calls :any:`variant\<Ts...>::swap`. Equivalent to :samp:`{lhs}.swap({rhs})`

Specializations
---------------
Expand All @@ -191,7 +215,7 @@ These are specializations placed in the :cxx:`std` namespace.

.. class:: hash<variant<Ts...>>

A specialization of ``std::hash<T>`` for variants. Requires that all
A specialization of :cxx:`std::hash<T>` for variants. Requires that all
:samp:`{Ts}...` in a :any:`variant` be specialized for :cxx:`std::hash`.

.. function:: auto const& get<N>(variant const&)
Expand All @@ -201,6 +225,6 @@ These are specializations placed in the :cxx:`std` namespace.
.. deprecated:: 1.2 Please use :any:`core::get\<N>`

Calls :any:`core::get`, and returns the value. This specialization is
provided to interact with ``std::tuple`` and to provide *some* semblance of
boost interoperability. However it does not support using the type to get
provided to interact with :cxx:`std::tuple` and to provide *some* semblance
of boost interoperability. However it does not support using the type to get
the value from the variant.
30 changes: 3 additions & 27 deletions include/core/internal.hpp
Expand Up @@ -79,14 +79,7 @@ struct unary {
using result_type = R;
};

/* constexpr mem_fn plumbing
* names here are kept short because we *have* to give some of these types a
* name, but the name doesn't really matter (nor are the attempts I've made
* at giving them proper names truly descriptive). Comments are placed above
* each one so that anyone reading won't wonder why the member function pointer
* trait base is named the onomatopoeia for giving someone a raspberry :P and
* so on and so forth.
*
/*
* If I can't amuse myself when working with C++ templates, then life isn't
* worth living. Bury me with my chevrons.
*/
Expand All @@ -100,26 +93,9 @@ constexpr T&& pass (remove_reference_t<T>&& t) noexcept {
return static_cast<T&&>(t);
}

/* mfptb == member function pointer trait base
* No, I don't feel like typing that out. I am mad that I have to do all
* this in the first place.
/* INVOKE pseudo-expression plumbing, *much* more simplified than previous
* versions of core
*/
template <class R, class... Args> struct mfptb {
constexpr mfptb () noexcept = default;
};
template <class R, class T, class U> struct mfptb<R, T, U> :
binary<T, U, R>
{ };
template <class R, class T> struct mfptb<R, T> :
unary<T, R>
{ };

template <class T> using ptr = typename ::std::add_pointer<T>::type;
template <class T> using cptr = ptr<typename ::std::add_const<T>::type>;
template <class T> using vptr = ptr<typename ::std::add_volatile<T>::type>;
template <class T> using cvptr = ptr<typename ::std::add_cv<T>::type>;

/* INVOKE pseudo-expression plumbing, *much* more simplified. */
struct undefined { constexpr undefined (...) noexcept { } };

/* We get some weird warnings under clang, so we actually give these functions
Expand Down

0 comments on commit e1d1dd7

Please sign in to comment.