Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [diff] [blame^] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #ifndef GSL_ALGORITHM_H |
| 18 | #define GSL_ALGORITHM_H |
| 19 | |
| 20 | #include <gsl/gsl_assert> // for Expects |
| 21 | #include <gsl/span> // for dynamic_extent, span |
| 22 | |
| 23 | #include <algorithm> // for copy_n |
| 24 | #include <cstddef> // for ptrdiff_t |
| 25 | #include <type_traits> // for is_assignable |
| 26 | |
| 27 | #ifdef _MSC_VER |
| 28 | #pragma warning(push) |
| 29 | |
| 30 | // turn off some warnings that are noisy about our Expects statements |
| 31 | #pragma warning(disable : 4127) // conditional expression is constant |
| 32 | #pragma warning(disable : 4996) // unsafe use of std::copy_n |
| 33 | |
| 34 | // blanket turn off warnings from CppCoreCheck for now |
| 35 | // so people aren't annoyed by them when running the tool. |
| 36 | // more targeted suppressions will be added in a future update to the GSL |
| 37 | #pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495) |
| 38 | #endif // _MSC_VER |
| 39 | |
| 40 | namespace gsl |
| 41 | { |
| 42 | |
| 43 | template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType, |
| 44 | std::ptrdiff_t DestExtent> |
| 45 | void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest) |
| 46 | { |
| 47 | static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value, |
| 48 | "Elements of source span can not be assigned to elements of destination span"); |
| 49 | static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent || |
| 50 | (SrcExtent <= DestExtent), |
| 51 | "Source range is longer than target range"); |
| 52 | |
| 53 | Expects(dest.size() >= src.size()); |
| 54 | std::copy_n(src.data(), src.size(), dest.data()); |
| 55 | } |
| 56 | |
| 57 | } // namespace gsl |
| 58 | |
| 59 | #ifdef _MSC_VER |
| 60 | #pragma warning(pop) |
| 61 | #endif // _MSC_VER |
| 62 | |
| 63 | #endif // GSL_ALGORITHM_H |