libcamera/src
Laurent Pinchart 38dd90307a libcamera: Remove std::piecewise_construct where not necessary
When inserting an element with emplace(), the element is constructed
in-place with the parameters to the emplace() method being forwarded to
the constructor of the element. For std::map containers, the element is
an std::pair<const Key, T>. The constructors of std::pair<T1, T2> fall
into three categories:

(1) Default, copy and move constructors (and related versions)
(2) Constructors that take lvalue or rvalue references to T1 and T2
(3) A forwarding constructor that forwards parameters to the
    constructors of T1 and T2

The first category isn't useful in most cases for std::map::emplace(),
as the caller usually doesn't have an existing std::pair<const Key, T>
for the element to be inserted.

The constructor from the third category is useful to avoid constructing
intermediate Key or T instances when the caller doesn't have them
available. This constructor takes two std::tuple arguments that contain
the arguments for the Key and T constructors, respectively. Due to
template deduction rules, usage of such a constructor couldn't be
deduced by the compiler automatically in all cases, so the constructor
takes a first argument of type std::piecewise_construct_t that lets the
caller force the usage ot the forwarding constructor (also known for
this reason as the piecewise constructor). The caller uses a construct
such as

        map.emplace(std::piecewise_construct,
                    std::forward_as_tuple(args_for_Key, ...),
                    std::forward_as_tuple(args_for_T, ...));

This syntax is a bit heavy, but is required to construct Key and T
in-place from arguments to their non-default constructor (it is also the
only std::pair non-default constructor that can be used for non-copyable
non-movable types).

When the caller of std::map::emplace() already has references to a Key
and a T, they can be passed to the std::pair piecewise constructor, and
this will create std::tuple instance to wrap the Key and T references
arguments to ultimately pass them to the Key and T copy constructors.

        map.emplace(std::piecewise_construct,
                    std::forward_as_tuple(Key_value),
                    std::forward_as_tuple(T_value));

While this mechanism works, it's unnecessary complex. A constructor of
std::pair that takes references to Key and T can be used without any
performance penalty, as it will also call the copy constructor of Key
and T. In this case we can use a simpler constructor of std::pair, and
thus a simpler call of std::map::emplace.

        map.emplace(Key_value, T_value);

We have a couple occurrences of this above misuse of piecewise
construction. Simplify them, which simplifies the code and reduces the
generated code size.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2020-01-16 19:29:16 +02:00
..
android libcamera: Switch from utils::make_unique to std::make_unique 2020-01-14 19:06:40 +02:00
cam libcamera: camera: Remove the prepared state 2020-01-12 16:10:38 +01:00
ipa libcamera: Switch from utils::make_unique to std::make_unique 2020-01-14 19:06:40 +02:00
libcamera libcamera: Remove std::piecewise_construct where not necessary 2020-01-16 19:29:16 +02:00
qcam libcamera: camera: Remove the prepared state 2020-01-12 16:10:38 +01:00
v4l2 libcamera: Switch from utils::make_unique to std::make_unique 2020-01-14 19:06:40 +02:00
meson.build v4l2: v4l2_compat: Add V4L2 compatibility layer 2020-01-03 19:53:20 -05:00