libiqxmlrpc  0.12.4
 All Classes Namespaces Files Functions Typedefs Enumerations
util.h
1 // Libiqxmlrpc - an object-oriented XML-RPC solution.
2 // Copyright (C) 2011 Anton Dedov
3 
4 #ifndef _iqxmlrpc_util_h_
5 #define _iqxmlrpc_util_h_
6 
7 #include "lock.h"
8 
9 #include <boost/utility.hpp>
10 
11 #include <functional>
12 #include <memory>
13 
14 namespace iqxmlrpc {
15 namespace util {
16 
17 template <class M>
18 class Select2nd:
19  public std::unary_function<typename M::value_type, typename M::mapped_type>
20 {
21 public:
22  typename M::mapped_type operator ()(typename M::value_type& i)
23  {
24  return i.second;
25  }
26 };
27 
28 template <class Iter>
29 void delete_ptrs(Iter first, Iter last)
30 {
31  for(; first != last; ++first)
32  delete *first;
33 }
34 
35 template <class Iter, class AccessOp>
36 void delete_ptrs(Iter first, Iter last, AccessOp op)
37 {
38  for(; first != last; ++first)
39  delete op(*first);
40 }
41 
42 template <class Ptr>
43 class ExplicitPtr {
44  Ptr p_;
45 
46 public:
47  explicit ExplicitPtr(Ptr p): p_(p) {}
48 
50  p_(ep.release())
51  {
52  }
53 
54  ~ExplicitPtr()
55  {
56  delete release();
57  }
58 
59  Ptr release()
60  {
61  Ptr p(p_);
62  p_ = 0;
63  return p;
64  }
65 };
66 
68 template <class Lock>
69 class LockedBool: boost::noncopyable {
70  bool val;
71  Lock lock;
72 
73 public:
74  LockedBool(bool default_):
75  val(default_) {}
76 
77  ~LockedBool() {}
78 
79  operator bool()
80  {
81  typename Lock::scoped_lock lk(lock);
82  return val;
83  }
84 
85  void operator =(bool b)
86  {
87  typename Lock::scoped_lock lk(lock);
88  val = b;
89  }
90 };
91 
92 } // namespace util
93 } // namespace iqxmlrpc
94 
95 #endif