xsi_key.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_XSI_KEY_HPP
  11. #define BOOST_INTERPROCESS_XSI_KEY_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/detail/workaround.hpp>
  22. #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  23. #error "This header can't be used in operating systems without XSI (System V) shared memory support"
  24. #endif
  25. #include <boost/interprocess/creation_tags.hpp>
  26. #include <boost/interprocess/exceptions.hpp>
  27. #include <boost/interprocess/detail/utilities.hpp>
  28. #include <boost/move/utility_core.hpp>
  29. #include <boost/interprocess/detail/os_file_functions.hpp>
  30. #include <boost/interprocess/interprocess_fwd.hpp>
  31. #include <boost/interprocess/exceptions.hpp>
  32. #include <sys/types.h>
  33. #include <sys/ipc.h>
  34. #include <cstddef>
  35. #include <boost/cstdint.hpp>
  36. //!\file
  37. //!Describes a class representing a xsi key type.
  38. namespace boost {
  39. namespace interprocess {
  40. //!A class that wraps XSI (System V) key_t type.
  41. //!This type calculates key_t from path and id using ftok,
  42. //!sets key to a specified value,
  43. //!or sets key to IPC_PRIVATE using the default constructor.
  44. class xsi_key
  45. {
  46. public:
  47. //!Default constructor.
  48. //!Represents a private xsi_key.
  49. xsi_key()
  50. : m_key(IPC_PRIVATE)
  51. {}
  52. //!Creates a new XSI key using a specified value. Constructor is explicit to avoid ambiguity with shmid.
  53. explicit xsi_key(key_t key)
  54. : m_key(key)
  55. {}
  56. //!Creates a new XSI shared memory with a key obtained from a call to ftok (with path
  57. //!"path" and id "id"), of size "size" and permissions "perm".
  58. //!If the shared memory previously exists, throws an error.
  59. xsi_key(const char *path, boost::uint8_t id)
  60. {
  61. key_t key;
  62. if(path){
  63. key = ::ftok(path, id);
  64. if(((key_t)-1) == key){
  65. error_info err = system_error_code();
  66. throw interprocess_exception(err);
  67. }
  68. }
  69. else{
  70. key = IPC_PRIVATE;
  71. }
  72. m_key = key;
  73. }
  74. //!Returns the internal key_t value
  75. key_t get_key() const
  76. { return m_key; }
  77. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  78. private:
  79. key_t m_key;
  80. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  81. };
  82. } //namespace interprocess {
  83. } //namespace boost {
  84. #include <boost/interprocess/detail/config_end.hpp>
  85. #endif //BOOST_INTERPROCESS_XSI_KEY_HPP