Add .clang-format file and format source files accordingly.

To apply .clang-format to the codebase, the following
command can be run:

git ls-files -- *.cpp *.h | xargs clang-format -i -style=file
This commit is contained in:
Kern Handa 2015-09-30 18:50:07 +00:00
parent 38a659c428
commit 8c3142ad61
14 changed files with 4380 additions and 3913 deletions

47
.clang-format Normal file
View File

@ -0,0 +1,47 @@
AccessModifierOffset: 0
AlignEscapedNewlinesLeft: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 100
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 0
Cpp11BracedListStyle: false
DerivePointerBinding: false
IndentCaseLabels: false
IndentFunctionDeclarationAfterType: false
IndentWidth: 4
Language: Cpp
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 100
PenaltyBreakComment: 100
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 100
PenaltyExcessCharacter: 1
PenaltyReturnTypeOnItsOwnLine: 20
PointerBindsToType: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
Standard: Cpp11
TabWidth: 4
UseTab: Never

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
@ -27,26 +27,41 @@ namespace gsl
//
// Having "fail fast" result in an exception makes unit testing
// the GSL classes that rely upon it much simpler.
// the GSL classes that rely upon it much simpler.
//
#if defined(GSL_THROWS_FOR_TESTING)
struct fail_fast : public std::runtime_error
struct fail_fast : public std::runtime_error
{
fail_fast() : std::runtime_error("") {}
explicit fail_fast(char const* const message) : std::runtime_error(message) {}
fail_fast() : std::runtime_error("")
{
}
explicit fail_fast(char const* const message) : std::runtime_error(message)
{
}
};
inline void fail_fast_assert(bool cond) { if (!cond) throw fail_fast(); }
inline void fail_fast_assert(bool cond, const char* const message) { if (!cond) throw fail_fast(message); }
inline void fail_fast_assert(bool cond)
{
if (!cond) throw fail_fast();
}
inline void fail_fast_assert(bool cond, const char* const message)
{
if (!cond) throw fail_fast(message);
}
#else
inline void fail_fast_assert(bool cond) { if (!cond) std::terminate(); }
inline void fail_fast_assert(bool cond, const char* const) { if (!cond) std::terminate(); }
inline void fail_fast_assert(bool cond)
{
if (!cond) std::terminate();
}
inline void fail_fast_assert(bool cond, const char* const)
{
if (!cond) std::terminate();
}
#endif // GSL_THROWS_FOR_TESTING
}
#endif // GSL_FAIL_FAST_H

View File

