object_name.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright Andrey Semashev 2016.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file utility/ipc/object_name.hpp
  9. * \author Andrey Semashev
  10. * \date 05.03.2016
  11. *
  12. * The header contains declaration of a system object name wrapper.
  13. */
  14. #ifndef BOOST_LOG_UTILITY_IPC_OBJECT_NAME_HPP_INCLUDED_
  15. #define BOOST_LOG_UTILITY_IPC_OBJECT_NAME_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #include <cstddef>
  18. #include <iosfwd>
  19. #include <string>
  20. #include <boost/move/core.hpp>
  21. #include <boost/log/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. namespace ipc {
  28. /*!
  29. * \brief A system object name class
  30. *
  31. * In order to identify a system-wide object such as a shared memory segment or a named synchronization primitive the object has to be given a name.
  32. * The format of the name is specific to the operating system and the \c object_name class provides an abstraction for names of objects. It also
  33. * provides means for scoping, which allows to avoid name clashes between different processes.
  34. *
  35. * The object name is a UTF-8 encoded string. The portable object name should consist of the following characters:
  36. *
  37. * <pre>
  38. * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  39. * a b c d e f g h i j k l m n o p q r s t u v w x y z
  40. * 0 1 2 3 4 5 6 7 8 9 . _ -
  41. * </pre>
  42. *
  43. * \note The character set corresponds to the POSIX Portable Filename Character Set (http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278).
  44. *
  45. * Use of other characters may result in non-portable system-specific behavior.
  46. *
  47. * The name can have one of the following scopes:
  48. *
  49. * \li \c global - objects within this scope are visible to any process on the system. In order to use this scope the process may need to have
  50. * extended privileges. This scope is not available for Windows Store applications.
  51. * \li \c user - objects within this scope can be opened by processes running under the same user as the current process.
  52. * \li \c session - objects within this scope are visible to processes within the session of the current process. The definition of a session may vary between
  53. * operating systems. On POSIX, a session is typically a group of processes attached to a single virtual terminal device. On Windows a session is
  54. * started when a user logs into the system. There is also a separate session for Windows services.
  55. * \li \c process_group - objects within this scope are visible to processes within the process group of the current process. Currently, on Windows all processes
  56. * running in the current session are considered members of the same process group. This may change in future.
  57. *
  58. * The scopes are not overlapping. For instance, if an object is created in the global scope, the object cannot be opened with the same name but in user's scope.
  59. *
  60. * Note that name scoping is not a security feature. On some systems any process on the system has technical capability to open objects within any scope.
  61. * The scope is only used to help avoid name clashes between processes using \c object_name to identify objects.
  62. */
  63. class object_name
  64. {
  65. public:
  66. //! Name scopes
  67. enum scope
  68. {
  69. global, //!< The name has global scope; any process in the system has the potential to open the resource identified by the name
  70. user, //!< The name is limited to processes running under the current user
  71. session, //!< The name is limited to processes running in the current login session
  72. process_group //!< The name is limited to processes running in the current process group
  73. };
  74. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  75. BOOST_COPYABLE_AND_MOVABLE(object_name)
  76. private:
  77. std::string m_name;
  78. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  79. public:
  80. /*!
  81. * Default constructor. The method creates an empty object name.
  82. *
  83. * \post <tt>empty() == true</tt>
  84. */
  85. object_name() BOOST_NOEXCEPT
  86. {
  87. }
  88. /*!
  89. * Move constructor.
  90. */
  91. object_name(BOOST_RV_REF(object_name) that) BOOST_NOEXCEPT
  92. {
  93. m_name.swap(that.m_name);
  94. }
  95. /*!
  96. * Copy constructor.
  97. */
  98. object_name(object_name const& that) : m_name(that.m_name)
  99. {
  100. }
  101. /*!
  102. * Constructor from the native string.
  103. *
  104. * \param str The object name string, must not be \c NULL. The string format is specific to the operating system.
  105. */
  106. static object_name from_native(const char* str)
  107. {
  108. object_name name;
  109. name.m_name = str;
  110. return name;
  111. }
  112. /*!
  113. * Constructor from the native string.
  114. *
  115. * \param str The object name string. The string format is specific to the operating system.
  116. */
  117. static object_name from_native(std::string const& str)
  118. {
  119. object_name name;
  120. name.m_name = str;
  121. return name;
  122. }
  123. /*!
  124. * Constructor from the object name
  125. * \param ns The scope of the object name
  126. * \param str The object name, must not be NULL.
  127. */
  128. BOOST_LOG_API object_name(scope ns, const char* str);
  129. /*!
  130. * Constructor from the object name
  131. * \param ns The scope of the object name
  132. * \param str The object name
  133. */
  134. BOOST_LOG_API object_name(scope ns, std::string const& str);
  135. /*!
  136. * Move assignment
  137. */
  138. object_name& operator= (BOOST_RV_REF(object_name) that) BOOST_NOEXCEPT
  139. {
  140. m_name.clear();
  141. m_name.swap(that.m_name);
  142. return *this;
  143. }
  144. /*!
  145. * Copy assignment
  146. */
  147. object_name& operator= (BOOST_COPY_ASSIGN_REF(object_name) that)
  148. {
  149. m_name = that.m_name;
  150. return *this;
  151. }
  152. /*!
  153. * Returns \c true if the object name is empty
  154. */
  155. bool empty() const BOOST_NOEXCEPT { return m_name.empty(); }
  156. /*!
  157. * Returns length of the name, in bytes
  158. */
  159. std::size_t size() const BOOST_NOEXCEPT { return m_name.size(); }
  160. /*!
  161. * Returns the name string
  162. */
  163. const char* c_str() const BOOST_NOEXCEPT { return m_name.c_str(); }
  164. /*!
  165. * Swaps the object name with another object name
  166. */
  167. void swap(object_name& that) BOOST_NOEXCEPT { m_name.swap(that.m_name); }
  168. /*!
  169. * Swaps two object names
  170. */
  171. friend void swap(object_name& left, object_name& right) BOOST_NOEXCEPT
  172. {
  173. left.swap(right);
  174. }
  175. /*!
  176. * Returns string representation of the object name
  177. */
  178. friend std::string to_string(object_name const& name)
  179. {
  180. return name.m_name;
  181. }
  182. /*!
  183. * Equality operator
  184. */
  185. friend bool operator== (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  186. {
  187. return left.m_name == right.m_name;
  188. }
  189. /*!
  190. * Inequality operator
  191. */
  192. friend bool operator!= (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  193. {
  194. return left.m_name != right.m_name;
  195. }
  196. /*!
  197. * Less operator
  198. */
  199. friend bool operator< (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  200. {
  201. return left.m_name < right.m_name;
  202. }
  203. /*!
  204. * Greater operator
  205. */
  206. friend bool operator> (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  207. {
  208. return left.m_name > right.m_name;
  209. }
  210. /*!
  211. * Less or equal operator
  212. */
  213. friend bool operator<= (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  214. {
  215. return left.m_name <= right.m_name;
  216. }
  217. /*!
  218. * Greater or equal operator
  219. */
  220. friend bool operator>= (object_name const& left, object_name const& right) BOOST_NOEXCEPT
  221. {
  222. return left.m_name >= right.m_name;
  223. }
  224. /*!
  225. * Stream ouput operator
  226. */
  227. template< typename CharT, typename TraitsT >
  228. friend std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, object_name const& name)
  229. {
  230. strm << name.c_str();
  231. return strm;
  232. }
  233. };
  234. } // namespace ipc
  235. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  236. } // namespace boost
  237. #include <boost/log/detail/footer.hpp>
  238. #endif // BOOST_LOG_UTILITY_IPC_OBJECT_NAME_HPP_INCLUDED_