This commit is contained in:
Михаил Капелько
2024-06-05 23:14:03 +03:00
parent b254e63ecd
commit d7fa9ec0fb
5 changed files with 39 additions and 37 deletions

View File

@@ -1,7 +1,6 @@
#include <list> #include <list>
#include <queue> #include <queue>
#include <string> #include <string>
#include "any.h"
#ifndef ctx_HEADER #ifndef ctx_HEADER
#define ctx_HEADER #define ctx_HEADER

View File

@@ -1,18 +1,18 @@
#include <string> #include <string>
#include "ctx.h" #include "ctx.h"
#include "memory_Context.h" #include "memory_Context.h"
#include <iostream> #include <iostream>
std::string s(const char *str) {
return std::string(str);
}
std::string ctx_test_Controller_executeFunctions_set() { std::string ctx_test_Controller_executeFunctions_set() {
auto c = memory_createContext(); auto c = memory_createContext();
ctx_Controller<memory_Context> ctrl(c); ctx_Controller<memory_Context> ctrl(c);
// Disable automatic invocation of executeFunctions. // Disable automatic invocation of executeFunctions.
ctrl.isProcessingQueue = true; ctrl.isProcessingQueue = true;
std::cout << "01\n"; ctrl.set("input", s("123"));
ctrl.set("input", "123");
std::cout << "input: '" << ctrl.context.input << "'" << std::endl;
std::cout << "02\n";
auto processInput = [](memory_Context c) -> memory_Context { auto processInput = [](memory_Context c) -> memory_Context {
if (c.recentField == "input") { if (c.recentField == "input") {
@@ -29,7 +29,14 @@ std::string ctx_test_Controller_executeFunctions_set() {
ctrl.executeFunctions(); ctrl.executeFunctions();
// Apply 'outputHelp'. // Apply 'outputHelp'.
ctrl.executeFunctions(); ctrl.executeFunctions();
std::cout << "input: '" << ctrl.context.input << "'" << std::endl; std::cout
<< "input: '"
<< ctrl.context.input
<< "'"
<< ((ctrl.context.input == "123") ? "1" : "0")
<< " "
<< ((ctrl.context.outputHelp == "Checked") ? "1" : "0")
<< std::endl;
if ( if (
ctrl.context.input == "123" && ctrl.context.input == "123" &&
ctrl.context.outputHelp == "Checked" ctrl.context.outputHelp == "Checked"

View File

@@ -1,4 +1,6 @@
c++ -o test_memory_C++ -std=c++11 -I. \ #c++ -o test_memory_C++ -std=c++11 -I. \
#c++ -o test_memory_C++ -std=c++17 -I. \
c++ -o test_memory_C++ -std=c++20 -I. \
memory.cpp \ memory.cpp \
memory_Context.cpp \ memory_Context.cpp \
memory_test.cpp \ memory_test.cpp \

View File

@@ -1,10 +1,9 @@
#include <utility> #include <any>
#include "any.h"
#include "memory_Context.h" #include "memory_Context.h"
#include <iostream> #include <iostream>
libany::any memory_Context::field(const std::string &fieldName) { std::any memory_Context::field(const std::string &fieldName) {
if (fieldName == "didLaunch") { if (fieldName == "didLaunch") {
return didLaunch; return didLaunch;
} else if (fieldName == "exit") { } else if (fieldName == "exit") {
@@ -42,47 +41,41 @@ libany::any memory_Context::field(const std::string &fieldName) {
return victory; return victory;
} }
void memory_Context::setField(const std::string &fieldName, libany::any value) { void memory_Context::setField(const std::string &fieldName, std::any value) {
if (fieldName == "didLaunch") { if (fieldName == "didLaunch") {
didLaunch = libany::any_cast<bool>(value); didLaunch = std::any_cast<bool>(value);
} else if (fieldName == "exit") { } else if (fieldName == "exit") {
exit = libany::any_cast<bool>(value); exit = std::any_cast<bool>(value);
} else if (fieldName == "hiddenItems") { } else if (fieldName == "hiddenItems") {
hiddenItems = libany::any_cast<std::vector<int> >(value); hiddenItems = std::any_cast<std::vector<int> >(value);
} else if (fieldName == "input") { } else if (fieldName == "input") {
std::cout << "memory_Context-01\n"; input = std::any_cast<std::string>(value);
try {
input = libany::any_cast<std::string>(value);
} catch (libany::bad_any_cast &e) {
std::cout << "memory_Context-01 exception/type: " << e.what() << ", " << value.type().name() << std::endl;
}
std::cout << "memory_Context-02\n";
} else if (fieldName == "mismatchedItems") { } else if (fieldName == "mismatchedItems") {
mismatchedItems = libany::any_cast<std::vector<int> >(value); mismatchedItems = std::any_cast<std::vector<int> >(value);
} else if (fieldName == "outputGoOn") { } else if (fieldName == "outputGoOn") {
outputGoOn = libany::any_cast<std::string>(value); outputGoOn = std::any_cast<std::string>(value);
} else if (fieldName == "outputGreeting") { } else if (fieldName == "outputGreeting") {
outputGreeting = libany::any_cast<std::string>(value); outputGreeting = std::any_cast<std::string>(value);
} else if (fieldName == "outputHelp") { } else if (fieldName == "outputHelp") {
outputHelp = libany::any_cast<std::string>(value); outputHelp = std::any_cast<std::string>(value);
} else if (fieldName == "outputMatchedItems") { } else if (fieldName == "outputMatchedItems") {
outputMatchedItems = libany::any_cast<std::string>(value); outputMatchedItems = std::any_cast<std::string>(value);
} else if (fieldName == "outputMismatchedItems") { } else if (fieldName == "outputMismatchedItems") {
outputMismatchedItems = libany::any_cast<std::string>(value); outputMismatchedItems = std::any_cast<std::string>(value);
} else if (fieldName == "outputPromptSelection") { } else if (fieldName == "outputPromptSelection") {
outputPromptSelection = libany::any_cast<std::string>(value); outputPromptSelection = std::any_cast<std::string>(value);
} else if (fieldName == "outputVictory") { } else if (fieldName == "outputVictory") {
outputVictory = libany::any_cast<std::string>(value); outputVictory = std::any_cast<std::string>(value);
} else if (fieldName == "playfieldItems") { } else if (fieldName == "playfieldItems") {
playfieldItems = libany::any_cast<std::map<int, int> >(value); playfieldItems = std::any_cast<std::map<int, int> >(value);
} else if (fieldName == "playfieldSize") { } else if (fieldName == "playfieldSize") {
playfieldSize = libany::any_cast<int>(value); playfieldSize = std::any_cast<int>(value);
} else if (fieldName == "selectedId") { } else if (fieldName == "selectedId") {
selectedId = libany::any_cast<int>(value); selectedId = std::any_cast<int>(value);
} else if (fieldName == "selectedItems") { } else if (fieldName == "selectedItems") {
selectedItems = libany::any_cast<std::vector<int> >(value); selectedItems = std::any_cast<std::vector<int> >(value);
} else if (fieldName == "victory") { } else if (fieldName == "victory") {
victory = libany::any_cast<bool>(value); victory = std::any_cast<bool>(value);
} }
} }

View File

@@ -1,3 +1,4 @@
#include <any>
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -26,8 +27,8 @@ struct memory_Context {
std::vector<int> selectedItems; std::vector<int> selectedItems;
bool victory = false; bool victory = false;
libany::any field(const std::string &fieldName); std::any field(const std::string &fieldName);
void setField(const std::string &fieldName, libany::any value); void setField(const std::string &fieldName, std::any value);
}; };
memory_Context memory_createContext(); memory_Context memory_createContext();