• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • misc
argsparser.cpp
Go to the documentation of this file.
1 // Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "argsparser.h"
19 
20 #include "../objects/object_imp.h"
21 #include "../objects/object_holder.h"
22 
23 #include <cassert>
24 #include <algorithm>
25 #include <kdebug.h>
26 
27 void ArgsParser::initialize( const struct spec* args, int n )
28 {
29  std::vector<spec> vect( args, args + n );
30  initialize( vect );
31 }
32 
33 ArgsParser::ArgsParser()
34 {
35 }
36 
37 ArgsParser::ArgsParser( const std::vector<spec>& args )
38 {
39  initialize( args );
40 }
41 
42 void ArgsParser::initialize( const std::vector<spec>& args )
43 {
44  margs = args;
45 }
46 
47 ArgsParser::ArgsParser( const spec* args, int n )
48 {
49  initialize( args, n );
50 }
51 
52 static bool hasimp( const ObjectCalcer& o, const ObjectImpType* imptype )
53 {
54  return o.imp()->inherits( imptype );
55 }
56 
57 static bool hasimp( const ObjectImp& o, const ObjectImpType* imptype )
58 {
59  return o.inherits( imptype );
60 }
61 
62 static bool isvalid( const ObjectImp& o )
63 {
64  return o.valid();
65 }
66 
67 static bool isvalid( const ObjectCalcer& o )
68 {
69  return o.imp()->valid();
70 }
71 
72 // we use a template method that is used for both Objects and Args to
73 // not have to write the same thing twice..
74 template <class Collection>
75 static int check( const Collection& c, const std::vector<ArgsParser::spec>& margs )
76 {
77  std::vector<bool> found( margs.size() );
78 
79  for ( typename Collection::const_iterator o = c.begin(); o != c.end(); ++o )
80  {
81  for ( uint i = 0; i < margs.size(); ++i )
82  {
83  if ( hasimp( **o, margs[i].type ) && !found[i] )
84  {
85  // object o is of a type that we're looking for
86  found[i] = true;
87  goto matched;
88  };
89  };
90  return ArgsParser::Invalid;
91  matched:
92  ;
93  };
94  for( uint i = 0; i < margs.size(); ++i )
95  if ( !found[i] ) return ArgsParser::Valid;
96  return ArgsParser::Complete;
97 }
98 
99 int ArgsParser::check( const Args& os ) const
100 {
101  return ::check( os, margs );
102 }
103 
104 int ArgsParser::check( const std::vector<ObjectCalcer*>& os ) const
105 {
106  return ::check( os, margs );
107 }
108 
109 template <typename Collection>
110 static Collection parse( const Collection& os,
111  const std::vector<ArgsParser::spec> margs )
112 {
113  Collection ret( margs.size(), static_cast<typename Collection::value_type>( 0 ) );
114 
115  for ( typename Collection::const_iterator o = os.begin(); o != os.end(); ++o )
116  {
117  for( uint i = 0; i < margs.size(); ++i )
118  if ( hasimp( **o, margs[i].type ) && ret[i] == 0 )
119  {
120  // object o is of a type that we're looking for
121  ret[i] = *o;
122  goto added;
123  }
124  added:
125  ;
126  };
127  // remove 0's from the output..
128  ret.erase(
129  std::remove( ret.begin(), ret.end(),
130  static_cast<typename Collection::value_type>( 0 ) ),
131  ret.end() );
132  return ret;
133 }
134 
135 Args ArgsParser::parse( const Args& os ) const
136 {
137  return ::parse( os, margs );
138 }
139 
140 std::vector<ObjectCalcer*> ArgsParser::parse( const std::vector<ObjectCalcer*>& os ) const
141 {
142  return ::parse( os, margs );
143 }
144 
145 ArgsParser ArgsParser::without( const ObjectImpType* type ) const
146 {
147  std::vector<spec> ret;
148  ret.reserve( margs.size() - 1 );
149  for ( uint i = 0; i < margs.size(); ++i )
150  if ( margs[i].type != type )
151  ret.push_back( margs[i] );
152  return ArgsParser( ret );
153 }
154 
155 ArgsParser::spec ArgsParser::findSpec( const ObjectImp* obj, const Args& parents ) const
156 {
157  spec ret;
158  ret.type = 0;
159 
160  std::vector<bool> found( margs.size(), false );
161 
162  for ( Args::const_iterator o = parents.begin();
163  o != parents.end(); ++o )
164  {
165  for ( uint i = 0; i < margs.size(); ++i )
166  {
167  if ( (*o)->inherits( margs[i].type ) && !found[i] )
168  {
169  // object o is of a type that we're looking for
170  found[i] = true;
171  if ( *o == obj ) return margs[i];
172  // i know that "goto's are *evil*", but they're very useful
173  // and completely harmless if you use them as better "break;"
174  // statements.. trust me ;)
175  goto matched;
176  };
177  };
178  matched:
179  ;
180  };
181  kDebug() << "no proper spec found :(";
182  return ret;
183 }
184 
185 const ObjectImpType* ArgsParser::impRequirement(
186  const ObjectImp* o, const Args& parents ) const
187 {
188  spec s = findSpec( o, parents );
189  return s.type;
190 }
191 
192 std::string ArgsParser::usetext( const ObjectImp* obj, const Args& sel ) const
193 {
194  spec s = findSpec( obj, sel );
195  return s.usetext;
196 }
197 
198 template<typename Collection>
199 static bool checkArgs( const Collection& os, uint min, const std::vector<ArgsParser::spec>& argsspec )
200 {
201  assert( os.size() <= argsspec.size() );
202  if( os.size() < min ) return false;
203  uint checknum = os.size();
204  for ( uint i = 0; i < checknum; ++i )
205  {
206  if( !isvalid( *os[i] ) ) return false;
207  if( !hasimp( *os[i], argsspec[i].type ) ) return false;
208  }
209  return true;
210 }
211 
212 bool ArgsParser::checkArgs( const Args& os ) const
213 {
214  return checkArgs( os, margs.size() );
215 }
216 
217 bool ArgsParser::checkArgs( const Args& os, uint min ) const
218 {
219  return ::checkArgs( os, min, margs );
220 }
221 
222 bool ArgsParser::checkArgs( const std::vector<ObjectCalcer*>& os ) const
223 {
224  return checkArgs( os, margs.size() );
225 }
226 
227 bool ArgsParser::checkArgs( const std::vector<ObjectCalcer*>& os, uint minobjects ) const
228 {
229  return ::checkArgs( os, minobjects, margs );
230 }
231 
232 ArgsParser::~ArgsParser()
233 {
234 }
235 
236 bool ArgsParser::isDefinedOnOrThrough( const ObjectImp* o, const Args& parents ) const
237 {
238  spec s = findSpec( o, parents );
239  return s.onOrThrough;
240 }
241 
242 std::string ArgsParser::selectStatement( const Args& selection ) const
243 {
244  std::vector<bool> found( margs.size(), false );
245 
246  for ( Args::const_iterator o = selection.begin();
247  o != selection.end(); ++o )
248  {
249  for ( uint i = 0; i < margs.size(); ++i )
250  {
251  if ( (*o)->inherits( margs[i].type ) && !found[i] )
252  {
253  // object o is of a type that we're looking for
254  found[i] = true;
255  break;
256  }
257  }
258  }
259  for ( uint i = 0; i < margs.size(); ++i )
260  {
261  if ( !found[i] )
262  return margs[i].selectstat;
263  }
264  kDebug() << "no proper select statement found :(";
265  return "";
266 }
267 
checkArgs
static bool checkArgs(const Collection &os, uint min, const std::vector< ArgsParser::spec > &argsspec)
Definition: argsparser.cpp:199
ObjectImpType
Instances of this class represent a certain ObjectImp type.
Definition: object_imp.h:95
ObjectImp::inherits
bool inherits(const ObjectImpType *t) const
Returns true if this ObjectImp inherits the ObjectImp type represented by t.
Definition: object_imp.cc:279
ArgsParser::~ArgsParser
~ArgsParser()
Definition: argsparser.cpp:232
ArgsParser::ArgsParser
ArgsParser()
Definition: argsparser.cpp:33
ArgsParser::selectStatement
std::string selectStatement(const Args &sel) const
returns the select statement for the next selectable argument when the given args are selected...
Definition: argsparser.cpp:242
check
static int check(const Collection &c, const std::vector< ArgsParser::spec > &margs)
Definition: argsparser.cpp:75
ArgsParser::Complete
Definition: argsparser.h:112
ArgsParser::spec::type
const ObjectImpType * type
Definition: argsparser.h:113
ArgsParser::initialize
void initialize(const std::vector< spec > &args)
Definition: argsparser.cpp:42
ArgsParser::spec::usetext
std::string usetext
Definition: argsparser.h:113
ArgsParser::without
ArgsParser without(const ObjectImpType *type) const
returns a new ArgsParser that wants the same args, except for the ones of the given type...
Definition: argsparser.cpp:145
ObjectCalcer
An ObjectCalcer is an object that represents an algorithm for calculating an ObjectImp from other Obj...
Definition: object_calcer.h:66
Args
std::vector< const ObjectImp * > Args
Definition: objects/common.h:47
parse
static Collection parse(const Collection &os, const std::vector< ArgsParser::spec > margs)
Definition: argsparser.cpp:110
isvalid
static bool isvalid(const ObjectImp &o)
Definition: argsparser.cpp:62
ArgsParser::Invalid
Definition: argsparser.h:112
ArgsParser::checkArgs
bool checkArgs(const std::vector< ObjectCalcer * > &os) const
Definition: argsparser.cpp:222
ArgsParser::spec
Definition: argsparser.h:113
ArgsParser
This class is meant to take care of checking the types of the parents to ObjectCalcer's, and to put them in the correct order.
Definition: argsparser.h:106
ArgsParser::isDefinedOnOrThrough
bool isDefinedOnOrThrough(const ObjectImp *o, const Args &parents) const
Supposing that parents would be given as parents, this function returns whether the returned ObjectIm...
Definition: argsparser.cpp:236
ArgsParser::impRequirement
const ObjectImpType * impRequirement(const ObjectImp *o, const Args &parents) const
returns the minimal ObjectImp ID that o needs to inherit in order to be useful.
Definition: argsparser.cpp:185
hasimp
static bool hasimp(const ObjectCalcer &o, const ObjectImpType *imptype)
Definition: argsparser.cpp:52
ObjectCalcer::imp
virtual const ObjectImp * imp() const =0
Returns the ObjectImp of this ObjectCalcer.
ArgsParser::usetext
std::string usetext(const ObjectImp *o, const Args &sel) const
returns the usetext for the argument that o would be used for, if sel were used as parents...
Definition: argsparser.cpp:192
ArgsParser::check
int check(const Args &os) const
Definition: argsparser.cpp:99
ObjectImp
The ObjectImp class represents the behaviour of an object after it is calculated. ...
Definition: object_imp.h:226
ObjectImp::valid
bool valid() const
Returns true if this is a valid ObjectImp.
Definition: object_imp.cc:41
argsparser.h
ArgsParser::Valid
Definition: argsparser.h:112
ArgsParser::spec::onOrThrough
bool onOrThrough
Definition: argsparser.h:113
uint
unsigned int uint
Definition: object_imp.h:87
ArgsParser::parse
Args parse(const Args &os) const
Definition: argsparser.cpp:135
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal