Skip to content

Commit cf079ca

Browse files
authored
Merge pull request #2 from yangosoft/wip/shm-win
Added wstring constructor for shm class
2 parents 76fa7a4 + 7d0e9d8 commit cf079ca

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/include/cpputils2/win/shm/shm.hpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "cpputils2/common/types.hpp"
1111

12+
#include <string>
13+
1214
#include <windows.h>
1315
#include <stdio.h>
1416
#include <conio.h>
@@ -27,15 +29,22 @@ namespace CppUtils2
2729
}
2830

2931
Shm(const std::string& file_mem_path)
32+
: mem_path(file_mem_path.begin(), file_mem_path.end()), hMapFile(nullptr), pBuf{ nullptr }
33+
{
34+
}
35+
36+
Shm(const std::wstring& file_mem_path)
3037
: mem_path(file_mem_path), hMapFile(nullptr), pBuf{ nullptr }
3138
{
3239
}
3340

41+
42+
3443
/// @brief Open an existing shared memory
3544
/// @param file_mem_path Path to the shared memory
3645
/// @param mem_size Size of the shared memory
3746
/// @return 0 on success, -1 on error
38-
Result open_existing(const std::string& file_mem_path, std::size_t mem_size)
47+
Result open_existing(const std::wstring& file_mem_path, std::size_t mem_size)
3948
{
4049
mem_path = file_mem_path;
4150
return open_existing(mem_size);
@@ -50,7 +59,7 @@ namespace CppUtils2
5059
{
5160
return Result::RET_ERROR;
5261
}
53-
int flags = 0;
62+
auto flags = Operation::OPEN;
5463
return shared_open(flags, mem_size);
5564
}
5665

@@ -64,7 +73,7 @@ namespace CppUtils2
6473
return Result::RET_ERROR;
6574
}
6675

67-
int flags = 1;
76+
auto flags = Operation::CREATE;
6877

6978
return shared_open(flags, mem_size);
7079
}
@@ -105,27 +114,28 @@ namespace CppUtils2
105114
}
106115

107116
private:
108-
std::string mem_path;
117+
std::wstring mem_path;
109118
HANDLE hMapFile;
110119
LPVOID pBuf;
111-
112-
Result shared_open(int flags, std::size_t mem_size)
120+
enum class Operation: int
113121
{
114-
if (flags == 1) {
115-
116-
//TCHAR szName[] = TEXT(mem_path.c_str());
122+
OPEN,
123+
CREATE
124+
};
117125

118-
hMapFile = CreateFileMappingA(
126+
Result shared_open(Operation flags, std::size_t mem_size)
127+
{
128+
if (flags == Operation::CREATE) {
129+
hMapFile = CreateFileMapping(
119130
INVALID_HANDLE_VALUE, // use paging file
120131
nullptr, // default security
121132
PAGE_READWRITE, // read/write access
122133
0, // maximum object size (high-order DWORD)
123134
mem_size, // maximum object size (low-order DWORD)
124-
mem_path.c_str()); // name of mapping object
135+
(LPCTSTR)mem_path.c_str()); // name of mapping object
125136

126137
if (hMapFile == NULL)
127138
{
128-
129139
return Result::RET_ERROR;
130140
}
131141

@@ -138,19 +148,17 @@ namespace CppUtils2
138148
if (pBuf == nullptr)
139149
{
140150
//_tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError());
141-
142151
CloseHandle(hMapFile);
143-
144152
return Result::RET_ERROR;
145153
}
146154

147155
}
148156
else {
149157

150-
hMapFile = OpenFileMappingA(
158+
hMapFile = OpenFileMapping(
151159
FILE_MAP_ALL_ACCESS, // read/write access
152160
FALSE, // do not inherit the name
153-
mem_path.c_str()); // name of mapping object
161+
(LPCTSTR)mem_path.c_str()); // name of mapping object
154162

155163
if (hMapFile == NULL)
156164
{

src/test/cpputils2_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace
145145

146146
#ifdef _WIN32
147147
TEST(ExampleSHM, TestSHM) {
148-
CppUtils2::Shm shm("test_shm");
148+
CppUtils2::Shm shm(L"test_shm");
149149
auto ret = shm.allocate(sizeof(int32_t));
150150
EXPECT_NE(ret, CppUtils2::Result::RET_ERROR);
151151
void* ptr = shm.get_raw_ptr();
@@ -159,9 +159,10 @@ namespace
159159
shm.unlink();
160160
EXPECT_TRUE(true);
161161
}
162+
162163
TEST(ExampleShm, TestExisting)
163164
{
164-
CppUtils2::Shm shm("test_shm");
165+
CppUtils2::Shm shm(L"test_shm");
165166
auto ret = shm.allocate(sizeof(int32_t));
166167
EXPECT_NE(ret, CppUtils2::Result::RET_ERROR);
167168

0 commit comments

Comments
 (0)