xref: /llvm-project/llvm/include/llvm/XRay/FDRLogBuilder.h (revision aa5c09bead260a6008ed7e92a1ee7b1023703b40)
1a6c6343aSDean Michael Berris //===- FDRLogBuilder.h - XRay FDR Log Building Utility --------------------===//
2a6c6343aSDean Michael Berris //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a6c6343aSDean Michael Berris //
7a6c6343aSDean Michael Berris //===----------------------------------------------------------------------===//
8*aa5c09beSKazu Hirata #ifndef LLVM_XRAY_FDRLOGBUILDER_H
9*aa5c09beSKazu Hirata #define LLVM_XRAY_FDRLOGBUILDER_H
10a6c6343aSDean Michael Berris 
11a6c6343aSDean Michael Berris #include "llvm/XRay/FDRRecords.h"
12a6c6343aSDean Michael Berris 
13a6c6343aSDean Michael Berris namespace llvm {
14a6c6343aSDean Michael Berris namespace xray {
15a6c6343aSDean Michael Berris 
16a6c6343aSDean Michael Berris /// The LogBuilder class allows for creating ad-hoc collections of records
17a6c6343aSDean Michael Berris /// through the `add<...>(...)` function. An example use of this API is in
18a6c6343aSDean Michael Berris /// crafting arbitrary sequences of records:
19a6c6343aSDean Michael Berris ///
20a6c6343aSDean Michael Berris ///   auto Records = LogBuilder()
21a6c6343aSDean Michael Berris ///       .add<BufferExtents>(256)
22a6c6343aSDean Michael Berris ///       .add<NewBufferRecord>(1)
23a6c6343aSDean Michael Berris ///       .consume();
24a6c6343aSDean Michael Berris ///
25a6c6343aSDean Michael Berris class LogBuilder {
26a6c6343aSDean Michael Berris   std::vector<std::unique_ptr<Record>> Records;
27a6c6343aSDean Michael Berris 
28a6c6343aSDean Michael Berris public:
add(T &&...A)29a6c6343aSDean Michael Berris   template <class R, class... T> LogBuilder &add(T &&... A) {
30a6c6343aSDean Michael Berris     Records.emplace_back(new R(std::forward<T>(A)...));
31a6c6343aSDean Michael Berris     return *this;
32a6c6343aSDean Michael Berris   }
33a6c6343aSDean Michael Berris 
consume()34a6c6343aSDean Michael Berris   std::vector<std::unique_ptr<Record>> consume() { return std::move(Records); }
35a6c6343aSDean Michael Berris };
36a6c6343aSDean Michael Berris 
37a6c6343aSDean Michael Berris } // namespace xray
38a6c6343aSDean Michael Berris } // namespace llvm
39a6c6343aSDean Michael Berris 
40*aa5c09beSKazu Hirata #endif // LLVM_XRAY_FDRLOGBUILDER_H
41