@ -1,17 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
@ -19,141 +19,194 @@
#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 "array_view.h" // array_view, strided_array_view...
#include "string_view.h" // zstring, string_view, zstring_builder...
#include <memory>
namespace gsl
{
//
// GSL.owner: ownership pointers
// GSL.owner: ownership pointers
//
using std::unique_ptr;
using std::shared_ptr;
template <class T>
using owner = T;
template <class T> using owner = T;
//
// GSL.assert: assertions
//
#define Expects(x) gsl::fail_fast_assert((x))
#define Ensures(x) gsl::fail_fast_assert((x))
#define Expects(x) gsl::fail_fast_assert((x))
#define Ensures(x) gsl::fail_fast_assert((x))
//
// GSL.util: utilities
//
// Final_act allows you to ensure something gets run at the end of a scope
template <class F>
class Final_act
template <class F> class Final_act
{
public:
explicit Final_act(F f) : f_(std::move(f)), invoke_(true) {}
public:
explicit Final_act(F f) : f_(std::move(f)), invoke_(true)
{
}
Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true) { other.invoke_ = false; }
Final_act(Final_act&& other) : f_(std::move(other.f_)), invoke_(true)
{
other.invoke_ = false;
}
Final_act(const Final_act&) = delete;
Final_act& operator=(const Final_act&) = delete;
~Final_act() { if (invoke_) f_(); }
~Final_act()
{
if (invoke_) f_();
}
private:
private:
F f_;
bool invoke_;
};
// finally() - convenience function to generate a Final_act
template <class F>
Final_act<F> finally(const F &f) { return Final_act<F>(f); }
template <class 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)); }
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>
T narrow_cast(U u) { return static_cast<T>(u); }
template <class T, class U> T narrow_cast(U u)
{
return static_cast<T>(u);
}
struct narrowing_error : public std::exception {};
struct narrowing_error : public std::exception
{
};
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
template<class T, class U>
T narrow(U u) { T t = narrow_cast<T>(u); if (static_cast<U>(t) != u) throw narrowing_error(); return t; }
template <class T, class U> T narrow(U u)
{
T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) throw narrowing_error();
return t;
}
//
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector
//
template <class T, size_t N>
T& at(T(&arr)[N], size_t index) { fail_fast_assert(index < N); return arr[index]; }
template <class T, size_t N> T& at(T(&arr)[N], size_t index)
{
fail_fast_assert(index < N);
return arr[index];
}
template <class T, size_t N>
T& at(std::array<T, N>& arr, size_t index) { fail_fast_assert(index < N); return arr[index]; }
template <class T, size_t N> T& at(std::array<T, N>& arr, size_t index)
{
fail_fast_assert(index < N);
return arr[index];
}
template <class Cont>
typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index < cont.size()); return cont[index]; }
template <class Cont> typename Cont::value_type& at(Cont& cont, size_t index)
{
fail_fast_assert(index < cont.size());
return cont[index];
}
//
// not_null
//
// Restricts a pointer or smart pointer to only hold non-null values.
//
//
// Has zero size overhead over T.
//
// If T is a pointer (i.e. T == U*) then
// - allow construction from U* or U&
// If T is a pointer (i.e. T == U*) then
// - allow construction from U* or U&
// - disallow construction from nullptr_t
// - disallow default construction
// - ensure construction from U* fails with nullptr
// - allow implicit conversion to U*
//
template<class T>
class not_null
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(); }
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;
public:
not_null(T t) : ptr_(t)
{
ensure_invariant();
}
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)
not_null(const not_null<U>& other)
{
*this = other;
}
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
not_null& operator=(const not_null<U> &other)
not_null& operator=(const not_null<U>& other)
{
ptr_ = other.get();
return *this;
}
// prevents compilation when someone attempts to assign a nullptr
// 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;
T get() const {
not_null<T>& operator=(int) = delete;
T get() const
{
#ifdef _MSC_VER
__assume(ptr_ != nullptr);
#endif
return ptr_;
} // the assume() should help the optimizer
operator T() const { return get(); }
T operator->() const { return get(); }
operator T() const
{
return get();
}
T operator->() const
{
return get();
}
bool operator==(const T& rhs) const { return ptr_ == rhs; }
bool operator!=(const T& rhs) const { return !(*this == rhs); }
private:
bool operator==(const T& rhs) const
{
return ptr_ == rhs;
}
bool operator!=(const T& rhs) const
{
return !(*this == rhs);
}
private:
T ptr_;
// we assume that the compiler can hoist/prove away most of the checks inlined from this function
// we assume that the compiler can hoist/prove away most of the checks inlined from this
// function
// if not, we could make them optional via conditional compilation
void ensure_invariant() const { fail_fast_assert(ptr_ != nullptr); }
void ensure_invariant() const
{
fail_fast_assert(ptr_ != nullptr);
}
// unwanted operators...pointers only point to single objects!
// TODO ensure all arithmetic ops on this type are unavailable
@ -168,26 +221,30 @@ private:
};
//
//
// maybe_null
//
// Describes an optional pointer - provides symmetry with not_null
//
template<class T>
class maybe_null_ret;
template <class T> class maybe_null_ret;
template<class T>
class maybe_null_dbg
template <class T> class maybe_null_dbg
{
template<class U>
friend class maybe_null_dbg;
template <class U> friend class maybe_null_dbg;
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) {}
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& operator=(const T& p)
{
if (ptr_ != p)
@ -199,7 +256,9 @@ public:
}
maybe_null_dbg(const maybe_null_dbg& rhs) : ptr_(rhs.ptr_), tested_(false) {}
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)
@ -212,10 +271,13 @@ public:
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) {}
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)
maybe_null_dbg& operator=(const not_null<U>& other)
{
ptr_ = other.get();
tested_ = false;
@ -224,10 +286,13 @@ public:
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.ptr_), tested_(false) {}
maybe_null_dbg(const maybe_null_dbg<U>& other)
: ptr_(other.ptr_), 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)
maybe_null_dbg& operator=(const maybe_null_dbg<U>& other)
{
ptr_ = other.ptr_;
tested_ = false;
@ -236,10 +301,13 @@ public:
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) {}
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)
maybe_null_dbg& operator=(const maybe_null_ret<U>& other)
{
ptr_ = other.get();
tested_ = false;
@ -247,27 +315,53 @@ public:
}
bool present() const { tested_ = true; return ptr_ != nullptr; }
bool present() const
{
tested_ = true;
return ptr_ != nullptr;
}
bool operator==(const T& rhs) const { tested_ = true; return ptr_ == rhs; }
bool operator!=(const T& rhs) const { return !(*this == rhs); }
bool operator==(const T& rhs) const
{
tested_ = true;
return ptr_ == rhs;
}
bool operator!=(const T& rhs) const
{
return !(*this == rhs);
}
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
bool operator==(const maybe_null_dbg<U>& rhs) const { tested_ = true; rhs.tested_ = true; return ptr_ == rhs.ptr_; }
bool operator==(const maybe_null_dbg<U>& rhs) const
{
tested_ = true;
rhs.tested_ = true;
return ptr_ == rhs.ptr_;
}
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
bool operator!=(const maybe_null_dbg<U>& rhs) const { return !(*this == rhs); }
bool operator!=(const maybe_null_dbg<U>& rhs) const
{
return !(*this == rhs);
}
T get() const {
T get() const
{
fail_fast_assert(tested_);
#ifdef _MSC_VER
__assume(ptr_ != nullptr);
#endif
return ptr_;
return ptr_;
}
operator T() const { return get(); }
T operator->() const { return get(); }
operator T() const
{
return get();
}
T operator->() const
{
return get();
}
private:
private:
// unwanted operators...pointers only point to single objects!
// TODO ensure all arithmetic ops on this type are unavailable
maybe_null_dbg<T>& operator++() = delete;
@ -283,25 +377,38 @@ private:
mutable bool tested_;
};
template<class T>
class maybe_null_ret
template <class T> class maybe_null_ret
{
static_assert(std::is_assignable<T&, std::nullptr_t>::value, "T cannot be assigned nullptr.");
public:
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; }
public:
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;
maybe_null_ret& operator=(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()) {}
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)
maybe_null_ret& operator=(const not_null<U>& other)
{
ptr_ = other.get();
return *this;
@ -309,10 +416,13 @@ public:
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()) {}
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)
maybe_null_ret& operator=(const maybe_null_ret<U>& other)
{
ptr_ = other.get();
return *this;
@ -320,24 +430,39 @@ public:
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(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)
maybe_null_ret& operator=(const maybe_null_dbg<U>& other)
{
ptr_ = other.get();
return *this;
}
bool present() const { return ptr_ != nullptr; }
bool present() const
{
return ptr_ != nullptr;
}
T get() const { return ptr_; }
T get() const
{
return ptr_;
}
operator T() const { return get(); }
T operator->() const { return get(); }
operator T() const
{
return get();
}
T operator->() const
{
return get();
}
private:
private:
// unwanted operators...pointers only point to single objects!
// TODO ensure all arithmetic ops on this type are unavailable
maybe_null_ret<T>& operator++() = delete;
@ -352,7 +477,7 @@ private:
T ptr_;
};
template<class T> using maybe_null = maybe_null_ret<T>;
template <class T> using maybe_null = maybe_null_ret<T>;
} // namespace gsl

