Skip to content

Commit

Permalink
Merge pull request #45 from dic-iit/fix/string_assignement
Browse files Browse the repository at this point in the history
Allow the assignment from string also for non standard strings
  • Loading branch information
S-Dafarra committed Mar 22, 2021
2 parents c735e35 + 4138057 commit f1bd959
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Do not use find_dependency in matioCppConfig.cmake if OVERRIDE_MODULE_PATH is used: [#41](https://github.com/dic-iit/matio-cpp/pull/41).
- Make sure to add a NULL terminator in the cases where the new matio version uses strlen: [#44](https://github.com/dic-iit/matio-cpp/pull/44).
- Allow the assignment from string also for non standard strings: [#45](https://github.com/dic-iit/matio-cpp/pull/45).

## [0.1.0] - 2021-02-02

Expand Down
4 changes: 2 additions & 2 deletions include/matioCpp/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ class matioCpp::Vector : public matioCpp::Variable
/**
* @brief Assignement operator from another string.
* @param other The input string.
* @note This is available only if the type is char.
* @note This is available only if the type is char, char16_t, char32_t, uint8_t, uint16_t or uint32_t.
* @return A reference to this Vector.
*/
Vector<T>& operator=(const std::string& other);
Vector<T>& operator=(const string_input_type &other);

/**
* @brief Assignement operator from a vector of booleans.
Expand Down
7 changes: 5 additions & 2 deletions include/matioCpp/impl/Vector.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,12 @@ matioCpp::Vector<typename matioCpp::Vector<T>::element_type> &matioCpp::Vector<T
}

template<typename T>
matioCpp::Vector<T> &matioCpp::Vector<T>::operator=(const std::string &other)
matioCpp::Vector<T> &matioCpp::Vector<T>::operator=(const string_input_type &other)
{
static_assert (std::is_same<T, char>::value,"The assignement operator from a string is available only if the type of the vector is char");
static_assert (matioCpp::is_string_compatible<T>::value ||
matioCpp::is_string16_compatible<T>::value ||
matioCpp::is_string32_compatible<T>::value,
"The assignement operator from a string is available only if the type of the vector is char, char16_t, char32_t, uint8_t, uint16_t or uint32_t.");
if (size() == other.size())
{
memcpy(toMatio()->data, other.data(), size() * sizeof(T));
Expand Down
26 changes: 23 additions & 3 deletions test/VectorUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,29 @@ TEST_CASE("Assignments")

SECTION("String")
{
matioCpp::Vector<char> string;
string = "test";
REQUIRE(string() == "test");
matioCpp::Vector<char> string8;
string8 = "test";
REQUIRE(string8() == "test");

matioCpp::Vector<uint8_t> varUint("test");
varUint = "test_string_int";
REQUIRE(varUint() == "test_string_int");

matioCpp::String16 var16("test");
var16 = u"test_string16";
REQUIRE(var16() == u"test_string16");

matioCpp::Vector<uint16_t> varUint16("test");
varUint16 = u"test_string_int16";
REQUIRE(varUint16() == u"test_string_int16");

matioCpp::String32 var32("test");
var32 = U"test_string32";
REQUIRE(var32() == U"test_string32");

matioCpp::Vector<uint32_t> varUint32("test");
varUint32 = U"test_string_int32";
REQUIRE(varUint32() == U"test_string_int32");
}

SECTION("Vector of bool")
Expand Down

0 comments on commit f1bd959

Please sign in to comment.