1 ///
2 /// Copyright (c) 2012, Texas A&M University
3 /// All rights reserved.
4 ///
5 /// Redistribution and use in source and binary forms, with or without
6 /// modification, are permitted provided that the following conditions
7 /// are met:
8 ///
9 ///  * Redistributions of source code must retain the above copyright
10 ///    notice, this list of conditions and the following disclaimer.
11 ///  * Redistributions in binary form must reproduce the above
12 ///    copyright notice, this list of conditions and the following
13 ///    disclaimer in the documentation and/or other materials provided
14 ///    with the distribution.
15 ///  * Neither the name of Texas A&M University nor the names of its
16 ///    contributors may be used to endorse or promote products derived
17 ///    from this software without specific prior written permission.
18 ///
19 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 /// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 /// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 /// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 /// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 /// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 /// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 /// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 /// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 /// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 /// POSSIBILITY OF SUCH DAMAGE.
31 ///
32 /// The following software was written as part of a collaboration with the
33 /// University of South Carolina, Interdisciplinary Mathematics Institute.
34 ///
35 
36 /// @file   commandQueue.cpp
37 /// @details Implementation of the class CommandQueue, see commandQueue.h for
38 /// details.
39 /// @author  Yue Li and Matthew Hielsberg
40 
41 #include <pcl/apps/point_cloud_editor/commandQueue.h>
42 #include <pcl/apps/point_cloud_editor/command.h>
43 
CommandQueue()44 CommandQueue::CommandQueue () : depth_limit_(DEFAULT_MAX_SIZE_)
45 {
46 }
47 
CommandQueue(unsigned int max_size)48 CommandQueue::CommandQueue (unsigned int max_size) : depth_limit_(max_size)
49 {
50 }
51 
52 void
enforceDequeLimit()53 CommandQueue::enforceDequeLimit ()
54 {
55   // the following loop should actually execute only one iteration.
56   while (command_deque_.size() > depth_limit_)
57     command_deque_.pop_front();
58 }
59 
60 void
execute(const CommandPtr & command_ptr)61 CommandQueue::execute (const CommandPtr& command_ptr)
62 {
63   if (!command_ptr)
64     return;
65   command_ptr->execute();
66   if (command_ptr->hasUndo())
67   {
68     command_deque_.push_back(command_ptr);
69     enforceDequeLimit();
70   }
71 }
72 
73 void
undo()74 CommandQueue::undo ()
75 {
76   // no-op when no command is in the queue.
77   if (command_deque_.empty())
78     return;
79   (command_deque_.back())->undo();
80   command_deque_.pop_back();
81 }
82 
83 unsigned int
setMaxSize(unsigned int size)84 CommandQueue::setMaxSize (unsigned int size)
85 {
86   depth_limit_ = size;
87   if (depth_limit_ > command_deque_.max_size())
88     depth_limit_ = command_deque_.max_size();
89   // resize should be faster than enforceDequeLimit
90   if (command_deque_.size() > depth_limit_)
91     command_deque_.resize(depth_limit_);
92   return (depth_limit_);
93 }
94 
95 
96 
97