mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-22 14:38:14 +02:00
move constructor deleted copy constructor
This commit is contained in:
parent
48e4cec56f
commit
d4e4fb8ffe
@ -10,42 +10,12 @@
|
||||
#include "sls_detector_funcs.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define VERBOSE
|
||||
|
||||
int main() {
|
||||
// const std::string hostname = "beb083";
|
||||
// auto d = slsDetector(hostname);
|
||||
|
||||
// d.setOnline(1);
|
||||
// std::cout << "type: " << d.getDetectorTypeAsString() << '\n';
|
||||
// std::cout << "hostname: " << d.getHostname() << '\n';
|
||||
// std::cout << "receiver: " << d.getReceiverOnline() << '\n';
|
||||
|
||||
// std::cout << "control: " << d.getControlPort() << '\n';
|
||||
// std::cout << "stop: " << d.getStopPort() << '\n';
|
||||
// std::cout << "receiver: " << d.getReceiverPort() << '\n';
|
||||
// std::cout << "exptime: " << d.setTimer(slsDetectorDefs::timerIndex::ACQUISITION_TIME) << '\n';
|
||||
|
||||
|
||||
|
||||
// auto d2 = slsDetector(type, 0, 1);
|
||||
// d2.setHostname("beb098");.
|
||||
// auto d2 = slsDetector();
|
||||
// std::cout << "hn: " << d2.getHostname() << '\n';
|
||||
|
||||
// sls::Timer t;
|
||||
// for (int i = 0; i != 100; ++i) {
|
||||
// int fnum = 1;
|
||||
// int ret = slsDetectorDefs::FAIL;
|
||||
// slsDetectorDefs::detectorType retval = slsDetectorDefs::detectorType::GENERIC;
|
||||
|
||||
// auto cs = sls::ClientSocket("beb083", 1952);
|
||||
// cs.sendData(reinterpret_cast<char *>(&fnum), sizeof(fnum));
|
||||
// cs.receiveData(reinterpret_cast<char *>(&ret), sizeof(ret));
|
||||
// cs.receiveData(reinterpret_cast<char *>(&retval), sizeof(retval));
|
||||
// std::cout << "retval: " << retval << '\n';
|
||||
// }
|
||||
// t.print_elapsed();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) {
|
||||
}
|
||||
|
||||
// multi
|
||||
auto multiShm = SharedMemory<sharedMultiSlsDetector>(multiId, -1);
|
||||
SharedMemory<sharedMultiSlsDetector> multiShm(multiId, -1);
|
||||
int numDetectors = 0;
|
||||
|
||||
if (multiShm.IsExisting()) {
|
||||
@ -255,7 +255,7 @@ void multiSlsDetector::freeSharedMemory(int multiId, int detPos) {
|
||||
}
|
||||
|
||||
for (int i = 0; i < numDetectors; ++i) {
|
||||
auto shm = SharedMemory<sharedSlsDetector>(multiId, i);
|
||||
SharedMemory<sharedSlsDetector> shm(multiId, i);
|
||||
shm.RemoveSharedMemory();
|
||||
}
|
||||
}
|
||||
|
@ -1,178 +0,0 @@
|
||||
// #include "SharedMemory.h"
|
||||
// #include "sls_detector_exceptions.h"
|
||||
// #include "ansi.h"
|
||||
// #include "logger.h"
|
||||
|
||||
// #include "slsDetector.h"
|
||||
// #include "multiSlsDetector.h"
|
||||
|
||||
// #include <iostream>
|
||||
// #include <stdio.h> // printf
|
||||
// #include <cerrno> // errno
|
||||
// #include <cstring> // strerror
|
||||
// #include <unistd.h>
|
||||
// #include <fcntl.h> // O_CREAT, O_TRUNC..
|
||||
// #include <sys/stat.h> // fstat
|
||||
// #include <sys/mman.h> // shared memory
|
||||
// #include <sstream>
|
||||
// #include "stdlib.h"
|
||||
|
||||
// #define SHM_MULTI_PREFIX "/slsDetectorPackage_multi_"
|
||||
// #define SHM_SLS_PREFIX "_sls_"
|
||||
// #define SHM_ENV_NAME "SLSDETNAME"
|
||||
|
||||
// template<typename T>
|
||||
// SharedMemory<T>::SharedMemory(int multiId, int slsId):
|
||||
// fd(-1),
|
||||
// shmSize(0)
|
||||
// {
|
||||
// name = ConstructSharedMemoryName(multiId, slsId);
|
||||
// }
|
||||
|
||||
|
||||
// template<typename T>
|
||||
// SharedMemory<T>::~SharedMemory(){
|
||||
// if (fd >= 0)
|
||||
// close(fd);
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// bool SharedMemory<T>::IsExisting() {
|
||||
// bool ret = true;
|
||||
// int tempfd = shm_open(name.c_str(), O_RDWR, 0);
|
||||
// if ((tempfd < 0) && (errno == ENOENT)) {
|
||||
// ret = false;
|
||||
// }
|
||||
// close(tempfd);
|
||||
// return ret;
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// std::string SharedMemory<T>::GetName() {
|
||||
// return name;
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::CreateSharedMemory(size_t sz){
|
||||
// // create
|
||||
// fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
// if (fd < 0) {
|
||||
// FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
|
||||
// // resize
|
||||
// if (ftruncate(fd, sz) < 0) {
|
||||
// FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
|
||||
// close(fd);
|
||||
// RemoveSharedMemory();
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
|
||||
// // map
|
||||
// // void* addr = MapSharedMemory(sz);
|
||||
// shared_struct = MapSharedMemory(sz);
|
||||
// FILE_LOG(logINFO) << "Shared memory created " << name;
|
||||
|
||||
// // return addr;
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::OpenSharedMemory(size_t sz){
|
||||
// // open
|
||||
// fd = shm_open(name.c_str(), O_RDWR, 0);
|
||||
// if (fd < 0) {
|
||||
// FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
|
||||
// shared_struct = MapSharedMemory(sz);
|
||||
// // return MapSharedMemory(sz);
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::UnmapSharedMemory() {
|
||||
// if (munmap(shared_struct, shmSize) < 0) {
|
||||
// FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno);
|
||||
// close(fd);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void SharedMemory<T>::RemoveSharedMemory() {
|
||||
// if (shm_unlink(name.c_str()) < 0) {
|
||||
// // silent exit if shm did not exist anyway
|
||||
// if (errno == ENOENT)
|
||||
// return;
|
||||
// FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
// FILE_LOG(logINFO) << "Shared memory deleted " << name;
|
||||
// }
|
||||
|
||||
|
||||
// template<typename T>
|
||||
// T* SharedMemory<T>::MapSharedMemory(size_t sz) {
|
||||
// void* addr = mmap(nullptr, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
// if (addr == MAP_FAILED) {
|
||||
// FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno);
|
||||
// close(fd);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
// shmSize = sz;
|
||||
// close(fd);
|
||||
// return (T*)addr;
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// std::string SharedMemory<T>::ConstructSharedMemoryName(int multiId, int slsId) {
|
||||
|
||||
// // using environment path
|
||||
// std::string sEnvPath = "";
|
||||
// char* envpath = getenv(SHM_ENV_NAME);
|
||||
// if (envpath != nullptr) {
|
||||
// sEnvPath.assign(envpath);
|
||||
// sEnvPath.insert(0,"_");
|
||||
// }
|
||||
|
||||
// std::stringstream ss;
|
||||
// if (slsId < 0)
|
||||
// ss << SHM_MULTI_PREFIX << multiId << sEnvPath;
|
||||
// else
|
||||
// ss << SHM_MULTI_PREFIX << multiId << SHM_SLS_PREFIX << slsId << sEnvPath;
|
||||
|
||||
// std::string temp = ss.str();
|
||||
// if (temp.length() > NAME_MAX) {
|
||||
// FILE_LOG(logERROR) << "Shared memory initialization failed. " <<
|
||||
// temp << " has " << temp.length() << " characters. \n"
|
||||
// "Maximum is " << NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME;
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
// return temp;
|
||||
// // }
|
||||
|
||||
// template<typename T>
|
||||
// int SharedMemory<T>::VerifySizeMatch(size_t expectedSize) {
|
||||
// struct stat sb;
|
||||
// // could not fstat
|
||||
// if (fstat(fd, &sb) < 0) {
|
||||
// FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match "
|
||||
// "(could not fstat): " << strerror(errno);
|
||||
// close(fd);
|
||||
// throw SharedMemoryException();
|
||||
// }
|
||||
|
||||
// //size does not match
|
||||
// long unsigned int sz = (long unsigned int)sb.st_size;
|
||||
// if (sz != expectedSize) {
|
||||
// FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match";
|
||||
// FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz;
|
||||
// throw SharedMemoryException();
|
||||
// return 1;
|
||||
// }
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// template class SharedMemory<sharedSlsDetector>;
|
||||
// template class SharedMemory<sharedMultiSlsDetector>;
|
@ -39,13 +39,48 @@ class SharedMemory {
|
||||
* @param multiId multi detector id
|
||||
* @param slsId sls detector id, -1 if a multi detector shared memory
|
||||
*/
|
||||
SharedMemory(int multiId, int slsId){
|
||||
SharedMemory(int multiId, int slsId) {
|
||||
name = ConstructSharedMemoryName(multiId, slsId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
/**
|
||||
* Delete the copy constructor and copy assignment since we don't want two
|
||||
* objects managing the same resource
|
||||
*/
|
||||
SharedMemory(const SharedMemory &) = delete;
|
||||
SharedMemory &operator=(const SharedMemory &other) = delete;
|
||||
|
||||
//Move constructor
|
||||
SharedMemory(SharedMemory &&other) : name(other.name),
|
||||
fd(other.fd),
|
||||
shmSize(other.shmSize),
|
||||
shared_struct(other.shared_struct) {
|
||||
|
||||
other.fd = -1;
|
||||
other.shared_struct = nullptr;
|
||||
other.shmSize = 0;
|
||||
}
|
||||
|
||||
//Move assignment
|
||||
SharedMemory &operator=(SharedMemory &&other) {
|
||||
name = other.name;
|
||||
if (fd) {
|
||||
close(fd);
|
||||
}
|
||||
fd = other.fd;
|
||||
other.fd = -1;
|
||||
|
||||
if (shared_struct != nullptr) {
|
||||
UnmapSharedMemory();
|
||||
}
|
||||
shared_struct = other.shared_struct;
|
||||
other.shared_struct = nullptr;
|
||||
|
||||
shmSize = other.shmSize;
|
||||
other.shmSize = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~SharedMemory() {
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
@ -73,17 +108,20 @@ class SharedMemory {
|
||||
/**
|
||||
* Get shared memory name
|
||||
*/
|
||||
std::string GetName() {
|
||||
std::string GetName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return shmSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Shared memory and call MapSharedMemory to map it to an address
|
||||
* throws a SharedMemoryException exception on failure to create, ftruncate or map
|
||||
* @param sz of shared memory
|
||||
*/
|
||||
void CreateSharedMemory(size_t sz = 0) {
|
||||
// create
|
||||
if (sz == 0) {
|
||||
sz = sizeof(T);
|
||||
}
|
||||
@ -94,7 +132,6 @@ class SharedMemory {
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// resize
|
||||
if (ftruncate(fd, sz) < 0) {
|
||||
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno);
|
||||
close(fd);
|
||||
@ -102,12 +139,8 @@ class SharedMemory {
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// map
|
||||
// void* addr = MapSharedMemory(sz);
|
||||
shared_struct = MapSharedMemory(sz);
|
||||
FILE_LOG(logINFO) << "Shared memory created " << name;
|
||||
|
||||
// return addr;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,7 +149,6 @@ class SharedMemory {
|
||||
* @param sz of shared memory
|
||||
*/
|
||||
void OpenSharedMemory(size_t sz = 0) {
|
||||
// open
|
||||
if (sz == 0) {
|
||||
sz = sizeof(T);
|
||||
}
|
||||
@ -128,7 +160,6 @@ class SharedMemory {
|
||||
}
|
||||
|
||||
shared_struct = MapSharedMemory(sz);
|
||||
// return MapSharedMemory(sz);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -166,15 +197,16 @@ class SharedMemory {
|
||||
*/
|
||||
static const int NAME_MAX = 255;
|
||||
|
||||
/*
|
||||
Using the call operator to access the pointer
|
||||
|
||||
/**
|
||||
*Using the call operator to access the pointer
|
||||
*/
|
||||
|
||||
T *operator()() {
|
||||
return shared_struct;
|
||||
}
|
||||
|
||||
/**
|
||||
*Using the call operator to access the pointer, const overload
|
||||
*/
|
||||
const T *operator()() const {
|
||||
return shared_struct;
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
|
||||
* so sls shared memory will be created */
|
||||
|
||||
// ensure shared memory was not created before
|
||||
auto shm = SharedMemory<sharedSlsDetector>(multiId, id);
|
||||
SharedMemory<sharedSlsDetector> shm(multiId, id);
|
||||
if (shm.IsExisting()) {
|
||||
FILE_LOG(logWARNING) << "This shared memory should have been "
|
||||
"deleted before! "
|
||||
@ -202,7 +202,7 @@ int64_t slsDetector::getId(idMode mode) {
|
||||
}
|
||||
|
||||
void slsDetector::freeSharedMemory(int multiId, int slsId) {
|
||||
auto shm = SharedMemory<sharedSlsDetector>(multiId, slsId);
|
||||
SharedMemory<sharedSlsDetector> shm(multiId, slsId);
|
||||
shm.RemoveSharedMemory();
|
||||
}
|
||||
|
||||
@ -640,7 +640,7 @@ int slsDetector::receiveModule(sls_detector_module *myMod) {
|
||||
}
|
||||
|
||||
slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, bool verify) {
|
||||
auto shm = SharedMemory<sharedSlsDetector>(multiId, detId);
|
||||
SharedMemory<sharedSlsDetector> shm(multiId, detId);
|
||||
if (!shm.IsExisting()) {
|
||||
FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n"
|
||||
"Corrupted Multi Shared memory. Please free shared memory.";
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "catch.hpp"
|
||||
#include "string_utils.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
struct Data {
|
||||
int x;
|
||||
double y;
|
||||
@ -30,7 +32,7 @@ TEST_CASE("Create SharedMemory read and write") {
|
||||
}
|
||||
|
||||
TEST_CASE("Open existing SharedMemory and read") {
|
||||
|
||||
|
||||
{
|
||||
SharedMemory<double> shm(0, -1);
|
||||
shm.CreateSharedMemory();
|
||||
@ -77,4 +79,50 @@ TEST_CASE("Open two shared memories to the same place") {
|
||||
shm2.RemoveSharedMemory();
|
||||
CHECK(shm.IsExisting() == false);
|
||||
CHECK(shm2.IsExisting() == false);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Move SharedMemory"){
|
||||
|
||||
SharedMemory<Data> shm(0,-1);
|
||||
CHECK(shm.GetName() == "/slsDetectorPackage_multi_0");
|
||||
shm.CreateSharedMemory();
|
||||
shm()->x = 9;
|
||||
|
||||
CHECK(shm.size()== sizeof(Data));
|
||||
|
||||
SharedMemory<Data> shm2(1,-1);
|
||||
shm2 = std::move(shm); //shm is now a moved from object!
|
||||
|
||||
CHECK(shm2()->x == 9);
|
||||
CHECK(shm() == nullptr);
|
||||
CHECK(shm.size() == 0);
|
||||
|
||||
CHECK(shm2.GetName() == "/slsDetectorPackage_multi_0");
|
||||
shm2.RemoveSharedMemory();
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Create several shared memories") {
|
||||
constexpr int N = 5;
|
||||
std::vector<SharedMemory<int>> v;
|
||||
v.reserve(N);
|
||||
for (int i = 0; i != N; ++i) {
|
||||
v.emplace_back(i, -1);
|
||||
CHECK(v[i].IsExisting() == false);
|
||||
v[i].CreateSharedMemory();
|
||||
*v[i]() = i;
|
||||
CHECK(*v[i]() == i);
|
||||
}
|
||||
|
||||
for (int i = 0; i != N; ++i) {
|
||||
CHECK(*v[i]() == i);
|
||||
CHECK(v[i].GetName() == std::string("/slsDetectorPackage_multi_")+std::to_string(i));
|
||||
}
|
||||
|
||||
for (int i = 0; i != N; ++i) {
|
||||
v[i].RemoveSharedMemory();
|
||||
CHECK(v[i].IsExisting() == false);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user