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* get(const std::string& key)
76  {
77  container_type::iterator it = data.find(key);
78  return it == data.end() ? 0 : it->second->cast<T>();
79  }
80 
83  template <typename T, template <class> class destroyPolicy>
84  void put(const std::string& key, T* o)
85  {
86  try
87  {
89  privatePut(key, ptr);
90  }
91  catch (const std::bad_alloc&)
92  {
93  destroyPolicy<T>::destroy(o);
94  throw;
95  }
96  }
97 
98  template <typename T>
99  void put(const std::string& key, T* o)
100  { put<T, cxxtools::DeletePolicy>(key, o); }
101 
102  template <typename T>
103  void put(const std::string& key, T* o, bool transferOwnership)
104  {
105  if (transferOwnership)
106  put<T, cxxtools::DeletePolicy>(key, o);
107  else
108  put<T, NullDestroyPolicy>(key, o);
109  }
110 
111  void erase(const std::string& key) { data.erase(key); }
112  bool empty() const { return data.empty(); }
113  void clear() { data.clear(); }
114 
115  typedef container_type::const_iterator const_iterator;
116  typedef container_type::iterator iterator;
117  typedef container_type::value_type value_type;
118  const_iterator begin() const { return data.begin(); }
119  const_iterator end() const { return data.end(); }
120  iterator begin() { return data.begin(); }
121  iterator end() { return data.end(); }
122  };
123 }
124 
125 #endif // TNT_SCOPE_H
126