Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Types | Public Member Functions | Protected Attributes
torch::nn::ModuleHolder< Contained > Class Template Reference

A ModuleHolder is essentially a wrapper around std::shared_ptr<M> where M is an nn::Module subclass, with convenient constructors defined for the kind of constructions we want to allow for our modules. More...

#include <pimpl.h>

Inheritance diagram for torch::nn::ModuleHolder< Contained >:
torch::detail::ModuleHolderIndicator

Public Types

using ContainedType = Contained
 

Public Member Functions

 ModuleHolder ()
 Default constructs the contained module if if has a default constructor, else produces a static error. More...
 
 ModuleHolder (std::nullptr_t)
 Constructs the ModuleHolder with an empty contained value. More...
 
template<typename Head , typename... Tail, typename = typename std::enable_if< !(torch::detail::is_module_holder_of<Head, ContainedType>::value && (sizeof...(Tail) == 0))>::type>
 ModuleHolder (Head &&head, Tail &&...tail)
 Constructs the ModuleHolder with a contained module, forwarding all arguments to its constructor. More...
 
 ModuleHolder (std::shared_ptr< Contained > module)
 Constructs the ModuleHolder from a pointer to the contained type. More...
 
 operator bool () const noexcept
 Returns true if the ModuleHolder contains a module, or false if it is nullptr. More...
 
Contained * operator-> ()
 Forwards to the contained module.
 
const Contained * operator-> () const
 Forwards to the contained module.
 
Contained & operator* ()
 Returns a reference to the contained module.
 
const Contained & operator* () const
 Returns a const reference to the contained module.
 
const std::shared_ptr< Contained > & ptr () const
 Returns a shared pointer to the underlying module.
 
Contained * get ()
 Returns a pointer to the underlying module.
 
const Contained * get () const
 Returns a const pointer to the underlying module.
 
template<typename... Args>
auto operator() (Args &&...args) -> torch::detail::return_type_of_forward_t< Contained, Args... >
 Calls the forward() method of the contained module.
 
template<typename Arg >
auto operator[] (Arg &&arg) -> decltype((*impl_)[::std::forward< Arg >(arg)])
 Forwards to the subscript operator of the contained module. More...
 
bool is_empty () const noexcept
 Returns true if the ModuleHolder does not contain a module.
 

Protected Attributes

std::shared_ptr< Contained > impl_
 The module pointer this class wraps. More...
 

Detailed Description

template<typename Contained>
class torch::nn::ModuleHolder< Contained >

A ModuleHolder is essentially a wrapper around std::shared_ptr<M> where M is an nn::Module subclass, with convenient constructors defined for the kind of constructions we want to allow for our modules.

Definition at line 26 of file pimpl.h.

Constructor & Destructor Documentation

template<typename Contained>
torch::nn::ModuleHolder< Contained >::ModuleHolder ( )
inline

Default constructs the contained module if if has a default constructor, else produces a static error.

NOTE: This uses the behavior of template classes in C++ that constructors (or any methods) are only compiled when actually used.

Definition at line 42 of file pimpl.h.

template<typename Contained>
torch::nn::ModuleHolder< Contained >::ModuleHolder ( std::nullptr_t  )
inline

Constructs the ModuleHolder with an empty contained value.

Access to the underlying module is not permitted and will throw an exception, until a value is assigned.

Definition at line 53 of file pimpl.h.

template<typename Contained>
template<typename Head , typename... Tail, typename = typename std::enable_if< !(torch::detail::is_module_holder_of<Head, ContainedType>::value && (sizeof...(Tail) == 0))>::type>
torch::nn::ModuleHolder< Contained >::ModuleHolder ( Head &&  head,
Tail &&...  tail 
)
inlineexplicit

Constructs the ModuleHolder with a contained module, forwarding all arguments to its constructor.

Definition at line 63 of file pimpl.h.

template<typename Contained>
torch::nn::ModuleHolder< Contained >::ModuleHolder ( std::shared_ptr< Contained >  module)
inline

Constructs the ModuleHolder from a pointer to the contained type.

Example: Linear(std::make_shared<LinearImpl>(...)).

Definition at line 70 of file pimpl.h.

Member Function Documentation

template<typename Contained>
torch::nn::ModuleHolder< Contained >::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the ModuleHolder contains a module, or false if it is nullptr.

Definition at line 75 of file pimpl.h.

template<typename Contained>
template<typename Arg >
auto torch::nn::ModuleHolder< Contained >::operator[] ( Arg &&  arg) -> decltype((*impl_)[::std::forward<Arg>(arg)])
inline

Forwards to the subscript operator of the contained module.

NOTE: std::forward is qualified to prevent VS2017 emitting error C2872: 'std': ambiguous symbol

Definition at line 132 of file pimpl.h.

Field Documentation

template<typename Contained>
std::shared_ptr<Contained> torch::nn::ModuleHolder< Contained >::impl_
protected

The module pointer this class wraps.

NOTE: Must be placed at the top of the class so that we can use it with trailing return types below.

Definition at line 31 of file pimpl.h.


The documentation for this class was generated from the following file: