mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add 'result'
This commit is contained in:
parent
85931e7d20
commit
e5f722937e
16
include/libipc/mmap.h
Normal file
16
include/libipc/mmap.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* @file mmap.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Define the methods of memory-mapped file I/O
|
||||||
|
* @date 2022-04-17
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
#include "libipc/result.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
37
include/libipc/result.h
Normal file
37
include/libipc/result.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* @file result.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Define the return value type with a status code
|
||||||
|
* @date 2022-04-17
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ostream> // std::ostream
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
#include "libipc/export.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
class LIBIPC_EXPORT result {
|
||||||
|
std::uint64_t status_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
result() noexcept;
|
||||||
|
result(bool ok, std::uint64_t code = 0) noexcept;
|
||||||
|
|
||||||
|
std::uint64_t code() const noexcept;
|
||||||
|
bool ok() const noexcept;
|
||||||
|
|
||||||
|
explicit operator bool() const noexcept {
|
||||||
|
return ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator==(result const &lhs, result const &rhs) noexcept;
|
||||||
|
friend bool operator!=(result const &lhs, result const &rhs) noexcept;
|
||||||
|
|
||||||
|
friend std::ostream &operator<<(std::ostream &o, result const &r) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
@ -2,6 +2,14 @@
|
|||||||
* @file shm.h
|
* @file shm.h
|
||||||
* @author mutouyun (orz@orzz.org)
|
* @author mutouyun (orz@orzz.org)
|
||||||
* @brief Define the shared memory access interface
|
* @brief Define the shared memory access interface
|
||||||
* @date 2022-04-03
|
* @date 2022-04-17
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
|
|||||||
50
src/libipc/result.cpp
Normal file
50
src/libipc/result.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
#include "libipc/result.h"
|
||||||
|
#include "libipc/utility/horrible_cast.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct result_code_info {
|
||||||
|
std::uint64_t ok : 1;
|
||||||
|
std::uint64_t code: 63;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::uint64_t make_status(bool ok, std::uint64_t code) noexcept {
|
||||||
|
return horrible_cast<std::uint64_t>(result_code_info{ok ? 1ull : 0ull, code});
|
||||||
|
}
|
||||||
|
|
||||||
|
result_code_info info_cast(std::uint64_t status) noexcept {
|
||||||
|
return horrible_cast<result_code_info>(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
result::result() noexcept
|
||||||
|
: result(false, 0) {}
|
||||||
|
|
||||||
|
result::result(bool ok, std::uint64_t code) noexcept
|
||||||
|
: status_(make_status(ok, code)) {}
|
||||||
|
|
||||||
|
std::uint64_t result::code() const noexcept {
|
||||||
|
return info_cast(status_).code;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool result::ok() const noexcept {
|
||||||
|
return info_cast(status_).ok != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(result const &lhs, result const &rhs) noexcept {
|
||||||
|
return lhs.status_ == rhs.status_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(result const &lhs, result const &rhs) noexcept {
|
||||||
|
return lhs.status_ != rhs.status_;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &o, result const &r) noexcept {
|
||||||
|
o << "[" << (r.ok() ? "succ" : "fail") << ", code = " << r.code() << "]";
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
25
src/libipc/utility/horrible_cast.h
Normal file
25
src/libipc/utility/horrible_cast.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* @file src/horrible_cast.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @date 2022-04-17
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits> // std::decay_t
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
constexpr auto horrible_cast(U &&in) noexcept
|
||||||
|
-> std::enable_if_t<(sizeof(T) == sizeof(std::decay_t<U>)), T> {
|
||||||
|
union {
|
||||||
|
std::decay_t<U> in;
|
||||||
|
T out;
|
||||||
|
} u {std::forward<U>(in)};
|
||||||
|
return u.out;
|
||||||
|
}
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
@ -58,7 +58,7 @@ auto clear(T *p) noexcept -> std::enable_if_t<!is_comfortable<T>::value> {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Obj {
|
class Obj {
|
||||||
public:
|
public:
|
||||||
template <typename... A>
|
template <typename... A>
|
||||||
static T *make(A &&... args) {
|
static T *make(A &&... args) {
|
||||||
return pimpl::make<T>(std::forward<A>(args)...);
|
return pimpl::make<T>(std::forward<A>(args)...);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user