Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion include/bmi_lgar.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private:
void serialize_wetting_front_list(Archive &ar, wetting_front **head, int &count);

void new_serialized();
void load_serialized(const char* data);
void load_serialized(char* data);
void free_serialized();
};

Expand Down
12 changes: 11 additions & 1 deletion include/vecbuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ class vecbuf : public std::basic_streambuf<CharT> {
// Forwarder for std::vector::clear()
constexpr void clear() { vector_.clear(); }

// Forwarder for std::vector::resize(size)
constexpr void resize(size_type size) { vector_.resize(size); }

// Forwarder for std::vector::reserve
constexpr void reserve(size_type capacity) { vector_.reserve(capacity); setp_from_vector(); }

// Increase the capacity of the buffer by reserving the current_size + additional_capacity
constexpr void reserve_additional(size_type additional_capacity) { reserve(size() + additional_capacity); }

// Forwarder for std::vector::data
constexpr const value_type* data() const { return vector_.data(); }
constexpr value_type* data() { return vector_.data(); }

// Forwarder for std::vector::size
constexpr size_type size() const { return vector_.size(); }
Expand Down Expand Up @@ -112,4 +115,11 @@ class vecbuf : public std::basic_streambuf<CharT> {

};

class membuf : public std::streambuf {
public:
membuf(char *begin, size_t size) {
this->setg(begin, begin, begin + size);
}
};

#endif
20 changes: 17 additions & 3 deletions src/bmi_lgar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ GetVarGrid(std::string name)
|| name.compare("groundwater_to_stream_recharge") == 0
|| name.compare("mass_balance") == 0
|| name.compare(NWM_PONDED_DEPTH_OUT_VAR) == 0
|| name.compare("reset_time") == 0
) // double
return 1;
else if (
Expand Down Expand Up @@ -1086,6 +1087,11 @@ SetValue (std::string name, void *src)
} else if (name.compare("serialization_create") == 0) {
this->new_serialized();
return;
} else if (name.compare("reset_time") == 0) {
// time_s and timesteps seems to be used exclusively for reporting current time
this->state->lgar_bmi_params.time_s = this->GetStartTime();
this->state->lgar_bmi_params.timesteps = 0;
return;
}
void * dest = NULL;
dest = this->GetValuePtr(name);
Expand Down Expand Up @@ -1430,20 +1436,28 @@ void BmiLGAR::serialize_wetting_front_list(Archive &ar, wetting_front **head, in
}

void BmiLGAR::new_serialized() {
this->m_serialized.clear();
// resize with reserved space for storing size
this->m_serialized.resize(sizeof(uint64_t));
boost::archive::binary_oarchive archive(this->m_serialized);
try {
archive << (*this);
this->m_serialized_length = this->m_serialized.size();
// get serialized size without header and copy size to the beginning of the buffer
uint64_t serialized_size = this->m_serialized_length - sizeof(uint64_t);
memcpy(this->m_serialized.data(), &serialized_size, sizeof(uint64_t));
} catch (const std::exception &e) {
Logger::Log(LogLevel::SEVERE, "Serializing LASAM encountered an error: %s", e.what());
this->free_serialized();
throw;
}
}

void BmiLGAR::load_serialized(const char* data) {
std::stringstream stream(data);
void BmiLGAR::load_serialized(char* data) {
// copy size from the start of data
uint64_t size;
memcpy(&size, data, sizeof(uint64_t));
// create stream from everything past the size header
membuf stream(data + sizeof(uint64_t), size);
boost::archive::binary_iarchive archive(stream);
try {
archive >> (*this);
Expand Down