ExpectationsList.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef UTILS__CALITKO_MOCKS__EXPECTATIONS_LIST_H
00024 #define UTILS__CALITKO_MOCKS__EXPECTATIONS_LIST_H
00025 
00026 #include "Imports.h"
00027 #include "BasicTypes.h"
00028 
00029 namespace Utils {
00030 namespace CalitkoMocks {
00031 
00033 
00058 template <typename Function>
00059 class ExpectationsList
00060 {
00061 public:
00062     ExpectationsList()
00063      : added(), taken()
00064     {
00065     }
00066 
00067     ~ExpectationsList()
00068     {
00069         while (!added.empty()) {
00070             delete added.front();
00071             added.pop_front();
00072         }
00073         while (!taken.empty()) {
00074             delete taken.front();
00075             taken.pop_front();
00076         }
00077     }
00078 
00079     bool empty()
00080     {
00081         return added.empty();
00082     }
00083 
00084     Function & add (auto_ptr <Function> foo)
00085     {
00086         added.push_back (foo.get());
00087         foo.release();
00088         return *foo;
00089     }
00090 
00091     // \todo These checks became quite messed... the design should be reworked!
00092     Function & take()
00093     {
00094         Function * f = added.front();
00095         // Because of numberOfTimes we might have a verified expectation
00096         // belonging to an already checked CallDriver still in the list.
00097         // Ignore all such:
00098         while (f->wasVerified()) {
00099             taken.push_back (f);
00100             added.pop_front();
00101             f = added.front();
00102         }
00103         // Only move it to taked if f is now called max numberOfTimes:
00104         if (f->isLastExpectedCall()) {
00105             taken.push_back (f);
00106             added.pop_front();
00107         }
00108         return *f;
00109     }
00110 
00111 private:
00112     // Forbid copying and assignments:
00113     ExpectationsList (const ExpectationsList &);
00114     ExpectationsList & operator= (const ExpectationsList &);
00115 
00116     std::list <Function *>  added;
00117     std::list <Function *>  taken;
00118 };
00119 
00121 
00148 template <typename Foo>
00149 struct ExpectationPair
00150 {
00151     ExpectationsList <Foo>      &list;
00152     mutable auto_ptr <Foo>      function;
00153 
00154     ExpectationPair (ExpectationsList <Foo> &l, auto_ptr <Foo> f)
00155         : list (l), function (f) {}
00156     ExpectationPair (const ExpectationPair &other)
00157         : list (other.list), function (other.function) {}
00158 };
00159 
00160 } // namespace CalitkoMocks
00161 } // namespace Utils
00162 
00163 #endif // UTILS__CALITKO_MOCKS__EXPECTATIONS_LIST_H