You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.4 KiB

/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
namespace Magnum {
/** @page method-chaining Method chaining
@brief Little feature helping to reduce typing and encourage best practices.
@m_footernavigation
Method chaining ([Wikipedia](http://en.wikipedia.org/wiki/Method_chaining)) is a
feature which allows you to chain method calls one after another without
repeatedly specifying variable the method is called on. Its primary goal is to
reduce unnecessary repeated names, improving code readability.
Magnum uses this feature mainly for configuring OpenGL objects (such as
various mesh and framebuffer options, shader uniforms etc.). Because OpenGL was
designed with "bind-to-modify" approach, most configuration calls internally
need to bind the object first and only after that change the parameters (unless
@gl_extension{ARB,direct_state_access} extension is available to avoid this). To
reduce unneeded bind calls, Magnum binds the object only if it is not already
bound somewhere. Method chaining encourages you to configure whole object in
one run, effectively reducing the number of needed bindings. Consider the
following example:
@snippet MagnumGL.cpp method-chaining-texture
This code is written that similar configuration steps are grouped together,
which might be good when somebody needs to change something for all three
textures at once, but on the other hand the code is cluttered with repeated
names and after each configuration step the texture must be rebound to another.
With method chaining used the code looks much lighter and each object is
configured in one run, reducing count of bind calls from 9 to 3.
@snippet MagnumGL.cpp method-chaining-texture-chained
Method chaining is not used on non-configuring functions, such as
@ref GL::Framebuffer::clear() or @ref GL::AbstractShaderProgram::draw(), as
their desired use is commonly as a last step in the chain, after everything
else.
Method chaining is also used in @ref SceneGraph and other libraries and in some
cases it allows you to just "configure and forget" without even saving the
created object to some variable, for example when adding static object to an
scene:
@code{.cpp}
Scene3D scene;
(*(new MyObject(&scene)))
.rotateX(90.0_degf)
.translate({-1.5f, 0.5f, 7.0f});
@endcode
*/
}