Schnek
blockdata.hpp
1 /*
2  * blockdata.hpp
3  *
4  * Created on: 26 Jun 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_BLOCKDATA_HPP_
28 #define SCHNEK_BLOCKDATA_HPP_
29 
30 #include "../util/singleton.hpp"
31 #include "../util/logger.hpp"
32 #include <map>
33 #include <string>
34 
35 #undef LOGLEVEL
36 #define LOGLEVEL 0
37 
38 namespace schnek
39 {
40 
41 template<typename T>
42 class BlockData : public Singleton<BlockData<T> >
43 {
44  private:
45  typedef std::map<std::string, T*> DataMap;
46  typedef boost::shared_ptr<DataMap> pDataMap;
47  typedef std::map<long, pDataMap> BlockDataMap;
48 
49  BlockDataMap blockDataMap;
50 
51  public:
52  void add(long blockId, std::string key, T &data);
53  T* get(long blockId, std::string key);
54  bool exists(long blockId, std::string key);
55 };
56 
57 
58 template<typename T>
59 void BlockData<T>::add(long blockId, std::string key, T &data)
60 {
61  SCHNEK_TRACE_LOG(2, "BlockData<T>::add(" <<blockId << ", " << key << ")")
62  if (0 == blockDataMap.count(blockId))
63  {
64  pDataMap pdm(new DataMap());
65  blockDataMap[blockId] = pdm;
66  }
67 
68  (*blockDataMap[blockId])[key] = &data;
69 }
70 
71 template<typename T>
72 T* BlockData<T>::get(long blockId, std::string key)
73 {
74  SCHNEK_TRACE_LOG(2, "BlockData<T>::get(" <<blockId << ", " << key << ")")
75 
76  if (0 == blockDataMap.count(blockId))
77  throw VariableNotFoundException("Could not find Block that holds variable "+key);
78  if (0 == blockDataMap[blockId]->count(key))
79  throw VariableNotFoundException("Could not find Block variable "+key);
80 
81  return (*blockDataMap[blockId])[key];
82 }
83 
84 template<typename T>
85 bool BlockData<T>::exists(long blockId, std::string key)
86 {
87  SCHNEK_TRACE_LOG(2, "BlockData<T>::exists(" <<blockId << ", " << key << ")")
88  if (0 == blockDataMap.count(blockId)) return false;
89  return (blockDataMap[blockId]->count(key) > 0);
90 }
91 
92 } //namespace
93 
94 
95 #endif // SCHNEK_BLOCKDATA_HPP_
96 
Definition: singleton.hpp:79
Definition: algo.hpp:30
Definition: blockdata.hpp:42
Definition: types.hpp:53
#define SCHNEK_TRACE_LOG(i, x)
Definition: logger.hpp:54