1 // STLUtil.cc for FbTk
2 // Copyright (c) 2006 Fluxbox Team (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 #ifndef FBTK_STLUTIL_HH
23 #define FBTK_STLUTIL_HH
24 
25 /// contains useful utilities for STL
26 namespace FbTk {
27 namespace STLUtil {
28 
29 template<bool C, typename Ta, typename Tb>
30 struct IfThenElse;
31 
32 template<typename Ta, typename Tb>
33 struct IfThenElse<true, Ta, Tb> {
operator ()FbTk::STLUtil::IfThenElse34     Ta& operator ()(Ta& ta, Tb& tb) const {
35         return ta;
36     }
37     typedef Ta ResultType;
38 };
39 
40 template<typename Ta, typename Tb>
41 struct IfThenElse<false, Ta, Tb> {
operator ()FbTk::STLUtil::IfThenElse42     Tb& operator ()(Ta& ta, Tb& tb) const {
43         return tb;
44     }
45     typedef Tb ResultType;
46 };
47 
48 /// calls delete on each item in the container and then clears the container
49 template <typename A>
destroyAndClear(A & a)50 void destroyAndClear(A &a) {
51     typedef typename A::iterator iterator;
52     iterator it = a.begin();
53     iterator it_end = a.end();
54     for (; it != it_end; ++it)
55         delete (*it);
56 
57     a.clear();
58 }
59 
60 /// calls delete on each item value in the map and then clears the map
61 template <typename A>
destroyAndClearSecond(A & a)62 void destroyAndClearSecond(A &a) {
63     typedef typename A::iterator iterator;
64     iterator it = a.begin();
65     iterator it_end = a.end();
66     for (; it != it_end; ++it)
67         delete it->second;
68     a.clear();
69 }
70 
71 
72 template <typename C, typename F>
forAll(C & c,F f)73 F forAll(C& c, F f) {
74     typedef typename C::iterator iterator;
75     iterator i = c.begin();
76     iterator e = c.end();
77     for (; i != e; i++) {
78         f(*i);
79     }
80     return f;
81 }
82 
83 template <typename C, typename I, typename F>
forAllIf(C & c,I i,F f)84 F forAllIf(C& c, I i, F f) {
85     typedef typename C::iterator iterator;
86     iterator it = c.begin();
87     iterator end = c.end();
88     for (; it != end; ++it) {
89         if (i(*it))
90             f(*it);
91     }
92     return f;
93 }
94 
95 
96 
97 
98 } // end namespace STLUtil
99 } // end namespace FbTk
100 
101 #endif // STLUTIL_Hh
102