xsi_key.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <boost/detail/workaround.hpp>
  15. #if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
  16. #error "This header can't be used in operating systems without XSI (System V) shared memory support"
  17. #endif
  18. #include <boost/interprocess/creation_tags.hpp>
  19. #include <boost/interprocess/exceptions.hpp>
  20. #include <boost/interprocess/detail/utilities.hpp>
  21. #include <boost/move/move.hpp>
  22. #include <boost/interprocess/detail/os_file_functions.hpp>
  23. #include <boost/interprocess/interprocess_fwd.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <sys/types.h>
  26. #include <sys/ipc.h>
  27. #include <cstddef>
  28. #include <boost/cstdint.hpp>
  29. //!\file
  30. //!Describes a class representing a xsi key type.
  31. namespace boost {
  32. namespace interprocess {
  33. //!A class that wraps XSI (System V) key_t type.
  34. //!This type calculates key_t from path and id using ftok
  35. //!or sets key to IPC_PRIVATE using the default constructor.
  36. class xsi_key
  37. {
  38. public:
  39. //!Default constructor.
  40. //!Represents a private xsi_key.
  41. xsi_key()
  42. : m_key(IPC_PRIVATE)
  43. {}
  44. //!Creates a new XSI shared memory with a key obtained from a call to ftok (with path
  45. //!"path" and id "id"), of size "size" and permissions "perm".
  46. //!If the shared memory previously exists, throws an error.
  47. xsi_key(const char *path, boost::uint8_t id)
  48. {
  49. key_t key;
  50. if(path){
  51. key = ::ftok(path, id);
  52. if(((key_t)-1) == key){
  53. error_info err = system_error_code();
  54. throw interprocess_exception(err);
  55. }
  56. }
  57. else{
  58. key = IPC_PRIVATE;
  59. }
  60. m_key = key;
  61. }
  62. //!Returns the internal key_t value
  63. key_t get_key() const
  64. { return m_key; }
  65. /// @cond
  66. private:
  67. key_t m_key;
  68. /// @endcond
  69. };
  70. } //namespace interprocess {
  71. } //namespace boost {
  72. #include <boost/interprocess/detail/config_end.hpp>
  73. #endif //BOOST_INTERPROCESS_XSI_KEY_HPP