1 /*!
2  * \file  src/Glossary/GlossaryEntry.cxx
3  * \brief
4  * \author Thomas Helfer
5  * \brief 31 mars 2014
6  * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights
7  * reserved.
8  * This project is publicly released under either the GNU GPL Licence
9  * or the CECILL-A licence. A copy of thoses licences are delivered
10  * with the sources of TFEL. CEA or EDF may also distribute this
11  * project under specific licensing conditions.
12  */
13 
14 #include<iostream>
15 #include<stdexcept>
16 #include"TFEL/Raise.hxx"
17 #include"TFEL/Utilities/StringAlgorithms.hxx"
18 #include"TFEL/Glossary/GlossaryEntry.hxx"
19 
20 namespace tfel
21 {
22 
23   namespace glossary
24   {
25 
nothrow(const char * c)26     static std::string nothrow(const char *c) noexcept{
27       std::string r;
28       try{
29 	r = c;
30       } catch(std::exception& e){
31 	std::cerr << "tfel::glossary::nothrow2: "
32 		  << e.what() << '\n';
33       } catch(...){
34 	std::cerr << "tfel::glossary::nothrow2: "
35 		  << "unknown exception\n";
36       }
37       return r;
38     }
39 
nothrow2(const char * c)40     static std::vector<std::string> nothrow2(const char *c) noexcept{
41       std::vector<std::string> r;
42       try{
43 	r.emplace_back(c);
44       } catch(std::exception& e){
45 	std::cerr << "tfel::glossary::nothrow2: "
46 		  << e.what() << '\n';
47       } catch(...){
48 	std::cerr << "tfel::glossary::nothrow2: "
49 		  << "unknown exception\n";
50       }
51       return r;
52     }
53 
nothrow2(const char * const * const cb,const char * const * const ce)54     static std::vector<std::string> nothrow2(const char *const *const cb,
55 					     const char *const *const ce) noexcept{
56       std::vector<std::string> r;
57       try{
58 	r.insert(r.end(),cb,ce);
59       } catch(std::exception& e){
60 	std::cerr << "tfel::glossary::nothrow2: "
61 		  << e.what() << '\n';
62       } catch(...){
63 	std::cerr << "tfel::glossary::nothrow2: "
64 		  << "unknown exception\n";
65       }
66       return r;
67     }
68 
tokenize(const char * c)69     static std::vector<std::string> tokenize(const char * c) noexcept{
70       std::vector<std::string> r;
71       try{
72 	r = tfel::utilities::tokenize(c,"@^separator^@");
73       } catch(std::exception& e){
74 	std::cerr << "tfel::glossary::tokenize: "
75 		  << e.what() << '\n';
76       } catch(...){
77 	std::cerr << "tfel::glossary::tokenize: "
78 		  << "unknown exception\n";
79       }
80       return r;
81     }
82 
GlossaryEntry(const std::string & k,const std::string & n,const std::string & u,const std::string & t,const std::string & sd,const std::vector<std::string> & d,const std::vector<std::string> & no)83     GlossaryEntry::GlossaryEntry(const std::string& k,
84 				 const std::string& n,
85 				 const std::string& u,
86 				 const std::string& t,
87 				 const std::string& sd,
88 				 const std::vector<std::string>& d,
89 				 const std::vector<std::string>& no)
90       : key(k),
91 	names(1u,n),
92 	unit(u),
93 	type(t),
94 	short_description(sd),
95 	description(d),
96 	notes(no)
97     {
98       this->check();
99     }
100 
GlossaryEntry(const std::string & k,const std::string & n,const std::string & u,const std::string & t,const std::string & sd,const std::string & d,const std::string & no)101     GlossaryEntry::GlossaryEntry(const std::string& k,
102 				 const std::string& n,
103 				 const std::string& u,
104 				 const std::string& t,
105 				 const std::string& sd,
106 				 const std::string& d,
107 				 const std::string& no)
108       : key(k),
109 	names(1u,n),
110 	unit(u),
111 	type(t),
112 	short_description(sd),
113 	description(tfel::utilities::tokenize(d,"@^separator^@")),
114 	notes(tfel::utilities::tokenize(no,"@^separator^@"))
115     {
116       this->check();
117     }
118 
GlossaryEntry(const char * const k,const char * const n,const char * const u,const char * const t,const char * const sd,const char * const d,const char * const no)119     GlossaryEntry::GlossaryEntry(const char* const k,
120 				 const char* const n,
121 				 const char* const u,
122 				 const char* const t,
123 				 const char* const sd,
124 				 const char* const d,
125 				 const char* const no) noexcept
126       : key(nothrow(k)),
127 	names(nothrow2(n)),
128 	unit(nothrow(u)),
129 	type(nothrow(t)),
130 	short_description(nothrow(sd)),
131 	description(tfel::glossary::tokenize(d)),
132 	notes(tfel::glossary::tokenize(no))
133     {
134       try{
135 	this->check();
136       } catch(std::exception& ex){
137 	std::cerr << "GlossaryEntry::GlossaryEntry: " << ex.what() << '\n';
138       } catch(...){
139 	std::cerr << "GlossaryEntry::GlossaryEntry: "
140 		  << "unknown exception\n";
141       }
142     }
143 
GlossaryEntry(const std::string & k,const std::vector<std::string> & n,const std::string & u,const std::string & t,const std::string & sd,const std::vector<std::string> & d,const std::vector<std::string> & no)144     GlossaryEntry::GlossaryEntry(const std::string& k,
145 				 const std::vector<std::string>& n,
146 				 const std::string& u,
147 				 const std::string& t,
148 				 const std::string& sd,
149 				 const std::vector<std::string>& d,
150 				 const std::vector<std::string>& no)
151       : key(k),
152 	names(n),
153 	unit(u),
154 	type(t),
155 	short_description(sd),
156 	description(d),
157 	notes(no)
158     {
159       this->check();
160     }
161 
GlossaryEntry(const std::string & k,const std::vector<std::string> & n,const std::string & u,const std::string & t,const std::string & sd,const std::string & d,const std::string & no)162     GlossaryEntry::GlossaryEntry(const std::string& k,
163 				 const std::vector<std::string>& n,
164 				 const std::string& u,
165 				 const std::string& t,
166 				 const std::string& sd,
167 				 const std::string& d,
168 				 const std::string& no)
169       : key(k),
170 	names(n),
171 	unit(u),
172 	type(t),
173 	short_description(sd),
174 	description(tfel::utilities::tokenize(d,"@^separator^@")),
175 	notes(tfel::utilities::tokenize(no,"@^separator^@"))
176     {
177       this->check();
178     }
179 
GlossaryEntry(const std::string & k,const char * const * const b,const char * const * const e,const std::string & u,const std::string & t,const std::string & sd,const std::vector<std::string> & d,const std::vector<std::string> & no)180     GlossaryEntry::GlossaryEntry(const std::string& k,
181 				 const char * const * const b,
182 				 const char * const * const e,
183 				 const std::string& u,
184 				 const std::string& t,
185 				 const std::string& sd,
186 				 const std::vector<std::string>& d,
187 				 const std::vector<std::string>& no)
188       : key(k),
189 	names(b,e),
190 	unit(u),
191 	type(t),
192 	short_description(sd),
193 	description(d),
194 	notes(no)
195     {
196       this->check();
197     }
198 
GlossaryEntry(const std::string & k,const char * const * const b,const char * const * const e,const std::string & u,const std::string & t,const std::string & sd,const std::string & d,const std::string & no)199     GlossaryEntry::GlossaryEntry(const std::string& k,
200 				 const char * const * const b,
201 				 const char * const * const e,
202 				 const std::string& u,
203 				 const std::string& t,
204 				 const std::string& sd,
205 				 const std::string& d,
206 				 const std::string& no)
207       : key(k),
208 	names(b,e),
209 	unit(u),
210 	type(t),
211 	short_description(sd),
212 	description(tfel::utilities::tokenize(d,"@^separator^@")),
213 	notes(tfel::utilities::tokenize(no,"@^separator^@"))
214     {
215       this->check();
216     }
217 
GlossaryEntry(const char * const k,const char * const * const b,const char * const * const e,const char * const u,const char * const t,const char * const sd,const char * const d,const char * const no)218     GlossaryEntry::GlossaryEntry(const char* const k,
219 				 const char * const * const b,
220 				 const char * const * const e,
221 				 const char* const u,
222 				 const char* const t,
223 				 const char* const sd,
224 				 const char* const d,
225 				 const char* const no) noexcept
226     : key(nothrow(k)),
227       names(nothrow2(b,e)),
228       unit(nothrow(u)),
229       type(nothrow(t)),
230       short_description(nothrow(sd)),
231       description(tfel::glossary::tokenize(d)),
232       notes(tfel::glossary::tokenize(no))
233     {
234       try{
235 	this->check();
236       } catch(std::exception& ex){
237 	std::cerr << "GlossaryEntry::GlossaryEntry: " << ex.what() << '\n';
238       } catch(...){
239 	std::cerr << "GlossaryEntry::GlossaryEntry: "
240 		  << "unknown exception\n";
241       }
242     }
243 
244     GlossaryEntry::GlossaryEntry(const GlossaryEntry&) = default;
245     GlossaryEntry::GlossaryEntry(GlossaryEntry&&) = default;
246 
check() const247     void GlossaryEntry::check() const
248     {
249       raise_if(this->names.empty(),"GlossaryEntry::check: "
250 	       "no name specified for key '"+this->getKey()+"'");
251       raise_if(!((this->type=="scalar")||
252 		 (this->type=="vector")||
253 		 (this->type=="symmetric tensor")||
254 		 (this->type=="tensor")),
255 	       "GlossaryEntry::check: "
256 	       "unsupported type '"+this->type+"' "
257 	       "for entry '"+this->getKey()+"'");
258     } // end of GlossaryEntry::check
259 
getKey() const260     const std::string& GlossaryEntry::getKey() const
261     {
262       return this->key;
263     }
264 
getNames() const265     const std::vector<std::string>& GlossaryEntry::getNames() const
266     {
267       return this->names;
268     }
269 
getUnit() const270     const std::string& GlossaryEntry::getUnit() const
271     {
272       return this->unit;
273     }
274 
getType() const275     const std::string& GlossaryEntry::getType() const
276     {
277       return this->type;
278     }
279 
getShortDescription() const280     const std::string& GlossaryEntry::getShortDescription() const
281     {
282       return this->short_description;
283     }
284 
getDescription() const285     const std::vector<std::string>& GlossaryEntry::getDescription() const
286     {
287       return this->description;
288     }
289 
getNotes() const290     const std::vector<std::string>& GlossaryEntry::getNotes() const
291     {
292       return this->notes;
293     }
294 
operator const std::string&() const295     GlossaryEntry::operator const std::string& () const
296     {
297       return this->key;
298     } // end of operator std::string
299 
300     GlossaryEntry::~GlossaryEntry() = default;
301 
operator <(const GlossaryEntry & e1,const GlossaryEntry & e2)302     bool operator < (const GlossaryEntry& e1,
303 		     const GlossaryEntry& e2)
304     {
305       return e1.key < e2.key;
306     }
307 
operator !=(const std::string & e1,const GlossaryEntry & e2)308     bool operator != (const std::string& e1,
309 		      const GlossaryEntry& e2)
310     {
311       return e1 != e2.key;
312     }
313 
operator !=(const GlossaryEntry & e1,const std::string & e2)314     bool operator != (const GlossaryEntry& e1,
315 		      const std::string& e2)
316     {
317       return e1.key != e2;
318     }
319 
320     TFELGLOSSARY_VISIBILITY_EXPORT bool
operator ==(const std::string & e1,const GlossaryEntry & e2)321     operator == (const std::string& e1,
322 		 const GlossaryEntry& e2)
323     {
324       return e1 == e2.key;
325     }
326 
327     TFELGLOSSARY_VISIBILITY_EXPORT bool
operator ==(const GlossaryEntry & e1,const std::string & e2)328     operator == (const GlossaryEntry& e1,
329 		 const std::string& e2)
330     {
331       return e1.key == e2;
332     }
333 
334   } // end of namespace glossary
335 
336 } // end of namespace tfel
337