|
1 |
| -#ifndef SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_ |
2 |
| -#define SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_ |
| 1 | +#ifndef SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_ |
| 2 | +#define SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_ |
3 | 3 |
|
4 | 4 | #include "../types.hpp"
|
5 | 5 |
|
|
12 | 12 |
|
13 | 13 | namespace cuda {
|
14 | 14 |
|
15 |
| -namespace rtc { |
| 15 | +namespace marshalling { |
16 | 16 |
|
17 | 17 | namespace detail_ {
|
18 | 18 |
|
@@ -117,10 +117,82 @@ MarshalTarget& operator<<(MarshalTarget& mt, detail_::opt_start_t<Delimiter>& op
|
117 | 117 | }
|
118 | 118 | return mt;
|
119 | 119 | }
|
| 120 | + |
| 121 | + |
| 122 | +// A partial specialization gadget |
| 123 | +template<typename CompilationOptions, typename MarshalTarget, typename Delimiter> |
| 124 | +struct gadget { |
| 125 | + /** |
| 126 | + * Uses the streaming/left-shift operator (<<) to render a delimited sequence of |
| 127 | + * command-line-argument-like options (with or without a value as relevant) |
| 128 | + * into some target entity - which could be a buffer of chars or a more complex |
| 129 | + * structure like @ref marshalled_options_t. |
| 130 | + */ |
| 131 | + static void process( |
| 132 | + const CompilationOptions &opts, MarshalTarget &marshalled, Delimiter delimiter, |
| 133 | + bool need_delimiter_after_last_option); |
| 134 | +}; |
| 135 | + |
| 136 | + |
| 137 | +template <typename CompilationOptions, typename MarshalTarget, typename Delimiter> |
| 138 | +void process( |
| 139 | + const CompilationOptions& opts, MarshalTarget& marshalled, Delimiter delimiter, |
| 140 | + bool need_delimiter_after_last_option = false) |
| 141 | +{ |
| 142 | + return detail_::gadget<CompilationOptions, MarshalTarget, Delimiter>::process( |
| 143 | + opts, marshalled, delimiter, need_delimiter_after_last_option); |
| 144 | +} |
| 145 | + |
120 | 146 | } // namespace detail_
|
121 | 147 |
|
122 |
| -} // namespace rtc |
| 148 | +/** |
| 149 | + * Finalize a compilation options "building" object into a structure passable to some of the |
| 150 | + * CUDA JIT compilation APIs |
| 151 | + * |
| 152 | + * @tparam Kind The kind of JITable program options to render |
| 153 | + * |
| 154 | + * @return A structure of multiple strings, passable to various CUDA APIs, but no longer |
| 155 | + * easy to modify and manipulate. |
| 156 | + */ |
| 157 | +template <typename CompilationOptions> |
| 158 | +inline detail_::marshalled_options_t marshal(const CompilationOptions& opts) |
| 159 | +{ |
| 160 | + using detail_::marshalled_options_t; |
| 161 | + marshalled_options_t marshalled; |
| 162 | + // TODO: Can we easily determine the max number of options here? |
| 163 | + enum : bool { need_delimiter_after_last_option = true }; |
| 164 | + marshalling::detail_::process(opts, marshalled, marshalled_options_t::advance_gadget{}, |
| 165 | + need_delimiter_after_last_option); |
| 166 | + return marshalled; |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Finalize a set of options into the form of a string appendable to a command-line |
| 171 | + * |
| 172 | + * @tparam Kind The kind of options to render |
| 173 | + * |
| 174 | + * @return a string made up of command-line options - switches and options with arguments, |
| 175 | + * designated by single or double dashes. |
| 176 | + * |
| 177 | + * @note An implementation of a processor/renderer of individual options must be |
| 178 | + * provided via detail_::process() for this function to be usable with any particular |
| 179 | + * type of options. |
| 180 | + * |
| 181 | + */ |
| 182 | +template <typename CompilationOptions> |
| 183 | +inline ::std::string render(const CompilationOptions& opts) |
| 184 | +{ |
| 185 | + ::std::ostringstream oss; |
| 186 | + detail_::process(opts, oss, ' '); |
| 187 | + if (oss.tellp() > 0) { |
| 188 | + // Remove the last, excessive, delimiter |
| 189 | + oss.seekp(-1,oss.cur); |
| 190 | + } |
| 191 | + return oss.str(); |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace marshalling |
123 | 195 |
|
124 | 196 | } // namespace cuda
|
125 | 197 |
|
126 |
| -#endif /* SRC_CUDA_API_DETAIL_MARSHALLED_OPTIONS_HPP_ */ |
| 198 | +#endif /* SRC_CUDA_API_DETAIL_OPTION_MARSHALLING_HPP_ */ |
0 commit comments