H3D API  2.4.1
TypedFieldCheck.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
28 //
30 #ifndef __TYPEDFIELDCHECK_H__
31 #define __TYPEDFIELDCHECK_H__
32 
33 #include <string>
34 #include <H3D/Field.h>
35 #include <H3DUtil/Exception.h>
37 #include <H3D/TypedFieldAnyTmpl.h>
38 
39 namespace H3D {
46 
50  namespace TypedFieldTypeCheck {
51 
58 
71  template< class Type >
72  struct AnyNumber {
77  static void checkTemplateArguments() {
78  TypesType< Type >::checkTemplateArguments();
79  }
80 
81 H3D_PUSH_WARNINGS()
82 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
89  static void checkType( Field *f, int index ) {
90  TypesType< Type >::checkType( f );
91  }
92  };
93 H3D_POP_WARNINGS()
94 
95 
99  template<H3D_TYPES_TEMPLATE_LIST>
100  struct AnyNumber< H3D_TYPES_CLASS > {
104  static void checkTemplateArguments() {
105  H3D_TYPES_CLASS::checkTemplateArguments();
106  }
107 
114  static void checkType( Field *f, int index ) {
115  int nr_types = H3D_TYPES_CLASS::numberTypes();
116  H3D_TYPES_CLASS::checkType( f, index % nr_types );
117  }
118  };
119 
126  template< class Type >
127  struct AnyNumber< AnyNumber< Type > > {
133  static void checkTemplateArguments() {
134  string s="No AnyNumber statements allowed in AnyNumber statement.";
135  throw InvalidTemplateArguments( s );
136  }
142  static void checkType( Field *f, int index ) {
143  string s="No AnyNumber statements allowed in AnyNumber statement.";
144  throw InvalidTemplateArguments( s );
145  }
146  };
147 
153  template< class Type >
154  struct AnyType {
157  // It is correct in this case since it is a normal type
158  // (all illegal types are handled as template specialisations ).
159  static void checkTemplateArguments() {}
160 
163  static bool checkType( Field *f ) {
164  return dynamic_cast< Type * >( f ) != NULL;
165  }
166  };
167 
174  template<H3D_ANY_TEMPLATE_LIST>
175  struct AnyType< H3D_ANY_CLASS > {
181  static void checkTemplateArguments() {
182  string s = "No Any statements allowed in Any statement.";
183  throw InvalidTemplateArguments( s );
184  }
185 
191  static bool checkType( Field *f ) {
192  string s = "No Any statements allowed in Any statement.";
193  throw InvalidTemplateArguments( s );
194  }
195  };
196 
203  template<H3D_TYPES_TEMPLATE_LIST>
204  struct AnyType< H3D_TYPES_CLASS > {
205 
211  static void checkTemplateArguments() {
212  string s = "No Types statements allowed in Any statement.";
213  throw InvalidTemplateArguments( s );
214  }
215 
221  static bool checkType( Field *f ) {
222  string s = "No Types statements allowed in Any statement.";
223  throw InvalidTemplateArguments( s );
224  }
225  };
226 
233  template< class Type >
234  struct AnyType< AnyNumber< Type > > {
235 
241  static void checkTemplateArguments() {
242  string s = "No AnyNumber statements allowed in Any statement.";
243  throw InvalidTemplateArguments( s );
244  }
245 
251  static bool checkType( Field *f ) {
252  string s = "No AnyNumber statements allowed in Any statement.";
253  throw InvalidTemplateArguments( s );
254  }
255  };
256 
262  template< class Type >
263  struct TypesType {
266  // It is correct in this case since it is a normal types
267  // (all illegal types are handled as template specialisations ).
268  static void checkTemplateArguments() {}
269 
272  static void checkType( Field *f ) {
273  if( !dynamic_cast< Type * >( f ) ) {
274  throw InvalidType( f->getTypeName(),
275  Type::classTypeName() );
276  }
277  }
278  };
279 
284  template< H3D_ANY_TEMPLATE_LIST >
285  struct TypesType< H3D_ANY_CLASS > {
288  static void checkTemplateArguments() {
289  H3D_ANY_CLASS::checkTemplateArguments();
290  }
291 
294  static void checkType( Field *f ) {
295  H3D_ANY_CLASS::checkType( f );
296  }
297  };
298 
305  template< H3D_TYPES_TEMPLATE_LIST >
306  struct TypesType< H3D_TYPES_CLASS > {
312  static void checkTemplateArguments() {
313  string s = "No Types statements allowed in another Types statement.";
314  throw InvalidTemplateArguments( s );
315  }
316 
322  static void checkType( Field *f ) {
323  string s = "No Types statements allowed in another Types statement.";
324  throw InvalidTemplateArguments( s );
325  }
326  };
327 
335  template< class Type >
336  struct TypesType< AnyNumber< Type > > {
337 
343  static void checkTemplateArguments() {
344  string s ="No AnyNumber statements allowed in Types statement.";
345  throw InvalidTemplateArguments( s );
346  }
347 
353  static bool checkType( Field *f ) {
354  string s ="No AnyNumber statements allowed in Types statement.";
355  throw InvalidTemplateArguments( s );
356  }
357  };
358 
364  template< class RequiredArgType,
365  class OptionalArgTypes >
366  struct TypedFieldCheck {
370  static void checkTemplateArguments() {
371  TypesType<RequiredArgType>::checkTemplateArguments();
372  TypedFieldCheck< void,
373  OptionalArgTypes >::checkTemplateArguments();
374  }
375 
384  static void checkFieldType( Field *f,
385  int field_index,
386  int type_index ) {
387  if( field_index == type_index ) {
388  try {
389  TypesType<RequiredArgType>::checkType( f );
390  } catch( ... ) {
391  stringstream s;
392  s << "Route " << field_index << " has type "
393  << f->getTypeName() <<". Expecting "
394  << RequiredArgType::classTypeName() << ".";
395  throw H3D::TypedFieldError( s.str() );
396  }
397  } else {
398  TypedFieldCheck< void,
399  OptionalArgTypes >::checkFieldType( f,
400  field_index,
401  type_index + 1 );
402  }
403  }
404  };
405 
411  template< H3D_TYPES_TEMPLATE_LIST, class OptionalArgTypes >
412  struct TypedFieldCheck< H3D_TYPES_CLASS, OptionalArgTypes >{
416  static void checkTemplateArguments() {
417  H3D_TYPES_CLASS::checkTemplateArguments();
418  TypedFieldCheck< void,
419  OptionalArgTypes >::checkTemplateArguments();
420  }
421 
430  static void checkFieldType( Field *f,
431  int field_index,
432  int type_index ) {
433  int nr_types = H3D_TYPES_CLASS::numberTypes();
434  if( field_index < type_index + nr_types ) {
435  try {
436  H3D_TYPES_CLASS::checkType( f,
437  field_index - type_index );
438  } catch( const InvalidType &e ) {
439  stringstream s;
440  s << "Route " << field_index << " has type "
441  << e.value <<". Expecting "
442  << e.message;
443  throw H3D::TypedFieldError( s.str() );
444  }
445  } else {
446  TypedFieldCheck< void,
447  OptionalArgTypes >::checkFieldType( f,
448  field_index,
449  type_index +
450  nr_types );
451  }
452  }
453  };
454 
461  template< class Type, class OptionalArgTypes >
462  struct TypedFieldCheck< AnyNumber< Type >, OptionalArgTypes >{
468  static void checkTemplateArguments() {
469  string s = "AnyNumber statements not allowed as RequiredArgTypes";
470  throw InvalidTemplateArguments( s );
471  }
472 
478  static void checkFieldType( Field *f,
479  int field_index,
480  int type_index ) {
481  string s = "AnyNumber statements not allowed as RequiredArgTypes";
482  throw InvalidTemplateArguments( s );
483  }
484  };
485 
490  template<>
491  struct TypedFieldCheck< void, void > {
495  static void checkTemplateArguments() {}
496 
497 H3D_PUSH_WARNINGS()
498 H3D_DISABLE_UNUSED_PARAMETER_WARNING()
507  static void checkFieldType( Field *f,
508  int field_index,
509  int type_index ) {
510  stringstream s;
511  s << "Too many routes. Expecing a maximum of "
512  << type_index << " routes.";
513  throw TypedFieldError( s.str() );
514  }
515  };
516 H3D_POP_WARNINGS()
517 
518 
523  template< class OptionalArgType >
524  struct TypedFieldCheck< void, OptionalArgType > {
528  static void checkTemplateArguments() {
529  TypesType< OptionalArgType >::checkTemplateArguments();
530  }
531 
540  static void checkFieldType( Field *f,
541  int field_index,
542  int type_index ) {
543  if( field_index == type_index ) {
544  try {
545  TypesType<OptionalArgType>::checkType( f );
546  } catch( ... ) {
547  stringstream s;
548  s << "Route " << field_index << " has type "
549  << f->getTypeName() <<". Expecting "
550  << OptionalArgType::classTypeName()
551  << ".";
552  throw H3D::TypedFieldError( s.str() );
553  }
554  } else {
555  stringstream s;
556  s << "Too many routes. Expecing a maximum of "
557  << type_index << " routes.";
558  throw H3D::TypedFieldError( s.str() );
559  }
560  }
561  };
562 
568  template< H3D_TYPES_TEMPLATE_LIST >
569  struct TypedFieldCheck< void, H3D_TYPES_CLASS >{
573  static void checkTemplateArguments() {
574  H3D_TYPES_CLASS::checkTemplateArguments();
575  }
576 
585  static void checkFieldType( Field *f,
586  int field_index,
587  int type_index ) {
588  int nr_types = H3D_TYPES_CLASS::numberTypes();
589  if( field_index < type_index + nr_types ) {
590  H3D_TYPES_CLASS::checkType( f,
591  field_index - type_index );
592  } else {
593  stringstream s;
594  s << "Too many routes. Expecting a maximum of "
595  << type_index << " routes.";
596  throw H3D::TypedFieldError( s.str() );
597  }
598  }
599  };
600 
606  template< class Type >
607  struct TypedFieldCheck< void, AnyNumber< Type > >{
611  static void checkTemplateArguments() {
613  }
614 
623  static void checkFieldType( Field *f,
624  int field_index,
625  int type_index ) {
626  try {
627  AnyNumber< Type >::checkType( f, field_index - type_index );
628  } catch( const InvalidType &e ) {
629  stringstream s;
630  s << "Route " << field_index << " has type "
631  << e.value <<". Expecting "
632  << e.message << ".";
633  throw H3D::TypedFieldError( s.str() );
634  }
635  }
636  };
637  }
638 
639 }
640 
641 #endif
Contains the Field class.
This file has been automatically generated from the Util/TypedFieldAnyTmpl.py.
This file has been automatically generated from the Util/TypedFieldTypesTmpl.py.
The Field class.
Definition: Field.h:46
An exception thrown when the field does not conform to the type specification from a TypedField.
Definition: TypedFieldCheck.h:45
InvalidTemplateArguments An exception thrown when the template argument list does not follow the rule...
Definition: TypedFieldCheck.h:57
H3D_API_EXCEPTION(Vec2dNormalizeError)
H3D API namespace.
Definition: Anchor.h:38
The AnyNumber template can be used in the OptionalArgTypes argument of a TypedField template instanti...
Definition: TypedFieldCheck.h:72
static void checkType(Field *f, int index)
Check that a field is of the correct type.
Definition: TypedFieldCheck.h:89
static void checkTemplateArguments()
Check that the template arguments are on the correct form, i.e.
Definition: TypedFieldCheck.h:77