dune-fem  2.4.1-rc
gridname.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_MISC_GRIDNAME_HH
2 #define DUNE_FEM_MISC_GRIDNAME_HH
3 
4 // C++ includes
5 #include <cstdlib>
6 #include <climits>
7 #include <iostream>
8 #include <string>
9 #include <typeinfo>
10 #include <vector>
11 
12 // dune-common includes
13 #include <dune/common/exceptions.hh>
14 
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
22  // Internal forward declaration
23  // ----------------------------
24 
25  template< class Grid > struct GridName;
26 
27 
28 
29  // gridName
30  // --------
31 
32  template< class GridImp >
33  static const std::string &gridName ()
34  {
35  return GridName< GridImp >::str();
36  }
37 
38  template< class GridImp >
39  static const std::string &gridName ( const GridImp &grid )
40  {
41  return gridName< GridImp >();
42  }
43 
44 
45 
46  // UnknownGridException
47  // --------------------
48 
49  class UnknownGridException : public Exception {};
50 
51 
52 
53  // GridName
54  // --------
55 
56  template< class GridImp >
57  struct GridName
58  {
59  static const std::string &str ()
60  {
61  static std::string str = computeString();
62  return str;
63  }
64 
65  private:
66  static std::string computeString ()
67  {
68  std::string name( typeid( GridImp ).name() );
69 
70  size_t dunePos = name.find( "Dune" );
71  name.erase( 0, dunePos+4 );
72 
73  char *endptr = 0;
74  // get position of strings that are not numbers
75  long int result = std::strtol( name.c_str(), &endptr, 0 );
76  if( result == LONG_MAX || result == LONG_MIN )
77  DUNE_THROW( UnknownGridException, "GridName: faild to determine name of grid!" );
78 
79  if( endptr )
80  name = std::string( endptr );
81 
82  // Grid seems to be followed by IL
83  size_t pos = name.find( "GridI" );
84  pos += 4; // add length of Grid to get pos of IL
85 
86  if( pos < name.size() )
87  name.erase( pos, name.size() - pos );
88 #ifndef NDEBUG
89  std::vector< std::string > knownGrids;
90  knownGrids.push_back( "AlbertaGrid" );
91  knownGrids.push_back( "ALUConformGrid" );
92  knownGrids.push_back( "ALUCubeGrid" );
93  knownGrids.push_back( "ALUGrid" );
94  knownGrids.push_back( "ALUSimplexGrid" );
95  knownGrids.push_back( "CacheItGrid" );
96  knownGrids.push_back( "CartesianGrid" );
97  knownGrids.push_back( "GeometryGrid" );
98  knownGrids.push_back( "OneDGrid" );
99  knownGrids.push_back( "P4estGrid" );
100  knownGrids.push_back( "ParallelGrid" );
101  knownGrids.push_back( "ParallelSimplexGrid" );
102  knownGrids.push_back( "PrismGrid" );
103  knownGrids.push_back( "SGrid" );
104  knownGrids.push_back( "SPGrid" );
105  knownGrids.push_back( "UGGrid" );
106  knownGrids.push_back( "YaspGrid" );
107 
108  bool found = false ;
109  for( size_t i=0; i < knownGrids.size(); ++i )
110  {
111  if( name == knownGrids[ i ] )
112  {
113  found = true;
114  break;
115  }
116  }
117 
118  if( !found )
119  {
120  std::cerr << "WARNING: Grid name `" << name << "' not found in list of known grids! Please add in file " << __FILE__ << std::endl;
121  }
122 #endif
123  return name;
124  }
125  };
126 
127  } // namespace Fem
128 
129 } // namespace Dune
130 
131 #endif // #ifndef DUNE_FEM_MISC_GRIDNAME_HH
static const std::string & str()
Definition: gridname.hh:59
Definition: gridname.hh:49
static const std::string & gridName()
Definition: gridname.hh:33
Definition: gridname.hh:25
Definition: coordinate.hh:4