source: libcfa/src/parseconfig.hfa@ a3f2a3e

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since a3f2a3e was a3f2a3e, checked in by Jacob Prud'homme <jafprudhomme@…>, 4 years ago

Simplified validation logic

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#pragma once
2
3#include <math.trait.hfa>
4
5
6// *********************************** initial declarations ***********************************
7
8
9enum config_format {
10 TABULAR_CONFIG
11};
12
13struct config_entry {
14 const char * key;
15 void * variable;
16 bool (*parse)( const char *, void * );
17 bool (*validate)( void * );
18};
19
20bool null_validator( void * ) { return true; }
21
22static inline void ?{}( config_entry & this ) {}
23
24forall(T & | { bool parse( const char *, T & ); })
25static inline void ?{}( config_entry & this, const char * key, T & variable ) {
26 this.key = key;
27 this.variable = (void *)&variable;
28 this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
29 this.validate = null_validation;
30}
31
32forall(T & | { bool parse( const char *, T & ); })
33static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*validate)(T &) ) {
34 this.key = key;
35 this.variable = (void *)&variable;
36 this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
37 this.validate = (bool (*)(void *))(bool (*)(T &))validate;
38}
39
40forall(T &)
41static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &) ) {
42 this.key = key;
43 this.variable = (void *)&variable;
44 this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
45 this.validate = null_validation;
46}
47
48forall(T &)
49static inline void ?{}( config_entry & this, const char * key, T & variable, bool (*parse)(const char *, T &), bool (*validate)(T &) ) {
50 this.key = key;
51 this.variable = (void *)&variable;
52 this.parse = (bool (*)(const char *, void *))(bool (*)(const char *, T &))parse;
53 this.validate = (bool (*)(void *))(bool (*)(T &))validate;
54}
55
56
57// *********************************** exceptions ***********************************
58
59
60exception Parse_Failure {};
61
62void ?{}( Parse_Failure & this );
63
64exception Validation_Failure {};
65
66void ?{}( Validation_Failure & this );
67
68
69// *********************************** main code ***********************************
70
71
72void parse_config( const char * config_file, config_entry entries[], size_t num_entries, config_format format );
73
74bool parse( const char *, const char * & );
75bool parse( const char *, int & );
76bool parse( const char *, unsigned & );
77bool parse( const char *, unsigned long & );
78bool parse( const char *, unsigned long long & );
79bool parse( const char *, float & );
80bool parse( const char *, double & );
81
82
83// *********************************** validation ***********************************
84
85
86forall(T | Relational( T ))
87bool is_nonnegative( T & );
88
89forall(T | Relational( T ))
90bool is_positive( T & );
91
92forall(T | Relational( T ))
93bool is_nonpositive( T & );
94
95forall(T | Relational( T ))
96bool is_negative( T & );
97
98
99// Local Variables: //
100// mode: c //
101// tab-width: 4 //
102// End: //
Note: See TracBrowser for help on using the repository browser.