-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ struct handle_types | |
{ | ||
invalid = 0, | ||
file, | ||
device, | ||
event, | ||
section, | ||
symlink, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <serialization.hpp> | ||
|
||
struct io_device | ||
{ | ||
io_device() = default; | ||
virtual ~io_device() = default; | ||
|
||
// TODO | ||
virtual void read() = 0; | ||
virtual void write() = 0; | ||
|
||
virtual void serialize(utils::buffer_serializer& buffer) const = 0; | ||
virtual void deserialize(utils::buffer_deserializer& buffer) = 0; | ||
}; | ||
|
||
// TODO | ||
inline std::unique_ptr<io_device> create_device(const std::wstring_view device) | ||
{ | ||
(void)device; | ||
return {}; | ||
} | ||
|
||
class io_device_container : public io_device | ||
{ | ||
public: | ||
io_device_container() = default; | ||
|
||
io_device_container(std::wstring device) | ||
: device_name_(std::move(device)) | ||
{ | ||
this->setup(); | ||
} | ||
|
||
void read() override | ||
{ | ||
this->assert_validity(); | ||
this->device_->read(); | ||
} | ||
|
||
void write() override | ||
{ | ||
this->assert_validity(); | ||
this->device_->write(); | ||
} | ||
|
||
void serialize(utils::buffer_serializer& buffer) const override | ||
{ | ||
this->assert_validity(); | ||
|
||
buffer.write_string(this->device_name_); | ||
this->device_->serialize(buffer); | ||
} | ||
|
||
void deserialize(utils::buffer_deserializer& buffer) override | ||
{ | ||
buffer.read_string(this->device_name_); | ||
this->setup(); | ||
this->device_->deserialize(buffer); | ||
} | ||
|
||
private: | ||
std::wstring device_name_{}; | ||
std::unique_ptr<io_device> device_{}; | ||
|
||
void setup() | ||
{ | ||
this->device_ = create_device(this->device_name_); | ||
} | ||
|
||
void assert_validity() const | ||
{ | ||
if (!this->device_) | ||
{ | ||
throw std::runtime_error("Device not created!"); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters