Add support for save/loading std::strings.

This commit is contained in:
Jonathan G Rennison
2017-03-29 19:41:06 +01:00
parent f58fe00294
commit dd0666c5f4
4 changed files with 99 additions and 10 deletions

View File

@@ -1078,6 +1078,18 @@ static inline size_t SlCalcNetStringLen(const char *ptr, size_t length)
return min(strlen(ptr), length - 1);
}
/**
* Calculate the gross length of the std::string that it
* will occupy in the savegame. This includes the real length,
* and the length that the index will occupy.
* @param str reference to the std::string
* @return return the gross length of the string
*/
static inline size_t SlCalcStdStrLen(const std::string &str)
{
return str.size() + SlGetArrayLength(str.size()); // also include the length of the index
}
/**
* Calculate the gross length of the string that it
* will occupy in the savegame. This includes the real length, returned
@@ -1189,6 +1201,41 @@ static void SlString(void *ptr, size_t length, VarType conv)
}
}
/**
* Save/Load a std::string.
* @param ptr the std::string being manipulated
* @param conv must be SLE_FILE_STRING
*/
static void SlStdString(std::string &str, VarType conv)
{
switch (_sl.action) {
case SLA_SAVE: {
SlWriteArrayLength(str.size());
SlCopyBytes(const_cast<char *>(str.data()), str.size());
break;
}
case SLA_LOAD_CHECK:
case SLA_LOAD: {
size_t len = SlReadArrayLength();
str.resize(len);
SlCopyBytes(const_cast<char *>(str.c_str()), len);
StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK;
if ((conv & SLF_ALLOW_CONTROL) != 0) {
settings = settings | SVS_ALLOW_CONTROL_CODE;
}
if ((conv & SLF_ALLOW_NEWLINE) != 0) {
settings = settings | SVS_ALLOW_NEWLINE;
}
str_validate(str, settings);
break;
}
case SLA_PTRS: break;
case SLA_NULL: break;
default: NOT_REACHED();
}
}
/**
* Return the size in bytes of a certain type of atomic array
* @param length The length of the array counted in elements
@@ -1491,6 +1538,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
case SL_LST:
case SL_DEQ:
case SL_VEC:
case SL_STDSTR:
/* CONDITIONAL saveload types depend on the savegame version */
if (!SlIsObjectValidInSavegame(sld)) break;
@@ -1502,6 +1550,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
case SL_LST: return SlCalcListLen<std::list<void *>>(GetVariableAddress(object, sld));
case SL_DEQ: return SlCalcListLen<std::deque<void *>>(GetVariableAddress(object, sld));
case SL_VEC: return SlCalcListLen<std::vector<void *>>(GetVariableAddress(object, sld));
case SL_STDSTR: return SlCalcStdStrLen(*static_cast<std::string *>(GetVariableAddress(object, sld)));
default: NOT_REACHED();
}
break;
@@ -1550,6 +1599,9 @@ static bool IsVariableSizeRight(const SaveLoad *sld)
/* These should be pointer sized, or fixed array. */
return sld->size == sizeof(void *) || sld->size == sld->length;
case SL_STDSTR:
return sld->size == sizeof(std::string);
default:
return true;
}
@@ -1572,6 +1624,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
case SL_LST:
case SL_DEQ:
case SL_VEC:
case SL_STDSTR:
/* CONDITIONAL saveload types depend on the savegame version */
if (!SlIsObjectValidInSavegame(sld)) return false;
if (SlSkipVariableOnLoad(sld)) return false;
@@ -1601,6 +1654,7 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
case SL_LST: SlList<std::list<void *>>(ptr, (SLRefType)conv); break;
case SL_DEQ: SlList<std::deque<void *>>(ptr, (SLRefType)conv); break;
case SL_VEC: SlList<std::vector<void *>>(ptr, (SLRefType)conv); break;
case SL_STDSTR: SlStdString(*static_cast<std::string *>(ptr), sld->conv); break;
default: NOT_REACHED();
}
break;