summaryrefslogtreecommitdiffstats
path: root/kig/misc/argsparser.cpp
blob: c2387970fc01cd05c855dbd118aa63fee097ae63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (C)  2002  Dominique Devriese <devriese@kde.org>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.

#include "argsparser.h"

#include "../objects/object_imp.h"
#include "../objects/object_holder.h"

#include <cassert>
#include <algorithm>
#include <kdebug.h>

void ArgsParser::initialize( const struct spec* args, int n )
{
  std::vector<spec> vect( args, args + n );
  initialize( vect );
}

ArgsParser::ArgsParser()
{
}

ArgsParser::ArgsParser( const std::vector<spec>& args )
{
  initialize( args );
}

void ArgsParser::initialize( const std::vector<spec>& args )
{
  margs = args;
}

ArgsParser::ArgsParser( const spec* args, int n )
{
  initialize( args, n );
}

static bool hasimp( const ObjectCalcer& o, const ObjectImpType* imptype )
{
  return o.imp()->inherits( imptype );
}

static bool hasimp( const ObjectImp& o, const ObjectImpType* imptype )
{
  return o.inherits( imptype );
}

static bool isvalid( const ObjectImp& o )
{
  return o.valid();
}

static bool isvalid( const ObjectCalcer& o )
{
  return o.imp()->valid();
}

// we use a template method that is used for both Objects and Args to
// not have to write the same thing twice..
template <class Collection>
static int check( const Collection& c, const std::vector<ArgsParser::spec>& margs )
{
  std::vector<bool> found( margs.size() );

  for ( typename Collection::const_iterator o = c.begin(); o != c.end(); ++o )
  {
    for ( uint i = 0; i < margs.size(); ++i )
    {
      if ( hasimp( **o, margs[i].type ) && !found[i] )
      {
        // object o is of a type that we're looking for
        found[i] = true;
        goto matched;
      };
    };
    return ArgsParser::Invalid;
  matched:
    ;
  };
  for( uint i = 0; i < margs.size(); ++i )
    if ( !found[i] ) return ArgsParser::Valid;
  return ArgsParser::Complete;
}

int ArgsParser::check( const Args& os ) const
{
  return ::check( os, margs );
}

int ArgsParser::check( const std::vector<ObjectCalcer*>& os ) const
{
  return ::check( os, margs );
}

template <typename Collection>
static Collection parse( const Collection& os,
                         const std::vector<ArgsParser::spec> margs )
{
  Collection ret( margs.size(), static_cast<typename Collection::value_type>( 0 ) );

  for ( typename Collection::const_iterator o = os.begin(); o != os.end(); ++o )
  {
    for( uint i = 0; i < margs.size(); ++i )
      if ( hasimp( **o, margs[i].type ) && ret[i] == 0 )
      {
        // object o is of a type that we're looking for
        ret[i] = *o;
        goto added;
      }
  added:
    ;
  };
  // remove 0's from the output..
  ret.erase(
    std::remove( ret.begin(), ret.end(),
                 static_cast<typename Collection::value_type>( 0 ) ),
    ret.end() );
  return ret;
}

Args ArgsParser::parse( const Args& os ) const
{
  return ::parse( os, margs );
}

std::vector<ObjectCalcer*> ArgsParser::parse( const std::vector<ObjectCalcer*>& os ) const
{
  return ::parse( os, margs );
}

ArgsParser ArgsParser::without( const ObjectImpType* type ) const
{
  std::vector<spec> ret;
  ret.reserve( margs.size() - 1 );
  for ( uint i = 0; i < margs.size(); ++i )
    if ( margs[i].type != type )
      ret.push_back( margs[i] );
  return ArgsParser( ret );
}

ArgsParser::spec ArgsParser::findSpec( const ObjectImp* obj, const Args& parents ) const
{
  spec ret;
  ret.type = 0;

  std::vector<bool> found( margs.size(), false );

  for ( Args::const_iterator o = parents.begin();
        o != parents.end(); ++o )
  {
    for ( uint i = 0; i < margs.size(); ++i )
    {
      if ( (*o)->inherits( margs[i].type ) && !found[i] )
      {
        // object o is of a type that we're looking for
        found[i] = true;
        if ( *o == obj ) return margs[i];
        // i know that "goto's are *evil*", but they're very useful
        // and completely harmless if you use them as better "break;"
        // statements.. trust me ;)
        goto matched;
      };
    };
  matched:
    ;
  };
  kdDebug() << k_funcinfo << "no proper spec found :(" << endl;
  return ret;
}

const ObjectImpType* ArgsParser::impRequirement(
  const ObjectImp* o, const Args& parents ) const
{
  spec s = findSpec( o, parents );
  return s.type;
}

std::string ArgsParser::usetext( const ObjectImp* obj, const Args& sel ) const
{
  spec s = findSpec( obj, sel );
  return s.usetext;
}

template<typename Collection>
static bool checkArgs( const Collection& os, uint min, const std::vector<ArgsParser::spec>& argsspec )
{
  assert( os.size() <= argsspec.size() );
  if( os.size() < min ) return false;
  uint checknum = os.size();
  for ( uint i = 0; i < checknum; ++i )
  {
    if( !isvalid( *os[i] ) ) return false;
    if( !hasimp( *os[i], argsspec[i].type ) ) return false;
  }
  return true;
}

bool ArgsParser::checkArgs( const Args& os ) const
{
  return checkArgs( os, margs.size() );
}

bool ArgsParser::checkArgs( const Args& os, uint min ) const
{
  return ::checkArgs( os, min, margs );
}

bool ArgsParser::checkArgs( const std::vector<ObjectCalcer*>& os ) const
{
  return checkArgs( os, margs.size() );
}

bool ArgsParser::checkArgs( const std::vector<ObjectCalcer*>& os, uint minobjects ) const
{
  return ::checkArgs( os, minobjects, margs );
}

ArgsParser::~ArgsParser()
{
}

bool ArgsParser::isDefinedOnOrThrough( const ObjectImp* o, const Args& parents ) const
{
  spec s = findSpec( o, parents );
  return s.onOrThrough;
}

std::string ArgsParser::selectStatement( const Args& selection ) const
{
  std::vector<bool> found( margs.size(), false );

  for ( Args::const_iterator o = selection.begin();
        o != selection.end(); ++o )
  {
    for ( uint i = 0; i < margs.size(); ++i )
    {
      if ( (*o)->inherits( margs[i].type ) && !found[i] )
      {
        // object o is of a type that we're looking for
        found[i] = true;
        break;
      }
    }
  }
  for ( uint i = 0; i < margs.size(); ++i )
  {
    if ( !found[i] )
      return margs[i].selectstat;
  }
  kdDebug() << k_funcinfo << "no proper select statement found :(" << endl;
  return 0;
}