使用访问者模式在C ++阵列上操作
The C++ MATLAB®数据API支持通过金宝appmatlab :: data :: apply_visitor
和matlab :: data :: apply_visitor_ref
功能。这些函数接受数组或数组参考和访客类作为输入。
Theapply_visitor
和apply_visitor_ref
功能根据输入数组类型派遣由访问者类定义的操作。访问者类定义操作以执行特定类型的数组。
在这样的情况下使用访客模式:
There are many operations that you need to perform on an array and the way to perform them depends on the type of the array.
函数返回的数组可能具有不同的已知类型,您想处理所有情况。
您正在使用诸如单元阵列或结构阵列之类的异质结构。
调度在数组或数组引用
Theapply_visitor
function dispatches to the visitor class operation based on the type of the input array. The syntax for callingapply_visitor
accepts amatlab::data::Array
和your visitor class instance:
auto apply_visitor(matlab::data::Array a, V visitor)
Theapply_visitor_ref
函数根据输入传递的数组参考的类型分配给访问者类操作。呼叫的语法apply_visitor_ref
accepts amatlab::data::ArrayRef
和your visitor class instance:
auto apply_visitor_ref(const matlab::data::ArrayRef& a, V visitor)
Overloadingoperator()
Implement your visitor class to overload the operatoroperator()
对于要操作的数组类型。例如,假设您要实现的一个操作是返回一个包含的文本matlab :: data :: chararray
作为一个std::string
. Implement the operation like this:
std :: String operator()(MATLAB :: DATA :: CHARARRAY ARR){return arr.toascii();}
As another example, suppose that you want to negate the logical values in amatlab :: data :: typedarray
. In this case, use a reference to the array:
void operator()(TypedArrayRefboolArrRef) { std::cout << "Negate logical value: " << std::endl; for (auto &b : boolArrRef) { b = !b; } }
You must use an element reference in the range-basedfor
循环更改数组中的值。
Visitor Class to Display Contents of Cell Array
This example shows how to use a visitor class to define operations to perform on specific types ofmatlab::data::Array
.
TheDisplayVisitor
类实施操作以显示类型阵列的单元阵列的内容bool
,double
, 和char
,并包含单元阵列。您可以添加新的操作来支持其他单元格数来通过添加更多的过载功能金宝app。
typeDisplayVisitor.cpp
#include "MatlabDataArray.hpp" #includeusing namespace matlab::data; void DisplayCell(const CellArray cellArray); class DisplayVisitor { public: template void operator()(U arr) {} void operator()(const TypedArray boolArr) { std::cout << "Cell contains logical array: " << std::endl; for (auto b : boolArr) { printf_s("%d ", b); } std::cout << "\n"; } void operator()(const TypedArray doubleArr) { std::cout << "Cell contains double array: " << std::endl; for (auto elem : doubleArr) { std::cout << elem << " "; } std::cout << "\n"; } void operator()(const CharArray charArr) { std::cout << "Cell contains char array: " << std::endl; for (auto elem : charArr) { std::cout << char(elem); } std::cout << "\n"; } void operator()(const CellArray containedCellArray) { DisplayCell(containedCellArray); } }; void DisplayCell(const CellArray cellArray) { DisplayVisitor v; for (auto elem : cellArray) { apply_visitor(elem, v); } }
To use the class, pass a cell array to theDisplayCell
function.
typecallDisplayCell.cpp
int main() { ArrayFactory factory; // Create cell array matlab::data::CellArray cellArray = factory.createCellArray({ 1,4 }, factory.createCharArray("A char array"), factory.createArray({ 1,2 }, { false, true }), factory.createArray ({ 2,2 }, { 1.2, 2.2, 3.2, 4.2 }), factory.createCellArray({ 1,1 }, false)); // Call function DisplayCell(cellArray); return 0; }
Visitor Class to Modify Contents of Cell Array
在此示例中,CellModifyVisitor
class implements the operations to modify the contents of cell arrays of typesbool
,double
, 和char
,并包含单元阵列。您可以添加新的操作来支持其他单元格数来通过添加更多的过载功能金宝app。
Themodifycell
功能调用apply_visitor_ref
在单元格数组中的每个元素的循环中。因为目的是修改单元格数组的内容,因此此示例使用对单元格阵列内容的引用。
typeCellModifyVisitor.cpp
#include“ matlabdataarray.hpp” #include“ matlabengine.hpp” #include使用命名空间matlab :: data;void modifycell(Cellarray&Cellarray);类CellModifyVisitor {public:template void operator()(u arr){} void operator()(typedArrayRef boolRef){std :: cout << <<“否定逻辑值:;for(auto&b:boolarrref){b =!b;}} void operator()(typedArrayRef doublearrref){std :: cout << <<“添加1个值:” << std :: endl;for(auto&elem:doublearrref){elem = elem + 1;} std :: cout <<“ \ n”;} void operator()(chararrayref chararrref){std :: cout <<“ motify char array” << std :: endl;ArrayFactory工厂;chararrref = factory.createchararray(“修改后的char数组”); } void operator()(CellArrayRef containedCellArray) { CellModifyVisitor v; for (auto elem : containedCellArray) { apply_visitor_ref(elem, v); } } }; void ModifyCell(CellArray &cellArray) { CellModifyVisitor v; for (auto elem : cellArray) { apply_visitor_ref(elem, v); } }
To use the class, pass a cell array to themodifycell
function.
typecallModifyCell.cpp
int main() { ArrayFactory factory; // Create cell array matlab::data::CellArray cellArray = factory.createCellArray({ 1,4 }, factory.createCharArray("A char array"), factory.createArray({ 1,2 }, { false, true }), factory.createArray ({ 2,2 }, { 1.2, 2.2, 3.2, 4.2 }), factory.createCellArray({ 1,1 }, false)); // Call function ModifyCell(cellArray); return 0; }
See Also
matlab :: data :: apply_visitor
|matlab :: data :: apply_visitor_ref