Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
SharedMemory.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#include <sese/Config.h>
25#include <sese/util/Result.h>
26#include <sese/util/ErrorCode.h>
27#ifdef WIN32
28#else
29#include <sys/shm.h>
30#endif
31
32namespace sese::system {
33
35class SharedMemory final {
36public:
37 using Ptr = std::unique_ptr<SharedMemory>;
38
44 static SharedMemory::Ptr create(const char *name, size_t size) noexcept;
45
46 static Result<Ptr, ErrorCode> createEx(const char *name, size_t size) noexcept;
47
52 static SharedMemory::Ptr use(const char *name) noexcept;
53
54 static Result<Ptr, ErrorCode> useEx(const char *name) noexcept;
55
57 ~SharedMemory() noexcept;
60 void *getBuffer() noexcept;
61
62private:
63 SharedMemory() = default;
64
65private:
66#ifdef WIN32
67 void *hMapFile = nullptr;
68#else
69 int id{};
70 static key_t name2key(const char *name) noexcept;
71#endif
72 void *buffer = nullptr;
73 bool isOwner{};
74};
75} // namespace sese::system