this repo has no description
at develop 5.2 kB view raw
1/* -*- mode: C++; c-basic-offOptions::set: 2; indent-tabs-mode: nil -*- */ 2 3/* 4 * Main authors: 5 * Guido Tack <guido.tack@monash.edu> 6 */ 7 8/* This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 11 12#include <minizinc/options.hh> 13 14#include <unordered_map> 15 16namespace MiniZinc { 17 18Expression* Options::getParam(const std::string& name) const { 19 std::unordered_map<std::string, KeepAlive>::const_iterator it = _options.find(name); 20 if (it == _options.end()) { 21 std::stringstream ss; 22 ss << "Could not find option: \"" << name << "\"." << std::endl; 23 throw InternalError(ss.str()); 24 } 25 return (it->second)(); 26} 27 28void Options::setIntParam(const std::string& name, KeepAlive ka) { 29 Expression* e = ka(); 30 if (e && e->type().ispar() && e->type().isint()) { 31 _options[name] = ka; 32 } else { 33 std::stringstream ss; 34 ss << "For option: " << name << " expected Par Int, received " << e->type().nonEnumToString() 35 << std::endl; 36 throw InternalError(ss.str()); 37 } 38} 39void Options::setFloatParam(const std::string& name, KeepAlive ka) { 40 Expression* e = ka(); 41 if (e && e->type().ispar() && e->type().isfloat()) { 42 _options[name] = ka; 43 } else { 44 std::stringstream ss; 45 ss << "For option: " << name << " expected Par Float, received " << e->type().nonEnumToString() 46 << std::endl; 47 throw InternalError(ss.str()); 48 } 49} 50void Options::setBoolParam(const std::string& name, KeepAlive ka) { 51 Expression* e = ka(); 52 if (e && e->type().ispar() && e->type().isbool()) { 53 _options[name] = ka; 54 } else { 55 std::stringstream ss; 56 ss << "For option: " << name << " expected Par Bool, received " << e->type().nonEnumToString() 57 << std::endl; 58 throw InternalError(ss.str()); 59 } 60} 61void Options::setStringParam(const std::string& name, KeepAlive ka) { 62 Expression* e = ka(); 63 if (e && e->type().ispar() && e->type().isstring()) { 64 _options[name] = ka; 65 } else { 66 std::stringstream ss; 67 ss << "For option: " << name << " expected Par String, received " << e->type().nonEnumToString() 68 << std::endl; 69 throw InternalError(ss.str()); 70 } 71} 72 73void Options::setIntParam(const std::string& name, long long int e) { 74 GCLock lock; 75 IntLit* il = IntLit::a(e); 76 KeepAlive ka(il); 77 78 setIntParam(name, ka); 79}; 80void Options::setFloatParam(const std::string& name, double e) { 81 GCLock lock; 82 FloatLit* fl = FloatLit::a(e); 83 KeepAlive ka(fl); 84 85 setFloatParam(name, ka); 86} 87void Options::setBoolParam(const std::string& name, bool e) { 88 KeepAlive ka(constants().boollit(e)); 89 setBoolParam(name, ka); 90} 91void Options::setStringParam(const std::string& name, std::string e) { 92 GCLock lock; 93 StringLit* sl = new StringLit(Location(), e); 94 KeepAlive ka(sl); 95 96 setStringParam(name, ka); 97} 98 99long long int Options::getIntParam(const std::string& name) const { 100 if (IntLit* il = getParam(name)->dyn_cast<IntLit>()) { 101 return il->v().toInt(); 102 } else { 103 std::stringstream ss; 104 ss << "Option: \"" << name << "\" is not Par Int" << std::endl; 105 throw InternalError(ss.str()); 106 } 107} 108long long int Options::getIntParam(const std::string& name, long long int def) const { 109 if (hasParam(name)) { 110 if (IntLit* il = getParam(name)->dyn_cast<IntLit>()) { 111 return il->v().toInt(); 112 } 113 } 114 return def; 115} 116double Options::getFloatParam(const std::string& name) const { 117 if (FloatLit* fl = getParam(name)->dyn_cast<FloatLit>()) { 118 return fl->v().toDouble(); 119 } else { 120 std::stringstream ss; 121 ss << "Option: \"" << name << "\" is not Par Float" << std::endl; 122 throw InternalError(ss.str()); 123 } 124} 125double Options::getFloatParam(const std::string& name, double def) const { 126 if (hasParam(name)) { 127 if (FloatLit* fl = getParam(name)->dyn_cast<FloatLit>()) { 128 return fl->v().toDouble(); 129 } 130 } 131 return def; 132} 133bool Options::getBoolParam(const std::string& name) const { 134 if (BoolLit* bl = getParam(name)->dyn_cast<BoolLit>()) { 135 return bl->v(); 136 } else { 137 std::stringstream ss; 138 ss << "Option: \"" << name << "\" is not Par Bool" << std::endl; 139 throw InternalError(ss.str()); 140 } 141} 142bool Options::getBoolParam(const std::string& name, bool def) const { 143 if (hasParam(name)) { 144 if (BoolLit* bl = getParam(name)->dyn_cast<BoolLit>()) { 145 return bl->v(); 146 } 147 } 148 return def; 149} 150std::string Options::getStringParam(const std::string& name) const { 151 if (StringLit* sl = getParam(name)->dyn_cast<StringLit>()) { 152 return sl->v().str(); 153 } else { 154 std::stringstream ss; 155 ss << "Option: \"" << name << "\" is not Par String" << std::endl; 156 throw InternalError(ss.str()); 157 } 158} 159std::string Options::getStringParam(const std::string& name, std::string def) const { 160 if (hasParam(name)) { 161 if (StringLit* sl = getParam(name)->dyn_cast<StringLit>()) { 162 return sl->v().str(); 163 } 164 } 165 return def; 166} 167bool Options::hasParam(const std::string& name) const { 168 return _options.find(name) != _options.end(); 169} 170std::ostream& Options::dump(std::ostream& os) { 171 for (auto& it : _options) os << it.first << ':' << it.second() << ' '; 172 return os; 173} 174} // namespace MiniZinc