-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCWFGM_WeatherStream.Serialize.cpp
More file actions
117 lines (96 loc) · 3.96 KB
/
CWFGM_WeatherStream.Serialize.cpp
File metadata and controls
117 lines (96 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* WISE_Weather_Module: CWFGM_WeatherStream.Serialize.cpp
* Copyright (C) 2023 WISE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "propsysreplacement.h"
#include "CWFGM_WeatherStream.h"
#include "WeatherCom_ext.h"
#include "GridCom_ext.h"
#include "results.h"
#include "DayCondition.h"
#include <errno.h>
#ifdef DEBUG
#include <assert.h>
#endif
HRESULT CCWFGM_WeatherStream::newCondition(DailyCondition** cond)
{
*cond = new DailyCondition(&m_weatherCondition);
return S_OK;
}
HRESULT CCWFGM_WeatherStream::Import(const std::string & fileName, std::uint16_t options) {
if (!fileName.length()) return E_INVALIDARG;
SEM_BOOL engaged;
CRWThreadSemaphoreEngage engage(m_lock, SEM_TRUE, &engaged, 1000000LL);
if (!engaged) return ERROR_SCENARIO_SIMULATION_RUNNING;
if (m_weatherCondition.NumDays()) {
std::uint16_t purge = options & CWFGM_WEATHERSTREAM_IMPORT_PURGE;
std::uint16_t overwrite_append = options & (CWFGM_WEATHERSTREAM_IMPORT_SUPPORT_APPEND | CWFGM_WEATHERSTREAM_IMPORT_SUPPORT_OVERWRITE);
if (purge && overwrite_append)
return E_INVALIDARG;
if (options & (~(CWFGM_WEATHERSTREAM_IMPORT_PURGE | CWFGM_WEATHERSTREAM_IMPORT_SUPPORT_APPEND | CWFGM_WEATHERSTREAM_IMPORT_SUPPORT_OVERWRITE)))
return E_INVALIDARG;
if (!options)
return E_INVALIDARG;
}
HRESULT success = m_weatherCondition.Import(fileName.c_str(), options, nullptr);
m_bRequiresSave = true;
m_weatherCondition.m_options |= 0x00000020;
m_cache.Clear();
return success;
}
std::int32_t CCWFGM_WeatherStream::serialVersionUid(const SerializeProtoOptions& options) const noexcept {
return options.fileVersion();
}
WISE::WeatherProto::CwfgmWeatherStream* CCWFGM_WeatherStream::serialize(const SerializeProtoOptions& options) {
auto stream = new WISE::WeatherProto::CwfgmWeatherStream();
stream->set_version(serialVersionUid(options));
stream->set_allocated_condition(m_weatherCondition.serialize(options));
return stream;
}
CCWFGM_WeatherStream* CCWFGM_WeatherStream::deserialize(const google::protobuf::Message& message, std::shared_ptr<validation::validation_object> valid, const std::string& name) {
auto stream = dynamic_cast_assert<const WISE::WeatherProto::CwfgmWeatherStream*>(&message);
if (!stream)
{
if (valid)
valid->add_child_validation("WISE.WeatherProto.CwfgmWeatherStream", name, validation::error_level::SEVERE, validation::id::object_invalid, message.GetDescriptor()->name());
weak_assert(false);
throw ISerializeProto::DeserializeError("WISE.WeatherProto.CwfgmWeatherStation: Protobuf object invalid", ERROR_PROTOBUF_OBJECT_INVALID);
}
if ((stream->version() != 1) && (stream->version() != 2))
{
if (valid)
valid->add_child_validation("WISE.WeatherProto.CwfgmWeatherStream", name, validation::error_level::SEVERE, validation::id::version_mismatch, message.GetDescriptor()->name());
weak_assert(false);
throw ISerializeProto::DeserializeError("WISE.WeatherProto.CwfgmWeatherStation: Version is invalid", ERROR_PROTOBUF_OBJECT_VERSION_INVALID);
}
auto vt = validation::conditional_make_object(valid, "WISE.WeatherProto.CwfgmWeatherStream", name);
auto v = vt.lock();
try
{
m_weatherCondition.deserialize(stream->condition(), v, "condition");
}
catch (const ISerializeProto::DeserializeError& de)
{
m_loadWarning = de.what();
throw de;
}
catch (std::exception & e)
{
m_loadWarning = e.what();
throw e;
}
return this;
}