Fixed explicit ctor stuff
[libcds.git] / cds / container / moir_queue.h
index 832e5c9d0cdfd86547ca2d758bcf884083cfb3e5..42e86df91db479fc5d9e66ffc1b4959415a91347 100644 (file)
@@ -1,7 +1,35 @@
-//$$CDS-header$$
-
-#ifndef __CDS_CONTAINER_MOIR_QUEUE_H
-#define __CDS_CONTAINER_MOIR_QUEUE_H
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+
+    Source code repo: http://github.com/khizmax/libcds/
+    Download: http://sourceforge.net/projects/libcds/files/
+    
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this
+      list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
+*/
+
+#ifndef CDSLIB_CONTAINER_MOIR_QUEUE_H
+#define CDSLIB_CONTAINER_MOIR_QUEUE_H
 
 #include <memory>
 #include <cds/container/msqueue.h>
@@ -15,7 +43,7 @@ namespace cds { namespace container {
         struct make_moir_queue: public cds::container::details::make_msqueue< GC, T, Traits >
         {
             typedef cds::container::details::make_msqueue< GC, T, Traits > base_class;
-            typedef cds::intrusive::MoirQueue< GC, base_class::node_type, base_class::intrusive_traits > type;
+            typedef cds::intrusive::MoirQueue< GC, typename base_class::node_type, typename base_class::intrusive_traits > type;
         };
     }
     //@endcond
@@ -37,15 +65,15 @@ namespace cds { namespace container {
             typedef cds::container::MoirQueue< cds::gc::HP, Foo, myTraits > myQueue;
 
             // Equivalent make_traits example:
-            typedef cds::container::MoirQueue< cds::gc::HP, Foo, 
-                typename cds::container::msqueue::make_traits< 
+            typedef cds::container::MoirQueue< cds::gc::HP, Foo,
+                typename cds::container::msqueue::make_traits<
                     cds::opt::stat< cds::container::msqueue::stat<> >,
                     cds::opt::item_counter< cds::atomicity::item_counter >
                 >::type
             > myQueue;
             \endcode
     */
-    template <typename GC, typename T, typename Traits>
+    template <typename GC, typename T, typename Traits = cds::container::msqueue::traits >
     class MoirQueue:
 #ifdef CDS_DOXYGEN_INVOKED
         private intrusive::MoirQueue< GC, intrusive::msqueue::node< T >, Traits >
@@ -65,6 +93,8 @@ namespace cds { namespace container {
             typedef MoirQueue< GC2, T2, Traits2 > other   ;   ///< Rebinding result
         };
 
+        static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm
+
     public:
         typedef T value_type ; ///< Value type stored in the queue
         typedef typename base_class::gc                 gc;             ///< Garbage collector
@@ -124,7 +154,7 @@ namespace cds { namespace container {
         /// Enqueues \p val value into the queue.
         /**
             The function makes queue node in dynamic memory calling copy constructor for \p val
-            and then it calls intrusive::MoirQueue::enqueue.
+            and then it calls \p intrusive::MoirQueue::enqueue.
             Returns \p true if success, \p false otherwise.
         */
         bool enqueue( value_type const& val )
@@ -137,6 +167,17 @@ namespace cds { namespace container {
             return false;
         }
 
+        /// Enqueues \p val value into the queue, move semantics
+        bool enqueue( value_type&& val )
+        {
+            scoped_node_ptr p( alloc_node_move( std::move( val )));
+            if ( base_class::enqueue( *p ) ) {
+                p.release();
+                return true;
+            }
+            return false;
+        }
+
         /// Enqueues \p data to queue using a functor
         /**
             \p Func is a functor calling to create a new node.
@@ -178,6 +219,12 @@ namespace cds { namespace container {
             return enqueue( val );
         }
 
+        /// Synonym for \p enqueue() function, move semantics
+        bool push( value_type&& val )
+        {
+            return enqueue( std::move( val ));
+        }
+
         /// Synonym for \p enqueue_with() function
         template <typename Func>
         bool push_with( Func f )
@@ -193,7 +240,7 @@ namespace cds { namespace container {
         */
         bool dequeue( value_type& dest )
         {
-            return dequeue_with( [&dest]( value_type& src ) { dest = src;  } );
+            return dequeue_with( [&dest]( value_type& src ) { dest = std::move( src ); });
         }
 
         /// Dequeues a value using a functor
@@ -265,6 +312,6 @@ namespace cds { namespace container {
 
 }}  // namespace cds::container
 
-#endif  // #ifndef __CDS_CONTAINER_MOIR_QUEUE_H
+#endif  // #ifndef CDSLIB_CONTAINER_MOIR_QUEUE_H