SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tag_dealer.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_TAGDEALER_HPP
9 #define SIG_UTIL_TAGDEALER_HPP
10 
11 #include "../functional/high_order.hpp"
12 #include "../string/manipulate.hpp"
13 
14 namespace sig
15 {
16 namespace impl
17 {
18  template <class S> struct space{};
19  template <> struct space<std::string>{ std::string operator()() const{ return " "; } };
20  template <> struct space<std::wstring>{ std::wstring operator()() const{ return L" "; } };
21 }
22 
24 
29 template <class S>
30 class TagDealer
31 {
32  const S tel_;
33  const S ter_;
34 
35 public:
39  TagDealer(S tag_encloser_left, S tag_encloser_right) : tel_(tag_encloser_left), ter_(tag_encloser_right){};
40 
48  S encode(S const& src, S const& tag) const{
49  auto tag_str = tel_ + tag + ter_;
50  return tag_str + src + tag_str;
51  }
52 
60  auto decode(S const& src, S const& tag) ->Maybe<S> const{
61  auto tag_str = tel_ + tag + ter_;
62  auto parse = split(impl::space<S>()() + src, tag_str);
63 
64  return parse.size() < 2 ? Nothing(S()) : Just<S>(parse[1]);
65  }
66 
67  template < template < class T_, class Allocator = std::allocator<T_>> class Container >
68  S encode(Container<S> const& src, Container<S> const& tag) const;
69 
70  template < template < class T_, class Allocator = std::allocator<T_>> class Container >
71  auto decode(S const& src, Container<S> const& tag)->Maybe<Container<S>> const;
72 
73 };
74 
75 template <class S>
76 template < template < class T_, class Allocator = std::allocator<T_>> class Container >
77 S TagDealer<S>::encode(Container<S> const& src, Container<S> const& tag) const
78 {
79  auto calc = zipWith([&](typename Container<S>::value_type s, typename Container<S>::value_type t){ return encode(s, t); }, src, tag);
80  return std::accumulate(calc.begin(), calc.end(), S());
81 }
82 
83 template <class S>
84 template < template < class T_, class Allocator = std::allocator<T_>> class Container >
85 auto TagDealer<S>::decode(S const& src, Container<S> const& tag) ->Maybe<Container<S>> const
86 {
87  Container<S> result;
88  for (auto const& e : tag){
89  auto d = decode(src, e);
90  if (sig::isJust(d)) result.push_back(sig::fromJust(std::move(d)));
91  }
92  return result.empty() ? Nothing(result) : Just<Container<S>>(std::move(result));
93 }
94 
95 }
96 
97 #endif
TagDealer(S tag_encloser_left, S tag_encloser_right)
Definition: tag_dealer.hpp:39
auto decode(S const &src, S const &tag) -> Maybe< S > const
Definition: tag_dealer.hpp:60
HTML風にタグをエンコード・デコードする
Definition: tag_dealer.hpp:30
auto split(S const &src, impl::string_t< S > const &delimiter) -> CSeq< TS >
文字列(src)をある文字列(delimiter)を目印に分割する
Definition: manipulate.hpp:40
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
std::wstring operator()() const
Definition: tag_dealer.hpp:20
S encode(S const &src, S const &tag) const
Definition: tag_dealer.hpp:48
std::string operator()() const
Definition: tag_dealer.hpp:19
bool isJust(Maybe< T > const &m)
Justであるか調べる関数.Maybe a -> Bool.
Definition: maybe.hpp:84
auto zipWith(F &&func, C1 &&list1, C2 &&list2)
2引数高階関数
Definition: high_order.hpp:88
T & fromJust(Maybe< T > &m)
Justから値を取り出す関数.Maybe a -> a.
Definition: maybe.hpp:113
Definition: array.hpp:15