mirror of
https://github.com/microsoft/GSL.git
synced 2025-11-17 15:59:22 +01:00
Merge branch 'master' into fixmaybenull
Conflicts: include/gsl.h tests/maybenull_tests.cpp
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef GSL_FAIL_FAST_H
|
||||
#define GSL_FAIL_FAST_H
|
||||
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -45,3 +48,5 @@ inline void fail_fast_assert(bool cond, const char* const) { if (!cond) std::ter
|
||||
#endif // SAFER_CPP_TESTING
|
||||
|
||||
}
|
||||
|
||||
#endif // GSL_FAIL_FAST_H
|
||||
|
||||
147
include/gsl.h
147
include/gsl.h
@@ -16,6 +16,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef GSL_GSL_H
|
||||
#define GSL_GSL_H
|
||||
|
||||
#include "array_view.h" // array_view, strided_array_view...
|
||||
#include "string_view.h" // zstring, string_view, zstring_builder...
|
||||
#include <memory>
|
||||
@@ -47,7 +50,7 @@ template <class F>
|
||||
class Final_act
|
||||
{
|
||||
public:
|
||||
explicit Final_act(F f) : f_(f) {}
|
||||
explicit Final_act(F f) : f_(std::move(f)) {}
|
||||
|
||||
Final_act(const Final_act&& other) : f_(other.f_) {}
|
||||
Final_act(const Final_act&) = delete;
|
||||
@@ -61,7 +64,10 @@ private:
|
||||
|
||||
// finally() - convenience function to generate a Final_act
|
||||
template <class F>
|
||||
Final_act<F> finally(F f) { return Final_act<F>(f); }
|
||||
Final_act<F> finally(const F &f) { return Final_act<F>(f); }
|
||||
|
||||
template <class F>
|
||||
Final_act<F> finally(F &&f) { return Final_act<F>(std::forward<F>(f)); }
|
||||
|
||||
// narrow_cast(): a searchable way to do narrowing casts of values
|
||||
template<class T, class U>
|
||||
@@ -102,23 +108,30 @@ typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index
|
||||
template<class T>
|
||||
class not_null
|
||||
{
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
not_null(T t) : ptr_(t) { ensure_invariant(); }
|
||||
|
||||
// deleting these two prevents compilation when initialized with a nullptr or literal 0
|
||||
not_null(std::nullptr_t) = delete;
|
||||
not_null(int) = delete;
|
||||
not_null& operator=(const T& t) { ptr_ = t; ensure_invariant(); return *this; }
|
||||
|
||||
not_null(const not_null &other) = default;
|
||||
not_null& operator=(const not_null &other) = default;
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
not_null(const not_null<U> &other) : ptr_(other.get())
|
||||
not_null(const not_null<U> &other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
not_null<T>& operator=(const T& t) { ptr_ = t; ensure_invariant(); return *this; }
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
not_null& operator=(const not_null<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// prevents compilation when someone attempts to assign a nullptr
|
||||
not_null(std::nullptr_t) = delete;
|
||||
not_null(int) = delete;
|
||||
not_null<T>& operator=(std::nullptr_t) = delete;
|
||||
not_null<T>& operator=(int) = delete;
|
||||
|
||||
@@ -159,29 +172,21 @@ private:
|
||||
//
|
||||
// Describes an optional pointer - provides symmetry with not_null
|
||||
//
|
||||
template<class T>
|
||||
class maybe_null_ret;
|
||||
|
||||
template<class T>
|
||||
class maybe_null_dbg
|
||||
{
|
||||
template<class U>
|
||||
friend class maybe_null_dbg;
|
||||
public:
|
||||
static_assert(std::is_constructible<T, std::nullptr_t>::value, "maybe_null's template parameter must be constructible from nullptr");
|
||||
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
maybe_null_dbg() : ptr_(nullptr), tested_(false) {}
|
||||
maybe_null_dbg(std::nullptr_t) : ptr_(nullptr), tested_(false) {}
|
||||
|
||||
maybe_null_dbg(const T& p) : ptr_(p), tested_(false) {}
|
||||
maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const not_null<U> &other) : ptr_(other.get()), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const maybe_null_dbg<U> &other) : ptr_(other.get()), tested_(false)
|
||||
{
|
||||
}
|
||||
|
||||
maybe_null_dbg& operator=(const T& p)
|
||||
{
|
||||
if (ptr_ != p)
|
||||
@@ -192,6 +197,8 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) {}
|
||||
maybe_null_dbg& operator=(const maybe_null_dbg& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
@@ -202,6 +209,43 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const not_null<U> &other) : ptr_(other.get()), tested_(false) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const not_null<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
tested_ = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const maybe_null_dbg<U> &other) : ptr_(other.get()), tested_(false) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const maybe_null_dbg<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
tested_ = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg(const maybe_null_ret<U> &other) : ptr_(other.get()), tested_(false) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_dbg& operator=(const maybe_null_ret<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
tested_ = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool present() const { tested_ = true; return ptr_ != nullptr; }
|
||||
|
||||
bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; }
|
||||
@@ -241,34 +285,49 @@ private:
|
||||
template<class T>
|
||||
class maybe_null_ret
|
||||
{
|
||||
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
|
||||
public:
|
||||
static_assert(std::is_constructible<T, std::nullptr_t>::value, "maybe_null's template parameter must be constructible from nullptr");
|
||||
|
||||
maybe_null_ret() : ptr_(nullptr) {}
|
||||
maybe_null_ret(std::nullptr_t) : ptr_(nullptr) {}
|
||||
|
||||
maybe_null_ret(const T& p) : ptr_(p) {}
|
||||
maybe_null_ret& operator=(const T& p) { ptr_ = p; return *this; }
|
||||
|
||||
maybe_null_ret(const maybe_null_ret& rhs) = default;
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const not_null<U> &other) : ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_ret<U> &other) : ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_dbg<U> &other) : ptr_(other.get())
|
||||
{
|
||||
}
|
||||
|
||||
maybe_null_ret& operator=(const T& p) { if (ptr_ != p) { ptr_ = p; } return *this; }
|
||||
maybe_null_ret& operator=(const maybe_null_ret& rhs) = default;
|
||||
|
||||
bool operator==(const T& rhs) const { return ptr_ == rhs; }
|
||||
bool operator!=(const T& rhs) const { return ptr_ != rhs; }
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const not_null<U> &other) : ptr_(other.get()) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const not_null<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_ret<U> &other) : ptr_(other.get()) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const maybe_null_ret<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret(const maybe_null_dbg<U> &other) : ptr_(other.get()) {}
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
maybe_null_ret& operator=(const maybe_null_dbg<U> &other)
|
||||
{
|
||||
ptr_ = other.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
bool present() const { return ptr_ != nullptr; }
|
||||
|
||||
@@ -295,3 +354,5 @@ private:
|
||||
template<class T> using maybe_null = maybe_null_ret<T>;
|
||||
|
||||
} // namespace Guide
|
||||
|
||||
#endif // GSL_GSL_H
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef GSL_STRING_VIEW_H
|
||||
#define GSL_STRING_VIEW_H
|
||||
|
||||
#include "array_view.h"
|
||||
#include <cstring>
|
||||
|
||||
@@ -176,3 +179,5 @@ using zstring_builder = basic_zstring_builder<char, Max>;
|
||||
template <size_t Max = dynamic_range>
|
||||
using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
|
||||
}
|
||||
|
||||
#endif // GSL_STRING_VIEW_H
|
||||
|
||||
Reference in New Issue
Block a user