Home | History | Annotate | Download | only in gsl
      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 #endif // _MSC_VER
     35 
     36 namespace gsl
     37 {
     38 // Note: this will generate faster code than std::copy using span iterator in older msvc+stl
     39 // not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
     40 template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
     41           std::ptrdiff_t DestExtent>
     42 void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
     43 {
     44     static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
     45                   "Elements of source span can not be assigned to elements of destination span");
     46     static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
     47                       (SrcExtent <= DestExtent),
     48                   "Source range is longer than target range");
     49 
     50     Expects(dest.size() >= src.size());
     51     GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
     52     std::copy_n(src.data(), src.size(), dest.data());
     53 }
     54 
     55 } // namespace gsl
     56 
     57 #ifdef _MSC_VER
     58 #pragma warning(pop)
     59 #endif // _MSC_VER
     60 
     61 #endif // GSL_ALGORITHM_H
     62