FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
string_mapped.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/util/string.hpp>
10
11// includes, system
12#include <map>
13#include <iostream>
14
15namespace FEAT
16{
30 template<typename ValueType_>
32 {
33 private:
35 ValueType_& _value;
37 const std::map<String, ValueType_>& _value_map;
38
39 public:
49 explicit StringMapped(ValueType_& value, const std::map<String, ValueType_>& value_map) :
51 _value_map(value_map)
52 {
53 }
54
69 bool lookup(const String& str)
70 {
71 // try to find
72 auto it = _value_map.find(str);
73 if(it == _value_map.end())
74 return false;
75 // okay, assign value
76 _value = it->second;
77 return true;
78 }
79
92 friend std::istream& operator>>(std::istream& is, StringMapped& val)
93 {
94 String str;
95 // Try to fetch a string from the stream
96 if(!(is >> str).fail())
97 {
98 // Try to look up the string; if that fails, set the failbit
99 if(!val.lookup(str))
100 {
101 is.setstate(std::ios::failbit);
102 }
103 }
104 return is;
105 }
106
108 friend std::istream& operator>>(std::istream& is, StringMapped&& val)
109 {
110 String str;
111 // Try to fetch a string from the stream
112 if(!(is >> str).fail())
113 {
114 // Try to look up the string; if that fails, set the failbit
115 if(!val.lookup(str))
116 {
117 is.setstate(std::ios::failbit);
118 }
119 }
120 return is;
121 }
123
136 friend std::ostream& operator<<(std::ostream& os, const StringMapped& val)
137 {
138 return (os << (val._value));
139 }
140
142 friend std::ostream& operator<<(std::ostream& os, StringMapped&& val)
143 {
144 return (os << (val._value));
145 }
147 }; // class StringMapped
148
161 template<typename ValueType_>
162 inline StringMapped<ValueType_> string_mapped(ValueType_& value, const std::map<String, ValueType_>& value_map)
163 {
164 return StringMapped<ValueType_>(value, value_map);
165 }
166
182 template<typename ValueType_>
183 inline bool string_mapped_lookup(ValueType_& value, const std::map<String, ValueType_>& value_map, const String& str)
184 {
185 return string_mapped(value, value_map).lookup(str);
186 }
187} // namespace FEAT
String class implementation.
Definition: string.hpp:46
String-Mapped class template.
const std::map< String, ValueType_ > & _value_map
the string map
ValueType_ & _value
the value
StringMapped(ValueType_ &value, const std::map< String, ValueType_ > &value_map)
Constructor.
friend std::istream & operator>>(std::istream &is, StringMapped &val)
Input stream extraction operator.
bool lookup(const String &str)
Looks up the value in the map from a given string.
friend std::ostream & operator<<(std::ostream &os, const StringMapped &val)
Output stream insertion operator.
FEAT namespace.
Definition: adjactor.hpp:12
StringMapped< ValueType_ > string_mapped(ValueType_ &value, const std::map< String, ValueType_ > &value_map)
Creates a StringMapped object.
@ value
specifies whether the space should supply basis function values
bool string_mapped_lookup(ValueType_ &value, const std::map< String, ValueType_ > &value_map, const String &str)
Looks up a StringMapped value.