source: wkhtmltox/trunk/fuentes/src/shared/arghandler.inl @ 51

Last change on this file since 51 was 51, checked in by mabarracus, 4 years ago

wip

File size: 5.5 KB
Line 
1// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2// vi:set ts=4 sts=4 sw=4 noet :
3//
4// Copyright 2010, 2011 wkhtmltopdf authors
5//
6// This file is part of wkhtmltopdf.
7//
8// wkhtmltopdf is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Lesser General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// wkhtmltopdf is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public License
19// along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef __ARGHANDLER_INL__
22#define __ARGHANDLER_INL__
23#include "commandlineparserbase.hh"
24#include <wkhtmltox/loadsettings.hh>
25
26template <typename T> class DstArgHandler: public ArgHandler {
27public:
28        T & dst;
29        DstArgHandler(T & d): dst(d) {};
30
31        T & realDst(const CommandLineParserBase & cp, char * page) {
32                return * reinterpret_cast<T*>(cp.mapAddress(reinterpret_cast<char *>(&dst), page));
33        }
34};
35
36/*!
37  Sets a variable to some constant
38*/
39template <typename T> class ConstSetter: public DstArgHandler<T> {
40public:
41        typedef DstArgHandler<T> p_t;
42        const T src;
43        ConstSetter(T & arg, const T s): p_t(arg), src(s) {};
44        bool operator() (const char **, CommandLineParserBase & cp, char * ps) {
45                p_t::realDst(cp, ps)=src;
46                return true;
47        }
48
49        virtual QString getDesc() const {
50                if (src != p_t::dst) return p_t::desc;
51                return p_t::desc + " (default)";
52        }
53};
54
55struct StringPairCreator {
56        typedef QPair<QString, QString> T;
57        inline T operator()(const QString & key, const QString & value) const {
58                return T(key, value);
59        }
60};
61
62template <bool file>
63struct PostItemCreator {
64        typedef wkhtmltopdf::settings::PostItem T;
65        inline T operator()(const QString & key, const QString & value) const {
66                T p;
67                p.name = key;
68                p.value = value;
69                p.file = file;
70                return p;
71        }
72};
73
74/*!
75  Putting values into a map
76*/
77template <typename T=StringPairCreator>
78struct MapSetter: public DstArgHandler< QList< typename T::T > > {
79        typedef DstArgHandler<QList< typename T::T > > p_t;
80        MapSetter(QList<typename T::T > & a, QString keyName, QString valueName) : p_t(a) {
81                p_t::argn.push_back(keyName);
82                p_t::argn.push_back(valueName);
83        }
84        virtual bool operator() (const char ** args, CommandLineParserBase & cp, char * ps) {
85                p_t::realDst(cp, ps).append( T()(args[0], args[1]) );
86                return true;
87        }
88};
89
90struct StringListSetter: public DstArgHandler<QList<QString> > {
91        typedef DstArgHandler<QList<QString> > p_t;
92        StringListSetter(QList<QString> & a, QString valueName) : p_t (a) {
93                p_t::argn.push_back(valueName);
94        }
95        virtual bool operator() (const char ** args, CommandLineParserBase & cp, char * ps) {
96                p_t::realDst(cp, ps).append( args[0] );
97                return true;
98        }
99};
100
101/*!
102  SomeSetter template method base
103*/
104template <typename TT>
105struct SomeSetterTM {
106        typedef TT T;
107        //T strToT(const char * val, bool & ok);
108        static QString TToStr(const T &, bool & ok) {ok=false; return "";}
109};
110
111/*!
112  TemplateMethod class used to set a single variable of some type TT::T
113*/
114template <typename TT>
115struct SomeSetter: public DstArgHandler<typename TT::T > {
116        typedef DstArgHandler<typename TT::T > p_t;
117        typedef typename TT::T T;
118        bool hasDef;
119
120        SomeSetter(T & a, QString an, bool def=true): p_t(a), hasDef(def) {
121                p_t::argn.push_back(an);
122        }
123
124        bool operator() (const char ** vals, CommandLineParserBase & cp, char * ps) {
125                bool ok;
126                p_t::realDst(cp, ps) = TT::strToT(vals[0], ok);
127                return ok;
128        }
129
130        virtual QString getDesc() const {
131                if (!hasDef) return p_t::desc;
132                bool ok;
133                QString x = TT::TToStr(p_t::dst, ok);
134                if (!ok) return p_t::desc;
135                return p_t::desc + " (default " + x + ")";
136        }
137};
138
139struct IntTM: public SomeSetterTM<int> {
140        static int strToT(const char * val, bool & ok) {
141                return QString(val).toInt(&ok);
142        }
143        static QString TToStr(const int & t, bool & ok) {
144                ok=(t!=-1);
145                return QString::number(t);
146        }
147};
148
149/*!
150  Argument handler setting an int variable
151*/
152typedef SomeSetter<IntTM> IntSetter;
153
154struct FloatTM: public SomeSetterTM<float> {
155        static float strToT(const char * val, bool & ok) {
156                return QString(val).toFloat(&ok);
157        }
158        static QString TToStr(const float & t, bool & ok) {
159                ok=(t!=-1);
160                return QString::number(t);
161        }
162};
163/*!
164  Argument handler setting an float variable
165*/
166typedef SomeSetter<FloatTM> FloatSetter;
167
168struct StrTM: public SomeSetterTM<const char *> {
169        static const char * strToT(const char * val, bool & ok) {
170                ok=true;
171                return val;
172        }
173        static QString TToStr(const char * t, bool & ok) {
174                ok = (t[0] != '\0');
175                return QString(t);
176        }
177};
178/*!
179  Argument handler setting a string variable
180*/
181typedef SomeSetter<StrTM> StrSetter;
182
183struct QStrTM: public SomeSetterTM<QString> {
184        static QString strToT(const char * val, bool & ok) {
185                ok=true;
186                return QString::fromLocal8Bit(val);
187        }
188        static QString TToStr(const QString & t, bool & ok) {
189                ok=!t.isEmpty();
190                return t;
191        }
192};
193/*!
194  Argument handler setting a string variable
195*/
196typedef SomeSetter<QStrTM> QStrSetter;
197
198
199/*!
200  Argument handler responsible for calling a function
201*/
202template <typename T> struct Caller: public ArgHandler {
203        Caller() {}
204        Caller(QString a1) {
205                argn.push_back(a1);
206        }
207        bool operator() (const char **vals, CommandLineParserBase & s, char * page) {
208                return T()(vals, s, page);
209        }
210};
211#endif //__ARGHANDLER_INL__
Note: See TracBrowser for help on using the repository browser.