Browse Source

Improved template implementation files documentation.

* ResourceManager won't have template implementation file, as it would
   add too much work on the user.
 * Color won't have either, as some internal functions are impossible to
   explicitly instantiate.
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
e7539b6fbd
  1. 21
      doc/compilation-speedup.dox
  2. 4
      src/Color.h
  3. 11
      src/ResourceManager.h

21
doc/compilation-speedup.dox

@ -38,17 +38,18 @@ Template implementation files have `*.hpp` extension (hinting that they are
something between `*.h` and `*.cpp` files). something between `*.h` and `*.cpp` files).
Template implementation file can be included along the header itself and it Template implementation file can be included along the header itself and it
will just work, but it doesn't positively affect compilation time. If you are will just work, but it will negatively affect compilation time. If you are
using one template specialization on many places, template implementation using one template specialization in many places, the compiler performs
files give you the ability to explicitly instantiate the template in some compilation of the same template specialization many times. Template
source file. Then you can include only the header everywhere else and leave implementation files give you the ability to explicitly instantiate the
the rest on the linker. template only once in some dedicated source file. Then you can include just
the header everywhere else and leave the rest on the linker.
Templated classes which have implementation files state in their documentation Templated classes which have implementation files state in their documentation
all common specializations that are already compiled in the libraries. So, all common specializations that are already compiled in the libraries. So,
unless the templated class is too generic (ResourceManager for example) or you unless the templated class is too generic or you need something special, you
need something special, you don't have to mess with Object implementation don't have to mess with template implementation files at all. See
files at all. See Color3 or SceneGraph::Object for an example. SceneGraph::Object or SceneGraph::AbstractCamera for an example.
Sometimes you however need to use your own specialization and that's why Sometimes you however need to use your own specialization and that's why
template implementation files are included in the library. For example we want template implementation files are included in the library. For example we want
@ -66,8 +67,8 @@ using namespace Magnum::SceneGraph;
template class Object<MatrixTransformation3D<GLdouble>>; template class Object<MatrixTransformation3D<GLdouble>>;
@endcode @endcode
All other files using the same object specialization now need to include only All other files using the same object specialization now need to include only
SceneGraph/Object.h header and if we compile our `Object.cpp` together with SceneGraph/Object.h header. Thus the Object specialization will be compiled
the rest, the Object specialization will be compiled only once. only once in our `Object.cpp` file, saving precious compilation time.
@subsection compilation-speedup-extern-templates Extern templates @subsection compilation-speedup-extern-templates Extern templates

4
src/Color.h

@ -143,6 +143,8 @@ range @f$ [0.0, 1.0] @f$.
@todo Signed normalization to [-1.0, 1.0] like in OpenGL? @todo Signed normalization to [-1.0, 1.0] like in OpenGL?
*/ */
/* Not using template specialization because some internal functions are
impossible to explicitly instantiate */
template<class T> class Color3: public Math::Vector3<T> { template<class T> class Color3: public Math::Vector3<T> {
public: public:
/** @brief Corresponding floating-point type for HSV computation */ /** @brief Corresponding floating-point type for HSV computation */
@ -287,6 +289,8 @@ MAGNUM_VECTOR_SUBCLASS_OPERATOR_IMPLEMENTATION(Color3, 3)
See Color3 for more information. See Color3 for more information.
*/ */
/* Not using template specialization because some internal functions are
impossible to explicitly instantiate */
template<class T> class Color4: public Math::Vector4<T> { template<class T> class Color4: public Math::Vector4<T> {
public: public:
/** @copydoc Color3::FloatingPointType */ /** @copydoc Color3::FloatingPointType */

11
src/ResourceManager.h

@ -133,12 +133,12 @@ namespace Implementation {
inline Data(): data(nullptr), state(ResourceDataState::Mutable), policy(ResourcePolicy::Manual), referenceCount(0) {} inline Data(): data(nullptr), state(ResourceDataState::Mutable), policy(ResourcePolicy::Manual), referenceCount(0) {}
Data(Data&& other): data(other.data), state(other.state), policy(other.policy), referenceCount(other.referenceCount) { inline Data(Data&& other): data(other.data), state(other.state), policy(other.policy), referenceCount(other.referenceCount) {
other.data = nullptr; other.data = nullptr;
other.referenceCount = 0; other.referenceCount = 0;
} }
~Data() { inline ~Data() {
CORRADE_ASSERT(referenceCount == 0, "ResourceManager: cannot destruct it while data are still referenced", ); CORRADE_ASSERT(referenceCount == 0, "ResourceManager: cannot destruct it while data are still referenced", );
delete data; delete data;
} }
@ -389,6 +389,8 @@ template<class T, class U = T> class Resource {
Provides storage for arbitrary set of types, accessible globally using Provides storage for arbitrary set of types, accessible globally using
instance(). instance().
@section ResourceManager-usage Usage
Each resource is referenced from Resource class. For optimizing performance, Each resource is referenced from Resource class. For optimizing performance,
each resource can be set as mutable or final. Mutable resources can be each resource can be set as mutable or final. Mutable resources can be
modified by the manager and thus each %Resource instance asks the manager for modified by the manager and thus each %Resource instance asks the manager for
@ -444,6 +446,9 @@ cube->draw();
- Destroying resource references and deleting manager instance when nothing - Destroying resource references and deleting manager instance when nothing
references the resources anymore. references the resources anymore.
*/ */
/* Due to too much work involved with explicit template instantiation (all
Resource combinations, all ResourceManagerData...), this class doesn't have
template implementation file. */
template<class... Types> class ResourceManager: protected Implementation::ResourceManagerData<Types>... { template<class... Types> class ResourceManager: protected Implementation::ResourceManagerData<Types>... {
public: public:
/** @brief Global instance */ /** @brief Global instance */
@ -557,7 +562,7 @@ template<class... Types> class ResourceManager: protected Implementation::Resour
}; };
/** @debugoperator{Magnum::ResourceKey} */ /** @debugoperator{Magnum::ResourceKey} */
template<class T> Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const ResourceKey& value) { template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::Debug debug, const ResourceKey& value) {
return debug << static_cast<const Corrade::Utility::HashDigest<sizeof(std::size_t)>&>(value); return debug << static_cast<const Corrade::Utility::HashDigest<sizeof(std::size_t)>&>(value);
} }

Loading…
Cancel
Save