1 ////////////////////////////////////////////////////////////
 
   3 // SFML - Simple and Fast Multimedia Library
 
   4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
 
   6 // This software is provided 'as-is', without any express or implied warranty.
 
   7 // In no event will the authors be held liable for any damages arising from the use of this software.
 
   9 // Permission is granted to anyone to use this software for any purpose,
 
  10 // including commercial applications, and to alter it and redistribute it freely,
 
  11 // subject to the following restrictions:
 
  13 // 1. The origin of this software must not be misrepresented;
 
  14 //    you must not claim that you wrote the original software.
 
  15 //    If you use this software in a product, an acknowledgment
 
  16 //    in the product documentation would be appreciated but is not required.
 
  18 // 2. Altered source versions must be plainly marked as such,
 
  19 //    and must not be misrepresented as being the original software.
 
  21 // 3. This notice may not be removed or altered from any source distribution.
 
  23 ////////////////////////////////////////////////////////////
 
  25 #ifndef SFML_THREADLOCALPTR_HPP
 
  26 #define SFML_THREADLOCALPTR_HPP
 
  28 ////////////////////////////////////////////////////////////
 
  30 ////////////////////////////////////////////////////////////
 
  31 #include <SFML/System/ThreadLocal.hpp>
 
  36 ////////////////////////////////////////////////////////////
 
  37 /// \brief Pointer to a thread-local variable
 
  39 ////////////////////////////////////////////////////////////
 
  41 class ThreadLocalPtr : private ThreadLocal
 
  45     ////////////////////////////////////////////////////////////
 
  46     /// \brief Default constructor
 
  48     /// \param value Optional value to initalize the variable
 
  50     ////////////////////////////////////////////////////////////
 
  51     ThreadLocalPtr(T* value = NULL);
 
  53     ////////////////////////////////////////////////////////////
 
  54     /// \brief Overload of unary operator *
 
  56     /// Like raw pointers, applying the * operator returns a
 
  57     /// reference to the pointed object.
 
  59     /// \return Reference to the pointed object
 
  61     ////////////////////////////////////////////////////////////
 
  62     T& operator *() const;
 
  64     ////////////////////////////////////////////////////////////
 
  65     /// \brief Overload of operator ->
 
  67     /// Like raw pointers, applying the -> operator returns the
 
  70     /// \return Pointed object
 
  72     ////////////////////////////////////////////////////////////
 
  73     T* operator ->() const;
 
  75     ////////////////////////////////////////////////////////////
 
  76     /// \brief Cast operator to implicitely convert the
 
  77     ///        pointer to its raw pointer type (T*)
 
  79     /// \return Pointer to the actual object
 
  81     ////////////////////////////////////////////////////////////
 
  84     ////////////////////////////////////////////////////////////
 
  85     /// \brief Assignment operator for a raw pointer parameter
 
  87     /// \param value Pointer to assign
 
  89     /// \return Reference to self
 
  91     ////////////////////////////////////////////////////////////
 
  92     ThreadLocalPtr<T>& operator =(T* value);
 
  94     ////////////////////////////////////////////////////////////
 
  95     /// \brief Assignment operator for a ThreadLocalPtr parameter
 
  97     /// \param right ThreadLocalPtr to assign
 
  99     /// \return Reference to self
 
 101     ////////////////////////////////////////////////////////////
 
 102     ThreadLocalPtr<T>& operator =(const ThreadLocalPtr<T>& right);
 
 107 #include <SFML/System/ThreadLocalPtr.inl>
 
 110 #endif // SFML_THREADLOCALPTR_HPP
 
 113 ////////////////////////////////////////////////////////////
 
 114 /// \class sf::ThreadLocalPtr
 
 117 /// sf::ThreadLocalPtr is a type-safe wrapper for storing
 
 118 /// pointers to thread-local variables. A thread-local
 
 119 /// variable holds a different value for each different
 
 120 /// thread, unlike normal variable that are shared.
 
 122 /// Its usage is completely transparent, so that it is similar
 
 123 /// to manipulating the raw pointer directly (like any smart pointer).
 
 129 /// sf::ThreadLocalPtr<MyClass> objectPtr;
 
 133 ///     objectPtr = &object1; // doesn't impact thread2
 
 139 ///     objectPtr = &object2; // doesn't impact thread1
 
 145 ///     // Create and launch the two threads
 
 146 ///     sf::Thread t1(&thread1);
 
 147 ///     sf::Thread t2(&thread2);
 
 155 /// ThreadLocalPtr is designed for internal use; however you
 
 156 /// can use it if you feel like it fits well your implementation.
 
 158 ////////////////////////////////////////////////////////////