2 * Copyright (C) 2015 Topology LP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #ifndef CPPCODEC_DETAIL_DATA_ACCESS
25 #define CPPCODEC_DETAIL_DATA_ACCESS
27 #include <stdint.h> // for size_t
32 // This file contains a number of templated data accessors that can be
33 // implemented in the cppcodec::data namespace for types that don't fulfill
34 // the default type requirements:
35 // For result types: init(Result&, ResultState&, size_t capacity),
36 // put(Result&, ResultState&, char), finish(Result&, State&)
37 // For const (read-only) types: char_data(const T&)
38 // For both const and result types: size(const T&)
40 template <typename T> inline size_t size(const T& t) { return t.size(); }
41 template <typename T, size_t N> inline constexpr size_t size(const T (&t)[N]) noexcept {
42 return N * sizeof(t[0]);
46 class specific_t : public general_t {};
48 class empty_result_state {
49 template <typename Result> inline void size(const Result& result) { return size(result); }
52 // SFINAE: Generic fallback in case no specific state function applies.
53 template <typename Result>
54 inline empty_result_state create_state(Result&, general_t) { return empty_result_state(); }
57 // Generic templates for containers: Use these init()/put()/finish()
58 // implementations if no specialization was found.
61 template <typename Result>
62 inline void init(Result& result, empty_result_state&, size_t capacity)
65 result.reserve(capacity);
68 template <typename Result>
69 inline void finish(Result&, empty_result_state&)
71 // Default is to push_back(), which already increases the size.
74 // For the put() default implementation, we try calling push_back() with either uint8_t or char,
75 // whichever compiles. Scary-fancy template magic from http://stackoverflow.com/a/1386390.
77 struct flag { char c[2]; }; // sizeof > 1
80 int operator,(flag, flag);
81 template <typename T> void operator,(flag, T&); // map everything else to void
82 char operator,(int, flag); // sizeof 1
85 template <typename Result> inline void put_uint8(Result& result, uint8_t c) { result.push_back(c); }
87 template <bool> struct put_impl;
88 template <> struct put_impl<true> { // put_uint8() available
89 template<typename Result> static void put(Result& result, uint8_t c) { put_uint8(result, c); }
91 template <> struct put_impl<false> { // put_uint8() not available
92 template<typename Result> static void put(Result& result, uint8_t c) {
93 result.push_back(static_cast<char>(c));
97 template <typename Result> inline void put(Result& result, empty_result_state&, uint8_t c)
99 using namespace fallback;
100 put_impl<sizeof(fallback::flag(), put_uint8(result, c), fallback::flag()) != 1>::put(result, c);
104 // Specialization for container types with direct mutable data access.
105 // The expected way to specialize is to subclass empty_result_state and
106 // return an instance of it from a create_state() template specialization.
107 // You can then create overloads for init(), put() and finish()
108 // that are more specific than the empty_result_state ones above.
109 // See the example below for direct access to a mutable data() method.
111 // If desired, a non-templated overload for both specific types
112 // (result & state) can be added to tailor it to that particular result type.
115 template <typename Result> class direct_data_access_result_state : empty_result_state
118 using result_type = Result;
120 inline void init(Result& result, size_t capacity)
122 // resize(0) is not called here since we don't rely on it
123 result.reserve(capacity);
125 inline void put(Result& result, char c)
127 // This only compiles if decltype(data) == char*
128 result.data()[m_offset++] = static_cast<char>(c);
130 inline void finish(Result& result)
132 result.resize(m_offset);
134 inline size_t size(const Result&)
142 // SFINAE: Select a specific state based on the result type and possible result state type.
143 // Implement this if direct data access (`result.data()[0] = 'x') isn't already possible
144 // and you want to specialize it for your own result type.
145 template <typename Result, typename ResultState =
146 typename direct_data_access_result_state<Result>::result_type::value>
147 inline ResultState create_state(Result&, specific_t) { return ResultState(); }
149 template <typename Result>
150 inline void init(Result& result, direct_data_access_result_state<Result>& state, size_t capacity)
155 // Specialized put function for direct_data_access_result_state.
156 template <typename Result>
157 inline void put(Result& result, direct_data_access_result_state<Result>& state, char c)
159 state.put(result, c);
162 // char_data() is only used to read, not for result buffers.
163 template <typename T> inline const char* char_data(const T& t)
165 return reinterpret_cast<const char*>(t.data());
167 template <typename T, size_t N> inline const char* char_data(const T (&t)[N]) noexcept
169 return reinterpret_cast<const char*>(&(t[0]));
172 template <typename T> inline const uint8_t* uchar_data(const T& t)
174 return reinterpret_cast<const uint8_t*>(char_data(t));
178 } // namespace cppcodec