View File

@ -1,17 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
@ -27,24 +27,20 @@ namespace gsl
//
// czstring and wzstring
//
// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays)
// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays)
// that allow static analysis to help find bugs.
//
// There are no additional features/semantics that we can find a way to add inside the
// type system for these types that will not either incur significant runtime costs or
// (sometimes needlessly) break existing programs when introduced.
//
template<size_t Max = dynamic_range>
using czstring = const char*;
template <size_t Max = dynamic_range> using czstring = const char*;
template<size_t Max = dynamic_range>
using cwzstring = const wchar_t*;
template <size_t Max = dynamic_range> using cwzstring = const wchar_t*;
template<size_t Max = dynamic_range>
using zstring = char*;
template <size_t Max = dynamic_range> using zstring = char*;
template<size_t Max = dynamic_range>
using wzstring = wchar_t*;
template <size_t Max = dynamic_range> using wzstring = wchar_t*;
//
// string_view and relatives
@ -57,34 +53,33 @@ using wzstring = wchar_t*;
template <class CharT, size_t Extent = dynamic_range>
using basic_string_view = array_view<CharT, Extent>;
template<size_t Extent = dynamic_range>
using string_view = basic_string_view<char, Extent>;
template <size_t Extent = dynamic_range> using string_view = basic_string_view<char, Extent>;
template<size_t Extent = dynamic_range>
using cstring_view = basic_string_view<const char, Extent>;
template <size_t Extent = dynamic_range> using cstring_view = basic_string_view<const char, Extent>;
template<size_t Extent = dynamic_range>
using wstring_view = basic_string_view<wchar_t, Extent>;
template <size_t Extent = dynamic_range> using wstring_view = basic_string_view<wchar_t, Extent>;
template<size_t Extent = dynamic_range>
template <size_t Extent = dynamic_range>
using cwstring_view = basic_string_view<const wchar_t, Extent>;
//
// ensure_sentinel()
// ensure_sentinel()
//
// Provides a way to obtain an array_view from a contiguous sequence
// that ends with a (non-inclusive) sentinel value.
//
// Will fail-fast if sentinel cannot be found before max elements are examined.
//
template<class T, class SizeType, const T Sentinel>
array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max())
template <class T, class SizeType, const T Sentinel>
array_view<T, dynamic_range>
ensure_sentinel(const T* seq, SizeType max = std::numeric_limits<SizeType>::max())
{
auto cur = seq;
while ((cur - seq) < max && *cur != Sentinel) ++cur;
while ((cur - seq) < max && *cur != Sentinel)
++cur;
fail_fast_assert(*cur == Sentinel);
return{ seq, cur - seq };
return { seq, cur - seq };
}
@ -93,42 +88,51 @@ array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::n
// Will fail fast if a null-terminator cannot be found before
// the limit of size_type.
//
template<class T>
inline basic_string_view<T, dynamic_range> ensure_z(T* const & sz, size_t max = std::numeric_limits<size_t>::max())
template <class T>
inline basic_string_view<T, dynamic_range>
ensure_z(T* const& sz, size_t max = std::numeric_limits<size_t>::max())
{
return ensure_sentinel<0>(sz, max);
}
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation
inline basic_string_view<char, dynamic_range> ensure_z(char* const & sz, size_t max)
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const
// overloads to share an implementation
inline basic_string_view<char, dynamic_range> ensure_z(char* const& sz, size_t max)
{
auto len = strnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0);
return { sz, len };
}
inline basic_string_view<const char, dynamic_range> ensure_z(const char* const& sz, size_t max)
{
auto len = strnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0);
return { sz, len };
}
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const & sz, size_t max)
inline basic_string_view<wchar_t, dynamic_range> ensure_z(wchar_t* const& sz, size_t max)
{
auto len = wcsnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0);
return { sz, len };
}
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const & sz, size_t max)
inline basic_string_view<const wchar_t, dynamic_range> ensure_z(const wchar_t* const& sz, size_t max)
{
auto len = wcsnlen(sz, max);
fail_fast_assert(sz[len] == 0); return{ sz, len };
fail_fast_assert(sz[len] == 0);
return { sz, len };
}
template<class T, size_t N>
basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], N); }
template <class T, size_t N> basic_string_view<T, dynamic_range> ensure_z(T(&sz)[N])
{
return ensure_z(&sz[0], N);
}
template<class Cont>
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range> ensure_z(Cont& cont)
template <class Cont>
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range>
ensure_z(Cont& cont)
{
return ensure_z(cont.data(), cont.length());
}
@ -136,48 +140,69 @@ basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dy
//
// to_string() allow (explicit) conversions from string_view to string
//
template<class CharT, size_t Extent>
template <class CharT, size_t Extent>
std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_string_view<CharT, Extent> view)
{
return{ view.data(), view.length() };
return { view.data(), view.length() };
}
template<class CharT, size_t Extent = dynamic_range>
class basic_zstring_builder
template <class CharT, size_t Extent = dynamic_range> class basic_zstring_builder
{
public:
public:
using string_view_type = basic_string_view<CharT, Extent>;
using value_type = CharT;
using pointer = CharT*;
using size_type = typename string_view_type::size_type;
using iterator = typename string_view_type::iterator;
basic_zstring_builder(CharT* data, size_type length) : sv_(data, length) {}
basic_zstring_builder(CharT* data, size_type length) : sv_(data, length)
{
}
template<size_t Size>
basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr) {}
template <size_t Size> basic_zstring_builder(CharT(&arr)[Size]) : sv_(arr)
{
}
pointer data() const { return sv_.data(); }
string_view_type view() const { return sv_; }
pointer data() const
{
return sv_.data();
}
string_view_type view() const
{
return sv_;
}
size_type length() const { return sv_.length(); }
size_type length() const
{
return sv_.length();
}
pointer assume0() const { return data(); }
string_view_type ensure_z() const { return gsl::ensure_z(sv_); }
pointer assume0() const
{
return data();
}
string_view_type ensure_z() const
{
return gsl::ensure_z(sv_);
}
iterator begin() const { return sv_.begin(); }
iterator end() const { return sv_.end(); }
iterator begin() const
{
return sv_.begin();
}
iterator end() const
{
return sv_.end();
}
private:
private:
string_view_type sv_;
};
template <size_t Max = dynamic_range>
using zstring_builder = basic_zstring_builder<char, Max>;
template <size_t Max = dynamic_range> using zstring_builder = basic_zstring_builder<char, Max>;
template <size_t Max = dynamic_range>
using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
template <size_t Max = dynamic_range> using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
}
#endif // GSL_STRING_VIEW_H

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,20 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
using namespace gsl;
@ -34,7 +34,7 @@ SUITE(assertion_tests)
}
int g(int i)
{
{
i++;
Ensures(i > 0 && i < 10);
return i;
@ -47,7 +47,7 @@ SUITE(assertion_tests)
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,20 +1,20 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <vector>
@ -28,17 +28,17 @@ SUITE(at_tests)
int a[] = { 1, 2, 3, 4 };
for (int i = 0; i < 4; ++i)
CHECK(at(a, i) == i+1);
CHECK(at(a, i) == i + 1);
CHECK_THROW(at(a, 4), fail_fast);
}
TEST(std_array)
{
std::array<int,4> a = { 1, 2, 3, 4 };
std::array<int, 4> a = { 1, 2, 3, 4 };
for (int i = 0; i < 4; ++i)
CHECK(at(a, i) == i+1);
CHECK(at(a, i) == i + 1);
CHECK_THROW(at(a, 4), fail_fast);
}
@ -48,13 +48,13 @@ SUITE(at_tests)
std::vector<int> a = { 1, 2, 3, 4 };
for (int i = 0; i < 4; ++i)
CHECK(at(a, i) == i+1);
CHECK(at(a, i) == i + 1);
CHECK_THROW(at(a, 4), fail_fast);
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,99 +1,103 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <array_view.h>
#include <vector>
using namespace std;
using namespace gsl;;
using namespace gsl;
;
namespace
namespace
{
void use(unsigned int&) {}
void use(unsigned int&)
{
}
}
SUITE(bounds_test)
{
TEST(basic_bounds)
{
for (auto point : static_bounds <unsigned int, dynamic_range, 3, 4 > { 2 })
{
for (unsigned int j = 0; j < decltype(point)::rank; j++)
{
use(j);
use(point[j]);
}
}
}
TEST(bounds_basic)
{
static_bounds<size_t, 3, 4, 5> b;
auto a = b.slice();
static_bounds<size_t, 4, dynamic_range, 2> x{ 4 };
x.slice().slice();
}
TEST (arrayview_iterator)
{
static_bounds<size_t, 4, dynamic_range, 2> bounds{ 3 };
auto itr = bounds.begin();
TEST(basic_bounds)
{
for (auto point : static_bounds<unsigned int, dynamic_range, 3, 4>{ 2 })
{
for (unsigned int j = 0; j < decltype(point)::rank; j++)
{
use(j);
use(point[j]);
}
}
}
TEST(bounds_basic)
{
static_bounds<size_t, 3, 4, 5> b;
auto a = b.slice();
static_bounds<size_t, 4, dynamic_range, 2> x{ 4 };
x.slice().slice();
}
TEST(arrayview_iterator)
{
static_bounds<size_t, 4, dynamic_range, 2> bounds{ 3 };
auto itr = bounds.begin();
#ifdef CONFIRM_COMPILATION_ERRORS
array_view< int, 4, dynamic_range, 2> av(nullptr, bounds);
auto itr2 = av.cbegin();
for (auto & v : av) {
v = 4;
}
fill(av.begin(), av.end(), 0);
#endif
}
TEST (bounds_convertible)
{
static_bounds<size_t, 7, 4, 2> b1;
static_bounds<size_t, 7, dynamic_range, 2> b2 = b1;
#ifdef CONFIRM_COMPILATION_ERRORS
static_bounds<size_t, 7, dynamic_range, 1> b4 = b2;
array_view<int, 4, dynamic_range, 2> av(nullptr, bounds);
auto itr2 = av.cbegin();
for (auto& v : av)
{
v = 4;
}
fill(av.begin(), av.end(), 0);
#endif
static_bounds<size_t, dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<int, 7, 4, 2> b4 = b3;
}
static_bounds<size_t, dynamic_range> b11;
static_bounds<size_t, dynamic_range> b5;
static_bounds<size_t, 34> b6;
b5 = static_bounds<size_t, 20>();
CHECK_THROW(b6 = b5, fail_fast);
b5 = static_bounds<size_t, 34>();
b6 = b5;
TEST(bounds_convertible)
{
static_bounds<size_t, 7, 4, 2> b1;
static_bounds<size_t, 7, dynamic_range, 2> b2 = b1;
CHECK(b5 == b6);
CHECK(b5.size() == b6.size());
}
#ifdef CONFIRM_COMPILATION_ERRORS
static_bounds<size_t, 7, dynamic_range, 1> b4 = b2;
#endif
static_bounds<size_t, dynamic_range, dynamic_range, dynamic_range> b3 = b1;
static_bounds<int, 7, 4, 2> b4 = b3;
static_bounds<size_t, dynamic_range> b11;
static_bounds<size_t, dynamic_range> b5;
static_bounds<size_t, 34> b6;
b5 = static_bounds<size_t, 20>();
CHECK_THROW(b6 = b5, fail_fast);
b5 = static_bounds<size_t, 34>();
b6 = b5;
CHECK(b5 == b6);
CHECK(b5.size() == b6.size());
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
return UnitTest::RunAllTests();
}

View File

@ -1,29 +1,39 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <vector>
#include <iostream>
using namespace gsl;
struct MyBase { bool foo() { return true; } };
struct MyDerived : public MyBase {};
struct Unrelated {};
struct MyBase
{
bool foo()
{
return true;
}
};
struct MyDerived : public MyBase
{
};
struct Unrelated
{
};
SUITE(MaybeNullTests)
{
@ -31,46 +41,45 @@ SUITE(MaybeNullTests)
{
#ifdef CONFIRM_COMPILATION_ERRORS
// Forbid non-nullptr assignable types
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{1});
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{1});
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{ 1 });
maybe_null_ret<std::vector<int>> f_ret(std::vector<int>{ 1 });
maybe_null_ret<int> z_ret(10);
maybe_null_dbg<std::vector<int>> y_dbg({1,2});
maybe_null_dbg<std::vector<int>> y_dbg({ 1, 2 });
maybe_null_dbg<int> z_dbg(10);
maybe_null_dbg<std::vector<int>> y_dbg({1,2});
maybe_null_dbg<std::vector<int>> y_dbg({ 1, 2 });
#endif
int n = 5;
maybe_null_dbg<int *> opt_n(&n);
maybe_null_dbg<int*> opt_n(&n);
int result = 0;
bool threw = false;
CHECK_THROW(result = *opt_n, fail_fast);
maybe_null_ret<std::shared_ptr<int>> x_ret(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
maybe_null_dbg<std::shared_ptr<int>> x_dbg(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
maybe_null_ret<std::shared_ptr<int>> x_ret(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
maybe_null_dbg<std::shared_ptr<int>> x_dbg(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
}
TEST(TestMaybeNull2)
{
int n = 5;
maybe_null<int *> opt_n(&n);
maybe_null<int*> opt_n(&n);
int result = 0;
if (opt_n.present())
result = *opt_n;
if (opt_n.present()) result = *opt_n;
}
TEST(TestMaybeNull3)
{
int n = 5;
maybe_null<int *> opt_n(&n);
maybe_null<int*> opt_n(&n);
int result = 0;
if (opt_n != nullptr)
result = *opt_n;
if (opt_n != nullptr) result = *opt_n;
}
int test4_helper(maybe_null<int *> p)
int test4_helper(maybe_null<int*> p)
{
if (p != nullptr)
return *p;
if (p != nullptr) return *p;
return -1;
}
@ -81,7 +90,7 @@ SUITE(MaybeNullTests)
result = test4_helper(&n);
}
int test5_helper(maybe_null_dbg<int *> p)
int test5_helper(maybe_null_dbg<int*> p)
{
return *p;
}
@ -104,68 +113,64 @@ SUITE(MaybeNullTests)
#endif
int g_int;
void test7_helper(maybe_null<maybe_null<int *> *> outptr)
void test7_helper(maybe_null<maybe_null<int*>*> outptr)
{
g_int = 5;
if (outptr.present())
*outptr = &g_int;
if (outptr.present()) *outptr = &g_int;
}
void test7b_helper(maybe_null_dbg<maybe_null_dbg<int *> *> outptr)
void test7b_helper(maybe_null_dbg<maybe_null_dbg<int*>*> outptr)
{
g_int = 5;
if (outptr.present())
*outptr = &g_int;
if (outptr.present()) *outptr = &g_int;
}
TEST(TestMaybeNull7a)
{
maybe_null<int *> outval;
maybe_null<int*> outval;
test7_helper(&outval);
CHECK(outval.present() && *outval == 5);
}
TEST(TestMaybeNull7b)
{
maybe_null_dbg<int *> outval;
maybe_null_dbg<int*> outval;
test7b_helper(&outval);
CHECK_THROW((void)*outval, fail_fast);
CHECK_THROW((void)*outval, fail_fast);
}
int test8_helper1(maybe_null_dbg<int *> opt)
int test8_helper1(maybe_null_dbg<int*> opt)
{
return *opt;
}
int test8_helper2a(maybe_null_dbg<int *> opt)
int test8_helper2a(maybe_null_dbg<int*> opt)
{
if (!opt.present())
return 0;
if (!opt.present()) return 0;
return test8_helper1(opt);
}
TEST(TestMaybeNull8a)
{
int n = 5;
maybe_null_dbg<int *> opt(&n);
CHECK_THROW(test8_helper2a(opt), fail_fast);
maybe_null_dbg<int*> opt(&n);
CHECK_THROW(test8_helper2a(opt), fail_fast);
}
#ifdef CONVERT_TO_PTR_TO_CONST
int test9_helper(maybe_null<const int *> copt)
int test9_helper(maybe_null<const int*> copt)
{
if (copt.present())
return *copt;
if (copt.present()) return *copt;
return 0;
}
void TestMaybeNull9()
{
int n = 5;
maybe_null<int *> opt(&n);
CHECK_THROW(test9_helper(opt), fail_fast);
maybe_null<int*> opt(&n);
CHECK_THROW(test9_helper(opt), fail_fast);
}
#endif
@ -298,7 +303,7 @@ SUITE(MaybeNullTests)
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,35 +1,45 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <vector>
using namespace gsl;
struct MyBase {};
struct MyDerived : public MyBase {};
struct Unrelated {};
struct MyBase
{
};
struct MyDerived : public MyBase
{
};
struct Unrelated
{
};
// stand-in for a user-defined ref-counted class
template<typename T>
struct RefCounted
template <typename T> struct RefCounted
{
RefCounted(T* p) : p_(p) {}
operator T*() { return p_; }
RefCounted(T* p) : p_(p)
{
}
operator T*()
{
return p_;
}
T* p_;
};
@ -51,32 +61,33 @@ SUITE(NotNullTests)
not_null<int*> p = up;
// Forbid non-nullptr assignable types
not_null<std::vector<int>> f(std::vector<int>{1});
not_null<std::vector<int>> f(std::vector<int>{ 1 });
not_null<int> z(10);
not_null<std::vector<int>> y({1,2});
not_null<std::vector<int>> y({ 1, 2 });
#endif
int i = 12;
auto rp = RefCounted<int>(&i);
not_null<int*> p(rp);
CHECK(p.get() == &i);
int i = 12;
auto rp = RefCounted<int>(&i);
not_null<int*> p(rp);
CHECK(p.get() == &i);
not_null<std::shared_ptr<int>> x(std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
}
TEST(TestNotNullCasting)
{
MyBase base;
MyDerived derived;
Unrelated unrelated;
not_null<Unrelated*> u = &unrelated;
MyDerived derived;
Unrelated unrelated;
not_null<Unrelated*> u = &unrelated;
not_null<MyDerived*> p = &derived;
not_null<MyBase*> q = &base;
q = p; // allowed with heterogeneous copy ctor
q = p; // allowed with heterogeneous copy ctor
CHECK(q == p);
#ifdef CONFIRM_COMPILATION_ERRORS
q = u; // no viable conversion possible between MyBase* and Unrelated*
p = q; // not possible to implicitly convert MyBase* to MyDerived*
q = u; // no viable conversion possible between MyBase* and Unrelated*
p = q; // not possible to implicitly convert MyBase* to MyDerived*
not_null<Unrelated*> r = p;
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
@ -88,7 +99,7 @@ SUITE(NotNullTests)
TEST(TestNotNullAssignment)
{
int i = 12;
not_null<int*> p = &i;
not_null<int*> p = &i;
CHECK(helper(p));
int* q = nullptr;
@ -96,7 +107,7 @@ SUITE(NotNullTests)
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,20 +1,20 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <functional>
@ -37,7 +37,7 @@ SUITE(owner_tests)
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,20 +1,20 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <string_view.h>
#include <vector>
#include <cstdlib>
@ -26,7 +26,7 @@ SUITE(string_view_tests)
{
TEST(TestLiteralConstruction)
{
{
cwstring_view<> v = ensure_z(L"Hello");
CHECK(5 == v.length());
@ -34,7 +34,7 @@ SUITE(string_view_tests)
#ifdef CONFIRM_COMPILATION_ERRORS
wstring_view<> v2 = ensure0(L"Hello");
#endif
}
}
TEST(TestConstructFromStdString)
{
@ -50,8 +50,8 @@ SUITE(string_view_tests)
CHECK(v.length() == vec.size());
}
TEST(TestStackArrayConstruction)
{
TEST(TestStackArrayConstruction)
{
wchar_t stack_string[] = L"Hello";
{
@ -77,13 +77,13 @@ SUITE(string_view_tests)
CHECK(v.length() == 6);
CHECK(v.used_length() == v.length());
}
}
}
TEST(TestConversionToConst)
{
char stack_string[] = "Hello";
string_view<> v = ensure_z(stack_string);
cstring_view<> v2 = v;
cstring_view<> v2 = v;
CHECK(v.length() == v2.length());
}
@ -98,7 +98,7 @@ SUITE(string_view_tests)
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}

View File

@ -1,20 +1,20 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <UnitTest++/UnitTest++.h>
#include <UnitTest++/UnitTest++.h>
#include <gsl.h>
#include <functional>
@ -31,7 +31,10 @@ SUITE(utils_tests)
{
int i = 0;
{
auto _ = finally([&]() {f(i);});
auto _ = finally([&]()
{
f(i);
});
CHECK(i == 0);
}
CHECK(i == 1);
@ -41,12 +44,15 @@ SUITE(utils_tests)
{
int i = 0;
{
auto _1 = finally([&]() {f(i);});
auto _1 = finally([&]()
{
f(i);
});
{
auto _2 = std::move(_1);
CHECK(i == 0);
CHECK(i == 0);
}
CHECK(i == 1);
CHECK(i == 1);
}
CHECK(i == 1);
}
@ -62,7 +68,10 @@ SUITE(utils_tests)
}
int j = 0;
void g() { j += 1; };
void g()
{
j += 1;
};
TEST(finally_function_ptr)
{
j = 0;
@ -90,12 +99,12 @@ SUITE(utils_tests)
char c = narrow<char>(n);
CHECK(c == 120);
n = 300;
n = 300;
CHECK_THROW(narrow<char>(n), narrowing_error);
}
}
int main(int, const char *[])
int main(int, const char* [])
{
return UnitTest::RunAllTests();
}