scope.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_SCOPE_H
31 #define TNT_SCOPE_H
32 
33 #include <map>
34 #include <string>
35 #include <tnt/object.h>
36 #include <cxxtools/mutex.h>
37 #include <cxxtools/refcounted.h>
38 
39 namespace tnt
40 {
41  template <typename objectType>
43  {
44  public:
45  static void destroy(objectType* ptr) { }
46  };
47 
49  {
50  public:
52 
53  private:
54  typedef std::map<std::string, pointer_type> container_type;
55 
56  container_type _data;
57  mutable cxxtools::Mutex _mutex;
58 
59  void privatePut(const std::string& key, pointer_type o);
60 
61  // non-copyable
62  Scope(const Scope&);
63  Scope& operator=(const Scope&);
64 
65  public:
66  Scope();
67  virtual ~Scope();
68 
69  void lock() { _mutex.lock(); }
70  void unlock() { _mutex.unlock(); }
71 
72  bool has(const std::string& key) const
73  { return _data.find(key) != _data.end(); }
74 
75  template <typename T> T*
76  get(const std::string& key)
77  {
78  container_type::iterator it = _data.find(key);
79  return it == _data.end() ? 0 : it->second->cast<T>();
80  }
81 
84  template <typename T, template <class> class destroyPolicy>
85  void put(const std::string& key, T* o)
86  {
87  try
88  {
90  privatePut(key, ptr);
91  }
92  catch (const std::bad_alloc&)
93  {
94  destroyPolicy<T>::destroy(o);
95  throw;
96  }
97  }
98 
99  template <typename T>
100  void put(const std::string& key, T* o)
101  { put<T, cxxtools::DeletePolicy>(key, o); }
102 
103  template <typename T>
104  void put(const std::string& key, T* o, bool transferOwnership)
105  {
106  if (transferOwnership)
107  put<T, cxxtools::DeletePolicy>(key, o);
108  else
109  put<T, NullDestroyPolicy>(key, o);
110  }
111 
112  void erase(const std::string& key) { _data.erase(key); }
113  bool empty() const { return _data.empty(); }
114  void clear() { _data.clear(); }
115 
116  typedef container_type::const_iterator const_iterator;
117  typedef container_type::iterator iterator;
118  typedef container_type::value_type value_type;
119  const_iterator begin() const { return _data.begin(); }
120  const_iterator end() const { return _data.end(); }
121  iterator begin() { return _data.begin(); }
122  iterator end() { return _data.end(); }
123  };
124 }
125 
126 #endif // TNT_SCOPE_H
127