1 // cs_string.hh
2 // This file is part of libpbe; see http://anyterm.org/
3 // (C) 2007-2008 Philip Endecott
4 
5 // Distributed under the Boost Software License, Version 1.0:
6 //
7 // Permission is hereby granted, free of charge, to any person or organization
8 // obtaining a copy of the software and accompanying documentation covered by
9 // this license (the "Software") to use, reproduce, display, distribute,
10 // execute, and transmit the Software, and to prepare derivative works of the
11 // Software, and to permit third-parties to whom the Software is furnished to
12 // do so, all subject to the following:
13 //
14 // The copyright notices in the Software and this entire statement, including
15 // the above license grant, this restriction and the following disclaimer,
16 // must be included in all copies of the Software, in whole or in part, and
17 // all derivative works of the Software, unless such copies or derivative
18 // works are solely in the form of machine-executable object code generated by
19 // a source language processor.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 
29 #ifndef libpbe_charset_cs_string_hh
30 #define libpbe_charset_cs_string_hh
31 
32 #include "charset/charset_t.hh"
33 #include "charset/string_adaptor.hh"
34 
35 #include <string>
36 #include <memory>
37 
38 
39 namespace pbe {
40 
41 // String tagged with character set
42 // --------------------------------
43 //
44 // This class provides a string tagged with its character set.  It is
45 // implemented using a std::basic_string of the character set's unit_t
46 // and a string_adaptor.
47 
48 
49 // This base class is used so that ustr is constructed before it is
50 // passed by reference to string_adaptor.
51 template <typename unit_string_t>
52 struct cs_string_base {
53   unit_string_t ustr_;
cs_string_basepbe::cs_string_base54   cs_string_base() {}
cs_string_basepbe::cs_string_base55   cs_string_base(unit_string_t u): ustr_(u) {}
cs_string_basepbe::cs_string_base56   cs_string_base(const typename unit_string_t::pointer u): ustr_(u) {}
57 };
58 
59 
60 template< charset_t cset,
61           typename Alloc = std::allocator< typename charset_traits<cset>::unit_t >
62         >
63 class cs_string:
64   private cs_string_base< typename string_adaptor<cset,Alloc>::unit_string_t >,
65   public string_adaptor<cset,Alloc>
66 {
67   typedef string_adaptor<cset,Alloc> adaptor;
68   typedef cs_string_base< typename adaptor::unit_string_t > base;
69   typedef typename charset_traits<cset>::unit_t unit_t;
70 
71 public:
cs_string()72   cs_string(): adaptor(base::ustr_) {}
cs_string(const unit_t * s)73   cs_string(const unit_t* s): base(s), adaptor(base::ustr_) {}  // ???
cs_string(const cs_string & other)74   cs_string(const cs_string& other): base(other.unit_str()), adaptor(base::ustr_) {}
cs_string(typename adaptor::size_type n,typename adaptor::value_type c)75   cs_string(typename adaptor::size_type n, typename adaptor::value_type c):
76     adaptor(base::ustr_) {
77     append(n,c);
78   }
79   template <class InputIterator>
cs_string(InputIterator first,InputIterator last)80   cs_string(InputIterator first, InputIterator last):
81     adaptor(base::ustr_) {
82     append(first,last);
83   }
84 };
85 
86 
87 template <charset_t cset, typename Alloc>
operator +(const cs_string<cset,Alloc> & s1,const cs_string<cset,Alloc> & s2)88 cs_string<cset, Alloc> operator+(const cs_string<cset, Alloc>& s1,
89                                  const cs_string<cset, Alloc>& s2) {
90   cs_string<cset,Alloc> s = s1;
91   s.append(s2);
92   return s;
93 }
94 
95 template <charset_t cset, typename Alloc>
operator +(typename charset_traits<cset>::char_t c,const cs_string<cset,Alloc> & s2)96 cs_string<cset, Alloc> operator+(typename charset_traits<cset>::char_t c,
97                                  const cs_string<cset, Alloc>& s2) {
98   cs_string<cset,Alloc> s = c;
99   s.append(s2);
100   return s;
101 }
102 
103 template <charset_t cset, typename Alloc>
operator +(const cs_string<cset,Alloc> & s1,typename charset_traits<cset>::char_t c)104 cs_string<cset, Alloc> operator+(const cs_string<cset, Alloc>& s1,
105                                  typename charset_traits<cset>::char_t c) {
106   cs_string<cset,Alloc> s = s1;
107   s.append(c);
108   return s;
109 }
110 
111 
112 };
113 
114 #endif
115