|
1 | 1 | #ifndef JSS_UNIQUE_PTR_H_INCLUDED |
2 | 2 | #define JSS_UNIQUE_PTR_H_INCLUDED |
3 | 3 |
|
4 | | -#include "jss_core.h" |
| 4 | +#include "jss_core.h" |
| 5 | +#include "../utility/polymorphy.h" |
| 6 | + |
5 | 7 | #include <memory> |
6 | 8 |
|
7 | 9 | namespace JSON |
8 | | -{ |
9 | | - template <typename T, |
10 | | - typename = typename std::enable_if <Internal::can_stringify<T>::value>::type > |
11 | | - std::ostream& stringify(std::ostream& stream, const std::string& name, std::unique_ptr<T> const& value, StringificationOptions const& options = DEFAULT_OPTIONS) |
| 10 | +{ |
| 11 | + namespace internal |
| 12 | + { |
| 13 | + template <typename T, bool IsClass, bool IsPolymorphic> |
| 14 | + struct stringify_call_chooser |
| 15 | + { |
| 16 | + }; |
| 17 | + |
| 18 | + template <typename T> |
| 19 | + struct stringify_call_chooser <T, true, false> |
| 20 | + { |
| 21 | + static void exec(std::ostream& stream, const std::string& name, T const& value, StringificationOptions const& options) |
| 22 | + { |
| 23 | + value->stringify(stream, options); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + template <typename T> |
| 28 | + struct stringify_call_chooser <T, true, true> |
| 29 | + { |
| 30 | + static void exec(std::ostream& stream, const std::string& name, T const& value, StringificationOptions const& options) |
| 31 | + { |
| 32 | + //value->stringify(stream, options); |
| 33 | + polydecls <typename T::element_type>::smart_pointer_set(value, stream, options); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + template <typename T, bool IsPolymorphic> |
| 38 | + struct stringify_call_chooser <T, false, IsPolymorphic> |
| 39 | + { |
| 40 | + static void exec(std::ostream& stream, const std::string& name, T const& value, StringificationOptions const& options) |
| 41 | + { |
| 42 | + stringify(stream, name, *value, options); |
| 43 | + } |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + template <typename T, typename Deleter, |
| 48 | + typename = typename std::enable_if < |
| 49 | + Internal::can_stringify<T>::value || |
| 50 | + !std::is_same <typename polydecls <T>::type, no_poly>::value |
| 51 | + >::type > |
| 52 | + std::ostream& stringify(std::ostream& stream, const std::string& name, std::unique_ptr<T, Deleter> const& value, StringificationOptions const& options = {}) |
12 | 53 | { |
13 | 54 | if (!value) |
14 | | - throw UniquePtrNullptrException{}; |
15 | | - return stringify(stream, name, *value, options); |
| 55 | + throw UniquePtrNullptrException{}; |
| 56 | + |
| 57 | + if (options.in_object && !options.ignore_name) |
| 58 | + WRITE_NAME(stream); |
| 59 | + |
| 60 | + internal::stringify_call_chooser < |
| 61 | + typename std::decay<decltype(value)>::type, |
| 62 | + std::is_class <T>::value, |
| 63 | + !std::is_same <typename polydecls <T>::type, no_poly>::value |
| 64 | + >::exec(stream, name, value, options); |
| 65 | + return stream; |
16 | 66 | } |
17 | 67 | } |
18 | 68 |
|
|
0 commit comments