1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/ValueTransform.h
11 #ifndef ZYPP_BASE_VALUETRANSFORM_H
12 #define ZYPP_BASE_VALUETRANSFORM_H
16 #include "zypp/base/Iterator.h"
18 ///////////////////////////////////////////////////////////////////
21 ///////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
25 /// \class ValueTransform
26 /// \brief Helper managing raw values with transformed representation
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).
32 /// Used e.g. vor variable replaced config strings.
33 ///////////////////////////////////////////////////////////////////
34 template<class Tp, class TUnaryFunction>
38 typedef TUnaryFunction Transformator;
39 typedef typename Transformator::result_type TransformedType;
45 explicit ValueTransform( RawType raw_r )
46 : _raw( std::move(raw_r) )
49 ValueTransform( RawType raw_r, Transformator transform_r )
50 : _raw( std::move(raw_r) ), _transform( std::move(transform_r) )
54 /** Get the raw value */
55 const RawType & raw() const
58 /** Set the raw value */
63 /** Return a transformed copy of the raw value */
64 TransformedType transformed() const
65 { return _transform( _raw ); }
67 /** Return a transformed copy of an arbitrary \a RawType */
68 TransformedType transformed( const RawType & raw_r ) const
69 { return _transform( raw_r ); }
71 /** Return the transformator */
72 const Transformator & transformator() const
73 { return _transform; }
77 Transformator _transform;
80 ///////////////////////////////////////////////////////////////////
81 /// \class ContainerTransform
82 /// \brief Helper managing a container of raw values with transformed representation
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).
88 /// Offers iterating over transformed strings in the list.
89 ///////////////////////////////////////////////////////////////////
90 template<class TContainer, class TUnaryFunction>
91 struct ContainerTransform
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;
103 explicit ContainerTransform( Container raw_r )
104 : _raw( std::move(raw_r) )
107 ContainerTransform( Container raw_r, Transformator transform_r )
108 : _raw( std::move(raw_r) ), _transform( std::move(transform_r) )
113 { return _raw.empty(); }
115 size_type size() const
116 { return _raw.size(); }
118 typedef typename Container::const_iterator RawConstIterator;
120 RawConstIterator rawBegin() const
121 { return _raw.begin(); }
123 RawConstIterator rawEnd() const
124 { return _raw.end(); }
126 /** Get the raw value */
127 const Container & raw() const
130 /** Set the raw value */
135 typedef transform_iterator<Transformator, typename Container::const_iterator> TransformedConstIterator;
137 TransformedConstIterator transformedBegin() const
138 { return make_transform_iterator( _raw.begin(), _transform ); }
140 TransformedConstIterator transformedEnd() const
141 { return make_transform_iterator( _raw.end(), _transform ); }
143 /** Return copy with transformed variables (expensive) */
144 Container transformed() const
145 { return Container( transformedBegin(), transformedEnd() ); }
147 /** Return a transformed copy of an arbitrary \a RawType */
148 TransformedType transformed( const RawType & raw_r ) const
149 { return _transform( raw_r ); }
151 /** Return a transformed copy of a \a RawConstIterator raw value */
152 TransformedType transformed( const RawConstIterator & rawIter_r ) const
153 { return _transform( *rawIter_r ); }
155 /** Return the transformator */
156 const Transformator & transformator() const
157 { return _transform; }
161 Transformator _transform;
165 ///////////////////////////////////////////////////////////////////
167 ///////////////////////////////////////////////////////////////////
168 #endif // ZYPP_BASE_VALUETRANSFORM_H