Provide a "None" value for convenience when using Optional<T>()
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 21 Feb 2013 00:27:28 +0000 (00:27 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 21 Feb 2013 00:27:28 +0000 (00:27 +0000)
This implementation of NoneType/None does have some holes but I haven't
found one that doesn't - open to improvement.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175696 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/None.h [new file with mode: 0644]
include/llvm/ADT/Optional.h
lib/Support/LockFileManager.cpp

diff --git a/include/llvm/ADT/None.h b/include/llvm/ADT/None.h
new file mode 100644 (file)
index 0000000..83c9521
--- /dev/null
@@ -0,0 +1,27 @@
+//===-- None.h - Simple null value for implicit construction ------*- C++ -*-=//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file provides None, an enumerant for use in implicit constructors
+//  of various (usually templated) types to make such construction more
+//  terse.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_NONE_H
+#define LLVM_ADT_NONE_H
+
+namespace llvm {
+/// \brief A simple null object to allow implicit construction of Optional<T>
+/// and similar types without having to spell out the specialization's name.
+enum NoneType {
+  None
+};
+}
+
+#endif
index 6c91a13a5b5fb3fda8daca7578a1b58d1db95a85..b0d09f69603e2839dfd07f28d19ce0d6f5f0a417 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef LLVM_ADT_OPTIONAL_H
 #define LLVM_ADT_OPTIONAL_H
 
+#include "llvm/ADT/None.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/AlignOf.h"
 #include <cassert>
@@ -31,6 +32,7 @@ class Optional {
   AlignedCharArrayUnion<T> storage;
   bool hasVal;
 public:
+  Optional(NoneType) : hasVal(false) {}
   explicit Optional() : hasVal(false) {}
   Optional(const T &y) : hasVal(true) {
     new (storage.buffer) T(y);
index 31eec751b7b613b4e8072615976dca558077afae..92d8b83cf94ea38c5cebe3f806d1fa69e5af9819 100644 (file)
@@ -31,7 +31,7 @@ LockFileManager::readLockFile(StringRef LockFileName) {
   // to read, so we just return.
   bool Exists = false;
   if (sys::fs::exists(LockFileName, Exists) || !Exists)
-    return Optional<std::pair<std::string, int> >();
+    return None;
 
   // Read the owning host and PID out of the lock file. If it appears that the
   // owning process is dead, the lock file is invalid.
@@ -45,7 +45,7 @@ LockFileManager::readLockFile(StringRef LockFileName) {
   // Delete the lock file. It's invalid anyway.
   bool Existed;
   sys::fs::remove(LockFileName, Existed);
-  return Optional<std::pair<std::string, int> >();
+  return None;
 }
 
 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {