std::string_view 的使用
This commit is contained in:
@@ -5,24 +5,26 @@
|
||||
#include "Core/transmit_protocol/Channel_Simulator/global.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
namespace Psc {
|
||||
|
||||
|
||||
Pure_Share_Memory::Pure_Share_Memory(const std::string &name, size_t alloc_size) {
|
||||
mutex.init("Pure_Share_Memory_" + name + "_mutex");
|
||||
bool open = check_if_shared_memory_exists(name);
|
||||
Pure_Share_Memory::Pure_Share_Memory(std::string_view name, size_t alloc_size) {
|
||||
auto name_string = std::string(name);
|
||||
mutex.init("Pure_Share_Memory_" + name_string + "_mutex");
|
||||
bool open = check_if_shared_memory_exists(name_string);
|
||||
bool ok;
|
||||
bool create;
|
||||
std::ostringstream oss;
|
||||
if (open) {
|
||||
oss << "【打开共享内存】:[" << name << "]" << std::endl;
|
||||
ok = shm.open(name, alloc_size);
|
||||
ok = shm.open(name_string, alloc_size);
|
||||
create = false;
|
||||
} else {
|
||||
oss << "【创建共享内存】】:[" << name << "]" << std::endl;
|
||||
ok = shm.create(name, alloc_size);
|
||||
ok = shm.create(name_string, alloc_size);
|
||||
create = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
#include "../Base/global_include.h"
|
||||
#include "../system/export.h"
|
||||
#include <cassert>
|
||||
@@ -9,13 +10,13 @@
|
||||
#include <string>
|
||||
|
||||
namespace Psc {
|
||||
bool check_if_shared_memory_exists(const std::string &name);
|
||||
bool check_if_shared_memory_exists(std::string_view name);
|
||||
class Shared_Memory {
|
||||
public:
|
||||
Shared_Memory() = default;
|
||||
~Shared_Memory();
|
||||
bool create(const std::string &name, size_t size);
|
||||
bool open(const std::string &name, size_t size);
|
||||
bool create(std::string_view name, size_t size);
|
||||
bool open(std::string_view name, size_t size);
|
||||
[[nodiscard]] uint8_t *data() const { return (uint8_t *) m_ptr; }
|
||||
[[nodiscard]] size_t size() const { return m_size; }
|
||||
void close();
|
||||
@@ -35,7 +36,7 @@ namespace Psc {
|
||||
class Cross_Process_Mutex {
|
||||
public:
|
||||
explicit Cross_Process_Mutex();
|
||||
void init(const std::string &mutexName);
|
||||
void init(std::string_view mutexName);
|
||||
~Cross_Process_Mutex();
|
||||
void lock();
|
||||
void unlock();
|
||||
@@ -74,7 +75,7 @@ namespace Psc {
|
||||
public:
|
||||
Shared_Memory shm;
|
||||
Cross_Process_Mutex mutex;
|
||||
Pure_Share_Memory(const std::string &name, size_t alloc_size);
|
||||
Pure_Share_Memory(std::string_view name, size_t alloc_size);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -15,9 +15,10 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string_view>
|
||||
namespace Psc {
|
||||
|
||||
bool check_if_shared_memory_exists(const std::string &name) {
|
||||
bool check_if_shared_memory_exists(std::string_view name) {
|
||||
int fd = shm_open(name.c_str(), O_RDWR, 0777);
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
@@ -26,7 +27,7 @@ bool check_if_shared_memory_exists(const std::string &name) {
|
||||
return true;
|
||||
}
|
||||
Shared_Memory::~Shared_Memory() { close(); }
|
||||
bool Shared_Memory::create(const std::string &name, size_t size) {
|
||||
bool Shared_Memory::create(std::string_view name, size_t size) {
|
||||
m_name = name;
|
||||
m_size = size;
|
||||
m_fd = shm_open(name.c_str(), O_CREAT | O_RDWR, 0777);
|
||||
@@ -41,7 +42,7 @@ bool Shared_Memory::create(const std::string &name, size_t size) {
|
||||
}
|
||||
return m_ptr != MAP_FAILED;
|
||||
}
|
||||
bool Shared_Memory::open(const std::string &name, size_t size) {
|
||||
bool Shared_Memory::open(std::string_view name, size_t size) {
|
||||
m_name = name;
|
||||
m_size = size;
|
||||
m_fd = shm_open(name.c_str(), O_RDWR, 0777);
|
||||
@@ -72,7 +73,7 @@ struct MutexBlock {
|
||||
|
||||
Cross_Process_Mutex::Cross_Process_Mutex() {}
|
||||
|
||||
void Cross_Process_Mutex::init(const std::string &mutexName) {
|
||||
void Cross_Process_Mutex::init(std::string_view mutexName) {
|
||||
this->m_mutexName = mutexName;
|
||||
bool creator = false;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "Shared_Memory.h"
|
||||
#include <string_view>
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
namespace Psc {
|
||||
@@ -16,7 +17,7 @@ static std::string win_error_msg(DWORD err = GetLastError()) {
|
||||
}
|
||||
|
||||
Cross_Process_Mutex::Cross_Process_Mutex() : m_mutexHandle(nullptr) {}
|
||||
void Cross_Process_Mutex::init(const std::string &mutexName) {
|
||||
void Cross_Process_Mutex::init(std::string_view mutexName) {
|
||||
m_mutexName = mutexName;
|
||||
m_mutexHandle = OpenMutexA(MUTEX_ALL_ACCESS, FALSE, m_mutexName.c_str());
|
||||
if (!m_mutexHandle) {
|
||||
@@ -57,8 +58,9 @@ void Cross_Process_Mutex::unlock() {
|
||||
}
|
||||
}
|
||||
|
||||
bool check_if_shared_memory_exists(const std::string &name) {
|
||||
HANDLE hMapFile = OpenFileMappingA(FILE_MAP_READ, FALSE, name.c_str());
|
||||
bool check_if_shared_memory_exists(std::string_view name) {
|
||||
auto name_string = std::string(name);
|
||||
HANDLE hMapFile = OpenFileMappingA(FILE_MAP_READ, FALSE, name_string.c_str());
|
||||
if (!hMapFile) {
|
||||
DWORD err = GetLastError();
|
||||
if (err != ERROR_FILE_NOT_FOUND) {
|
||||
@@ -74,12 +76,13 @@ bool check_if_shared_memory_exists(const std::string &name) {
|
||||
|
||||
Shared_Memory::~Shared_Memory() { close(); }
|
||||
|
||||
bool Shared_Memory::create(const std::string &name, size_t size) {
|
||||
bool Shared_Memory::create(std::string_view name, size_t size) {
|
||||
std::cout << "创建共享内存【" << name << "】" << std::endl;
|
||||
auto name_string = std::string(name);
|
||||
m_size = size;
|
||||
|
||||
m_handle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
|
||||
0, (DWORD)size, name.c_str());
|
||||
0, (DWORD)size, name_string.c_str());
|
||||
|
||||
if (!m_handle) {
|
||||
std::cerr << "CreateFileMappingA failed for name=" << name << ": "
|
||||
@@ -98,11 +101,12 @@ bool Shared_Memory::create(const std::string &name, size_t size) {
|
||||
|
||||
return true;
|
||||
}
|
||||
bool Shared_Memory::open(const std::string &name, size_t size) {
|
||||
bool Shared_Memory::open(std::string_view name, size_t size) {
|
||||
std::cout << "打开共享内存【" << name << "】" << std::endl;
|
||||
auto name_string = std::string(name);
|
||||
m_size = size;
|
||||
|
||||
m_handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name.c_str());
|
||||
m_handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name_string.c_str());
|
||||
|
||||
if (!m_handle) {
|
||||
std::cerr << "OpenFileMappingA failed for name=" << name << ": "
|
||||
|
||||
Reference in New Issue
Block a user