[PATCH] D99560: Utility to construct visitors from lambdas.

Bakhtiyar Neyman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 30 15:25:20 PDT 2021


bakhtiyarneyman updated this revision to Diff 334284.
bakhtiyarneyman added a comment.

Added a unit test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99560/new/

https://reviews.llvm.org/D99560

Files:
  llvm/include/llvm/ADT/Visitor.h
  llvm/unittests/ADT/VisitorTest.cpp


Index: llvm/unittests/ADT/VisitorTest.cpp
===================================================================
--- /dev/null
+++ llvm/unittests/ADT/VisitorTest.cpp
@@ -0,0 +1,26 @@
+//===- VisitorTest.cpp - Unit tests for a sequence abstraciton -----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/Visitor.h"
+#include "gtest/gtest.h"
+
+#include <list>
+
+using namespace llvm;
+
+namespace {
+
+TEST(VisitorTest, Basic) {
+  auto visitor = llvm::makeVisitor([](int a) { return "int"; },
+                                   [](std::string a) { return "str"; });
+
+  EXPECT_EQ("int", visitor(42));
+  EXPECT_EQ("str", visitor("foo"));
+}
+
+} // anonymous namespace
Index: llvm/include/llvm/ADT/Visitor.h
===================================================================
--- /dev/null
+++ llvm/include/llvm/ADT/Visitor.h
@@ -0,0 +1,42 @@
+//===- llvm/ADT/Visitor.h - Visitors constructed from lambdas. --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the Visitor class, which is a utility that constructs
+// visitors from lambdas. Can be used for e.g. visiting PointerUnion.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_VISITOR_H
+#define LLVM_ADT_VISITOR_H
+
+namespace llvm {
+
+template <class... Fs> struct Visitor;
+
+template <class Head, class... Tail>
+struct Visitor<Head, Tail...> : Head, Visitor<Tail...> {
+  explicit Visitor(Head head, Tail... tail)
+      : Head(head), Visitor<Tail...>(tail...) {}
+
+  using Head::operator();
+  using Visitor<Tail...>::operator();
+};
+
+template <class Head> struct Visitor<Head> : Head {
+  explicit Visitor(Head head) : Head(head) {}
+
+  using Head::operator();
+};
+
+template <class... Lambdas> auto makeVisitor(Lambdas... lambdas) {
+  return Visitor<Lambdas...>(lambdas...);
+}
+
+} // namespace llvm
+
+#endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99560.334284.patch
Type: text/x-patch
Size: 2402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210330/a81e6d3e/attachment.bin>


More information about the llvm-commits mailing list