NGen
Loading...
Searching...
No Matches
protocol.hpp
1/*
2Author: Nels Frazier
3Copyright (C) 2025 Lynker
4------------------------------------------------------------------------
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16------------------------------------------------------------------------
17Version 0.3
18Remove "supported" member variable and added is_supported() method to protocol interface
19
20Version 0.2
21Enumerate protocol error types and add ProtocolError exception class
22Implement error handling via expected<T, ProtocolError> and error_or_warning
23Removed model member and required model reference in run(), check_support(), and initialize()
24Minor refactoring and style changes
25
26Version 0.1
27Virtual interface for BMI protocols
28*/
29
30#pragma once
31
32#include "Bmi_Adapter.hpp"
33#include "JSONProperty.hpp"
34#include <nonstd/expected.hpp>
35
36namespace models {
37namespace bmi {
38namespace protocols {
39using nonstd::expected;
40using nonstd::make_unexpected;
41
49
50class ProtocolError : public std::exception {
51 public:
52 ProtocolError() = delete;
53
54 ProtocolError(Error err, const std::string& message = "")
55 : err(std::move(err))
56 , message(std::move(message)) {
57 }
58
59 ProtocolError(const ProtocolError& other) = default;
60 ProtocolError(ProtocolError&& other) noexcept = default;
61 ProtocolError& operator=(const ProtocolError& other) = default;
62 ProtocolError& operator=(ProtocolError&& other) noexcept = default;
63 ~ProtocolError() = default;
64
65 auto to_string() const -> std::string {
66 switch (err) {
68 return "Error(Uninitialized Model)::" + message;
70 return "Warning(Unsupported Protocol)::" + message;
72 return "Error(Integration)::" + message;
74 return "Error(Protocol)::" + message;
76 return "Warning(Protocol)::" + message;
77 default:
78 return "Unknown Error: " + message;
79 }
80 }
81
82 auto error_code() const -> const Error& {
83 return err;
84 }
85
86 auto get_message() const -> const std::string& {
87 return message;
88 }
89
90 const char* what() const noexcept override {
92 return message.c_str();
93 }
94
95 private:
97 mutable std::string message;
98};
99
100struct Context {
102 const int total_steps;
103 const std::string& timestamp;
104 const std::string& id;
105};
106
107using ModelPtr = std::shared_ptr<models::bmi::Bmi_Adapter>;
109
116 public:
117 virtual ~NgenBmiProtocol() = default;
118
119 protected:
129 static auto error_or_warning(const ProtocolError& err) -> expected<void, ProtocolError> {
130 // Log warnings, but throw errors
131 switch (err.error_code()) {
133 throw err;
134 break;
139 std::cerr << err.to_string() << std::endl;
140 return make_unexpected<ProtocolError>(ProtocolError(std::move(err)));
141 default:
142 throw err;
143 }
144 assert(false && "Unreachable code reached in error_or_warning");
145 }
146
163 nsel_NODISCARD virtual auto run(const ModelPtr& model, const Context& ctx) const
164 -> expected<void, ProtocolError> = 0;
165
179 nsel_NODISCARD virtual expected<void, ProtocolError> check_support(const ModelPtr& model) = 0;
180
197 virtual auto initialize(const ModelPtr& model, const Properties& properties)
198 -> expected<void, ProtocolError> = 0;
199
204 virtual bool is_supported() const = 0;
205
215 friend class NgenBmiProtocols;
216};
217
218} // namespace protocols
219} // namespace bmi
220} // namespace models
Definition protocol.hpp:110
virtual bool is_supported() const =0
Whether the protocol is supported by the model.
virtual nsel_NODISCARD expected< void, ProtocolError > check_support(const ModelPtr &model)=0
Check if the BMI protocol is supported by the model.
virtual auto initialize(const ModelPtr &model, const Properties &properties) -> expected< void, ProtocolError >=0
Initialize the BMI protocol from a set of key/value properties.
virtual ~NgenBmiProtocol()=default
Abstract interface for a generic BMI protocol.
static auto error_or_warning(const ProtocolError &err) -> expected< void, ProtocolError >
Handle a ProtocolError by either throwing it or logging it as a warning.
Definition protocol.hpp:129
virtual nsel_NODISCARD auto run(const ModelPtr &model, const Context &ctx) const -> expected< void, ProtocolError >=0
Run the BMI protocol against the given model.
Definition protocols.hpp:45
Definition protocol.hpp:50
std::string message
Definition protocol.hpp:97
const char * what() const noexcept override
Definition protocol.hpp:90
Error err
Definition protocol.hpp:96
auto error_code() const -> const Error &
Definition protocol.hpp:82
ProtocolError & operator=(ProtocolError &&other) noexcept=default
ProtocolError(Error err, const std::string &message="")
Definition protocol.hpp:54
ProtocolError(const ProtocolError &other)=default
ProtocolError & operator=(const ProtocolError &other)=default
auto get_message() const -> const std::string &
Definition protocol.hpp:86
ProtocolError(ProtocolError &&other) noexcept=default
auto to_string() const -> std::string
Definition protocol.hpp:65
std::map< std::string, JSONProperty > PropertyMap
Shorthand for a mapping between strings and properties.
Definition JSONProperty.hpp:21
Definition deserialization.hpp:40
std::shared_ptr< models::bmi::Bmi_Adapter > ModelPtr
Definition protocol.hpp:107
geojson::PropertyMap Properties
Definition protocol.hpp:108
Error
Definition protocol.hpp:42
Definition AbstractCLibBmiAdapter.hpp:6
STL namespace.
Definition protocol.hpp:100
const std::string & id
Definition protocol.hpp:104
const std::string & timestamp
Definition protocol.hpp:103
const int total_steps
Definition protocol.hpp:102
const int current_time_step
Definition protocol.hpp:101