Example #1
0
    void validate_type(symbol const& name, char const* value, param_descrs const& d) {
        param_kind k = d.get_kind(name);
        std::stringstream strm;
        char const* _value = value;
        switch (k) {
        case CPK_UINT:
            for (; *value; ++value) {
                if (!('0' <= *value && *value <= '9')) {
                    strm << "Expected values for parameter " << name 
                         << " is an unsigned integer. It was given argument '" << _value << "'";
                    throw default_exception(strm.str());                    
                }
            }
            break;
        case CPK_DOUBLE:
            for (; *value; ++value) {
                if (!('0' <= *value && *value <= '9') && *value != '.' && *value != '-' && *value != '/') {
                    strm << "Expected values for parameter " << name 
                         << " is a double. It was given argument '" << _value << "'";
                    throw default_exception(strm.str());                                        
                }
            }
            break;

        case CPK_BOOL:
            if (strcmp(value, "true") != 0 && strcmp(value, "false") != 0) {
                strm << "Expected values for parameter " << name 
                     << " are 'true' or 'false'. It was given argument '" << value << "'";
                throw default_exception(strm.str());
            }
            break;
        default:
            break;
        }
    }
Example #2
0
 void set(param_descrs const & d, symbol const & param_name, char const * value, symbol const & mod_name) {
     param_kind k = d.get_kind(param_name);
     params_ref & ps = get_params(mod_name);
     if (k == CPK_INVALID) {
         throw_unknown_parameter(param_name, d, mod_name);
     }
     else if (k == CPK_UINT) {
         long val = strtol(value, nullptr, 10);
         ps.set_uint(param_name, static_cast<unsigned>(val));
     }
     else if (k == CPK_DOUBLE) {
         char * aux;
         double val = strtod(value, &aux);
         ps.set_double(param_name, val);
     }
     else if (k == CPK_BOOL) {
         if (strcmp(value, "true") == 0) {
             ps.set_bool(param_name, true);
         }
         else if (strcmp(value, "false") == 0) {
             ps.set_bool(param_name, false);
         }
         else {
             std::stringstream strm;
             strm << "invalid value '" << value << "' for Boolean parameter '" << param_name << "'";                
             if (mod_name == symbol::null) {
                 strm << " at module '" << mod_name << "'";
             }
             throw default_exception(strm.str());
         }
     }
     else if (k == CPK_SYMBOL) {
         ps.set_sym(param_name, symbol(value));
     }
     else if (k == CPK_STRING) {
         // There is no guarantee that (external) callers will not delete value after invoking gparams::set.
         // I see two solutions:
         //    1) Modify params_ref to create a copy of set_str parameters.
         //       This solution is not nice since we create copies and move the params_ref around.
         //       We would have to keep copying the strings.
         //       Moreover, when we use params_ref internally, the value is usually a static value. 
         //       So, we would be paying this price for nothing.
         //    2) "Copy" value by transforming it into a symbol. 
         //       I'm using this solution for now.
         ps.set_str(param_name, symbol(value).bare_str());
     }
     else {
         std::stringstream strm;
         strm << "unsupported parameter type '" << param_name << "'";            
         if (mod_name == symbol::null) {
             strm << " at module '" << mod_name << "'";            
         }
         throw exception(strm.str());
     }
 }
Example #3
0
 void validate(param_descrs const & p) const {
     svector<params::entry>::const_iterator it  = m_entries.begin();  
     svector<params::entry>::const_iterator end = m_entries.end();
     for (; it != end; ++it) {                                
         param_kind expected = p.get_kind(it->first);
         if (expected == CPK_INVALID)
             throw default_exception("unknown parameter '%s'", it->first.str().c_str());
         if (it->second.m_kind != expected) 
             throw default_exception("parameter kind mismatch '%s'", it->first.str().c_str());
     }
 }