deal2lkit: A ToolKit library for Deal.II
any_data.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2015 by the deal2lkit authors
4 //
5 // This file is part of the deal2lkit library.
6 //
7 // The deal2lkit library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal2lkit distribution.
13 //
14 //-----------------------------------------------------------
15 
16 #ifndef _d2k_any_data_h
17 #define _d2k_any_data_h
18 
19 #include <deal2lkit/config.h>
20 #include <deal.II/base/config.h>
23 
24 #include <boost/any.hpp>
25 #include <vector>
26 #include <algorithm>
27 #include <typeinfo>
28 
29 #include <map>
30 #include <deal2lkit/utilities.h>
31 
32 using namespace dealii;
33 
34 
35 D2K_NAMESPACE_OPEN
36 
65 class AnyData : public Subscriptor
66 {
67 public:
68 
75  template <typename type>
76  void add_copy (const type &entry, const std::string &name);
77 
84  template <typename type>
85  void add_ref (type &entry, const std::string &name);
86 
95  template <typename type>
96  type &get (const std::string &name);
97 
106  template <typename type>
107  const type &get (const std::string &name) const;
108 
115  inline bool have (const std::string &name) const;
116 
123  template <class STREAM>
124  void print_info (STREAM &os);
125 
127  DeclException1(ExcNameNotFound, std::string,
128  << "No entry with the name " << arg1 << " exists.");
129 
132  char *, char *,
133  << "The requested type " << arg1
134  << " and the stored type " << arg2
135  << " must coincide");
136 
137 
138 private:
139  std::map<std::string, boost::any> mydata;
140 }; // end class
141 
142 template <typename type>
143 void AnyData::add_copy (const type &entry, const std::string &name)
144 {
145  mydata[name] = entry;
146 }
147 
148 template <typename type>
149 void AnyData::add_ref (type &entry, const std::string &name)
150 {
151  type *ptr = &entry;
152  mydata[name] = ptr;
153 }
154 
155 template <typename type>
156 type &AnyData::get(const std::string &name)
157 {
158  Assert( mydata.find(name) != mydata.end(),
159  ExcNameNotFound(name));
160 
161  type *p=NULL;
162 
163  if (mydata[name].type() == typeid(type *) )
164  {
165  p = boost::any_cast<type *>(mydata[name]);
166  }
167  else if (mydata[name].type() == typeid(type))
168  {
169  p = boost::any_cast<type>(&mydata[name]);
170  }
171  else
172  {
173  Assert(false,
174  ExcTypeMismatch(typeid(type).name(),mydata[name].type().name()));
175  }
176 
177  return *p;
178 }
179 
180 template <typename type>
181 const type &AnyData::get(const std::string &name) const
182 {
183  Assert( mydata.find(name) != mydata.end(),
184  ExcNameNotFound(name));
185 
186  typedef std::map<std::string, boost::any>::const_iterator it_type;
187 
188  it_type it = mydata.find(name);
189 
190  if (it->second.type() == typeid(type *) )
191  {
192  const type *p = boost::any_cast<type *>(it->second);
193  return *p;
194  }
195  else if (it->second.type() == typeid(type))
196  {
197  const type *p = boost::any_cast<type>(&it->second);
198  return *p;
199  }
200  else
201  {
202  Assert(false,
203  ExcTypeMismatch(typeid(type).name(),it->second.type().name()));
204  const type *p=NULL;
205  return *p;
206  }
207 }
208 
209 
210 bool AnyData::have(const std::string &name) const
211 {
212  return mydata.find(name) != mydata.end();
213 }
214 
215 template <class STREAM>
216 inline
217 void AnyData::print_info(STREAM &os)
218 {
219  for (std::map<std::string, boost::any>::iterator it=mydata.begin(); it != mydata.end(); ++it)
220  {
221  os << it->first
222  << '\t' << '\t'
223  << demangle(it->second.type().name())
224  << std::endl;
225  }
226 }
227 
228 
229 D2K_NAMESPACE_CLOSE
230 
231 #endif
232 
static::ExceptionBase & ExcNameNotFound(std::string arg1)
#define DeclException2(Exception2, type1, type2, outsequence)
DeclException1(ExcCannottExecuteCommand, std::string,<< "Cannot execute command "<< arg1<< "\Please verify you have "<< "the needed permissions.")
Cannot execute std::system(command)
std::string type(const T &t)
Return a human readable name of the type passed as argument.
Definition: utilities.h:461
#define Assert(cond, exc)
static::ExceptionBase & ExcTypeMismatch(char *arg1, char *arg2)
std::string demangle(const char *name)
Demangle c++ names.
Definition: utilities.cc:28
Store any amount of any type of data accessible by an identifier string.
Definition: any_data.h:65