Skip to content

Commit

Permalink
Pass the TIVarFile instance as a context param of conversion functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed May 25, 2024
1 parent e8d2167 commit f04cdbd
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 46 deletions.
Binary file modified TIVarsLib.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions src/TIVarFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ namespace tivars
void TIVarFile::setContentFromString(const std::string& str, const options_t& options, uint16_t entryIdx)
{
auto& entry = this->entries[entryIdx];
entry.data = (entry._type.getHandlers().first)(str, options);
entry.data = (entry._type.getHandlers().first)(str, options, this);
this->refreshMetadataFields();
}
void TIVarFile::setContentFromString(const std::string& str, const options_t& options)
Expand Down Expand Up @@ -341,7 +341,7 @@ namespace tivars
std::string TIVarFile::getReadableContent(const options_t& options, uint16_t entryIdx)
{
const auto& entry = this->entries[entryIdx];
return (entry._type.getHandlers().second)(entry.data, options);
return (entry._type.getHandlers().second)(entry.data, options, this);
}
std::string TIVarFile::getReadableContent(const options_t& options)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TIVarTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace tivars

// Wrap the makeDataFromStr function by one that adds the type/subtype in the options
// Ideally, the handlers would parse the string and select the correct handler to dispatch...
#define GenericHandlerPair(which, type) make_pair([](const std::string& str, const options_t& options) -> data_t { \
#define GenericHandlerPair(which, type) make_pair([](const std::string& str, const options_t& options, const TIVarFile* _ctx) -> data_t { \
options_t options_withType = options; \
options_withType["_type"] = type; \
return (TH_Generic##which::makeDataFromString)(str, options_withType); \
return (TH_Generic##which::makeDataFromString)(str, options_withType, _ctx); \
}, &TH_Generic##which::makeStringFromData)

/**
Expand Down
7 changes: 4 additions & 3 deletions src/TypeHandlers/DummyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@

namespace tivars
{

data_t DummyHandler::makeDataFromString(const std::string& str, const options_t& options)
data_t DummyHandler::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)str;
(void)options;
(void)_ctx;
throw std::runtime_error("This type is not supported / implemented (yet?)");
}

std::string DummyHandler::makeStringFromData(const data_t& data, const options_t& options)
std::string DummyHandler::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)data;
(void)options;
(void)_ctx;
throw std::runtime_error("This type is not supported / implemented (yet?)");
}

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_DataAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace tivars
{
data_t STH_DataAppVar::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_DataAppVar::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -37,7 +37,7 @@ namespace tivars
return data;
}

std::string STH_DataAppVar::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_DataAppVar::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_ExactFraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace tivars
{

data_t STH_ExactFraction::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_ExactFraction::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -25,7 +25,7 @@ namespace tivars
}
}

std::string STH_ExactFraction::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_ExactFraction::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
if (data.size() != dataByteCount)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_ExactFractionPi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace tivars
{

data_t STH_ExactFractionPi::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_ExactFractionPi::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -25,7 +25,7 @@ namespace tivars
}
}

std::string STH_ExactFractionPi::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_ExactFractionPi::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
if (data.size() != dataByteCount)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_ExactPi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace tivars
{

data_t STH_ExactPi::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_ExactPi::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -25,7 +25,7 @@ namespace tivars
}
}

std::string STH_ExactPi::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_ExactPi::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
if (data.size() != dataByteCount)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_ExactRadical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace tivars
{

data_t STH_ExactRadical::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_ExactRadical::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -28,7 +28,7 @@ namespace tivars
}

// TODO: handle sign bit?
std::string STH_ExactRadical::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_ExactRadical::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_FP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static bool parseSign(std::string::const_iterator &i, const std::string::const_i

namespace tivars
{
data_t STH_FP::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_FP::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down Expand Up @@ -102,7 +102,7 @@ namespace tivars
return data;
}

std::string STH_FP::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_FP::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
bool scientific = false;
(void)options;
Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/STH_PythonAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace tivars
{
data_t STH_PythonAppVar::makeDataFromString(const std::string& str, const options_t& options)
data_t STH_PythonAppVar::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand All @@ -37,7 +37,7 @@ namespace tivars
return data;
}

std::string STH_PythonAppVar::makeStringFromData(const data_t& data, const options_t& options)
std::string STH_PythonAppVar::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/TH_GDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ namespace

namespace tivars
{
data_t TH_GDB::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_GDB::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down Expand Up @@ -463,7 +463,7 @@ namespace tivars
return data;
}

std::string TH_GDB::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_GDB::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
const size_t dataSizeActual = data.size();
if (dataSizeActual < dataByteCountMinimum)
Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/TH_GenericAppVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace tivars
{
data_t TH_GenericAppVar::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_GenericAppVar::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
return STH_DataAppVar::makeDataFromString(str, options);
}

std::string TH_GenericAppVar::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_GenericAppVar::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
if (data.size() < 2)
{
Expand Down
10 changes: 5 additions & 5 deletions src/TypeHandlers/TH_GenericComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace tivars

// For this, we're going to assume that both members are of the same type...
// TODO: guess, by parsing, the type instead of reading it from the options
data_t TH_GenericComplex::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_GenericComplex::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
constexpr size_t bytesPerMember = 9;
const auto& typeIter = options.find("_type");
Expand Down Expand Up @@ -81,7 +81,7 @@ namespace tivars
coeff = "0";
}

const data_t& member_data = handler(coeff, options);
const data_t& member_data = handler(coeff, options, _ctx);
data.insert(data.end(), member_data.begin(), member_data.end());

uint8_t flags = 0;
Expand All @@ -95,7 +95,7 @@ namespace tivars
return data;
}

std::string TH_GenericComplex::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_GenericComplex::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
constexpr size_t bytesPerMember = 9;

Expand Down Expand Up @@ -123,8 +123,8 @@ namespace tivars
const auto& handlerI = handlerIIter->second.second;

const data_t::const_iterator mid = data.cbegin() + bytesPerMember;
const std::string coeffR = handlerR(data_t(data.cbegin(), mid), options);
const std::string coeffI = handlerI(data_t(mid, data.cend()), options);
const std::string coeffR = handlerR(data_t(data.cbegin(), mid), options, _ctx);
const std::string coeffI = handlerI(data_t(mid, data.cend()), options, _ctx);

const bool coeffRZero = coeffR == "0";
const bool coeffIZero = coeffI == "0";
Expand Down
8 changes: 4 additions & 4 deletions src/TypeHandlers/TH_GenericList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace tivars
{
// TODO: also make it detect the type correctly...
data_t TH_GenericList::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_GenericList::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
const auto& typeIter = options.find("_type");
if (typeIter == options.end())
Expand Down Expand Up @@ -49,14 +49,14 @@ namespace tivars

for (const auto& numStr : arr)
{
const auto& tmp = handler(numStr, options);
const auto& tmp = handler(numStr, options, _ctx);
data.insert(data.end(), tmp.begin(), tmp.end());
}

return data;
}

std::string TH_GenericList::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_GenericList::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
const size_t byteCount = data.size();
if (byteCount < 2)
Expand Down Expand Up @@ -86,7 +86,7 @@ namespace tivars
std::string str = "{";
for (size_t i = 2, num = 0; i < byteCount; i += typeByteCount, num++)
{
str += handler(data_t(data.begin()+i, data.begin()+i+typeByteCount), options);
str += handler(data_t(data.begin()+i, data.begin()+i+typeByteCount), options, _ctx);
if (num < numCount - 1) // not last num
{
str += ',';
Expand Down
8 changes: 4 additions & 4 deletions src/TypeHandlers/TH_GenericReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace tivars
};

// TODO: guess, by parsing, the type instead of reading it from the options
data_t TH_GenericReal::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_GenericReal::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
const auto& typeIter = options.find("_type");
if (typeIter == options.end())
Expand All @@ -34,10 +34,10 @@ namespace tivars
{
throw std::runtime_error("Unknown/Invalid type for this TH_GenericReal: " + std::to_string(type));
}
return (handlerIter->second.first)(str, options);
return (handlerIter->second.first)(str, options, _ctx);
}

std::string TH_GenericReal::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_GenericReal::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
if (data.size() != dataByteCount)
{
Expand All @@ -49,6 +49,6 @@ namespace tivars
{
throw std::runtime_error("Unknown/Invalid type in this TH_GenericReal data: " + std::to_string(type));
}
return (handlerIter->second.second)(data, options);
return (handlerIter->second.second)(data, options, _ctx);
}
}
4 changes: 2 additions & 2 deletions src/TypeHandlers/TH_Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace tivars
{

data_t TH_Matrix::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_Matrix::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down Expand Up @@ -73,7 +73,7 @@ namespace tivars
return data;
}

std::string TH_Matrix::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_Matrix::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)options; // TODO: prettified option

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/TH_TempEqu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

namespace tivars
{
data_t TH_TempEqu::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_TempEqu::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
(void)str;
(void)options;

throw std::runtime_error("Unimplemented");
}

std::string TH_TempEqu::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_TempEqu::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
(void)options;

Expand Down
4 changes: 2 additions & 2 deletions src/TypeHandlers/TH_Tokenized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace tivars
const std::regex toPrettifyRX(R"(\[?\|([a-zA-Z]+)\]?)");
}

data_t TH_Tokenized::makeDataFromString(const std::string& str, const options_t& options)
data_t TH_Tokenized::makeDataFromString(const std::string& str, const options_t& options, const TIVarFile* _ctx)
{
data_t data;

Expand Down Expand Up @@ -109,7 +109,7 @@ namespace tivars
return data;
}

std::string TH_Tokenized::makeStringFromData(const data_t& data, const options_t& options)
std::string TH_Tokenized::makeStringFromData(const data_t& data, const options_t& options, const TIVarFile* _ctx)
{
const size_t dataSize = data.size();

Expand Down
5 changes: 3 additions & 2 deletions src/TypeHandlers/TypeHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

namespace tivars
{
class TIVarFile;

#define th() data_t makeDataFromString(const std::string& str, const options_t& options = options_t()); \
std::string makeStringFromData(const data_t& data, const options_t& options = options_t())
#define th() data_t makeDataFromString(const std::string& str, const options_t& options = options_t(), const TIVarFile* _ctx = nullptr); \
std::string makeStringFromData(const data_t& data, const options_t& options = options_t(), const TIVarFile* _ctx = nullptr);

namespace DummyHandler { th(); }

Expand Down

0 comments on commit f04cdbd

Please sign in to comment.