mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 17:26:48 +08:00
add: libimp for utilities
This commit is contained in:
parent
2fb49eb3c4
commit
7d6eac4e6b
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file src/construct.h
|
||||
* @file libimp/construct.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Construct an object from a memory buffer
|
||||
* @date 2022-02-27
|
||||
@ -12,10 +12,10 @@
|
||||
#include <memory> // std::construct_at, std::destroy_at, std::addressof
|
||||
#include <cstddef>
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libipc/detect_plat.h"
|
||||
#include "libimp/def.h"
|
||||
#include "libimp/detect_plat.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
/**
|
||||
* @brief Creates an object at a given address, like 'construct_at' in c++20
|
||||
@ -25,7 +25,7 @@ LIBIPC_NAMESPACE_BEG_
|
||||
template <typename T, typename... A>
|
||||
auto construct(void *p, A &&... args)
|
||||
-> std::enable_if_t<::std::is_constructible<T, A...>::value, T *> {
|
||||
#if defined(LIBIPC_CPP_20)
|
||||
#if defined(LIBIMP_CPP_20)
|
||||
return std::construct_at(static_cast<T *>(p), std::forward<A>(args)...);
|
||||
#else
|
||||
return ::new (p) T(std::forward<A>(args)...);
|
||||
@ -45,7 +45,7 @@ auto construct(void *p, A &&... args)
|
||||
|
||||
template <typename T>
|
||||
void *destroy(T *p) noexcept {
|
||||
#if defined(LIBIPC_CPP_17)
|
||||
#if defined(LIBIMP_CPP_17)
|
||||
std::destroy_at(p);
|
||||
#else
|
||||
p->~T();
|
||||
@ -55,9 +55,9 @@ void *destroy(T *p) noexcept {
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
void *destroy(T (*p)[N]) noexcept {
|
||||
#if defined(LIBIPC_CPP_20)
|
||||
#if defined(LIBIMP_CPP_20)
|
||||
std::destroy_at(p);
|
||||
#elif defined(LIBIPC_CPP_17)
|
||||
#elif defined(LIBIMP_CPP_17)
|
||||
std::destroy(std::begin(*p), std::end(*p));
|
||||
#else
|
||||
for (auto &elem : *p) destroy(std::addressof(elem));
|
||||
@ -65,4 +65,4 @@ void *destroy(T (*p)[N]) noexcept {
|
||||
return p;
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file src/countof.h
|
||||
* @file libimp/countof.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Returns the size of the given range
|
||||
* @date 2022-03-01
|
||||
@ -9,10 +9,10 @@
|
||||
#include <type_traits> // std::declval, std::true_type, std::false_type
|
||||
#include <cstddef> // std::size_t
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libipc/utility/generic.h"
|
||||
#include "libimp/def.h"
|
||||
#include "libimp/generic.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
/**
|
||||
* @see https://en.cppreference.com/w/cpp/iterator/size
|
||||
@ -76,4 +76,4 @@ constexpr auto countof(C const &c) noexcept(noexcept(R::countof(c))) {
|
||||
return R::countof(c);
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
22
include/libimp/def.h
Normal file
22
include/libimp/def.h
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @file libimp/def.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the trivial configuration information
|
||||
* @date 2022-04-23
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#if !defined(LIBIMP_NAMESPACE)
|
||||
# define LIBIMP_NAMESPACE imp
|
||||
#endif
|
||||
#define LIBIMP_NAMESPACE_BEG_ namespace LIBIMP_NAMESPACE {
|
||||
#define LIBIMP_NAMESPACE_END_ }
|
||||
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
// constants
|
||||
|
||||
LIBIMP_NAMESPACE_END_
|
||||
155
include/libimp/detect_plat.h
Normal file
155
include/libimp/detect_plat.h
Normal file
@ -0,0 +1,155 @@
|
||||
/**
|
||||
* @file libimp/detect_plat.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define platform detection related interfaces
|
||||
* @date 2022-02-27
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// OS
|
||||
|
||||
#if defined(WINCE) || defined(_WIN32_WCE)
|
||||
# define LIBIMP_OS_WINCE
|
||||
#elif defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
||||
(defined(__x86_64) && defined(__MSYS__))
|
||||
#define LIBIMP_OS_WIN64
|
||||
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
|
||||
defined(__NT__) || defined(__MSYS__)
|
||||
# define LIBIMP_OS_WIN32
|
||||
#elif defined(__linux__) || defined(__linux)
|
||||
# define LIBIMP_OS_LINUX
|
||||
#elif defined(__QNX__) || defined(__QNXNTO__)
|
||||
# define LIBIMP_OS_QNX
|
||||
#elif defined(ANDROID) || defined(__ANDROID__)
|
||||
# define LIBIMP_OS_ANDROID
|
||||
#else
|
||||
# error "This OS is unsupported."
|
||||
#endif
|
||||
|
||||
#if defined(LIBIMP_OS_WIN32) || defined(LIBIMP_OS_WIN64) || \
|
||||
defined(LIBIMP_OS_WINCE)
|
||||
# define LIBIMP_OS_WIN
|
||||
#endif
|
||||
|
||||
// Compiler
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# define LIBIMP_CC_MSVC
|
||||
#elif defined(__GNUC__)
|
||||
# define LIBIMP_CC_GNUC
|
||||
#else
|
||||
# error "This compiler is unsupported."
|
||||
#endif
|
||||
|
||||
// Instruction set
|
||||
// @see https://sourceforge.net/p/predef/wiki/Architectures/
|
||||
|
||||
#if defined(_M_X64) || defined(_M_AMD64) || \
|
||||
defined(__x86_64__) || defined(__x86_64) || \
|
||||
defined(__amd64__) || defined(__amd64)
|
||||
# define LIBIMP_INSTR_X64
|
||||
#elif defined(_M_IA64) || defined(__IA64__) || defined(_IA64) || \
|
||||
defined(__ia64__) || defined(__ia64)
|
||||
# define LIBIMP_INSTR_I64
|
||||
#elif defined(_M_IX86) || defined(_X86_) || defined(__i386__) || defined(__i386)
|
||||
# define LIBIMP_INSTR_X86
|
||||
#elif defined(_M_ARM64) || defined(__arm64__) || defined(__aarch64__)
|
||||
# define LIBIMP_INSTR_ARM64
|
||||
#elif defined(_M_ARM) || defined(_ARM) || defined(__arm__) || defined(__arm)
|
||||
# define LIBIMP_INSTR_ARM32
|
||||
#else
|
||||
# error "This instruction set is unsupported."
|
||||
#endif
|
||||
|
||||
#if defined(LIBIMP_INSTR_X86) || defined(LIBIMP_INSTR_X64)
|
||||
# define LIBIMP_INSTR_X86_64
|
||||
#elif defined(LIBIMP_INSTR_ARM32) || defined(LIBIMP_INSTR_ARM64)
|
||||
# define LIBIMP_INSTR_ARM
|
||||
#endif
|
||||
|
||||
// Byte order
|
||||
|
||||
#if defined(__BYTE_ORDER__)
|
||||
# define LIBIMP_ENDIAN_BIG (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
# define LIBIMP_ENDIAN_LIT (!LIBIMP_ENDIAN_BIG)
|
||||
#else
|
||||
# define LIBIMP_ENDIAN_BIG (0)
|
||||
# define LIBIMP_ENDIAN_LIT (1)
|
||||
#endif
|
||||
|
||||
// C++ version
|
||||
|
||||
#if (__cplusplus >= 202002L) && !defined(LIBIMP_CPP_20)
|
||||
# define LIBIMP_CPP_20
|
||||
#endif
|
||||
#if (__cplusplus >= 201703L) && !defined(LIBIMP_CPP_17)
|
||||
# define LIBIMP_CPP_17
|
||||
#endif
|
||||
#if /*(__cplusplus >= 201402L) &&*/ !defined(LIBIMP_CPP_14)
|
||||
# define LIBIMP_CPP_14
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_CPP_20) && \
|
||||
!defined(LIBIMP_CPP_17) && \
|
||||
!defined(LIBIMP_CPP_14)
|
||||
# error "This C++ version is unsupported."
|
||||
#endif
|
||||
|
||||
// C++ attributes
|
||||
|
||||
#if defined(__has_cpp_attribute)
|
||||
# if __has_cpp_attribute(fallthrough)
|
||||
# define LIBIMP_FALLTHROUGH [[fallthrough]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(maybe_unused)
|
||||
# define LIBIMP_UNUSED [[maybe_unused]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(likely)
|
||||
# define LIBIMP_LIKELY(...) (__VA_ARGS__) [[likely]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(unlikely)
|
||||
# define LIBIMP_UNLIKELY(...) (__VA_ARGS__) [[unlikely]]
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_FALLTHROUGH)
|
||||
# if defined(LIBIMP_CC_GNUC)
|
||||
# define LIBIMP_FALLTHROUGH __attribute__((__fallthrough__))
|
||||
# else
|
||||
# define LIBIMP_FALLTHROUGH
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_UNUSED)
|
||||
# if defined(LIBIMP_CC_GNUC)
|
||||
# define LIBIMP_UNUSED __attribute__((__unused__))
|
||||
# elif defined(LIBIMP_CC_MSVC)
|
||||
# define LIBIMP_UNUSED __pragma(warning(suppress: 4100 4101 4189))
|
||||
# else
|
||||
# define LIBIMP_UNUSED
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_LIKELY)
|
||||
# if defined(__has_builtin)
|
||||
# if __has_builtin(__builtin_expect)
|
||||
# define LIBIMP_LIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 1))
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_LIKELY)
|
||||
# define LIBIMP_LIKELY(...) (__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_UNLIKELY)
|
||||
# if defined(__has_builtin)
|
||||
# if __has_builtin(__builtin_expect)
|
||||
# define LIBIMP_UNLIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 0))
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIMP_UNLIKELY)
|
||||
# define LIBIMP_UNLIKELY(...) (__VA_ARGS__)
|
||||
#endif
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file src/enum_cast.h
|
||||
* @file libimp/enum_cast.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Returns the underlying type of the given enum
|
||||
* @date 2022-03-01
|
||||
@ -8,13 +8,13 @@
|
||||
|
||||
#include <type_traits> // std::underlying_type_t
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libimp/def.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
template <typename E>
|
||||
constexpr auto enum_cast(E e) noexcept {
|
||||
return static_cast<std::underlying_type_t<E>>(e);
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
46
include/libimp/export.h
Normal file
46
include/libimp/export.h
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @file libimp/export.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the symbol export interfaces
|
||||
* @date 2022-02-27
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "libimp/detect_plat.h"
|
||||
|
||||
#if defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
# define LIBIMP_DECL_EXPORT Q_DECL_EXPORT
|
||||
# define LIBIMP_DECL_IMPORT Q_DECL_IMPORT
|
||||
|
||||
#else // defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
/*
|
||||
* Compiler & system detection for LIBIMP_DECL_EXPORT & LIBIMP_DECL_IMPORT.
|
||||
* Not using QtCore cause it shouldn't depend on Qt.
|
||||
*/
|
||||
# if defined(LIBIMP_CC_MSVC) || defined(LIBIMP_OS_WIN)
|
||||
# define LIBIMP_DECL_EXPORT __declspec(dllexport)
|
||||
# define LIBIMP_DECL_IMPORT __declspec(dllimport)
|
||||
# elif defined(LIBIMP_OS_ANDROID) || defined(LIBIMP_OS_LINUX) || defined(LIBIMP_CC_GNUC)
|
||||
# define LIBIMP_DECL_EXPORT __attribute__((visibility("default")))
|
||||
# define LIBIMP_DECL_IMPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define LIBIMP_DECL_EXPORT __attribute__((visibility("default")))
|
||||
# define LIBIMP_DECL_IMPORT __attribute__((visibility("default")))
|
||||
# endif
|
||||
|
||||
#endif // defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
/*
|
||||
* Define LIBIMP_EXPORT for exporting function & class.
|
||||
*/
|
||||
#ifndef LIBIMP_EXPORT
|
||||
# if defined(LIBIMP_LIBRARY_SHARED_BUILDING__)
|
||||
# define LIBIMP_EXPORT LIBIMP_DECL_EXPORT
|
||||
# elif defined(LIBIMP_LIBRARY_SHARED_USING__)
|
||||
# define LIBIMP_EXPORT LIBIMP_DECL_IMPORT
|
||||
# else
|
||||
# define LIBIMP_EXPORT
|
||||
# endif
|
||||
#endif /*LIBIMP_EXPORT*/
|
||||
@ -1,14 +1,14 @@
|
||||
/**
|
||||
* @file src/generic.h
|
||||
* @file libimp/generic.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Tools for generic programming
|
||||
* @date 2022-03-01
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libimp/def.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
/**
|
||||
* @brief Utility metafunction that maps a sequence of any types to the type void
|
||||
@ -17,4 +17,4 @@ LIBIPC_NAMESPACE_BEG_
|
||||
template <typename...>
|
||||
using void_t = void;
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file src/horrible_cast.h
|
||||
* @file libimp/horrible_cast.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @date 2022-04-17
|
||||
*/
|
||||
@ -8,9 +8,9 @@
|
||||
#include <type_traits> // std::decay_t
|
||||
#include <utility>
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libimp/def.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
template <typename T, typename U>
|
||||
constexpr auto horrible_cast(U &&in) noexcept
|
||||
@ -22,4 +22,4 @@ constexpr auto horrible_cast(U &&in) noexcept
|
||||
return u.out;
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file src/pimpl.h
|
||||
* @file libimp/pimpl.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Pointer To Implementation (pImpl) idiom
|
||||
* @date 2022-02-27
|
||||
@ -10,10 +10,10 @@
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
|
||||
#include "libipc/utility/construct.h"
|
||||
#include "libipc/def.h"
|
||||
#include "libimp/construct.h"
|
||||
#include "libimp/def.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
namespace pimpl {
|
||||
|
||||
template <typename T, typename R = T *>
|
||||
@ -70,4 +70,4 @@ public:
|
||||
};
|
||||
|
||||
} // namespace pimpl
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file result.h
|
||||
* @file libimp/result.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the return value type with a status code
|
||||
* @date 2022-04-17
|
||||
@ -9,12 +9,12 @@
|
||||
#include <ostream> // std::ostream
|
||||
#include <cstdint>
|
||||
|
||||
#include "libipc/def.h"
|
||||
#include "libipc/export.h"
|
||||
#include "libimp/def.h"
|
||||
#include "libimp/export.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
|
||||
class LIBIPC_EXPORT result {
|
||||
class LIBIMP_EXPORT result {
|
||||
std::uint64_t status_;
|
||||
|
||||
public:
|
||||
@ -34,4 +34,4 @@ public:
|
||||
friend std::ostream &operator<<(std::ostream &o, result const &r) noexcept;
|
||||
};
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file def.h
|
||||
* @file libipc/def.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the trivial configuration information
|
||||
* @date 2022-02-27
|
||||
|
||||
@ -1,155 +0,0 @@
|
||||
/**
|
||||
* @file detect_plat.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define platform detection related interfaces
|
||||
* @date 2022-02-27
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// OS
|
||||
|
||||
#if defined(WINCE) || defined(_WIN32_WCE)
|
||||
# define LIBIPC_OS_WINCE
|
||||
#elif defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
||||
(defined(__x86_64) && defined(__MSYS__))
|
||||
#define LIBIPC_OS_WIN64
|
||||
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
|
||||
defined(__NT__) || defined(__MSYS__)
|
||||
# define LIBIPC_OS_WIN32
|
||||
#elif defined(__linux__) || defined(__linux)
|
||||
# define LIBIPC_OS_LINUX
|
||||
#elif defined(__QNX__) || defined(__QNXNTO__)
|
||||
# define LIBIPC_OS_QNX
|
||||
#elif defined(ANDROID) || defined(__ANDROID__)
|
||||
# define LIBIPC_OS_ANDROID
|
||||
#else
|
||||
# error "This OS is unsupported."
|
||||
#endif
|
||||
|
||||
#if defined(LIBIPC_OS_WIN32) || defined(LIBIPC_OS_WIN64) || \
|
||||
defined(LIBIPC_OS_WINCE)
|
||||
# define LIBIPC_OS_WIN
|
||||
#endif
|
||||
|
||||
// Compiler
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# define LIBIPC_CC_MSVC
|
||||
#elif defined(__GNUC__)
|
||||
# define LIBIPC_CC_GNUC
|
||||
#else
|
||||
# error "This compiler is unsupported."
|
||||
#endif
|
||||
|
||||
// Instruction set
|
||||
// @see https://sourceforge.net/p/predef/wiki/Architectures/
|
||||
|
||||
#if defined(_M_X64) || defined(_M_AMD64) || \
|
||||
defined(__x86_64__) || defined(__x86_64) || \
|
||||
defined(__amd64__) || defined(__amd64)
|
||||
# define LIBIPC_INSTR_X64
|
||||
#elif defined(_M_IA64) || defined(__IA64__) || defined(_IA64) || \
|
||||
defined(__ia64__) || defined(__ia64)
|
||||
# define LIBIPC_INSTR_I64
|
||||
#elif defined(_M_IX86) || defined(_X86_) || defined(__i386__) || defined(__i386)
|
||||
# define LIBIPC_INSTR_X86
|
||||
#elif defined(_M_ARM64) || defined(__arm64__) || defined(__aarch64__)
|
||||
# define LIBIPC_INSTR_ARM64
|
||||
#elif defined(_M_ARM) || defined(_ARM) || defined(__arm__) || defined(__arm)
|
||||
# define LIBIPC_INSTR_ARM32
|
||||
#else
|
||||
# error "This instruction set is unsupported."
|
||||
#endif
|
||||
|
||||
#if defined(LIBIPC_INSTR_X86) || defined(LIBIPC_INSTR_X64)
|
||||
# define LIBIPC_INSTR_X86_64
|
||||
#elif defined(LIBIPC_INSTR_ARM32) || defined(LIBIPC_INSTR_ARM64)
|
||||
# define LIBIPC_INSTR_ARM
|
||||
#endif
|
||||
|
||||
// Byte order
|
||||
|
||||
#if defined(__BYTE_ORDER__)
|
||||
# define LIBIPC_ENDIAN_BIG (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
# define LIBIPC_ENDIAN_LIT (!LIBIPC_ENDIAN_BIG)
|
||||
#else
|
||||
# define LIBIPC_ENDIAN_BIG (0)
|
||||
# define LIBIPC_ENDIAN_LIT (1)
|
||||
#endif
|
||||
|
||||
// C++ version
|
||||
|
||||
#if (__cplusplus >= 202002L) && !defined(LIBIPC_CPP_20)
|
||||
# define LIBIPC_CPP_20
|
||||
#endif
|
||||
#if (__cplusplus >= 201703L) && !defined(LIBIPC_CPP_17)
|
||||
# define LIBIPC_CPP_17
|
||||
#endif
|
||||
#if /*(__cplusplus >= 201402L) &&*/ !defined(LIBIPC_CPP_14)
|
||||
# define LIBIPC_CPP_14
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_CPP_20) && \
|
||||
!defined(LIBIPC_CPP_17) && \
|
||||
!defined(LIBIPC_CPP_14)
|
||||
# error "This C++ version is unsupported."
|
||||
#endif
|
||||
|
||||
// C++ attributes
|
||||
|
||||
#if defined(__has_cpp_attribute)
|
||||
# if __has_cpp_attribute(fallthrough)
|
||||
# define LIBIPC_FALLTHROUGH [[fallthrough]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(maybe_unused)
|
||||
# define LIBIPC_UNUSED [[maybe_unused]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(likely)
|
||||
# define LIBIPC_LIKELY(...) (__VA_ARGS__) [[likely]]
|
||||
# endif
|
||||
# if __has_cpp_attribute(unlikely)
|
||||
# define LIBIPC_UNLIKELY(...) (__VA_ARGS__) [[unlikely]]
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_FALLTHROUGH)
|
||||
# if defined(LIBIPC_CC_GNUC)
|
||||
# define LIBIPC_FALLTHROUGH __attribute__((__fallthrough__))
|
||||
# else
|
||||
# define LIBIPC_FALLTHROUGH
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_UNUSED)
|
||||
# if defined(LIBIPC_CC_GNUC)
|
||||
# define LIBIPC_UNUSED __attribute__((__unused__))
|
||||
# elif defined(LIBIPC_CC_MSVC)
|
||||
# define LIBIPC_UNUSED __pragma(warning(suppress: 4100 4101 4189))
|
||||
# else
|
||||
# define LIBIPC_UNUSED
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_LIKELY)
|
||||
# if defined(__has_builtin)
|
||||
# if __has_builtin(__builtin_expect)
|
||||
# define LIBIPC_LIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 1))
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_LIKELY)
|
||||
# define LIBIPC_LIKELY(...) (__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_UNLIKELY)
|
||||
# if defined(__has_builtin)
|
||||
# if __has_builtin(__builtin_expect)
|
||||
# define LIBIPC_UNLIKELY(...) (__builtin_expect(!!(__VA_ARGS__), 0))
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined(LIBIPC_UNLIKELY)
|
||||
# define LIBIPC_UNLIKELY(...) (__VA_ARGS__)
|
||||
#endif
|
||||
@ -1,46 +0,0 @@
|
||||
/**
|
||||
* @file export.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the symbol export interfaces
|
||||
* @date 2022-02-27
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "libipc/detect_plat.h"
|
||||
|
||||
#if defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
# define LIBIPC_DECL_EXPORT Q_DECL_EXPORT
|
||||
# define LIBIPC_DECL_IMPORT Q_DECL_IMPORT
|
||||
|
||||
#else // defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
/*
|
||||
* Compiler & system detection for LIBIPC_DECL_EXPORT & LIBIPC_DECL_IMPORT.
|
||||
* Not using QtCore cause it shouldn't depend on Qt.
|
||||
*/
|
||||
# if defined(LIBIPC_CC_MSVC) || defined(LIBIPC_OS_WIN)
|
||||
# define LIBIPC_DECL_EXPORT __declspec(dllexport)
|
||||
# define LIBIPC_DECL_IMPORT __declspec(dllimport)
|
||||
# elif defined(LIBIPC_OS_ANDROID) || defined(LIBIPC_OS_LINUX) || defined(LIBIPC_CC_GNUC)
|
||||
# define LIBIPC_DECL_EXPORT __attribute__((visibility("default")))
|
||||
# define LIBIPC_DECL_IMPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define LIBIPC_DECL_EXPORT __attribute__((visibility("default")))
|
||||
# define LIBIPC_DECL_IMPORT __attribute__((visibility("default")))
|
||||
# endif
|
||||
|
||||
#endif // defined(Q_DECL_EXPORT) && defined(Q_DECL_IMPORT)
|
||||
|
||||
/*
|
||||
* Define LIBIPC_EXPORT for exporting function & class.
|
||||
*/
|
||||
#ifndef LIBIPC_EXPORT
|
||||
# if defined(LIBIPC_LIBRARY_SHARED_BUILDING__)
|
||||
# define LIBIPC_EXPORT LIBIPC_DECL_EXPORT
|
||||
# elif defined(LIBIPC_LIBRARY_SHARED_USING__)
|
||||
# define LIBIPC_EXPORT LIBIPC_DECL_IMPORT
|
||||
# else
|
||||
# define LIBIPC_EXPORT
|
||||
# endif
|
||||
#endif /*LIBIPC_EXPORT*/
|
||||
@ -1,14 +1,14 @@
|
||||
/**
|
||||
* @file mmap.h
|
||||
* @file libipc/mmap.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the methods of memory-mapped file I/O
|
||||
* @date 2022-04-17
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "libimp/export.h"
|
||||
#include "libimp/result.h"
|
||||
#include "libipc/def.h"
|
||||
#include "libipc/export.h"
|
||||
#include "libipc/result.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file shm.h
|
||||
* @file libipc/shm.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define the shared memory access interface
|
||||
* @date 2022-04-17
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file spin_lock.h
|
||||
* @file libipc/spin_lock.h
|
||||
* @author mutouyun (orz@orzz.org)
|
||||
* @brief Define spin locks
|
||||
* @date 2022-02-27
|
||||
@ -13,7 +13,7 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "libipc/detect_plat.h"
|
||||
#include "libimp/detect_plat.h"
|
||||
#include "libipc/def.h"
|
||||
|
||||
/**
|
||||
|
||||
@ -1,46 +1,3 @@
|
||||
project(ipc)
|
||||
|
||||
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc SRC_FILES)
|
||||
|
||||
file(GLOB HEAD_FILES
|
||||
${LIBIPC_PROJECT_DIR}/include/libipc/*.h)
|
||||
|
||||
if (LIBIPC_BUILD_SHARED_LIBS)
|
||||
add_library(${PROJECT_NAME} SHARED ${SRC_FILES} ${HEAD_FILES})
|
||||
target_compile_definitions(${PROJECT_NAME}
|
||||
INTERFACE
|
||||
LIBIPC_LIBRARY_SHARED_USING__
|
||||
PRIVATE
|
||||
LIBIPC_LIBRARY_SHARED_BUILDING__)
|
||||
else()
|
||||
add_library(${PROJECT_NAME} STATIC ${SRC_FILES} ${HEAD_FILES})
|
||||
endif()
|
||||
|
||||
# set output directory
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
|
||||
# set version
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
VERSION 2.0.0
|
||||
SOVERSION 4)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC ${LIBIPC_PROJECT_DIR}/include
|
||||
PRIVATE ${LIBIPC_PROJECT_DIR}/src)
|
||||
|
||||
if(NOT MSVC)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC
|
||||
$<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},QNX>>:pthread>
|
||||
$<$<NOT:$<OR:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_SYSTEM_NAME},QNX>>>:rt>)
|
||||
endif()
|
||||
|
||||
install(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
RUNTIME DESTINATION bin
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib)
|
||||
add_subdirectory(libimp)
|
||||
add_subdirectory(libipc)
|
||||
|
||||
25
src/libimp/CMakeLists.txt
Normal file
25
src/libimp/CMakeLists.txt
Normal file
@ -0,0 +1,25 @@
|
||||
project(imp)
|
||||
|
||||
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libimp SRC_FILES)
|
||||
|
||||
file(GLOB HEAD_FILES
|
||||
${LIBIPC_PROJECT_DIR}/include/libimp/*.h)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
# set output directory
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC ${LIBIPC_PROJECT_DIR}/include
|
||||
PRIVATE ${LIBIPC_PROJECT_DIR}/src)
|
||||
|
||||
install(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
RUNTIME DESTINATION bin
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib)
|
||||
@ -1,8 +1,8 @@
|
||||
|
||||
#include "libipc/result.h"
|
||||
#include "libipc/utility/horrible_cast.h"
|
||||
#include "libimp/result.h"
|
||||
#include "libimp/horrible_cast.h"
|
||||
|
||||
LIBIPC_NAMESPACE_BEG_
|
||||
LIBIMP_NAMESPACE_BEG_
|
||||
namespace {
|
||||
|
||||
struct result_code_info {
|
||||
@ -47,4 +47,4 @@ std::ostream &operator<<(std::ostream &o, result const &r) noexcept {
|
||||
return o;
|
||||
}
|
||||
|
||||
LIBIPC_NAMESPACE_END_
|
||||
LIBIMP_NAMESPACE_END_
|
||||
47
src/libipc/CMakeLists.txt
Normal file
47
src/libipc/CMakeLists.txt
Normal file
@ -0,0 +1,47 @@
|
||||
project(ipc)
|
||||
|
||||
aux_source_directory(${LIBIPC_PROJECT_DIR}/src/libipc SRC_FILES)
|
||||
|
||||
file(GLOB HEAD_FILES
|
||||
${LIBIPC_PROJECT_DIR}/include/libipc/*.h)
|
||||
|
||||
if (LIBIPC_BUILD_SHARED_LIBS)
|
||||
add_library(${PROJECT_NAME} SHARED ${SRC_FILES} ${HEAD_FILES})
|
||||
target_compile_definitions(${PROJECT_NAME}
|
||||
INTERFACE
|
||||
LIBIPC_LIBRARY_SHARED_USING__
|
||||
PRIVATE
|
||||
LIBIPC_LIBRARY_SHARED_BUILDING__)
|
||||
else()
|
||||
add_library(${PROJECT_NAME} STATIC ${SRC_FILES} ${HEAD_FILES})
|
||||
endif()
|
||||
|
||||
# set output directory
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
||||
|
||||
# set version
|
||||
set_target_properties(${PROJECT_NAME}
|
||||
PROPERTIES
|
||||
VERSION 2.0.0
|
||||
SOVERSION 4)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC ${LIBIPC_PROJECT_DIR}/include
|
||||
PRIVATE ${LIBIPC_PROJECT_DIR}/src)
|
||||
|
||||
if(NOT MSVC)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC
|
||||
$<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},QNX>>:pthread>
|
||||
$<$<NOT:$<OR:$<STREQUAL:${CMAKE_SYSTEM_NAME},Windows>,$<STREQUAL:${CMAKE_SYSTEM_NAME},QNX>>>:rt>)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC imp)
|
||||
|
||||
install(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
RUNTIME DESTINATION bin
|
||||
LIBRARY DESTINATION lib
|
||||
ARCHIVE DESTINATION lib)
|
||||
@ -21,5 +21,5 @@ file(GLOB HEAD_FILES ${LIBIPC_PROJECT_DIR}/test/*.h)
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
link_directories(${LIBIPC_PROJECT_DIR}/3rdparty/gperftools)
|
||||
target_link_libraries(${PROJECT_NAME} gtest gtest_main ipc)
|
||||
target_link_libraries(${PROJECT_NAME} gtest gtest_main imp ipc)
|
||||
#target_link_libraries(${PROJECT_NAME} tcmalloc_minimal)
|
||||
|
||||
@ -4,45 +4,45 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "libipc/detect_plat.h"
|
||||
#include "libimp/detect_plat.h"
|
||||
|
||||
namespace {
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(detect_plat, os) {
|
||||
#if defined(LIBIPC_OS_WINCE)
|
||||
std::cout << "LIBIPC_OS_WINCE\n";
|
||||
#elif defined(LIBIPC_OS_WIN)
|
||||
std::cout << "LIBIPC_OS_WIN\n";
|
||||
#elif defined(LIBIPC_OS_LINUX)
|
||||
std::cout << "LIBIPC_OS_LINUX\n";
|
||||
#elif defined(LIBIPC_OS_QNX)
|
||||
std::cout << "LIBIPC_OS_QNX\n";
|
||||
#elif defined(LIBIPC_OS_ANDROID)
|
||||
std::cout << "LIBIPC_OS_ANDROID\n";
|
||||
#if defined(LIBIMP_OS_WINCE)
|
||||
std::cout << "LIBIMP_OS_WINCE\n";
|
||||
#elif defined(LIBIMP_OS_WIN)
|
||||
std::cout << "LIBIMP_OS_WIN\n";
|
||||
#elif defined(LIBIMP_OS_LINUX)
|
||||
std::cout << "LIBIMP_OS_LINUX\n";
|
||||
#elif defined(LIBIMP_OS_QNX)
|
||||
std::cout << "LIBIMP_OS_QNX\n";
|
||||
#elif defined(LIBIMP_OS_ANDROID)
|
||||
std::cout << "LIBIMP_OS_ANDROID\n";
|
||||
#else
|
||||
ASSERT_TRUE(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(detect_plat, cc) {
|
||||
#if defined(LIBIPC_CC_MSVC)
|
||||
std::cout << "LIBIPC_CC_MSVC\n";
|
||||
#elif defined(LIBIPC_CC_GNUC)
|
||||
std::cout << "LIBIPC_CC_GNUC\n";
|
||||
#if defined(LIBIMP_CC_MSVC)
|
||||
std::cout << "LIBIMP_CC_MSVC\n";
|
||||
#elif defined(LIBIMP_CC_GNUC)
|
||||
std::cout << "LIBIMP_CC_GNUC\n";
|
||||
#else
|
||||
ASSERT_TRUE(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(detect_plat, cpp) {
|
||||
#if defined(LIBIPC_CPP_20)
|
||||
std::cout << "LIBIPC_CPP_20\n";
|
||||
#elif defined(LIBIPC_CPP_17)
|
||||
std::cout << "LIBIPC_CPP_17\n";
|
||||
#elif defined(LIBIPC_CPP_14)
|
||||
std::cout << "LIBIPC_CPP_14\n";
|
||||
#if defined(LIBIMP_CPP_20)
|
||||
std::cout << "LIBIMP_CPP_20\n";
|
||||
#elif defined(LIBIMP_CPP_17)
|
||||
std::cout << "LIBIMP_CPP_17\n";
|
||||
#elif defined(LIBIMP_CPP_14)
|
||||
std::cout << "LIBIMP_CPP_14\n";
|
||||
#else
|
||||
ASSERT_TRUE(false);
|
||||
#endif
|
||||
@ -57,18 +57,18 @@ TEST(detect_plat, byte_order) {
|
||||
c.a = 1;
|
||||
return c.b == 1;
|
||||
};
|
||||
EXPECT_EQ(!!LIBIPC_ENDIAN_LIT, is_endian_little());
|
||||
EXPECT_NE(!!LIBIPC_ENDIAN_BIG, is_endian_little());
|
||||
EXPECT_EQ(!!LIBIMP_ENDIAN_LIT, is_endian_little());
|
||||
EXPECT_NE(!!LIBIMP_ENDIAN_BIG, is_endian_little());
|
||||
}
|
||||
|
||||
TEST(detect_plat, fallthrough) {
|
||||
switch (0) {
|
||||
case 0:
|
||||
std::cout << "fallthrough 0\n";
|
||||
LIBIPC_FALLTHROUGH;
|
||||
LIBIMP_FALLTHROUGH;
|
||||
case 1:
|
||||
std::cout << "fallthrough 1\n";
|
||||
LIBIPC_FALLTHROUGH;
|
||||
LIBIMP_FALLTHROUGH;
|
||||
default:
|
||||
std::cout << "fallthrough default\n";
|
||||
break;
|
||||
@ -76,14 +76,14 @@ TEST(detect_plat, fallthrough) {
|
||||
}
|
||||
|
||||
TEST(detect_plat, unused) {
|
||||
LIBIPC_UNUSED int abc;
|
||||
LIBIMP_UNUSED int abc;
|
||||
}
|
||||
|
||||
TEST(detect_plat, likely_unlikely) {
|
||||
int xx = sizeof(int);
|
||||
if LIBIPC_LIKELY(xx < sizeof(long long)) {
|
||||
if LIBIMP_LIKELY(xx < sizeof(long long)) {
|
||||
std::cout << "sizeof(int) < sizeof(long long)\n";
|
||||
} else if LIBIPC_UNLIKELY(xx < sizeof(char)) {
|
||||
} else if LIBIMP_UNLIKELY(xx < sizeof(char)) {
|
||||
std::cout << "sizeof(int) < sizeof(char)\n";
|
||||
} else {
|
||||
std::cout << "sizeof(int) < whatever\n";
|
||||
|
||||
@ -3,10 +3,10 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "libipc/result.h"
|
||||
#include "libimp/result.h"
|
||||
|
||||
TEST(result, ok) {
|
||||
ipc::result ret;
|
||||
imp::result ret;
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_FALSE(ret.ok());
|
||||
EXPECT_EQ(ret.code(), 0);
|
||||
@ -23,7 +23,7 @@ TEST(result, ok) {
|
||||
}
|
||||
|
||||
TEST(result, code) {
|
||||
ipc::result ret(true, 1234);
|
||||
imp::result ret(true, 1234);
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(ret.ok());
|
||||
EXPECT_EQ(ret.code(), 1234);
|
||||
@ -40,16 +40,16 @@ TEST(result, code) {
|
||||
}
|
||||
|
||||
TEST(result, compare) {
|
||||
ipc::result r1, r2;
|
||||
imp::result r1, r2;
|
||||
EXPECT_EQ(r1, r2);
|
||||
|
||||
ipc::result r3(true);
|
||||
imp::result r3(true);
|
||||
EXPECT_NE(r1, r3);
|
||||
|
||||
ipc::result r4(true, 222222);
|
||||
imp::result r4(true, 222222);
|
||||
EXPECT_NE(r3, r4);
|
||||
|
||||
ipc::result r5(false, 222222);
|
||||
imp::result r5(false, 222222);
|
||||
EXPECT_NE(r4, r5);
|
||||
r3 = r5;
|
||||
EXPECT_EQ(r3, r5);
|
||||
@ -57,17 +57,17 @@ TEST(result, compare) {
|
||||
|
||||
TEST(result, print) {
|
||||
std::stringstream ss;
|
||||
ipc::result r1;
|
||||
imp::result r1;
|
||||
ss << r1;
|
||||
EXPECT_EQ(ss.str(), "[fail, code = 0]");
|
||||
|
||||
ss = {};
|
||||
ipc::result r2(true, 65537);
|
||||
imp::result r2(true, 65537);
|
||||
ss << r2;
|
||||
EXPECT_EQ(ss.str(), "[succ, code = 65537]");
|
||||
|
||||
ss = {};
|
||||
ipc::result r3(true);
|
||||
imp::result r3(true);
|
||||
ss << r3;
|
||||
EXPECT_EQ(ss.str(), "[succ, code = 0]");
|
||||
}
|
||||
@ -5,12 +5,11 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "libipc/utility/construct.h"
|
||||
#include "libipc/utility/pimpl.h"
|
||||
#include "libipc/utility/countof.h"
|
||||
#include "libipc/utility/horrible_cast.h"
|
||||
|
||||
#include "libipc/detect_plat.h"
|
||||
#include "libimp/construct.h"
|
||||
#include "libimp/pimpl.h"
|
||||
#include "libimp/countof.h"
|
||||
#include "libimp/horrible_cast.h"
|
||||
#include "libimp/detect_plat.h"
|
||||
|
||||
TEST(utility, construct) {
|
||||
struct Foo {
|
||||
@ -19,11 +18,11 @@ TEST(utility, construct) {
|
||||
char c_;
|
||||
};
|
||||
std::aligned_storage_t<sizeof(Foo)> foo;
|
||||
Foo *pfoo = ipc::construct<Foo>(&foo, 123, short{321}, '1');
|
||||
Foo *pfoo = imp::construct<Foo>(&foo, 123, short{321}, '1');
|
||||
EXPECT_EQ(pfoo->a_, 123);
|
||||
EXPECT_EQ(pfoo->b_, 321);
|
||||
EXPECT_EQ(pfoo->c_, '1');
|
||||
ipc::destroy(pfoo);
|
||||
imp::destroy(pfoo);
|
||||
|
||||
static int bar_test_flag = 0;
|
||||
struct Bar : Foo {
|
||||
@ -34,34 +33,34 @@ TEST(utility, construct) {
|
||||
~Bar() { --bar_test_flag; }
|
||||
};
|
||||
std::aligned_storage_t<sizeof(Bar)> bar;
|
||||
Bar *pbar = ipc::construct<Bar>(&bar, 123, short(321), '1');
|
||||
Bar *pbar = imp::construct<Bar>(&bar, 123, short(321), '1');
|
||||
EXPECT_EQ(pbar->a_, 123);
|
||||
EXPECT_EQ(pbar->b_, 321);
|
||||
EXPECT_EQ(pbar->c_, '1');
|
||||
EXPECT_EQ(bar_test_flag, 1);
|
||||
ipc::destroy(pbar);
|
||||
imp::destroy(pbar);
|
||||
EXPECT_EQ(bar_test_flag, 0);
|
||||
|
||||
std::aligned_storage_t<sizeof(Bar)> bars[3];
|
||||
for (auto &b : bars) {
|
||||
auto pb = ipc::construct<Bar>(&b, 321, short(123), '3');
|
||||
auto pb = imp::construct<Bar>(&b, 321, short(123), '3');
|
||||
EXPECT_EQ(pb->a_, 321);
|
||||
EXPECT_EQ(pb->b_, 123);
|
||||
EXPECT_EQ(pb->c_, '3');
|
||||
}
|
||||
//EXPECT_EQ(bar_test_flag, ipc::countof(bars));
|
||||
ipc::destroy(reinterpret_cast<Bar(*)[3]>(&bars));
|
||||
//EXPECT_EQ(bar_test_flag, imp::countof(bars));
|
||||
imp::destroy(reinterpret_cast<Bar(*)[3]>(&bars));
|
||||
EXPECT_EQ(bar_test_flag, 0);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
struct Foo : ipc::pimpl::Obj<Foo> {
|
||||
struct Foo : imp::pimpl::Obj<Foo> {
|
||||
int *pi_;
|
||||
Foo(int *pi) : pi_(pi) {}
|
||||
};
|
||||
|
||||
struct Bar : ipc::pimpl::Obj<Bar> {
|
||||
struct Bar : imp::pimpl::Obj<Bar> {
|
||||
int *pi_;
|
||||
int *pj_;
|
||||
Bar(int *pi, int *pj) : pi_(pi), pj_(pj) {}
|
||||
@ -70,24 +69,24 @@ struct Bar : ipc::pimpl::Obj<Bar> {
|
||||
} // namespace
|
||||
|
||||
TEST(utility, pimpl_is_comfortable) {
|
||||
EXPECT_TRUE ((ipc::pimpl::is_comfortable<std::int32_t, std::int64_t>::value));
|
||||
EXPECT_TRUE ((ipc::pimpl::is_comfortable<std::int64_t, std::int64_t>::value));
|
||||
EXPECT_FALSE((ipc::pimpl::is_comfortable<std::int64_t, std::int32_t>::value));
|
||||
EXPECT_TRUE ((imp::pimpl::is_comfortable<std::int32_t, std::int64_t>::value));
|
||||
EXPECT_TRUE ((imp::pimpl::is_comfortable<std::int64_t, std::int64_t>::value));
|
||||
EXPECT_FALSE((imp::pimpl::is_comfortable<std::int64_t, std::int32_t>::value));
|
||||
|
||||
EXPECT_TRUE ((ipc::pimpl::is_comfortable<Foo>::value));
|
||||
EXPECT_FALSE((ipc::pimpl::is_comfortable<Bar>::value));
|
||||
EXPECT_TRUE ((imp::pimpl::is_comfortable<Foo>::value));
|
||||
EXPECT_FALSE((imp::pimpl::is_comfortable<Bar>::value));
|
||||
}
|
||||
|
||||
TEST(utility, pimpl_inherit) {
|
||||
int i = 123;
|
||||
Foo *pfoo = Foo::make(&i);
|
||||
EXPECT_EQ(ipc::pimpl::get(pfoo)->pi_, &i);
|
||||
EXPECT_EQ(imp::pimpl::get(pfoo)->pi_, &i);
|
||||
pfoo->clear();
|
||||
|
||||
int j = 321;
|
||||
Bar *pbar = Bar::make(&i, &j);
|
||||
EXPECT_EQ(ipc::pimpl::get(pbar)->pi_, &i);
|
||||
EXPECT_EQ(ipc::pimpl::get(pbar)->pj_, &j);
|
||||
EXPECT_EQ(imp::pimpl::get(pbar)->pi_, &i);
|
||||
EXPECT_EQ(imp::pimpl::get(pbar)->pj_, &j);
|
||||
pbar->clear();
|
||||
}
|
||||
|
||||
@ -95,14 +94,14 @@ TEST(utility, countof) {
|
||||
struct {
|
||||
constexpr int Size() const noexcept { return 3; }
|
||||
} sv;
|
||||
EXPECT_FALSE(ipc::detail::countof_trait_has_size<decltype(sv)>::value);
|
||||
EXPECT_TRUE (ipc::detail::countof_trait_has_Size<decltype(sv)>::value);
|
||||
EXPECT_FALSE(imp::detail::countof_trait_has_size<decltype(sv)>::value);
|
||||
EXPECT_TRUE (imp::detail::countof_trait_has_Size<decltype(sv)>::value);
|
||||
|
||||
std::vector<int> vec {1, 2, 3, 4, 5};
|
||||
int arr[] {7, 6, 5, 4, 3, 2, 1};
|
||||
EXPECT_EQ(ipc::countof(sv) , sv.Size());
|
||||
EXPECT_EQ(ipc::countof(vec), vec.size());
|
||||
EXPECT_EQ(ipc::countof(arr), sizeof(arr) / sizeof(arr[0]));
|
||||
EXPECT_EQ(imp::countof(sv) , sv.Size());
|
||||
EXPECT_EQ(imp::countof(vec), vec.size());
|
||||
EXPECT_EQ(imp::countof(arr), sizeof(arr) / sizeof(arr[0]));
|
||||
}
|
||||
|
||||
TEST(utility, horrible_cast) {
|
||||
@ -112,11 +111,11 @@ TEST(utility, horrible_cast) {
|
||||
|
||||
struct B {
|
||||
char a_[sizeof(int)];
|
||||
} b = ipc::horrible_cast<B>(a);
|
||||
} b = imp::horrible_cast<B>(a);
|
||||
|
||||
EXPECT_EQ(b.a_[1], 0);
|
||||
EXPECT_EQ(b.a_[2], 0);
|
||||
#if LIBIPC_ENDIAN_LIT
|
||||
#if LIBIMP_ENDIAN_LIT
|
||||
EXPECT_EQ(b.a_[0], 123);
|
||||
EXPECT_EQ(b.a_[3], 0);
|
||||
#else
|
||||
@ -124,5 +123,5 @@ TEST(utility, horrible_cast) {
|
||||
EXPECT_EQ(b.a_[0], 0);
|
||||
#endif
|
||||
|
||||
// ipc::horrible_cast<std::uint32_t>(0ll);
|
||||
// imp::horrible_cast<std::uint32_t>(0ll);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user