OpenMPCD
copy.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::ImplementationDetails::Configuration::copy` function.
4  */
5 
6 #ifndef OPENMPCD_IMPLEMENTATIONDETAILS_CONFIGURATION_COPY_HPP
7 #define OPENMPCD_IMPLEMENTATIONDETAILS_CONFIGURATION_COPY_HPP
8 
11 
12 #include <cstdio>
13 #include <cstdlib>
14 #include <libconfig.h++>
15 
16 namespace OpenMPCD
17 {
18 namespace ImplementationDetails
19 {
20 namespace Configuration
21 {
22 
23 /**
24  * Copies a `libconfig::Config` instance.
25  * @param[in] from The instance to copy from.
26  * @param[out] to The instance to copy to.
27  */
28 inline void copy(const libconfig::Config& from, libconfig::Config* const to)
29 {
30  #if defined(OPENMPCD_PLATFORM_POSIX)
31  char* buffer;
32  std::size_t bufferSize;
33 
34  FILE* const stream = open_memstream(&buffer, &bufferSize);
35 
36  if(!stream)
37  OPENMPCD_THROW(Exception, "Failed to allocate stream.");
38  #elif defined(OPENMPCD_PLATFORM_WIN32)
39  char filename[L_tmpnam_s];
40  if(tmpnam_s(filename) != 0)
41  OPENMPCD_THROW(Exception, "Failed to create temporary file name.");
42 
43  FILE* stream;
44  if(fopen_s(&stream, filename, "w+") != 0)
45  OPENMPCD_THROW(Exception, "Failed to open temporary file.");
46  #else
47  #error Unknown platform
48  #endif
49 
50  from.write(stream);
51 
52  rewind(stream);
53 
54  to->read(stream);
55 
56  fclose(stream);
57 
58  #ifdef OPENMPCD_PLATFORM_POSIX
59  free(buffer);
60  #endif
61 }
62 
63 } //namespace Configuration
64 } //namespace ImplementationDetails
65 } //namespace OpenMPCD
66 
67 #endif
PlatformDetection.hpp
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
Exceptions.hpp
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::ImplementationDetails::Configuration::copy
void copy(const libconfig::Config &from, libconfig::Config *const to)
Copies a libconfig::Config instance.
Definition: copy.hpp:28
OpenMPCD::Exception
The base exception class for OpenMPCD.
Definition: Exceptions.hpp:37