Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / base / ValueTransform.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/ValueTransform.h
10  */
11 #ifndef ZYPP_BASE_VALUETRANSFORM_H
12 #define ZYPP_BASE_VALUETRANSFORM_H
13
14 #include <iosfwd>
15
16 #include "zypp/base/Iterator.h"
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 {
21   ///////////////////////////////////////////////////////////////////
22   namespace base
23   {
24     ///////////////////////////////////////////////////////////////////
25     /// \class ValueTransform
26     /// \brief Helper managing raw values with transformed representation
27     ///
28     /// This helper enforces to explicitly state whether you are using
29     /// the raw or the variable replaced value. Usually you set \c raw
30     /// and get \c transformed (unless writing \c raw to some config file).
31     ///
32     /// Used e.g. vor variable replaced config strings.
33     ///////////////////////////////////////////////////////////////////
34     template<class Tp, class TUnaryFunction>
35     struct ValueTransform
36     {
37       typedef Tp RawType;
38       typedef TUnaryFunction Transformator;
39       typedef typename Transformator::result_type TransformedType;
40
41     public:
42       ValueTransform()
43       {}
44
45       explicit ValueTransform( RawType raw_r )
46       : _raw( std::move(raw_r) )
47       {}
48
49       ValueTransform( RawType raw_r, Transformator transform_r )
50       : _raw( std::move(raw_r) ), _transform( std::move(transform_r) )
51       {}
52
53     public:
54       /** Get the raw value */
55       const RawType & raw() const
56       { return _raw; }
57
58       /** Set the raw value */
59       RawType & raw()
60       { return _raw; }
61
62     public:
63       /** Return a transformed copy of the raw value */
64       TransformedType transformed() const
65       { return _transform( _raw ); }
66
67       /** Return a transformed copy of an arbitrary \a RawType */
68       TransformedType transformed( const RawType & raw_r ) const
69       { return _transform( raw_r ); }
70
71       /** Return the transformator */
72       const Transformator & transformator() const
73       { return _transform; }
74
75     private:
76       RawType _raw;
77       Transformator _transform;
78     };
79
80     ///////////////////////////////////////////////////////////////////
81     /// \class ContainerTransform
82     /// \brief Helper managing a container of raw values with transformed representation
83     ///
84     /// This helper enforces to explicitly state wheter you are using
85     /// the raw or the variable replaced value. Usually you set \c raw
86     /// and get \c transformed (uness writing \c raw to some config file).
87     ///
88     /// Offers iterating over transformed strings in the list.
89     ///////////////////////////////////////////////////////////////////
90     template<class TContainer, class TUnaryFunction>
91     struct ContainerTransform
92     {
93       typedef TContainer Container;
94       typedef TUnaryFunction Transformator;
95       typedef typename Container::size_type size_type;
96       typedef typename Container::value_type RawType;
97       typedef typename Transformator::result_type TransformedType;
98
99     public:
100       ContainerTransform()
101       {}
102
103       explicit ContainerTransform( Container raw_r )
104       : _raw( std::move(raw_r) )
105       {}
106
107       ContainerTransform( Container raw_r, Transformator transform_r )
108       : _raw( std::move(raw_r) ), _transform( std::move(transform_r) )
109       {}
110
111     public:
112       bool empty() const
113       { return _raw.empty(); }
114
115       size_type size() const
116       { return _raw.size(); }
117
118       typedef typename Container::const_iterator RawConstIterator;
119
120       RawConstIterator rawBegin() const
121       { return _raw.begin(); }
122
123       RawConstIterator rawEnd() const
124       { return _raw.end(); }
125
126       /** Get the raw value */
127       const Container & raw() const
128       { return _raw; }
129
130       /** Set the raw value */
131       Container & raw()
132       { return _raw; }
133
134     public:
135       typedef transform_iterator<Transformator, typename Container::const_iterator> TransformedConstIterator;
136
137       TransformedConstIterator transformedBegin() const
138       { return make_transform_iterator( _raw.begin(), _transform ); }
139
140       TransformedConstIterator transformedEnd() const
141       { return make_transform_iterator( _raw.end(), _transform ); }
142
143       /** Return copy with transformed variables (expensive) */
144       Container transformed() const
145       { return Container( transformedBegin(), transformedEnd() ); }
146
147       /** Return a transformed copy of an arbitrary \a RawType */
148       TransformedType transformed( const RawType & raw_r ) const
149       { return _transform( raw_r ); }
150
151       /** Return a transformed copy of a \a RawConstIterator raw value */
152       TransformedType transformed( const RawConstIterator & rawIter_r ) const
153       { return _transform( *rawIter_r ); }
154
155       /** Return the transformator */
156       const Transformator & transformator() const
157       { return _transform; }
158
159     private:
160       Container _raw;
161       Transformator _transform;
162     };
163
164   } // namespace base
165   ///////////////////////////////////////////////////////////////////
166 } // namespace zypp
167 ///////////////////////////////////////////////////////////////////
168 #endif // ZYPP_BASE_VALUETRANSFORM_H