Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage improvements #83

Merged
merged 6 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions include/matioCpp/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,8 @@ class matioCpp::Struct : public matioCpp::Variable
*/
const matioCpp::Variable operator[](index_type el) const;

/**
* @brief Access field at a specific index.
* @param el The name of the field to be accessed.
* @return A Variable with a weak ownership to the underlying mat variable. This means that the data can be changed,
* but the variable cannot be resized and the name cannot change.
* @note If the field is not found, the output variable is not valid.
*/
matioCpp::Variable operator[](const std::string& el);
using Variable::operator[];

/**
* @brief Access field at a specific index.
* @param el The name of the field to be accessed.
* @warning Each element of el has to be strictly smaller than the corresponding dimension.
* @return A const Variable with a weak ownership to the underlying mat variable.
* @note If the field is not found, the output variable is not valid.
*/
const matioCpp::Variable operator[](const std::string& el) const;
};

#endif // MATIOCPP_STRUCT_H
20 changes: 20 additions & 0 deletions include/matioCpp/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,26 @@ class matioCpp::Variable
*/
bool isValid() const;

/**
* @brief Access field with specific name.
* @param el The name of the field to be accessed.
* @return A Variable with a weak ownership to the underlying mat variable. This means that the data can be changed,
* but the variable cannot be resized and the name cannot change.
* @note This works only if the variable is a struct.
* @note If the field is not found, the output variable is not valid.
*/
matioCpp::Variable operator[](const std::string& el);

/**
* @brief Access field with specific name.
* @param el The name of the field to be accessed.
* @warning Each element of el has to be strictly smaller than the corresponding dimension.
* @return A const Variable with a weak ownership to the underlying mat variable.
* @note This works only if the variable is a struct.
* @note If the field is not found, the output variable is not valid.
*/
const matioCpp::Variable operator[](const std::string& el) const;

/**
* @brief Cast the variable as a Element.
*
Expand Down
7 changes: 7 additions & 0 deletions src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ void matioCpp::File::close()

bool matioCpp::File::open(const std::string &name, matioCpp::FileMode mode)
{
struct stat info;

if (stat(name.c_str(), &info) != 0)
S-Dafarra marked this conversation as resolved.
Show resolved Hide resolved
{
std::cerr << "[ERROR][matioCpp::File::open] The file " << name << " does not exists." << std::endl;
return false;
}
int matio_mode = mode == matioCpp::FileMode::ReadOnly ? mat_acc::MAT_ACC_RDONLY : mat_acc::MAT_ACC_RDWR;
m_pimpl->reset(Mat_Open(name.c_str(), matio_mode), mode);
return isOpen();
Expand Down
16 changes: 0 additions & 16 deletions src/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,3 @@ const matioCpp::Variable matioCpp::Struct::operator[](matioCpp::Struct::index_ty
assert(el < numberOfFields() && "The specified index is out of bounds");
return getStructField(el);
}

matioCpp::Variable matioCpp::Struct::operator[](const std::string &el)
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}

const matioCpp::Variable matioCpp::Struct::operator[](const std::string &el) const
{
size_t index = getFieldIndex(el);
assert(index < numberOfFields() && "The specified field does not exist.");
return getStructField(index);
}


26 changes: 26 additions & 0 deletions src/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,32 @@ bool matioCpp::Variable::isValid() const
return m_handler->get() && checkCompatibility(m_handler->get(), m_handler->variableType(), m_handler->valueType());
}

matioCpp::Variable matioCpp::Variable::operator[](const std::string& el)
{
if (variableType() != matioCpp::VariableType::Struct)
{
std::cerr << "[ERROR][matioCpp::Variable::operator[]] The operator[](string) can be used only with structs." << std::endl;
assert(false);
return matioCpp::Variable();
}
size_t index = getStructFieldIndex(el);
assert(index < getStructNumberOfFields() && "The specified field does not exist.");
return getStructField(index);
}

const matioCpp::Variable matioCpp::Variable::operator[](const std::string& el) const
{
if (variableType() != matioCpp::VariableType::Struct)
{
std::cerr << "[ERROR][matioCpp::Variable::operator[]] The operator[](string) can be used only with structs." << std::endl;
assert(false);
return matioCpp::Variable();
}
size_t index = getStructFieldIndex(el);
assert(index < getStructNumberOfFields() && "The specified field does not exist.");
S-Dafarra marked this conversation as resolved.
Show resolved Hide resolved
return getStructField(index);
}

matioCpp::CellArray matioCpp::Variable::asCellArray()
{
return matioCpp::CellArray(*m_handler);
Expand Down
6 changes: 6 additions & 0 deletions test/FileUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,9 @@ TEST_CASE("Write version 4")
REQUIRE(boolVector(2));
REQUIRE_FALSE(boolVector(3));
}

TEST_CASE("Open not existing file")
{
matioCpp::File file;
REQUIRE_FALSE(file.open("notExisting.mat"));
}
21 changes: 21 additions & 0 deletions test/VariableUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,26 @@ TEST_CASE("Conversions")
}
}

TEST_CASE("operator[](string)")
{
std::vector<size_t> dimensions = {1, 1};
std::vector<matvar_t*> pointers;
pointers.emplace_back(Mat_VarDuplicate(matioCpp::Vector<double>("vector").toMatio(), 1));
pointers.emplace_back(Mat_VarDuplicate(matioCpp::Element<int>("element").toMatio(), 1));
pointers.emplace_back(Mat_VarDuplicate(matioCpp::MultiDimensionalArray<double>("array").toMatio(), 1));
pointers.emplace_back(Mat_VarDuplicate(matioCpp::String("name", "content").toMatio(), 1));
pointers.emplace_back(Mat_VarDuplicate(matioCpp::Struct("otherStruct", { matioCpp::String("name", "anotherContent") }).toMatio(), 1));
pointers.emplace_back(nullptr);

matvar_t* matioVar = Mat_VarCreate("test", matio_classes::MAT_C_STRUCT, matio_types::MAT_T_STRUCT, 2, dimensions.data(), pointers.data(), 0);
REQUIRE(matioVar);

matioCpp::Variable sharedVar((matioCpp::SharedMatvar(matioVar)));

REQUIRE(sharedVar["name"].asString()() == "content");

const matioCpp::Variable& constRef = sharedVar;
REQUIRE(constRef["name"].asString()() == "content");

REQUIRE(sharedVar["otherStruct"]["name"].asString()() == "anotherContent");
}
Loading