Skip to content

Commit 718b928

Browse files
Began developing an atomic wrapper
1 parent c0e26fb commit 718b928

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

include/rfl/parsing/Parser.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define RFL_PARSING_PARSER_HPP_
33

44
#include "Parser_array.hpp"
5+
#include "Parser_atomic.hpp"
56
#include "Parser_base.hpp"
67
#include "Parser_basic_type.hpp"
78
#include "Parser_box.hpp"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef RFL_PARSING_PARSER_ATOMIC_HPP_
2+
#define RFL_PARSING_PARSER_ATOMIC_HPP_
3+
4+
#include <atomic>
5+
#include <map>
6+
#include <type_traits>
7+
8+
#include "../DefaultVal.hpp"
9+
#include "AreReaderAndWriter.hpp"
10+
#include "Parent.hpp"
11+
#include "Parser_base.hpp"
12+
#include "schema/Type.hpp"
13+
14+
namespace rfl::parsing {
15+
16+
template <class R, class W, class T, class ProcessorsType>
17+
requires AreReaderAndWriter<R, W, std::atomic<T>>
18+
struct Parser<R, W, std::atomic<T>, ProcessorsType> {
19+
using InputVarType = typename R::InputVarType;
20+
21+
using ParentType = Parent<W>;
22+
23+
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
24+
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r, _var);
25+
}
26+
27+
template <class P>
28+
static void write(const W& _w, const std::atomic<T>& _a, const P& _parent) {
29+
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
30+
_w, _a.load(std::memory_order_relaxed), _parent);
31+
}
32+
33+
static schema::Type to_schema(
34+
std::map<std::string, schema::Type>* _definitions) {
35+
using U = std::remove_cvref_t<T>;
36+
return schema::Type{
37+
Parser<R, W, U, ProcessorsType>::to_schema(_definitions)};
38+
}
39+
};
40+
41+
} // namespace rfl::parsing
42+
43+
#endif

tests/json/test_atomic.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <atomic>
2+
#include <rfl.hpp>
3+
#include <rfl/json.hpp>
4+
#include <string>
5+
6+
#include "write_and_read.hpp"
7+
8+
namespace test_atomic {
9+
10+
struct Stats {
11+
std::atomic<std::uint64_t> bytes_downloaded;
12+
std::atomic<bool> finished;
13+
14+
Stats(Stats&& other) noexcept
15+
: bytes_downloaded(
16+
other.bytes_downloaded.load(std::memory_order_relaxed)),
17+
finished(other.finished.load(std::memory_order_relaxed)) {}
18+
19+
Stats& operator=(Stats&& other) noexcept {
20+
bytes_downloaded.store(
21+
other.bytes_downloaded.load(std::memory_order_relaxed),
22+
std::memory_order_relaxed);
23+
finished.store(other.finished.load(std::memory_order_relaxed),
24+
std::memory_order_relaxed);
25+
return *this;
26+
}
27+
28+
Stats(const std::uint64_t _bytes_downloaded, const bool _finished)
29+
: bytes_downloaded(_bytes_downloaded), finished(_finished) {}
30+
};
31+
32+
TEST(json, test_atomic) {
33+
auto stats = Stats(123456789, true);
34+
35+
write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
36+
}
37+
} // namespace test_atomic

0 commit comments

Comments
 (0)