|
| 1 | +/** |
| 2 | + * @file Special Purpose Strings Library: polices.hpp |
| 3 | + * @author Daniel Evers |
| 4 | + * @brief Policy classes |
| 5 | + * @license MIT |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef SPSL_POLICIES_HPP_ |
| 9 | +#define SPSL_POLICIES_HPP_ |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <stdexcept> |
| 13 | + |
| 14 | +namespace spsl |
| 15 | +{ |
| 16 | +namespace policy |
| 17 | +{ |
| 18 | +/** |
| 19 | + * Policies in this namespace deal with possible buffer overflows. They are called by |
| 20 | + * @c StorageArray to check for and possibly handle overflows. |
| 21 | + * |
| 22 | + * All classes must implement the following (static) template functions: |
| 23 | + * (1) void checkReserve<size_type>(size_type cap, size_type max) |
| 24 | + * (2) size_type checkAssign<char_type, size_type>(const char_type* s, size_type n, size_type max) |
| 25 | + * (3) size_type checkAssign<char_type, size_type>(size_type n, char_type ch, size_type max) |
| 26 | + * (4) size_type checkAppend<char_type, size_type>(const char_type* s, size_type n, |
| 27 | + * size_type size, size_type max) |
| 28 | + * (5) size_type checkAppend<char_type, size_type>(size_type n, char_type ch, |
| 29 | + * size_type size, size_type max) |
| 30 | + * |
| 31 | + * Function (1) is called from within reserve() with the requested capacity and the maximum size. |
| 32 | + * Since reserve() itself is a no-op, this function returns nothing. |
| 33 | + * |
| 34 | + * Functions (2) and (3) are called from within assign() and similar methods with the string |
| 35 | + * or the characters to assign as well as the maximum capacity. They have to return a "proper" |
| 36 | + * number of characters to assign or must not return at all. |
| 37 | + * |
| 38 | + * Functions (4) and (5) are called from within append() and similar methods with the string |
| 39 | + * or the characters to append as well as the current size and the maximum capacity. They have to |
| 40 | + * return a "proper" number of characters to append or must not return at all. |
| 41 | + */ |
| 42 | +namespace overflow |
| 43 | +{ |
| 44 | + |
| 45 | +/** |
| 46 | + * Policy class that truncates strings to fit them into a buffer. All functions are constexpr |
| 47 | + * and noexcept. |
| 48 | + */ |
| 49 | +struct Truncate |
| 50 | +{ |
| 51 | + template <typename size_type> |
| 52 | + static constexpr bool checkReserve(size_type, size_type) noexcept |
| 53 | + { |
| 54 | + // this is a hack: constexpr functions using return type 'void' are possible since C++14 |
| 55 | + return true; |
| 56 | + } |
| 57 | + template <typename char_type, typename size_type> |
| 58 | + static constexpr size_type checkAssign(const char_type*, size_type n, size_type max) noexcept |
| 59 | + { |
| 60 | + return std::min(n, max); |
| 61 | + } |
| 62 | + template <typename char_type, typename size_type> |
| 63 | + static constexpr size_type checkAssign(size_type n, char_type, size_type max) noexcept |
| 64 | + { |
| 65 | + return std::min(n, max); |
| 66 | + } |
| 67 | + template <typename char_type, typename size_type> |
| 68 | + static constexpr size_type checkAppend(const char_type*, size_type n, size_type size, |
| 69 | + size_type max) noexcept |
| 70 | + { |
| 71 | + return std::min(n, max - size); |
| 72 | + } |
| 73 | + template <typename char_type, typename size_type> |
| 74 | + static constexpr size_type checkAppend(size_type n, char_type, size_type size, |
| 75 | + size_type max) noexcept |
| 76 | + { |
| 77 | + return std::min(n, max - size); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +/// Policy class that throws an exception if a string is too long. |
| 82 | +struct Throw |
| 83 | +{ |
| 84 | + template <typename size_type> |
| 85 | + static void checkReserve(size_type cap, size_type max) |
| 86 | + { |
| 87 | + if (cap > max) |
| 88 | + throw std::length_error("requested capacity exceeds maximum"); |
| 89 | + }; |
| 90 | + template <typename char_type, typename size_type> |
| 91 | + static size_type checkAssign(const char_type*, size_type n, size_type max) |
| 92 | + { |
| 93 | + if (n > max) |
| 94 | + throw std::length_error("string length exceeds maximum capacity"); |
| 95 | + return n; |
| 96 | + } |
| 97 | + template <typename char_type, typename size_type> |
| 98 | + static size_type checkAssign(size_type n, char_type, size_type max) |
| 99 | + { |
| 100 | + if (n > max) |
| 101 | + throw std::length_error("string length exceeds maximum capacity"); |
| 102 | + return n; |
| 103 | + } |
| 104 | + template <typename char_type, typename size_type> |
| 105 | + static size_type checkAppend(const char_type*, size_type n, size_type size, size_type max) |
| 106 | + { |
| 107 | + if (size + n > max) |
| 108 | + throw std::length_error("string length exceeds maximum capacity"); |
| 109 | + return n; |
| 110 | + } |
| 111 | + template <typename char_type, typename size_type> |
| 112 | + static size_type checkAppend(size_type n, char_type, size_type size, size_type max) |
| 113 | + { |
| 114 | + if (size + n > max) |
| 115 | + throw std::length_error("string length exceeds maximum capacity"); |
| 116 | + return n; |
| 117 | + } |
| 118 | +}; |
| 119 | +} |
| 120 | +} |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +#endif /* SPSL_POLICIES_HPP_ */ |
0 commit comments