|
2 | 2 | # define __BOOST_NETWORK_URI_DIRECTIVES_QUERY_INC__
|
3 | 3 |
|
4 | 4 |
|
| 5 | +# include <boost/network/support/is_pod.hpp> |
| 6 | +# include <boost/utility/enable_if.hpp> |
| 7 | +# include <boost/mpl/if.hpp> |
| 8 | +# include <boost/mpl/or.hpp> |
| 9 | + |
| 10 | + |
5 | 11 | namespace boost {
|
6 | 12 | namespace network {
|
7 | 13 | namespace uri {
|
| 14 | +template < |
| 15 | + class ValueType |
| 16 | + > |
| 17 | +struct query_directive { |
| 18 | + |
| 19 | + explicit query_directive(const ValueType &value) |
| 20 | + : value(value) |
| 21 | + {} |
| 22 | + |
| 23 | + template < |
| 24 | + class Tag |
| 25 | + , template <class> class Uri |
| 26 | + > |
| 27 | + typename enable_if<is_pod<Tag>, void>::type |
| 28 | + operator () (Uri<Tag> &uri) const { |
| 29 | + static const char separator[] = {'?'}; |
| 30 | + uri.append(boost::begin(separator), boost::end(separator)); |
| 31 | + uri.append(value); |
| 32 | + } |
| 33 | + |
| 34 | + template < |
| 35 | + class Tag |
| 36 | + , template <class> class Uri |
| 37 | + > |
| 38 | + typename enable_if<mpl::not_<is_pod<Tag> >, void>::type |
| 39 | + operator () (Uri<Tag> &uri) const { |
| 40 | + static const char separator[] = {'?'}; |
| 41 | + uri.append(boost::begin(separator), boost::end(separator)); |
| 42 | + uri.append(value); |
| 43 | + } |
| 44 | + |
| 45 | + const ValueType &value; |
| 46 | + |
| 47 | +}; |
| 48 | + |
| 49 | +template < |
| 50 | + class T |
| 51 | + > |
| 52 | +inline |
| 53 | +query_directive<T> query(const T &value) { |
| 54 | + return query_directive<T>(value); |
| 55 | +} |
| 56 | + |
| 57 | +template < |
| 58 | + class KeyType |
| 59 | + , class ValueType |
| 60 | + > |
| 61 | +struct query_key_value_directive { |
| 62 | + |
| 63 | + query_key_value_directive(const KeyType &key, const ValueType &value) |
| 64 | + : key(key), value(value) |
| 65 | + {} |
| 66 | + |
| 67 | + template < |
| 68 | + class Tag |
| 69 | + , template <class> class Uri |
| 70 | + > |
| 71 | + typename enable_if<is_pod<Tag>, void>::type |
| 72 | + operator () (Uri<Tag> &uri) const { |
| 73 | + static const char separator_1[] = {'?'}; |
| 74 | + static const char separator_2[] = {'='}; |
| 75 | + static const char separator_3[] = {';'}; |
| 76 | + if (!uri.query_range()) |
| 77 | + { |
| 78 | + uri.append(boost::begin(separator_1), boost::end(separator_1)); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + uri.append(boost::begin(separator_3), boost::end(separator_3)); |
| 83 | + } |
| 84 | + uri.append(key); |
| 85 | + uri.append(boost::begin(separator_2), boost::end(separator_2)); |
| 86 | + typename string<Tag>::type encoded_value; |
| 87 | + encode(boost::begin(value), boost::end(value), std::back_inserter(encoded_value)); |
| 88 | + uri.append(encoded_value); |
| 89 | + } |
| 90 | + |
| 91 | + template < |
| 92 | + class Tag |
| 93 | + , template <class> class Uri |
| 94 | + > |
| 95 | + typename enable_if<mpl::not_<is_pod<Tag> >, void>::type |
| 96 | + operator () (Uri<Tag> &uri) const { |
| 97 | + static const char separator_1[] = {'?'}; |
| 98 | + static const char separator_2[] = {'='}; |
| 99 | + static const char separator_3[] = {';'}; |
| 100 | + if (!uri.query_range()) |
| 101 | + { |
| 102 | + uri.append(boost::begin(separator_1), boost::end(separator_1)); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + uri.append(boost::begin(separator_3), boost::end(separator_3)); |
| 107 | + } |
| 108 | + uri.append(key); |
| 109 | + uri.append(boost::begin(separator_2), boost::end(separator_2)); |
| 110 | + uri.append(value); |
| 111 | + } |
| 112 | + |
| 113 | + const KeyType &key; |
| 114 | + const ValueType &value; |
| 115 | + |
| 116 | +}; |
8 | 117 |
|
| 118 | +template < |
| 119 | + class Key |
| 120 | + , class Value |
| 121 | + > |
| 122 | +inline |
| 123 | +query_key_value_directive<Key, Value> query(const Key &key, const Value &value) { |
| 124 | + return query_key_value_directive<Key, Value>(key, value); |
| 125 | +} |
9 | 126 | } // namespace uri
|
10 | 127 | } // namespace network
|
11 | 128 | } // namespace boost
|
|
0 commit comments