/src/spotify-json/include/spotify/json/detail/encode_helpers.hpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #pragma once |
18 | | |
19 | | #include <spotify/json/detail/macros.hpp> |
20 | | #include <spotify/json/encode_exception.hpp> |
21 | | #include <spotify/json/encode_context.hpp> |
22 | | |
23 | | namespace spotify { |
24 | | namespace json { |
25 | | namespace detail { |
26 | | |
27 | | json_noreturn void fail(const encode_context & /*context*/, const char *error); |
28 | | |
29 | | json_force_inline void fail_if( |
30 | | const encode_context &context, |
31 | | const bool condition, |
32 | 0 | const char *error) { |
33 | 0 | if (json_unlikely(condition)) { |
34 | 0 | fail(context, error); |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | | template <typename T> |
39 | | struct has_should_encode_method { |
40 | | template <typename U> |
41 | | static auto test(int) -> decltype( |
42 | | std::declval<U>().should_encode(std::declval<typename U::object_type>()), |
43 | | std::true_type()); |
44 | | |
45 | | template <typename> |
46 | | static std::false_type test(...); |
47 | | |
48 | | public: |
49 | | static constexpr bool value = std::is_same<decltype(test<T>(0)), std::true_type>::value; |
50 | | }; |
51 | | |
52 | | template <typename codec_type, typename value_type> |
53 | | typename std::enable_if<!has_should_encode_method<codec_type>::value, bool>::type |
54 | 0 | json_force_inline should_encode(const codec_type & /*codec*/, const value_type & /*value*/) { |
55 | 0 | return true; |
56 | 0 | } |
57 | | |
58 | | template <typename codec_type, typename value_type> |
59 | | typename std::enable_if<has_should_encode_method<codec_type>::value, bool>::type |
60 | | json_force_inline should_encode(const codec_type &codec, const value_type &value) { |
61 | | return codec.should_encode(value); |
62 | | } |
63 | | |
64 | | } // namespace detail |
65 | | } // namespace json |
66 | | } // namespace spotify |