Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Simplxss committed Nov 29, 2023
2 parents c3dd7fc + 6d154b9 commit fcd9109
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 68 deletions.
15 changes: 10 additions & 5 deletions include/mdf/ichannelconversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,39 +186,44 @@ class IChannelConversion : public IBlock {
/** \brief Returns formula string. */
[[nodiscard]] virtual const std::string& Formula() const;

[[nodiscard]] uint16_t NofParameters() const;
/** \brief Sets a floating point parameter value.
*
* @param index Parameter index to set.
* @param parameter Value to set.
*/
void Parameter(uint32_t index, double parameter);
void Parameter(uint16_t index, double parameter);

/** \brief Returns the parameter (double)
*
* @param index
* @return Parameter floating point value
*/
[[nodiscard]] double Parameter(uint32_t index) const;
[[nodiscard]] double Parameter(uint16_t index) const;

/** \brief Returns the parameter as a bit field (uint64_t)
*
* @param index
* @return Parameter floating point value
*/
[[nodiscard]] uint64_t ParameterUint(uint32_t index) const;
[[nodiscard]] uint64_t ParameterUint(uint16_t index) const;
/** \brief Sets an unsigned integer parameter value.
*
* @param index Parameter index to set.
* @param parameter Value to set.
*/
void Parameter(uint32_t index, uint64_t parameter);
void Parameter(uint16_t index, uint64_t parameter);

[[nodiscard]] virtual uint16_t NofReferences() const;
/** \brief Sets text reference (TX) block
*
* @param index Index of the text block (TX).
* @param text Text content of the TX block.
*/
virtual void Reference(size_t index, const std::string& text);
virtual void Reference(uint16_t index, const std::string& text);

/** \brief Returns the reference string by its index */
[[nodiscard]] virtual std::string Reference(uint16_t index) const;

/** \brief Sets the CN block data type.
*
Expand Down
41 changes: 29 additions & 12 deletions mdflib/src/cc4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ ConversionType Cc4Block::Type() const {
void Cc4Block::Decimals(uint8_t decimals) { precision_ = decimals; }

uint8_t Cc4Block::Decimals() const {
auto max = static_cast<uint8_t>(
const auto max = static_cast<uint8_t>(
channel_data_type_ == 4 ? std::numeric_limits<float>::max_digits10
: std::numeric_limits<double>::max_digits10);
return std::min(precision_, max);
Expand Down Expand Up @@ -187,7 +187,7 @@ void Cc4Block::GetBlockProperty(BlockPropertyList& dest) const {
dest.emplace_back("Nof Values", std::to_string(nof_values_));
dest.emplace_back("Min Range", ToString(range_min_));
dest.emplace_back("Max Range", ToString(range_max_));
for (uint32_t ii = 0; ii < static_cast<uint32_t>(value_list_.size()); ++ii) {
for (uint16_t ii = 0; ii < static_cast<uint16_t>(value_list_.size()); ++ii) {
std::ostringstream label;
label << "Value " << ii;
dest.emplace_back(label.str(), ToString(Parameter(ii)) );
Expand Down Expand Up @@ -282,9 +282,9 @@ size_t Cc4Block::Write(std::FILE* file) { // NOLINT
WriteMdComment(file, kIndexMd);
WriteBlock4(file, cc_block_, kIndexInverse);
for (uint16_t index_m = 0; index_m < nof_references_; ++index_m) {
const auto index = 4 + index_m;
const auto* block = ref_list_[index_m].get();
link_list_[index] = block != nullptr ? block->FilePosition() : 0;
const auto index = kIndexRef + index_m;
auto& block = ref_list_[index_m];
WriteBlock4(file, block, index);
}

auto bytes = MdfBlock::Write(file);
Expand Down Expand Up @@ -383,8 +383,8 @@ bool Cc4Block::ConvertValueRangeToText(double channel_value,
// First iterate to find the ref
size_t ref_index = ref_list_.size() - 1; // Default CC/TX
for (uint16_t n = 0; n < nof_values_; ++n) {
const uint32_t key_min_index = n * 2;
const uint32_t key_max_index = key_min_index + 1;
const uint16_t key_min_index = n * 2;
const uint16_t key_max_index = key_min_index + 1;
if (key_max_index >= value_list_.size()) {
break;
}
Expand Down Expand Up @@ -432,7 +432,7 @@ bool Cc4Block::ConvertTextToValue(const std::string& channel_value,
return false;
}
// Initialize to default value index
auto value_index = static_cast<uint32_t>(value_list_.size() - 1);
auto value_index = static_cast<uint16_t>(value_list_.size() - 1);
for (uint16_t n = 0; n < nof_values_; ++n) {
if (n >= ref_list_.size()) {
break;
Expand Down Expand Up @@ -519,18 +519,22 @@ void Cc4Block::Formula(const std::string& formula) {
}

const std::string& Cc4Block::Formula() const {
auto* tx4 = ref_list_.empty() ? nullptr :
const auto* tx4 = ref_list_.empty() ? nullptr :
dynamic_cast<Tx4Block*>(ref_list_[0].get());
if (tx4 != nullptr) {
if (tx4 != nullptr) {
formula_ = tx4->Text();
} else {
formula_.clear();
}
return IChannelConversion::Formula();
}

void Cc4Block::Reference(size_t index, const std::string& text) {
while (index < ref_list_.size()) {
[[nodiscard]] uint16_t Cc4Block::NofReferences() const {
return nof_references_;
}

void Cc4Block::Reference(uint16_t index, const std::string& text) {
while (index >= ref_list_.size()) {
auto temp = std::make_unique<Tx4Block>();
ref_list_.push_back(std::move(temp));
}
Expand All @@ -540,5 +544,18 @@ void Cc4Block::Reference(size_t index, const std::string& text) {
}
}

std::string Cc4Block::Reference(uint16_t index) const {
const auto* block = index < ref_list_.size() ?
ref_list_[index].get() : nullptr;
if (block == nullptr || block->BlockType() != "TX") {
return {"Not TX"};
}
const auto* tx4 = dynamic_cast<const Tx4Block*> (block);
if (tx4 != nullptr) {
return tx4->Text();
}
return {"Invalid"};

}

} // namespace mdf::detail
5 changes: 4 additions & 1 deletion mdflib/src/cc4block.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class Cc4Block : public MdfBlock, public IChannelConversion {

[[nodiscard]] const Cc4Block* Cc() const { return cc_block_.get(); }

void Reference(size_t index, const std::string& text) override;
[[nodiscard]] uint16_t NofReferences() const override;
void Reference(uint16_t index, const std::string& text) override;
[[nodiscard]] std::string Reference(uint16_t index) const override;

[[nodiscard]] const RefList& References() const { return ref_list_; }

[[nodiscard]] const MdfBlock* Find(int64_t index) const override;
Expand Down
2 changes: 2 additions & 0 deletions mdflib/src/dg4block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void Dg4Block::ReadData(std::FILE* file) const {
}
} else if (block->BlockType() == "SD") {
cn_block->ReadData(file);
} else if (block->BlockType() == "DZ") {
cn_block->ReadData(file);
}
break;

Expand Down
25 changes: 20 additions & 5 deletions mdflib/src/ichannelconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,18 @@ bool IChannelConversion::ConvertTextToTranslation(
return false;
}

void IChannelConversion::Parameter(uint32_t index, double parameter) {
uint16_t IChannelConversion::NofParameters() const {
return nof_values_;
}

void IChannelConversion::Parameter(uint16_t index, double parameter) {
while (index >= value_list_.size()) {
value_list_.emplace_back(0.0);
}
value_list_[index] = parameter;
}

double IChannelConversion::Parameter(uint32_t index) const {
double IChannelConversion::Parameter(uint16_t index) const {
double value = 0.0;
if (index < value_list_.size() ) {
const auto& val = value_list_[index];
Expand All @@ -321,7 +325,7 @@ double IChannelConversion::Parameter(uint32_t index) const {
return value;
}

uint64_t IChannelConversion::ParameterUint(uint32_t index) const {
uint64_t IChannelConversion::ParameterUint(uint16_t index) const {
uint64_t value = 0;
if (index < value_list_.size() ) {
const auto& val = value_list_[index];
Expand All @@ -334,7 +338,7 @@ uint64_t IChannelConversion::ParameterUint(uint32_t index) const {
return value;
}

void IChannelConversion::Parameter(uint32_t index, uint64_t parameter) {
void IChannelConversion::Parameter(uint16_t index, uint64_t parameter) {
switch (Type()) {
case ConversionType::BitfieldToText:
break;
Expand Down Expand Up @@ -369,8 +373,19 @@ const std::string &IChannelConversion::Formula() const {
return formula_;
}

void IChannelConversion::Reference(size_t index, const std::string &text) {
uint16_t IChannelConversion::NofReferences() const {
// Not supported function for MDF3
return 0;
}

void IChannelConversion::Reference(uint16_t index, const std::string &text) {
// Not supported function for MDF3
}

std::string IChannelConversion::Reference(uint16_t index) const {
// Not supported function for MDF3
return {};
}


} // end namespace mdf
7 changes: 4 additions & 3 deletions mdflib_test/src/testwrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,7 @@ TEST_F(TestWrite, Mdf4Mime) {

auto writer = MdfFactory::CreateMdfWriter(MdfWriterType::Mdf4Basic);
ASSERT_TRUE(writer->Init(mdf_file.string()));
writer->CompressData(true);

auto* header = writer->Header();
ASSERT_TRUE(header != nullptr);
Expand Down Expand Up @@ -2054,9 +2055,9 @@ TEST_F(TestWrite, Mdf4Mime) {
writer->StartMeasurement(tick_time);

std::vector<uint8_t> byte_array = {1,2,3,4,5}; // Not a very likely input

constexpr size_t kSamples = 10'000;
{
for (size_t sample = 0; sample < 10; ++sample) {
for (size_t sample = 0; sample < kSamples; ++sample) {
ch1->SetChannelValue(byte_array);
ch2->SetChannelValue(byte_array);
writer->SaveSample(*group1, tick_time);
Expand Down Expand Up @@ -2092,7 +2093,7 @@ TEST_F(TestWrite, Mdf4Mime) {
EXPECT_EQ(observer_list.size(), 3);
for (auto& observer : observer_list) {
ASSERT_TRUE(observer);
EXPECT_EQ(observer->NofSamples(), 10);
EXPECT_EQ(observer->NofSamples(), kSamples);
for (uint64_t sample = 0; sample < observer->NofSamples(); ++sample) {
if (observer->IsMaster()) {
continue; // Skip the time channel test
Expand Down
39 changes: 31 additions & 8 deletions mdflibrary/src/MdfChannelConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,48 @@ void MdfChannelConversion::Formula::set(String^ formula) {
}
}

double MdfChannelConversion::Parameter::get(int index) {
uint16_t MdfChannelConversion::NofParameters::get() {
return conversion_ != nullptr ? conversion_->NofParameters() : 0;
}

double MdfChannelConversion::Parameter(uint16_t index) {
return conversion_ != nullptr ?
conversion_->Parameter(static_cast<uint32_t>(index)) : 0.0;
conversion_->Parameter(index) : 0.0;
}

void MdfChannelConversion::Parameter::set(int index, double parameter) {
void MdfChannelConversion::Parameter(uint16_t index, double parameter) {
if (conversion_ != nullptr) {
conversion_->Parameter(static_cast<uint32_t>(index), parameter);
conversion_->Parameter(index, parameter);
}
}

uint64_t MdfChannelConversion::ParameterUInt::get(int index) {
uint64_t MdfChannelConversion::ParameterUInt(uint16_t index) {
return conversion_ != nullptr ?
conversion_->ParameterUint(static_cast<uint32_t>(index)) : 0;
conversion_->ParameterUint(index): 0;
}

void MdfChannelConversion::ParameterUInt(uint16_t index,
uint64_t parameter) {
if (conversion_ != nullptr) {
conversion_->Parameter(index, parameter);
}
}

uint16_t MdfChannelConversion::NofReferences::get() {
return conversion_ != nullptr ? conversion_->NofReferences() : 0;
}

String^ MdfChannelConversion::Reference(uint16_t index) {
return conversion_ != nullptr ?
MdfLibrary::Utf8Conversion(
conversion_->Reference(index)) :
gcnew String("");
}

void MdfChannelConversion::ParameterUInt::set(int index, uint64_t parameter) {
void MdfChannelConversion::Reference(uint16_t index, String^ reference) {
if (conversion_ != nullptr) {
conversion_->Parameter(static_cast<uint32_t>(index), parameter);
conversion_->Reference(index,
MdfLibrary::Utf8Conversion(reference));
}
}

Expand Down
22 changes: 14 additions & 8 deletions mdflibrary/src/MdfChannelConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ public ref class MdfChannelConversion {
property MdfChannelConversion^ Inverse { MdfChannelConversion^ get(); }
property MdfMetaData^ MetaData { MdfMetaData^ get(); }
property String^ Formula { String^ get(); void set(String^ formula); }
property double Parameter[int] {
double get( int index);
void set(int index, double parameter);
}
property uint64_t ParameterUInt[int] {
uint64_t get( int index);
void set(int index, uint64_t parameter);
}

property uint16_t NofParameters { uint16_t get(); }
double Parameter(uint16_t index);
void Parameter(uint16_t index, double parameter);

uint64_t ParameterUInt( uint16_t index);
void ParameterUInt(uint16_t index, uint64_t parameter);


property uint16_t NofReferences { uint16_t get(); }
String^ Reference( uint16_t index);
void Reference(uint16_t index, String^ reference);


MdfChannelConversion^ CreateInverse();
MdfMetaData^ CreateMetaData();
private:
Expand Down
Loading

0 comments on commit fcd9109

Please sign in to comment.