mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
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:
parent
38a659c428
commit
8c3142ad61
47
.clang-format
Normal file
47
.clang-format
Normal 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
@ -33,20 +33,35 @@ namespace gsl
|
||||
|
||||
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
|
||||
|
263
include/gsl.h
263
include/gsl.h
@ -32,8 +32,7 @@ namespace gsl
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
|
||||
template <class T>
|
||||
using owner = T;
|
||||
template <class T> using owner = T;
|
||||
|
||||
//
|
||||
// GSL.assert: assertions
|
||||
@ -46,17 +45,24 @@ using owner = T;
|
||||
//
|
||||
|
||||
// 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) {}
|
||||
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:
|
||||
F f_;
|
||||
@ -64,32 +70,53 @@ private:
|
||||
};
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@ -106,13 +133,21 @@ typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index
|
||||
// - 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(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;
|
||||
@ -136,24 +171,42 @@ public:
|
||||
not_null<T>& operator=(std::nullptr_t) = delete;
|
||||
not_null<T>& operator=(int) = delete;
|
||||
|
||||
T get() const {
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
@ -173,21 +226,25 @@ private:
|
||||
//
|
||||
// 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,7 +271,10 @@ 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)
|
||||
@ -224,7 +286,10 @@ 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)
|
||||
@ -236,7 +301,10 @@ 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)
|
||||
@ -247,16 +315,36 @@ 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);
|
||||
@ -264,8 +352,14 @@ public:
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
operator T() const { return get(); }
|
||||
T operator->() const { return get(); }
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
T operator->() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
private:
|
||||
// unwanted operators...pointers only point to single objects!
|
||||
@ -283,22 +377,35 @@ 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)
|
||||
@ -309,7 +416,10 @@ 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)
|
||||
@ -320,7 +430,10 @@ 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)
|
||||
@ -330,12 +443,24 @@ public:
|
||||
}
|
||||
|
||||
|
||||
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:
|
||||
// unwanted operators...pointers only point to single objects!
|
||||
|
@ -34,17 +34,13 @@ namespace gsl
|
||||
// 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,14 +53,11 @@ 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>
|
||||
using cwstring_view = basic_string_view<const wchar_t, Extent>;
|
||||
@ -79,10 +72,12 @@ using cwstring_view = basic_string_view<const wchar_t, Extent>;
|
||||
// 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())
|
||||
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 };
|
||||
}
|
||||
@ -94,41 +89,50 @@ array_view<T, dynamic_range> ensure_sentinel(const T* seq, SizeType max = std::n
|
||||
// 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())
|
||||
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
|
||||
// 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
basic_string_view<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range>
|
||||
ensure_z(Cont& cont)
|
||||
{
|
||||
return ensure_z(cont.data(), cont.length());
|
||||
}
|
||||
@ -143,8 +147,7 @@ std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_strin
|
||||
}
|
||||
|
||||
|
||||
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:
|
||||
using string_view_type = basic_string_view<CharT, Extent>;
|
||||
@ -153,31 +156,53 @@ public:
|
||||
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:
|
||||
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
|
||||
|
@ -31,9 +31,15 @@ using namespace gsl;
|
||||
|
||||
namespace
|
||||
{
|
||||
void use(int&) {}
|
||||
struct BaseClass {};
|
||||
struct DerivedClass : BaseClass {};
|
||||
void use(int&)
|
||||
{
|
||||
}
|
||||
struct BaseClass
|
||||
{
|
||||
};
|
||||
struct DerivedClass : BaseClass
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
SUITE(array_view_tests)
|
||||
@ -66,7 +72,10 @@ SUITE(array_view_tests)
|
||||
array_view<int, 7, 4, 2> av1(nullptr, b1);
|
||||
#endif
|
||||
|
||||
auto f = [&]() { array_view<int, 7, 4, 2> av1(nullptr); };
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 7, 4, 2> av1(nullptr);
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
|
||||
array_view<int, 7, dynamic_range, 2> av1(nullptr);
|
||||
@ -106,21 +115,24 @@ SUITE(array_view_tests)
|
||||
CHECK_THROW((av[{ 10, 2 }]), fail_fast);
|
||||
}
|
||||
|
||||
void overloaded_func(array_view<const int, dynamic_range, 3, 5> exp, int expected_value) {
|
||||
void overloaded_func(array_view<const int, dynamic_range, 3, 5> exp, int expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
}
|
||||
}
|
||||
|
||||
void overloaded_func(array_view<const char, dynamic_range, 3, 5> exp, char expected_value) {
|
||||
void overloaded_func(array_view<const char, dynamic_range, 3, 5> exp, char expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
}
|
||||
}
|
||||
|
||||
void fixed_func(array_view<int, 3, 3, 5> exp, int expected_value) {
|
||||
void fixed_func(array_view<int, 3, 3, 5> exp, int expected_value)
|
||||
{
|
||||
for (auto val : exp)
|
||||
{
|
||||
CHECK(val == expected_value);
|
||||
@ -138,7 +150,10 @@ SUITE(array_view_tests)
|
||||
fill(av.begin(), av.end(), 34);
|
||||
|
||||
int count = 0;
|
||||
for_each(av.rbegin(), av.rend(), [&](int val) { count += val; });
|
||||
for_each(av.rbegin(), av.rend(), [&](int val)
|
||||
{
|
||||
count += val;
|
||||
});
|
||||
CHECK(count == 34 * 60);
|
||||
overloaded_func(av, 34);
|
||||
|
||||
@ -157,7 +172,8 @@ SUITE(array_view_tests)
|
||||
auto image_ptr = new int[imgSize][3];
|
||||
|
||||
// size check will be done
|
||||
auto image_view = as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>());
|
||||
auto image_view =
|
||||
as_array_view(image_ptr, imgSize).as_array_view(dim<>(height), dim<>(width), dim<3>());
|
||||
|
||||
iota(image_view.begin(), image_view.end(), 1);
|
||||
|
||||
@ -250,7 +266,10 @@ SUITE(array_view_tests)
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
struct Foo {char c[11];};
|
||||
struct Foo
|
||||
{
|
||||
char c[11];
|
||||
};
|
||||
auto av9 = av7.as_array_view<Foo>();
|
||||
#endif
|
||||
}
|
||||
@ -323,7 +342,8 @@ SUITE(array_view_tests)
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const int, 1> sav_c{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const int, 1> sav_c{ array_view<const int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const int, 1> sav_c{ array_view<const int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_c.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_c.bounds().strides() == index<1>{ 1 });
|
||||
@ -332,7 +352,8 @@ SUITE(array_view_tests)
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<volatile int, 1> sav_v{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<volatile int, 1> sav_v{ array_view<volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<volatile int, 1> sav_v{ array_view<volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_v.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_v.bounds().strides() == index<1>{ 1 });
|
||||
@ -341,7 +362,8 @@ SUITE(array_view_tests)
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
@ -360,7 +382,8 @@ SUITE(array_view_tests)
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
@ -380,7 +403,8 @@ SUITE(array_view_tests)
|
||||
#if _MSC_VER > 1800
|
||||
strided_array_view<const volatile int, 1> sav_cv{ { src }, { 2, 1 } };
|
||||
#else
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{src}, strided_bounds<1>{2, 1} };
|
||||
strided_array_view<const volatile int, 1> sav_cv{ array_view<const volatile int>{ src },
|
||||
strided_bounds<1>{ 2, 1 } };
|
||||
#endif
|
||||
CHECK(sav_cv.bounds().index_bounds() == index<1>{ 2 });
|
||||
CHECK(sav_cv.bounds().strides() == index<1>{ 1 });
|
||||
@ -406,7 +430,8 @@ SUITE(array_view_tests)
|
||||
array_view<const int, 2> av2{ av };
|
||||
CHECK(av2[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const array_view<int, 2>, array_view<const int, 2>>::value, "ctor is not implicit!");
|
||||
static_assert(std::is_convertible<const array_view<int, 2>, array_view<const int, 2>>::value,
|
||||
"ctor is not implicit!");
|
||||
|
||||
const strided_array_view<int, 1> src{ arr, { 2, 1 } };
|
||||
strided_array_view<const int, 1> sav{ src };
|
||||
@ -414,7 +439,8 @@ SUITE(array_view_tests)
|
||||
CHECK(sav.bounds().stride() == 1);
|
||||
CHECK(sav[1] == 5);
|
||||
|
||||
static_assert(std::is_convertible<const strided_array_view<int, 1>, strided_array_view<const int, 1>>::value, "ctor is not implicit!");
|
||||
static_assert(std::is_convertible<const strided_array_view<int, 1>, strided_array_view<const int, 1>>::value,
|
||||
"ctor is not implicit!");
|
||||
}
|
||||
|
||||
// Check copy constructor
|
||||
@ -483,7 +509,8 @@ SUITE(array_view_tests)
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const strided_array_view<const int, 2> csav{ { src }, { { 5, 10 }, { 10, 1 } } };
|
||||
#endif
|
||||
const strided_array_view<const int, 2> csav{ array_view<const int, 5, 10>{ src }, { { 5, 10 },{ 10, 1 } } };
|
||||
const strided_array_view<const int, 2> csav{ array_view<const int, 5, 10>{ src },
|
||||
{ { 5, 10 }, { 10, 1 } } };
|
||||
|
||||
strided_array_view<int, 1> sav_sl = sav[2];
|
||||
CHECK(sav_sl[0] == 20);
|
||||
@ -503,11 +530,7 @@ SUITE(array_view_tests)
|
||||
// use cases, such as column-major multidimensional array
|
||||
// (aka. "FORTRAN" layout).
|
||||
|
||||
int cm_array[3 * 5] = {
|
||||
1, 4, 7, 10, 13,
|
||||
2, 5, 8, 11, 14,
|
||||
3, 6, 9, 12, 15
|
||||
};
|
||||
int cm_array[3 * 5] = { 1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 3, 6, 9, 12, 15 };
|
||||
strided_array_view<int, 2> cm_sav{ cm_array, { { 5, 3 }, { 1, 5 } } };
|
||||
|
||||
// Accessing elements
|
||||
@ -634,7 +657,8 @@ SUITE(array_view_tests)
|
||||
strided_array_view<int, 1> sav4{ av, { 1, 1, 1 } };
|
||||
strided_array_view<int, 2> sav5{ av.as_array_view(dim<2>(), dim<2>()), { 1 } };
|
||||
strided_array_view<int, 2> sav6{ av.as_array_view(dim<2>(), dim<2>()), { 1, 1, 1 } };
|
||||
strided_array_view<int, 2> sav7{ av.as_array_view(dim<2>(), dim<2>()), { { 1,1 },{ 1,1 },{ 1,1 } } };
|
||||
strided_array_view<int, 2> sav7{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1, 1 }, { 1, 1 }, { 1, 1 } } };
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -649,13 +673,17 @@ SUITE(array_view_tests)
|
||||
#ifdef _MSC_VER
|
||||
CHECK_THROW((strided_array_view<int, 1>{ av, { { 1, 1 }, { 1, 1 } } }), fail_fast);
|
||||
#endif
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1}, {1,1,1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), { { 1 }, { 1 } } }),
|
||||
fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1 }, { 1, 1, 1 } } }),
|
||||
fail_fast);
|
||||
#ifdef _MSC_VER
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()), {{1,1,1}, {1}} }), fail_fast);
|
||||
CHECK_THROW((strided_array_view<int, 2>{ av.as_array_view(dim<2>(), dim<2>()),
|
||||
{ { 1, 1, 1 }, { 1 } } }),
|
||||
fail_fast);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(strided_array_view_type_conversion)
|
||||
@ -692,7 +720,8 @@ SUITE(array_view_tests)
|
||||
// retype strided array with regular strides - from array_view
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, bytes.size() / 4 }, { bytes.size() / 2, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
strided_array_view<int, 2> sav3 = sav2.as_strided_array_view<int>();
|
||||
CHECK(sav3[0][0] == 0);
|
||||
@ -704,7 +733,8 @@ SUITE(array_view_tests)
|
||||
// retype strided array with not enough elements - last dimension of the array is too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4, 2 }, { 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
@ -712,23 +742,28 @@ SUITE(array_view_tests)
|
||||
// retype strided array with not enough elements - strides are too small
|
||||
{
|
||||
strided_bounds<2> bounds{ { 4, 2 }, { 2, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new typesize
|
||||
// retype strided array with not enough elements - last dimension does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, 6 }, { 4, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
|
||||
// retype strided array with not enough elements - strides does not divide by the new typesize
|
||||
// retype strided array with not enough elements - strides does not divide by the new
|
||||
// typesize
|
||||
{
|
||||
strided_bounds<2> bounds{ { 2, 1 }, { 6, 1 } };
|
||||
array_view<const byte, 2, dynamic_range> bytes2 = bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
array_view<const byte, 2, dynamic_range> bytes2 =
|
||||
bytes.as_array_view(dim<2>(), dim<>(bytes.size() / 2));
|
||||
strided_array_view<const byte, 2> sav2{ bytes2, bounds };
|
||||
CHECK_THROW(sav2.as_strided_array_view<int>(), fail_fast);
|
||||
}
|
||||
@ -881,7 +916,6 @@ SUITE(array_view_tests)
|
||||
CHECK(k[0] == 1);
|
||||
CHECK(k[1] == 2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void iterate_second_column(array_view<int, dynamic_range, dynamic_range> av)
|
||||
@ -1122,7 +1156,12 @@ SUITE(array_view_tests)
|
||||
{
|
||||
// get an array_view of 'c' values from the list of X's
|
||||
|
||||
struct X { int a; int b; int c; };
|
||||
struct X
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
};
|
||||
|
||||
X arr[4] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } };
|
||||
|
||||
@ -1137,7 +1176,9 @@ SUITE(array_view_tests)
|
||||
CHECK(av.bounds().index_bounds()[1] == 12);
|
||||
|
||||
// get the last 4 columns
|
||||
auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2], arr[0].c[3] } , { arr[1].c[0], ... } , ... }
|
||||
auto section = av.section({ 0, 2 * s }, { 4, s }); // { { arr[0].c[0], arr[0].c[1],
|
||||
// arr[0].c[2], arr[0].c[3] } , {
|
||||
// arr[1].c[0], ... } , ... }
|
||||
|
||||
// convert to array 4x1 array of integers
|
||||
auto cs = section.as_strided_array_view<int>(); // { { arr[0].c }, {arr[1].c } , ... }
|
||||
@ -1146,10 +1187,8 @@ SUITE(array_view_tests)
|
||||
CHECK(cs.bounds().index_bounds()[1] == 1);
|
||||
|
||||
// transpose to 1x4 array
|
||||
strided_bounds<2> reverse_bounds{
|
||||
{ cs.bounds().index_bounds()[1] , cs.bounds().index_bounds()[0] },
|
||||
{ cs.bounds().strides()[1], cs.bounds().strides()[0] }
|
||||
};
|
||||
strided_bounds<2> reverse_bounds{ { cs.bounds().index_bounds()[1], cs.bounds().index_bounds()[0] },
|
||||
{ cs.bounds().strides()[1], cs.bounds().strides()[0] } };
|
||||
|
||||
strided_array_view<int, 2> transposed{ cs.data(), cs.bounds().total_size(), reverse_bounds };
|
||||
|
||||
@ -1165,7 +1204,6 @@ SUITE(array_view_tests)
|
||||
CHECK(num == arr[i].c);
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(constructors)
|
||||
@ -1180,7 +1218,10 @@ SUITE(array_view_tests)
|
||||
CHECK(av3.length() == 0);
|
||||
|
||||
// Constructing from a nullptr + length is specifically disallowed
|
||||
auto f = [&]() {array_view<int, dynamic_range> av4(nullptr, 2);};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, dynamic_range> av4(nullptr, 2);
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
|
||||
int arr1[2][3];
|
||||
@ -1317,7 +1358,6 @@ SUITE(array_view_tests)
|
||||
CHECK_THROW(av1[10][3][4], fail_fast);
|
||||
|
||||
array_view<const double, dynamic_range, 6, 4> av2 = av1.as_array_view(dim<>(5), dim<6>(), dim<4>());
|
||||
|
||||
}
|
||||
|
||||
TEST(array_view_sub)
|
||||
@ -1403,8 +1443,7 @@ SUITE(array_view_tests)
|
||||
CHECK(!av);
|
||||
}
|
||||
|
||||
template <class T, class U>
|
||||
void AssertContentsMatch(T a1, U a2)
|
||||
template <class T, class U> void AssertContentsMatch(T a1, U a2)
|
||||
{
|
||||
CHECK(a1.length() == a2.length());
|
||||
for (size_t i = 0; i < a1.length(); ++i)
|
||||
@ -1488,7 +1527,10 @@ SUITE(array_view_tests)
|
||||
|
||||
{
|
||||
array_view<int, dynamic_range> av = arr;
|
||||
auto f = [&]() {array_view<int, 2, 1> av2 = av.as_array_view(dim<>(2), dim<>(2));};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 2, 1> av2 = av.as_array_view(dim<>(2), dim<>(2));
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
@ -1521,13 +1563,19 @@ SUITE(array_view_tests)
|
||||
}
|
||||
#endif
|
||||
{
|
||||
auto f = [&]() {array_view<int, 4> av4 = {arr2, 2};};
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 4> av4 = { arr2, 2 };
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
// this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one
|
||||
array_view<int, dynamic_range> av = arr2;
|
||||
auto f = [&](){ array_view<int, 4> av2 = av; };
|
||||
auto f = [&]()
|
||||
{
|
||||
array_view<int, 4> av2 = av;
|
||||
};
|
||||
CHECK_THROW(f(), fail_fast);
|
||||
}
|
||||
|
||||
@ -1557,7 +1605,6 @@ SUITE(array_view_tests)
|
||||
CHECK(wav.data() == (byte*)&a[0]);
|
||||
CHECK(wav.length() == sizeof(a));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(ArrayViewComparison)
|
||||
|
@ -19,11 +19,14 @@
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;;
|
||||
using namespace gsl;
|
||||
;
|
||||
|
||||
namespace
|
||||
{
|
||||
void use(unsigned int&) {}
|
||||
void use(unsigned int&)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
SUITE(bounds_test)
|
||||
@ -59,7 +62,8 @@ SUITE(bounds_test)
|
||||
|
||||
auto itr2 = av.cbegin();
|
||||
|
||||
for (auto & v : av) {
|
||||
for (auto& v : av)
|
||||
{
|
||||
v = 4;
|
||||
}
|
||||
fill(av.begin(), av.end(), 0);
|
||||
|
@ -21,9 +21,19 @@
|
||||
|
||||
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)
|
||||
{
|
||||
@ -45,8 +55,10 @@ SUITE(MaybeNullTests)
|
||||
|
||||
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)
|
||||
@ -54,8 +66,7 @@ SUITE(MaybeNullTests)
|
||||
int n = 5;
|
||||
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)
|
||||
@ -63,14 +74,12 @@ SUITE(MaybeNullTests)
|
||||
int n = 5;
|
||||
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)
|
||||
{
|
||||
if (p != nullptr)
|
||||
return *p;
|
||||
if (p != nullptr) return *p;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -108,16 +117,14 @@ SUITE(MaybeNullTests)
|
||||
{
|
||||
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)
|
||||
{
|
||||
g_int = 5;
|
||||
|
||||
if (outptr.present())
|
||||
*outptr = &g_int;
|
||||
if (outptr.present()) *outptr = &g_int;
|
||||
}
|
||||
|
||||
TEST(TestMaybeNull7a)
|
||||
@ -141,8 +148,7 @@ SUITE(MaybeNullTests)
|
||||
|
||||
int test8_helper2a(maybe_null_dbg<int*> opt)
|
||||
{
|
||||
if (!opt.present())
|
||||
return 0;
|
||||
if (!opt.present()) return 0;
|
||||
return test8_helper1(opt);
|
||||
}
|
||||
|
||||
@ -156,8 +162,7 @@ SUITE(MaybeNullTests)
|
||||
#ifdef CONVERT_TO_PTR_TO_CONST
|
||||
int test9_helper(maybe_null<const int*> copt)
|
||||
{
|
||||
if (copt.present())
|
||||
return *copt;
|
||||
if (copt.present()) return *copt;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -20,16 +20,26 @@
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
@ -60,7 +70,8 @@ SUITE(NotNullTests)
|
||||
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)
|
||||
|
@ -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,7 +44,10 @@ SUITE(utils_tests)
|
||||
{
|
||||
int i = 0;
|
||||
{
|
||||
auto _1 = finally([&]() {f(i);});
|
||||
auto _1 = finally([&]()
|
||||
{
|
||||
f(i);
|
||||
});
|
||||
{
|
||||
auto _2 = std::move(_1);
|
||||
CHECK(i == 0);
|
||||
@ -62,7 +68,10 @@ SUITE(utils_tests)
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
void g() { j += 1; };
|
||||
void g()
|
||||
{
|
||||
j += 1;
|
||||
};
|
||||
TEST(finally_function_ptr)
|
||||
{
|
||||
j = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user