rapidyaml  0.12.1
parse and emit YAML, and do it fast
vector.hpp
Go to the documentation of this file.
1 #ifndef _C4_STD_VECTOR_HPP_
2 #define _C4_STD_VECTOR_HPP_
3 
4 /** @file vector.hpp provides conversion and comparison facilities
5  * from/between std::vector<char> to c4::substr and c4::csubstr.
6  * @todo add to_span() and friends
7  */
8 
9 #ifndef C4CORE_SINGLE_HEADER
10 #include "c4/substr.hpp"
11 #endif
12 
13 #if defined(__GNUC__) && (__GNUC__ >= 6)
14 C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wnull-dereference")
15 #endif
16 #include <vector>
17 #if defined(__GNUC__) && (__GNUC__ >= 6)
18 C4_SUPPRESS_WARNING_GCC_POP
19 #endif
20 
21 namespace c4 {
22 
23 // mark std::string as a string type
24 template<class T> struct is_string;
25 template<class Alloc> struct is_string<std::vector<char,Alloc>> : public std::true_type {};
26 
27 // mark std::string as a writeable string type
28 template<class T> struct is_writeable_string;
29 template<class Alloc> struct is_writeable_string<std::vector<char,Alloc>> : public std::true_type {};
30 
31 
32 //-----------------------------------------------------------------------------
33 
34 /** get a substr (writeable string view) of an existing std::vector<char> */
35 template<class Alloc>
36 c4::substr to_substr(std::vector<char, Alloc> &vec)
37 {
38  char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.
39  return c4::substr(data, vec.size());
40 }
41 
42 /** get a csubstr (read-only string) view of an existing std::vector<char> */
43 template<class Alloc>
44 c4::csubstr to_csubstr(std::vector<char, Alloc> const& vec)
45 {
46  const char *data = vec.empty() ? nullptr : vec.data(); // data() may or may not return a null pointer.
47  return c4::csubstr(data, vec.size());
48 }
49 
50 
51 //-----------------------------------------------------------------------------
52 // comparisons between substrings and std::vector<char>
53 
54 template<class Alloc> C4_ALWAYS_INLINE bool operator!= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss != to_csubstr(s); }
55 template<class Alloc> C4_ALWAYS_INLINE bool operator== (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss == to_csubstr(s); }
56 template<class Alloc> C4_ALWAYS_INLINE bool operator>= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss >= to_csubstr(s); }
57 template<class Alloc> C4_ALWAYS_INLINE bool operator> (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss > to_csubstr(s); }
58 template<class Alloc> C4_ALWAYS_INLINE bool operator<= (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss <= to_csubstr(s); }
59 template<class Alloc> C4_ALWAYS_INLINE bool operator< (c4::csubstr ss, std::vector<char, Alloc> const& s) { return ss < to_csubstr(s); }
60 
61 template<class Alloc> C4_ALWAYS_INLINE bool operator!= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss != to_csubstr(s); }
62 template<class Alloc> C4_ALWAYS_INLINE bool operator== (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss == to_csubstr(s); }
63 template<class Alloc> C4_ALWAYS_INLINE bool operator>= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss <= to_csubstr(s); }
64 template<class Alloc> C4_ALWAYS_INLINE bool operator> (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss < to_csubstr(s); }
65 template<class Alloc> C4_ALWAYS_INLINE bool operator<= (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss >= to_csubstr(s); }
66 template<class Alloc> C4_ALWAYS_INLINE bool operator< (std::vector<char, Alloc> const& s, c4::csubstr ss) { return ss > to_csubstr(s); }
67 
68 
69 //-----------------------------------------------------------------------------
70 
71 /** copy a std::vector<char> to a substr */
72 template<class Alloc>
73 inline size_t to_chars(c4::substr buf, std::vector<char, Alloc> const& s)
74 {
75  size_t sz = s.size();
76  size_t len = buf.len < sz ? buf.len : sz;
77  buf.copy_from(csubstr(s.data(), len)); // copy only available chars
78  return sz; // return the number of needed chars
79 }
80 
81 /** copy a csubstr to an existing std::vector<char> */
82 template<class Alloc>
83 inline bool from_chars(c4::csubstr buf, std::vector<char, Alloc> * s)
84 {
85  s->resize(buf.len);
86  substr(s->data(), buf.len).copy_from(buf);
87  return true;
88 }
89 
90 } // namespace c4
91 
92 #endif // _C4_STD_VECTOR_HPP_
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2366
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2210
substr to_substr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2208
bool operator!=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2289
bool operator>(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2291
bool operator>=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2293
bool operator<=(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2292
bool operator==(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2288
bool operator<(const char(&s)[N], basic_substring< C > const that) noexcept
Definition: substr.hpp:2290
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2331
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
a traits class to mark a type as a string type (meaning c4::to_csubstr() can be used directly).
Definition: substr.hpp:2245
a traits class to mark a type as a writeable string type (meaning c4::to_substr() can be used directl...
Definition: substr.hpp:2248
read+write string views