mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
调整ut;添加新utility小工具
This commit is contained in:
parent
9fc6cca601
commit
1ff5900a6d
@ -45,7 +45,7 @@ auto construct(void *p, A &&... args)
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void *destroy(T *p) noexcept {
|
void *destroy(T *p) noexcept {
|
||||||
#if defined(LIBIPC_CPP_17) && !defined(LIBIPC_CC_GNUC)
|
#if defined(LIBIPC_CPP_17)
|
||||||
std::destroy_at(p);
|
std::destroy_at(p);
|
||||||
#else
|
#else
|
||||||
p->~T();
|
p->~T();
|
||||||
@ -57,7 +57,7 @@ template <typename T, std::size_t N>
|
|||||||
void *destroy(T (*p)[N]) noexcept {
|
void *destroy(T (*p)[N]) noexcept {
|
||||||
#if defined(LIBIPC_CPP_20)
|
#if defined(LIBIPC_CPP_20)
|
||||||
std::destroy_at(p);
|
std::destroy_at(p);
|
||||||
#elif defined(LIBIPC_CPP_17) && !defined(LIBIPC_CC_GNUC)
|
#elif defined(LIBIPC_CPP_17)
|
||||||
std::destroy(std::begin(*p), std::end(*p));
|
std::destroy(std::begin(*p), std::end(*p));
|
||||||
#else
|
#else
|
||||||
for (auto &elem : *p) destroy(std::addressof(elem));
|
for (auto &elem : *p) destroy(std::addressof(elem));
|
||||||
|
|||||||
34
src/libipc/utility/countof.h
Normal file
34
src/libipc/utility/countof.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* @file src/countof.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Returns the size of the given range
|
||||||
|
* @date 2022-03-01
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef> // std::size_t
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://en.cppreference.com/w/cpp/iterator/size
|
||||||
|
*/
|
||||||
|
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
constexpr std::size_t countof(T const (&)[N]) noexcept {
|
||||||
|
return N;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename C>
|
||||||
|
constexpr auto countof(C const &c) -> decltype(c.size()) {
|
||||||
|
return c.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename C>
|
||||||
|
constexpr auto countof(C const &c) -> decltype(c.Size()) {
|
||||||
|
return c.Size();
|
||||||
|
}
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
20
src/libipc/utility/enum_cast.h
Normal file
20
src/libipc/utility/enum_cast.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* @file src/enum_cast.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Returns the underlying type of the given enum
|
||||||
|
* @date 2022-03-01
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits> // std::underlying_type_t
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
template <typename E>
|
||||||
|
constexpr auto enum_cast(E e) noexcept {
|
||||||
|
return static_cast<std::underlying_type_t<E>>(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "libipc/utility/construct.h"
|
#include "libipc/utility/construct.h"
|
||||||
#include "libipc/utility/pimpl.h"
|
#include "libipc/utility/pimpl.h"
|
||||||
|
#include "libipc/utility/countof.h"
|
||||||
|
|
||||||
TEST(utility, construct) {
|
TEST(utility, construct) {
|
||||||
struct Foo {
|
struct Foo {
|
||||||
@ -20,18 +21,22 @@ TEST(utility, construct) {
|
|||||||
EXPECT_EQ(pfoo->c_, '1');
|
EXPECT_EQ(pfoo->c_, '1');
|
||||||
ipc::destroy(pfoo);
|
ipc::destroy(pfoo);
|
||||||
|
|
||||||
|
static int bar_test_flag = 0;
|
||||||
struct Bar : Foo {
|
struct Bar : Foo {
|
||||||
Bar(int a, short b, char c)
|
Bar(int a, short b, char c)
|
||||||
: Foo{a, b, c} {}
|
: Foo{a, b, c} {
|
||||||
~Bar() { a_ = 0; }
|
++bar_test_flag;
|
||||||
|
}
|
||||||
|
~Bar() { --bar_test_flag; }
|
||||||
};
|
};
|
||||||
std::aligned_storage_t<sizeof(Bar)> bar;
|
std::aligned_storage_t<sizeof(Bar)> bar;
|
||||||
Bar *pbar = ipc::construct<Bar>(&bar, 123, short(321), '1');
|
Bar *pbar = ipc::construct<Bar>(&bar, 123, short(321), '1');
|
||||||
EXPECT_EQ(pbar->a_, 123);
|
EXPECT_EQ(pbar->a_, 123);
|
||||||
EXPECT_EQ(pbar->b_, 321);
|
EXPECT_EQ(pbar->b_, 321);
|
||||||
EXPECT_EQ(pbar->c_, '1');
|
EXPECT_EQ(pbar->c_, '1');
|
||||||
|
EXPECT_EQ(bar_test_flag, 1);
|
||||||
ipc::destroy(pbar);
|
ipc::destroy(pbar);
|
||||||
EXPECT_EQ(pbar->a_, 0);
|
EXPECT_EQ(bar_test_flag, 0);
|
||||||
|
|
||||||
std::aligned_storage_t<sizeof(Bar)> bars[3];
|
std::aligned_storage_t<sizeof(Bar)> bars[3];
|
||||||
for (auto &b : bars) {
|
for (auto &b : bars) {
|
||||||
@ -40,11 +45,9 @@ TEST(utility, construct) {
|
|||||||
EXPECT_EQ(pb->b_, 123);
|
EXPECT_EQ(pb->b_, 123);
|
||||||
EXPECT_EQ(pb->c_, '3');
|
EXPECT_EQ(pb->c_, '3');
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(bar_test_flag, ipc::countof(bars));
|
||||||
ipc::destroy(reinterpret_cast<Bar(*)[3]>(&bars));
|
ipc::destroy(reinterpret_cast<Bar(*)[3]>(&bars));
|
||||||
for (auto &b : bars) {
|
EXPECT_EQ(bar_test_flag, 0);
|
||||||
auto pb = reinterpret_cast<Bar *>(&b);
|
|
||||||
EXPECT_EQ(pb->a_, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user