22 #ifndef ASTSHIM_KEYMAP_H
23 #define ASTSHIM_KEYMAP_H
37 void throwKeyNotFound(std::string
const &key) {
40 std::ostringstream os;
41 os <<
"Key \"" << key <<
"\" not found or has an undefined value";
42 throw std::runtime_error(os.str());
93 explicit KeyMap(std::string
const &options =
"")
94 :
Object(reinterpret_cast<AstObject *>(astKeyMap(
"%s", options.c_str()))) {
107 std::shared_ptr<KeyMap>
copy()
const {
118 bool ret = static_cast<bool>(
119 astMapDefined(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str()));
125 std::string
key(
int ind)
const {
126 int const len =
size();
127 if ((ind < 0) || (ind >= len)) {
128 std::ostringstream os;
129 os <<
"ind = " << ind <<
" not in range [0, " << len - 1 <<
"]";
130 throw std::invalid_argument(os.str());
132 const char *rawKey = astMapKey(reinterpret_cast<AstKeyMap const *>(
getRawPtr()), ind);
134 return std::string(rawKey);
143 bool ret = static_cast<bool>(
144 astMapHasKey(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str()));
151 int len = astMapLength(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str());
158 int const size = astMapSize(reinterpret_cast<AstKeyMap const *>(
getRawPtr()));
164 double getD(std::string
const &
key,
int ind)
const {
166 if (!astMapGetElemD(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &retVal)) {
167 throwKeyNotFound(
key);
174 std::vector<double>
getD(std::string
const &
key)
const {
176 std::vector<double> retVec(
size);
179 astMapGet1D(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(),
size, &nret,
187 float getF(std::string
const &
key,
int ind)
const {
189 if (!astMapGetElemF(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &retVal)) {
190 throwKeyNotFound(
key);
197 std::vector<float>
getF(std::string
const &
key)
const {
199 std::vector<float> retVec(
size);
202 astMapGet1F(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(),
size, &nret,
210 int getI(std::string
const &
key,
int ind)
const {
212 if (!astMapGetElemI(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &retVal)) {
213 throwKeyNotFound(
key);
220 std::vector<int>
getI(std::string
const &
key)
const {
222 std::vector<int> retVec(
size);
225 astMapGet1I(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(),
size, &nret,
233 short int getS(std::string
const &
key,
int ind)
const {
235 if (!astMapGetElemS(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &retVal)) {
236 throwKeyNotFound(
key);
243 std::vector<short int>
getS(std::string
const &
key)
const {
245 std::vector<short int> retVec(
size);
248 astMapGet1S(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(),
size, &nret,
256 char unsigned getB(std::string
const &
key,
int ind)
const {
257 char unsigned retVal;
258 if (!astMapGetElemB(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &retVal)) {
259 throwKeyNotFound(
key);
266 std::vector<char unsigned>
getB(std::string
const &
key)
const {
268 std::vector<char unsigned> retVec(
size);
271 astMapGet1B(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(),
size, &nret,
279 std::string
getC(std::string
const &
key,
int ind)
const {
280 int const maxChar = 1 + astMapLenC(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str());
281 std::unique_ptr<char[]> cstr(
new char[maxChar]);
282 if (!astMapGetElemC(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), maxChar, ind,
284 throwKeyNotFound(
key);
287 return std::string(cstr.get());
291 std::vector<std::string>
getC(std::string
const &
key)
const {
293 std::vector<std::string> retVec;
296 int const eltLen = 1 + astMapLenC(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str());
297 std::unique_ptr<char[]> cstr(
new char[
size * eltLen]);
299 astMapGet1C(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), eltLen,
size, &nret,
301 for (
int i = 0; i <
size; ++i) {
302 retVec.push_back(std::string(cstr.get() + i * eltLen));
310 std::shared_ptr<Object>
getA(std::string
const &
key,
int ind)
const {
311 std::shared_ptr<Object> retVal;
313 if (!astMapGetElemA(reinterpret_cast<AstKeyMap const *>(
getRawPtr()),
key.c_str(), ind, &rawObj)) {
314 throwKeyNotFound(
key);
317 return Object::fromAstObject<Object>(rawObj,
true);
321 std::vector<std::shared_ptr<Object>>
getA(std::string
const &
key)
const {
323 std::vector<std::shared_ptr<Object>> retVec;
324 for (
int i = 0; i <
size; ++i) {
325 retVec.push_back(
getA(
key, i));
331 void putD(std::string
const &
key,
double value, std::string
const &comment =
"") {
332 astMapPut0D(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value, comment.c_str());
337 void putD(std::string
const &
key, std::vector<double>
const &vec, std::string
const &comment =
"") {
338 _assertVectorNotEmpty(
key, vec.size());
339 astMapPut1D(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), vec.size(), vec.data(),
345 void putF(std::string
const &
key,
float value, std::string
const &comment =
"") {
346 astMapPut0F(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value, comment.c_str());
351 void putF(std::string
const &
key, std::vector<float>
const &vec, std::string
const &comment =
"") {
352 _assertVectorNotEmpty(
key, vec.size());
353 astMapPut1F(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), vec.size(), vec.data(),
359 void putI(std::string
const &
key,
int value, std::string
const &comment =
"") {
360 astMapPut0I(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value, comment.c_str());
365 void putI(std::string
const &
key, std::vector<int>
const &vec, std::string
const &comment =
"") {
366 _assertVectorNotEmpty(
key, vec.size());
367 astMapPut1I(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), vec.size(), vec.data(),
373 void putS(std::string
const &
key,
short int value, std::string
const &comment =
"") {
374 astMapPut0S(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value, comment.c_str());
379 void putS(std::string
const &
key, std::vector<short int>
const &vec, std::string
const &comment =
"") {
380 _assertVectorNotEmpty(
key, vec.size());
381 astMapPut1S(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), vec.size(), vec.data(),
387 void putB(std::string
const &
key,
char unsigned value, std::string
const &comment =
"") {
388 astMapPut0B(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value, comment.c_str());
393 void putB(std::string
const &
key, std::vector<char unsigned>
const &vec,
394 std::string
const &comment =
"") {
395 _assertVectorNotEmpty(
key, vec.size());
396 astMapPut1B(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), vec.size(), vec.data(),
402 void putC(std::string
const &
key, std::string
const &value, std::string
const &comment =
"") {
403 astMapPut0C(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), value.c_str(), comment.c_str());
408 void putC(std::string
const &
key, std::vector<std::string>
const &vec, std::string
const &comment =
"") {
409 _assertVectorNotEmpty(
key, vec.size());
411 for (
int i = 0,
size = vec.size(); i <
size; ++i) {
421 void putA(std::string
const &
key,
Object const &obj, std::string
const &comment =
"") {
422 AstObject *rawCopy = reinterpret_cast<AstObject *>(astCopy(obj.
getRawPtr()));
423 astMapPut0A(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), rawCopy, comment.c_str());
428 void putA(std::string
const &
key, std::vector<std::shared_ptr<Object const>>
const &vec,
429 std::string
const &comment =
"") {
430 _assertVectorNotEmpty(
key, vec.size());
432 for (
int i = 0,
size = vec.size(); i <
size; ++i) {
448 void putU(std::string
const &
key, std::string
const &comment =
"") {
449 astMapPutU(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), comment.c_str());
455 int const i = _getAppendIndex(
key);
456 astMapPutElemD(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
462 int const i = _getAppendIndex(
key);
463 astMapPutElemF(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
469 int const i = _getAppendIndex(
key);
470 astMapPutElemI(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
476 int const i = _getAppendIndex(
key);
477 astMapPutElemS(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
482 void append(std::string
const &
key,
char unsigned value) {
483 int const i = _getAppendIndex(
key);
484 astMapPutElemB(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
489 void append(std::string
const &
key, std::string
const &value) {
490 int const i = _getAppendIndex(
key);
491 astMapPutElemC(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value.c_str());
497 int const i = _getAppendIndex(
key);
498 AstObject *rawCopy = reinterpret_cast<AstObject *>(astCopy(value.
getRawPtr()));
499 astMapPutElemA(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, rawCopy);
505 _assertReplaceIndexInRange(
key, i);
506 astMapPutElemD(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
512 _assertReplaceIndexInRange(
key, i);
513 astMapPutElemF(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
519 _assertReplaceIndexInRange(
key, i);
520 astMapPutElemI(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
525 void replace(std::string
const &
key,
int i,
short int value) {
526 _assertReplaceIndexInRange(
key, i);
527 astMapPutElemS(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
532 void replace(std::string
const &
key,
int i,
char unsigned value) {
533 _assertReplaceIndexInRange(
key, i);
534 astMapPutElemB(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value);
539 void replace(std::string
const &
key,
int i, std::string
const &value) {
540 _assertReplaceIndexInRange(
key, i);
541 astMapPutElemC(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, value.c_str());
547 _assertReplaceIndexInRange(
key, i);
548 AstObject *rawCopy = reinterpret_cast<AstObject *>(astCopy(value.
getRawPtr()));
549 astMapPutElemA(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str(), i, rawCopy);
559 astMapRemove(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str());
568 void rename(std::string
const &oldKey, std::string
const &newKey) {
569 astMapRename(reinterpret_cast<AstKeyMap *>(
getRawPtr()), oldKey.c_str(), newKey.c_str());
577 int retVal = astMapType(reinterpret_cast<AstKeyMap *>(
getRawPtr()),
key.c_str());
579 return static_cast<DataType>(retVal);
585 return std::static_pointer_cast<KeyMap>(copyImpl<KeyMap, AstKeyMap>());
591 explicit KeyMap(AstKeyMap *rawKeyMap) :
Object(reinterpret_cast<AstObject *>(rawKeyMap)) {
593 std::ostringstream os;
594 os <<
"this is a " <<
getClassName() <<
", which is not a KeyMap";
595 throw std::invalid_argument(os.str());
601 void _assertVectorNotEmpty(std::string
const &
key,
int size)
const {
603 std::ostringstream os;
604 os <<
"vector supplied for key \"" <<
key <<
"\" has zero elements";
605 throw std::invalid_argument(os.str());
610 void _assertReplaceIndexInRange(std::string
const &
key,
int i)
const {
612 if ((i < 0) || (i >= len)) {
613 std::ostringstream os;
614 os <<
"i = " << i <<
" not in range [0, " << len - 1 <<
"] for key \"" <<
key <<
"\"";
615 throw std::invalid_argument(os.str());
620 int _getAppendIndex(std::string
const &
key)
const {
623 std::ostringstream os;
624 os <<
"key \"" <<
key <<
"\" not found";
625 throw std::invalid_argument(os.str());