1 /* $Id: User_field.hpp 607095 2020-04-30 12:31:05Z grichenk $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: .......
27 *
28 * File Description:
29 * .......
30 *
31 * Remark:
32 * This code was originally generated by application DATATOOL
33 * using specifications from the data definition file
34 * 'general.asn'.
35 */
36
37 #ifndef OBJECTS_GENERAL_USER_FIELD_HPP
38 #define OBJECTS_GENERAL_USER_FIELD_HPP
39
40
41 // generated includes
42 #include <objects/general/User_field_.hpp>
43
44 // generated classes
45
46 BEGIN_NCBI_SCOPE
47
48 BEGIN_objects_SCOPE // namespace ncbi::objects::
49
50 class NCBI_GENERAL_EXPORT CUser_field : public CUser_field_Base
51 {
52 typedef CUser_field_Base Tparent;
53 public:
54 // constructor
55 CUser_field(void);
56 // destructor
57 ~CUser_field(void);
58
59 enum EParseField {
60 eParse_String, ///< Add string even if all numbers
61 eParse_Number ///< Parse a real or integer number, otherwise string
62 };
63
64 /// set value
65 CUser_field& SetInt(int value);
66 CUser_field& SetInt8(Int8 value);
67 CUser_field& SetGi(TGi gi);
68 CUser_field& SetDouble(double value);
69 CUser_field& SetBool(bool value);
70 CUser_field& SetString(const char* value);
71 CUser_field& SetString(const string& value);
72
73 /// set a data field to a given value
74 /// Int8 and TGi values can be stored into 'real' or 'str' fields if
75 /// the value doesn't fit into 'int' field
76 CUser_field& SetValue(int value);
77 CUser_field& SetValue(Int8 value);
78 #ifdef NCBI_STRICT_GI
79 CUser_field& SetValue(TGi value);
80 #endif
81 CUser_field& SetValue(double value);
82 CUser_field& SetValue(bool value);
83 CUser_field& SetValue(const char* value);
84 CUser_field& SetValue(const string& value);
85 CUser_field& SetValue(const char* value, EParseField parse);
86 CUser_field& SetValue(const string& value, EParseField parse);
87 CUser_field& SetValue(const vector<int>& value);
88 CUser_field& SetValue(const vector<double>& value);
89 CUser_field& SetValue(const vector<string>& value);
90 CUser_field& SetValue(CUser_object& value);
91 CUser_field& SetValue(const vector< CRef<CUser_object> >& value);
92 CUser_field& SetValue(const vector< CRef<CUser_field> >& value);
93
94 /// get value
95 int GetInt(void) const;
96 Int8 GetInt8(void) const;
97 TGi GetGi(void) const;
98 bool GetBool(void) const;
99 double GetDouble(void) const;
100 const string& GetString(void) const;
101
102 template<class Type> Type GetValue(void) const;
103
104 /// add fields to the current user field
105 CUser_field& AddField(const string& label, int value);
106 CUser_field& AddField(const string& label, Int8 value);
107 #ifdef NCBI_STRICT_GI
108 CUser_field& AddField(const string& label, TGi value);
109 #endif
110 CUser_field& AddField(const string& label, double value);
111 CUser_field& AddField(const string& label, bool value);
112
113 CUser_field& AddField(const string& label, const char* value);
114 CUser_field& AddField(const string& label, const string& value);
115 CUser_field& AddField(const string& label, const char* value,
116 EParseField field);
117 CUser_field& AddField(const string& label, const string& value,
118 EParseField field);
119 CUser_field& AddField(const string& label, const vector<string>& value);
120
121 CUser_field& AddField(const string& label, const vector<int>& value);
122 CUser_field& AddField(const string& label, const vector<double>& value);
123
124 CUser_field& AddField(const string& label, CUser_object& value);
125 CUser_field& AddField(const string& label,
126 const vector< CRef<CUser_object> >& value);
127 CUser_field& AddField(const string& label,
128 const vector< CRef<CUser_field> >& value);
129
130 /// Access a named field in this user field. This will tokenize the
131 /// string 'str' on the delimiters; if the field doesn't exist, an
132 /// exception will be thrown.
133 const CUser_field& GetField(const string& str,
134 const string& delim = ".",
135 NStr::ECase use_case = NStr::eCase) const;
136
137 /// Return a field reference representing the tokenized key, or a
138 /// NULL reference if the key doesn't exist.
139 CConstRef<CUser_field> GetFieldRef(const string& str,
140 const string& delim = ".",
141 NStr::ECase use_case = NStr::eCase) const;
142
143 /// For functions that don't use delims, we instead use a chain of names.
144 struct NCBI_GENERAL_EXPORT SFieldNameChain {
145 /// Convenience func. This can be deprecated if a
146 /// NStr::Join function is created that takes
147 /// the type of TFieldNameChainUnderlying
148 void Join(ostream & out_name_strm, const string & delim = ".") const;
149
operator +=CUser_field::SFieldNameChain150 void operator += (const CTempStringEx & str) {
151 m_FieldNameChain.push_back(str);
152 }
153
operator <CUser_field::SFieldNameChain154 bool operator < (const SFieldNameChain & rhs) const {
155 return m_FieldNameChain < rhs.m_FieldNameChain;
156 }
157
158 typedef vector<CTempStringEx> TFieldNameChainUnderlying;
159 TFieldNameChainUnderlying m_FieldNameChain;
160 };
161
162 /// Maps field names like the input for GetFieldRef to the user-field.
163 typedef map<SFieldNameChain, CConstRef<CUser_field> > TMapFieldNameToRef;
164
165 /// Flags that affect behavior of TMapFieldNameToRef functions.
166 enum EFieldMapFlags {
167 fFieldMapFlags_ExcludeThis = (1<<0) ///< = 0x1 (excludes this CUser_field's name and mapping to self from results)
168 };
169 typedef int TFieldMapFlags; ///< holds bitwise OR of "EFieldMapFlags"
170
171 /// Recursively get the map of field names like the input
172 /// for GetFieldRef to the user-field.
173 ///
174 /// @param out_mapFieldNameToRef
175 /// The results are put into here, but it becomes INVALID if this
176 /// CUser_field or any of its descendents change their label.
177 /// It will NOT be cleared
178 /// before more data is added, and in that case would overwrite any
179 /// item which has the same key.
180 /// @param fFieldMapFlags
181 /// Flags that affect behavior of function.
182 /// @param parent_name
183 /// Outside users probably don't need this. It's really for
184 /// internal recursive calls when building the map.
185 void GetFieldsMap(
186 CUser_field::TMapFieldNameToRef & out_mapFieldNameToRef,
187 TFieldMapFlags fFieldMapFlags = 0,
188 const SFieldNameChain & parent_name = SFieldNameChain() ) const;
189
190 /// Access a named field in this user field. This will tokenize the
191 /// string 'str' on the delimiters and recursively add fields where needed
192 CUser_field& SetField(const string& str,
193 const string& delim = ".",
194 NStr::ECase use_case = NStr::eCase);
195
196 /// Return a field reference representing the tokenized key, or a
197 /// NULL reference if the key cannot be created for some reason.
198 CRef<CUser_field> SetFieldRef(const string& str,
199 const string& delim = ".",
200 NStr::ECase use_case = NStr::eCase);
201
202 /// Verify that a named field exists
203 bool HasField(const string& str,
204 const string& delim = ".",
205 NStr::ECase use_case = NStr::eCase) const;
206
207 /// Delete the named field.
208 /// return true if successful. false if field doesn't exist.
209 bool DeleteField(const string& str,
210 const string& delim = ".",
211 NStr::ECase use_case = NStr::eCase);
212
213
214 private:
215 // Prohibit copy constructor and assignment operator
216 CUser_field(const CUser_field& value);
217 CUser_field& operator=(const CUser_field& value);
218 };
219
220
221
222 /////////////////// CUser_field inline methods
223
224 // constructor
225 inline
CUser_field(void)226 CUser_field::CUser_field(void)
227 {
228 }
229
230
231 inline
SetString(const string & value)232 CUser_field& CUser_field::SetString(const string& value)
233 {
234 SetData().SetStr(value);
235 return *this;
236 }
237
238
239 inline
SetInt(int value)240 CUser_field& CUser_field::SetInt(int value)
241 {
242 SetData().SetInt(value);
243 return *this;
244 }
245
246
247 inline
SetBool(bool value)248 CUser_field& CUser_field::SetBool(bool value)
249 {
250 SetData().SetBool(value);
251 return *this;
252 }
253
254
255 inline
SetDouble(double value)256 CUser_field& CUser_field::SetDouble(double value)
257 {
258 SetData().SetReal(value);
259 return *this;
260 }
261
262
263 inline
SetValue(const char * value)264 CUser_field& CUser_field::SetValue(const char* value)
265 {
266 return SetString(value);
267 }
268
269
270 inline
SetValue(const string & value)271 CUser_field& CUser_field::SetValue(const string& value)
272 {
273 return SetString(value);
274 }
275
276
277 inline
SetValue(int value)278 CUser_field& CUser_field::SetValue(int value)
279 {
280 return SetInt(value);
281 }
282
283
284 inline
SetValue(bool value)285 CUser_field& CUser_field::SetValue(bool value)
286 {
287 return SetBool(value);
288 }
289
290
291 inline
SetValue(double value)292 CUser_field& CUser_field::SetValue(double value)
293 {
294 return SetDouble(value);
295 }
296
297
298 inline
SetValue(Int8 value)299 CUser_field& CUser_field::SetValue(Int8 value)
300 {
301 return SetInt8(value);
302 }
303
304
305 inline
SetGi(TGi value)306 CUser_field& CUser_field::SetGi(TGi value)
307 {
308 return SetValue(GI_TO(TIntId, value));
309 }
310
311
312 #ifdef NCBI_STRICT_GI
313 inline
SetValue(TGi value)314 CUser_field& CUser_field::SetValue(TGi value)
315 {
316 return SetGi(value);
317 }
318 #endif
319
320
321 inline
GetInt(void) const322 int CUser_field::GetInt(void) const
323 {
324 return GetData().GetInt();
325 }
326
327
328 inline
GetDouble(void) const329 double CUser_field::GetDouble(void) const
330 {
331 return GetData().GetReal();
332 }
333
334
335 inline
GetBool(void) const336 bool CUser_field::GetBool(void) const
337 {
338 return GetData().GetBool();
339 }
340
341
342 inline
GetString(void) const343 const string& CUser_field::GetString(void) const
344 {
345 return GetData().GetStr();
346 }
347
348
349 template<>
350 inline
GetValue(void) const351 int CUser_field::GetValue<int>(void) const
352 {
353 return GetInt();
354 }
355
356
357 template<>
358 inline
GetValue(void) const359 Int8 CUser_field::GetValue<Int8>(void) const
360 {
361 return GetInt8();
362 }
363
364
365 template<>
366 inline
GetValue(void) const367 double CUser_field::GetValue<double>(void) const
368 {
369 return GetDouble();
370 }
371
372
373 template<>
374 inline
GetValue(void) const375 bool CUser_field::GetValue<bool>(void) const
376 {
377 return GetBool();
378 }
379
380
381 template<>
382 inline
GetValue(void) const383 string CUser_field::GetValue<string>(void) const
384 {
385 return GetString();
386 }
387
388
389 inline
GetGi(void) const390 TGi CUser_field::GetGi(void) const
391 {
392 return GI_FROM(TIntId, GetValue<TIntId>());
393 }
394
395
396 #ifdef NCBI_STRICT_GI
397 template<>
398 inline
GetValue(void) const399 TGi CUser_field::GetValue<TGi>(void) const
400 {
401 return GetGi();
402 }
403 #endif
404
405
406 /////////////////// end of CUser_field inline methods
407
408
409 END_objects_SCOPE // namespace ncbi::objects::
410
411 END_NCBI_SCOPE
412
413 #endif // OBJECTS_GENERAL_USER_FIELD_HPP
414