SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TypeIDTable.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27  */
28 /// @todo Documentation Core/Utils/TypeIDTable.h
29 
30 #ifndef CORE_UTILS_TYPEIDTABLE_H
31 #define CORE_UTILS_TYPEIDTABLE_H
32 
33 #include <boost/thread/mutex.hpp>
34 #include <boost/noncopyable.hpp>
35 #include <boost/optional.hpp>
36 #include <Core/Utils/share.h>
37 
38 namespace SCIRun
39 {
40 namespace Core
41 {
42 namespace Utility
43 {
44  /// A thread-safe map used for constructor lookup in Mesh/Field factories.
45 
46  template <class CtorInfo>
47  class TypeIDTable : boost::noncopyable
48  {
49  public:
50  typedef boost::optional<const CtorInfo&> CtorInfoOption;
51 
52  //do locking internally
53  CtorInfoOption findConstructorInfo(const std::string& key) const
54  {
55  boost::mutex::scoped_lock s(lock_);
56  auto iter = lookup_.find(key);
57  if (iter == lookup_.end())
58  return CtorInfoOption();
59  return CtorInfoOption(iter->second);
60  }
61 
62  bool registerConstructorInfo(const std::string& key, const CtorInfo& info)
63  {
64  boost::mutex::scoped_lock s(lock_);
65  auto iter = lookup_.find(key);
66  if (iter != lookup_.end())
67  {
68  if (iter->second != info)
69  {
70  /// @todo: improve for testing
71  /// @todo: use real logger here
72  std::cerr << "WARNING: duplicate type exists: " << key << "\n";
73  return false;
74  }
75  }
76  lookup_[key] = info;
77  return true;
78  }
79 
80  size_t size() const { return lookup_.size(); }
81 
82  private:
83  mutable boost::mutex lock_;
84  std::map<std::string, CtorInfo> lookup_;
85  };
86 
87 }}}
88 
89 #endif
size_t size() const
Definition: TypeIDTable.h:80
bool registerConstructorInfo(const std::string &key, const CtorInfo &info)
Definition: TypeIDTable.h:62
A thread-safe map used for constructor lookup in Mesh/Field factories.
Definition: TypeIDTable.h:47
boost::optional< const CtorInfo & > CtorInfoOption
Definition: TypeIDTable.h:50
CtorInfoOption findConstructorInfo(const std::string &key) const
Definition: TypeIDTable.h:53