Skip to content

Commit 195c76f

Browse files
committed
TypeHandlersTuple handlers
1 parent 4978123 commit 195c76f

25 files changed

Lines changed: 202 additions & 108 deletions

TIVarsLib.wasm

3.51 KB
Binary file not shown.

cli/cli.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
using namespace std;
1515
using namespace tivars;
16-
using TH_Tokenized::LANG_EN;
17-
using TH_Tokenized::LANG_FR;
16+
using namespace tivars::TypeHandlers;
1817

1918
enum FileType
2019
{
@@ -213,10 +212,10 @@ int main(int argc, char** argv)
213212
string langStr = result["lang"].as<string>();
214213
if (langStr == "en")
215214
{
216-
contentOptions["lang"] = LANG_EN;
215+
contentOptions["lang"] = TH_Tokenized::LANG_EN;
217216
} else if (langStr == "fr")
218217
{
219-
contentOptions["lang"] = LANG_FR;
218+
contentOptions["lang"] = TH_Tokenized::LANG_FR;
220219
} else
221220
{
222221
cout << langStr << " is not a valid language code" << endl;

src/TIVarFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ namespace tivars
313313
void TIVarFile::setContentFromString(const std::string& str, const options_t& options, uint16_t entryIdx)
314314
{
315315
auto& entry = this->entries[entryIdx];
316-
entry.data = (entry._type.getHandlers().first)(str, options, this);
316+
entry.data = std::get<0>(entry._type.getHandlers())(str, options, this);
317317
this->refreshMetadataFields();
318318
}
319319
void TIVarFile::setContentFromString(const std::string& str, const options_t& options)
@@ -386,7 +386,7 @@ namespace tivars
386386
std::string TIVarFile::getReadableContent(const options_t& options, uint16_t entryIdx)
387387
{
388388
const auto& entry = this->entries[entryIdx];
389-
return (entry._type.getHandlers().second)(entry.data, options, this);
389+
return std::get<1>(entry._type.getHandlers())(entry.data, options, this);
390390
}
391391
std::string TIVarFile::getReadableContent(const options_t& options)
392392
{

src/TIVarType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace tivars
4242
class_<tivars::TIVarType>("TIVarType")
4343
.constructor<>()
4444
.constructor<const char*>()
45-
.constructor<int, const std::string&, const std::vector<std::string>&, const tivars::handler_pair_t&>()
45+
.constructor<int, const std::string&, const std::vector<std::string>&, const tivars::TypeHandlers::TypeHandlersTuple&>()
4646

4747
.function("getId" , &tivars::TIVarType::getId)
4848
.function("getName" , &tivars::TIVarType::getName)

src/TIVarType.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace tivars
2727
static TIVarType createFromID(uint8_t id) { return TIVarType{id}; }
2828
static TIVarType createFromName(const std::string& name) { return TIVarType{name}; }
2929

30-
TIVarType(int id, const std::string& name, const std::vector<std::string>& exts, const TypeHandlers::handler_pair_t& handlers) : id(id), name(name), exts(exts), handlers(handlers)
30+
TIVarType(int id, const std::string& name, const std::vector<std::string>& exts, const TypeHandlers::TypeHandlersTuple& handlers)
31+
: id(id), name(name), exts(exts), handlers(handlers)
3132
{}
3233

3334
~TIVarType() = default;
@@ -36,13 +37,13 @@ namespace tivars
3637
int getId() const { return this->id; }
3738
std::string getName() const { return this->name; }
3839
const std::vector<std::string>& getExts() const { return this->exts; }
39-
const TypeHandlers::handler_pair_t& getHandlers() const { return this->handlers; };
40+
TypeHandlers::TypeHandlersTuple getHandlers() const { return this->handlers; };
4041

4142
private:
4243
int id = -1;
4344
std::string name = "Unknown";
4445
std::vector<std::string> exts;
45-
TypeHandlers::handler_pair_t handlers;
46+
TypeHandlers::TypeHandlersTuple handlers;
4647

4748
};
4849

src/TIVarTypes.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ namespace tivars
3535

3636
// Wrap the makeDataFromStr function by one that adds the type/subtype in the options
3737
// Ideally, the handlers would parse the string and select the correct handler to dispatch...
38-
#define GenericHandlerPair(which, type) make_pair([](const std::string& str, const options_t& options, const TIVarFile* _ctx) -> data_t { \
39-
options_t options_withType = options; \
40-
options_withType["_type"] = type; \
41-
return (TypeHandlers::TH_Generic##which::makeDataFromString)(str, options_withType, _ctx); \
42-
}, &TypeHandlers::TH_Generic##which::makeStringFromData)
38+
#define GenericHandlerTuple(which, type) TypeHandlersTuple{ \
39+
[](const std::string& str, const options_t& options, const TIVarFile* _ctx) -> data_t { \
40+
options_t options_withType = options; \
41+
options_withType["_type"] = type; \
42+
return (TypeHandlers::TH_Generic##which::makeDataFromString)(str, options_withType, _ctx); \
43+
}, \
44+
&(TypeHandlers::TH_Generic##which::makeStringFromData), \
45+
&(TypeHandlers::TH_Generic##which::getMinVersionFromData), \
46+
}
4347

44-
void TIVarTypes::insertType(const std::string& name, int id, const std::vector<std::string>& exts, const handler_pair_t& handlers)
48+
void TIVarTypes::insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlersTuple& handlers)
4549
{
4650
const TIVarType varType(id, name, exts, handlers);
4751
types[name] = varType;
@@ -67,44 +71,44 @@ namespace tivars
6771
const std::string _;
6872

6973
/* Standard types */
70-
insertType("Real", 0x00, {"82n", "83n", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerPair(Real, 0x00) );
71-
insertType("RealList", 0x01, {"82l", "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerPair(List, 0x00) );
72-
insertType("Matrix", 0x02, {"82m", "83m", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm"}, make_handler_pair(TH_Matrix) );
73-
insertType("Equation", 0x03, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, make_handler_pair(TH_Tokenized) );
74-
insertType("String", 0x04, {"82s", "83s", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs"}, make_handler_pair(TH_Tokenized) );
75-
insertType("Program", 0x05, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, make_handler_pair(TH_Tokenized) );
76-
insertType("ProtectedProgram", 0x06, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, make_handler_pair(TH_Tokenized) );
74+
insertType("Real", 0x00, {"82n", "83n", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerTuple(Real, 0x00) );
75+
insertType("RealList", 0x01, {"82l", "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerTuple(List, 0x00) );
76+
insertType("Matrix", 0x02, {"82m", "83m", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm", "8xm"}, SpecificHandlerTuple(TH_Matrix) );
77+
insertType("Equation", 0x03, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, SpecificHandlerTuple(TH_Tokenized) );
78+
insertType("String", 0x04, {"82s", "83s", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs", "8xs"}, SpecificHandlerTuple(TH_Tokenized) );
79+
insertType("Program", 0x05, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, SpecificHandlerTuple(TH_Tokenized) );
80+
insertType("ProtectedProgram", 0x06, {"82p", "83p", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp", "8xp"}, SpecificHandlerTuple(TH_Tokenized) );
7781
insertType("Picture", 0x07, {"82i", "83i", "8xi", "8xi", "8xi", "8ci", "8ci", "8ci", "8ci"});
78-
insertType("GraphDataBase", 0x08, {"82d", "83d", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd"}, make_handler_pair(TH_GDB) );
82+
insertType("GraphDataBase", 0x08, {"82d", "83d", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd", "8xd"}, SpecificHandlerTuple(TH_GDB) );
7983
// insertType("Unknown", 0x09, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
8084
// insertType("UnknownEqu", 0x0A, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
81-
insertType("SmartEquation", 0x0B, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, make_handler_pair(TH_Tokenized) ); // aka "New Equation"
82-
insertType("Complex", 0x0C, { _ , "83c", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc"}, GenericHandlerPair(Complex, 0x0C) );
83-
insertType("ComplexList", 0x0D, { _ , "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerPair(List, 0x0C) );
85+
insertType("SmartEquation", 0x0B, {"82y", "83y", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy", "8xy"}, SpecificHandlerTuple(TH_Tokenized) ); // aka "New Equation"
86+
insertType("Complex", 0x0C, { _ , "83c", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x0C) );
87+
insertType("ComplexList", 0x0D, { _ , "83l", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl", "8xl"}, GenericHandlerTuple(List, 0x0C) );
8488
// insertType("Undef", 0x0E, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
8589
insertType("WindowSettings", 0x0F, {"82w", "83w", "8xw", "8xw", "8xw", "8xw", "8xw", "8xw", "8xw"});
8690
insertType("RecallWindow", 0x10, {"82z", "83z", "8xz", "8xz", "8xz", "8xz", "8xz", "8xz", "8xz"});
8791
insertType("TableRange", 0x11, {"82t", "83t", "8xt", "8xt", "8xt", "8xt", "8xt", "8xt", "8xt"});
8892
insertType("ScreenImage", 0x12, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
8993
insertType("Backup", 0x13, {"82b", "83b", "8xb", _ , "8xb", "8cb", _ , _ , _ });
9094
insertType("App", 0x14, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
91-
insertType("AppVar", 0x15, { _ , _ , "8xv", "8xv", "8xv", "8xv", "8xv", "8xv", "8xv"}, GenericHandlerPair(AppVar, 0x15) );
92-
insertType("PythonAppVar", 0x15, { _ , _ , _ , _ , _ , _ , "8xv", "8xv", "8xv"}, make_handler_pair(STH_PythonAppVar) );
95+
insertType("AppVar", 0x15, { _ , _ , "8xv", "8xv", "8xv", "8xv", "8xv", "8xv", "8xv"}, GenericHandlerTuple(AppVar, 0x15) );
96+
insertType("PythonAppVar", 0x15, { _ , _ , _ , _ , _ , _ , "8xv", "8xv", "8xv"}, SpecificHandlerTuple(STH_PythonAppVar) );
9397
insertType("TemporaryItem", 0x16, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
9498
insertType("GroupObject", 0x17, {"82g", "83g", "8xg", "8xg", "8xg", "8xg", "8cg", "8cg", "8cg"});
95-
insertType("RealFraction", 0x18, { _ , _ , _ , _ , "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerPair(Real, 0x18) );
99+
insertType("RealFraction", 0x18, { _ , _ , _ , _ , "8xn", "8xn", "8xn", "8xn", "8xn"}, GenericHandlerTuple(Real, 0x18) );
96100
insertType("MixedFraction", 0x19, { _ , _ , _ , _ , _ , _ , _ , _ , _ });
97101
insertType("Image", 0x1A, { _ , _ , _ , _ , _ , _ , "8ca", "8ca", "8ca"});
98102

99103
/* Exact values (TI-83 Premium CE [Edition Python] and TI-82 Advanced Edition Python) */
100104
/* See https://docs.google.com/document/d/1P_OUbnZMZFg8zuOPJHAx34EnwxcQZ8HER9hPeOQ_dtI and especially this lib's implementation */
101-
insertType("ExactComplexFrac", 0x1B, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1B) );
102-
insertType("ExactRealRadical", 0x1C, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x1C) );
103-
insertType("ExactComplexRadical", 0x1D, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1D) );
104-
insertType("ExactComplexPi", 0x1E, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1E) );
105-
insertType("ExactComplexPiFrac", 0x1F, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerPair(Complex, 0x1F) );
106-
insertType("ExactRealPi", 0x20, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x20) );
107-
insertType("ExactRealPiFrac", 0x21, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerPair(Real, 0x21) );
105+
insertType("ExactComplexFrac", 0x1B, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1B) );
106+
insertType("ExactRealRadical", 0x1C, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x1C) );
107+
insertType("ExactComplexRadical", 0x1D, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1D) );
108+
insertType("ExactComplexPi", 0x1E, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1E) );
109+
insertType("ExactComplexPiFrac", 0x1F, { _ , _ , _ , _ , _ , _ , _ , "8xc", "8xc"}, GenericHandlerTuple(Complex, 0x1F) );
110+
insertType("ExactRealPi", 0x20, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x20) );
111+
insertType("ExactRealPiFrac", 0x21, { _ , _ , _ , _ , _ , _ , _ , "8xn", "8xn"}, GenericHandlerTuple(Real, 0x21) );
108112

109113
/* System/Flash-related things */
110114
// 0x22 - IDList (68k calcs)

src/TIVarTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace tivars
2828
static bool isValidID(uint8_t id);
2929

3030
private:
31-
static void insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlers::handler_pair_t& handlers = make_handler_pair(TypeHandlers::DummyHandler));
31+
static void insertType(const std::string& name, int id, const std::vector<std::string>& exts, const TypeHandlers::TypeHandlersTuple& handlers = { &TypeHandlers::DummyHandler::makeDataFromString, &TypeHandlers::DummyHandler::makeStringFromData, &TypeHandlers::DummyHandler::getMinVersionFromData });
3232

3333
};
3434
}

src/TypeHandlers/STH_DataAppVar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@ namespace tivars::TypeHandlers
6464

6565
return str;
6666
}
67+
68+
uint8_t STH_DataAppVar::getMinVersionFromData(const data_t& data)
69+
{
70+
(void)data;
71+
return 0;
72+
}
6773
}

src/TypeHandlers/STH_ExactFraction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ namespace tivars::TypeHandlers
3535
return dec2frac(stod(STH_FP::makeStringFromData(data, options)));
3636
}
3737

38+
uint8_t STH_ExactFraction::getMinVersionFromData(const data_t& data)
39+
{
40+
(void)data;
41+
return 0;
42+
}
3843
}

src/TypeHandlers/STH_ExactFractionPi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ namespace tivars::TypeHandlers
3535
return dec2frac(stod(STH_FP::makeStringFromData(data, options)), "π");
3636
}
3737

38+
uint8_t STH_ExactFractionPi::getMinVersionFromData(const data_t& data)
39+
{
40+
(void)data;
41+
return 0;
42+
}
3843
}

0 commit comments

Comments
 (0)