-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcrtp_backend.h
93 lines (86 loc) · 2.98 KB
/
crtp_backend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright Codeplay Software Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PORTDNN_INCLUDE_BACKEND_CRTP_BACKEND_H_
#define PORTDNN_INCLUDE_BACKEND_CRTP_BACKEND_H_
/**
* \file
* Contains the implementation of \ref sycldnn::backend::CRTPBackend, a helper
* class for the curiously recurring template pattern as used in the various
* providers which make up the portDNN backends.
*
* See for \ref sycldnn::backend::EigenMatmulProvider for an example of its use.
*/
namespace sycldnn {
namespace backend {
/**
* A helper class for curiously recurring templated backend providers. Provides
* an underlying_backend method to access the derived type backend in the CRTP
* base class.
*/
template <typename Backend, template <typename> class Provider>
struct CRTPBackend {
protected:
/**
* Get a const reference to the underlying derived backend type used in the
* CRTP base class.
*
* The templated type deduction requires that this method is called through
* `this` in the base class:
* \code
* auto backend = this->underlying_backend();
* \endcode
* the method is not visible, and will cause a compile error, if called as:
* \code
* auto backend = underlying_backend();
* \endcode
*
* \return A const reference to the underlying backend
*/
Backend const& underlying_backend() const {
return static_cast<Backend const&>(*this);
}
/**
* Get a reference to the underlying derived backend type used in the CRTP
* base class.
*
* The templated type deduction requires that this method is called through
* `this` in the base class:
* \code
* auto backend = this->underlying_backend();
* \endcode
* the method is not visible, and will cause a compile error, if called as:
* \code
* auto backend = underlying_backend();
* \endcode
*
* \return A reference to the underlying backend
*/
Backend& underlying_backend() { return static_cast<Backend&>(*this); }
private:
// Making the default constructor private, and the Provider<Backend> a friend
// ensures that this class is used in the correct way for CRTP:
//
// struct Provider : CRTPBackend<Backend, Provider> {...}
//
// and will cause a compile time error if the inheritance instead looks like:
//
// struct Provider : CRTPBackend<Backend, OtherClass> {...}
CRTPBackend() = default;
friend Provider<Backend>;
};
} // namespace backend
} // namespace sycldnn
#endif // PORTDNN_INCLUDE_BACKEND_CRTP_BACKEND_H_