NCBI C++ ToolKit
registry.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: registry.cpp 44845 2020-03-25 22:06:56Z rudnev $
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  * Authors: Mike DiCuccio
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 #include <algorithm>
36 #include <iterator>
37 
39 
42 
43 const string CGuiRegistry::kDecimalDot = ".";
44 
46 : m_WritableSite(ePriority_Local)
47 {
48 }
49 
50 
52 {
53 }
54 
56 {
57  AddSite(istr, ePriority_Local);
58 }
59 
60 
62 {
64  m_Registries[ePriority_Local]->FromConfigFile(istr);
65 }
66 
67 
69 {
71  if (it != m_Registries.end()) {
72  it->second->ToConfigFile(ostr);
73  }
74 }
75 
76 
78 {
80 }
81 
82 
84 {
86  try {
87  site.Reset(new CRegistryFile(istr));
88  m_Registries[priority] = site;
89  }
90  catch (CException& e) {
91  LOG_POST(Error << "Error reading settings: " << e.GetMsg());
92  }
93  catch (std::exception& e) {
94  LOG_POST(Error << "Unknown error reading local settings; "
95  "the settings will be ignored " << e.what());
96  }
97  return site;
98 }
99 
100 
101 CRef<CRegistryFile> CGuiRegistry::AddSite(const objects::CUser_object& reg, int priority)
102 {
104  site.Reset(new CRegistryFile(reg));
105  m_Registries[priority] = site;
106  return site;
107 }
108 
109 
111 {
112  // this site might not exist. But if it doesn't, it will
113  // be created the first time someone writes to it.
114  m_WritableSite = priority;
115 }
116 
117 
119 {
120  return m_WritableSite;
121 }
122 
123 
124 void CGuiRegistry::Write(CNcbiOstream& ostr, int priority) const
125 {
126  CRef<CRegistryFile> reg = x_GetRegistryRef(priority);
127  if (reg) {
128  reg->Write(ostr);
129  }
130 }
131 
132 
133 int CGuiRegistry::GetInt(const string& key, int default_val) const
134 {
135  return CRegistryReadView::GetInt(GetField(key), default_val);
136 }
137 
138 double CGuiRegistry::GetReal(const string& key, double default_val) const
139 {
140  return CRegistryReadView::GetReal(GetField(key), default_val);
141 }
142 
143 bool CGuiRegistry::GetBool(const string& key, bool default_val) const
144 {
145  return CRegistryReadView::GetBool(GetField(key), default_val);
146 }
147 
148 string CGuiRegistry::GetString(const string& key, const string& default_val) const
149 {
150  return CRegistryReadView::GetString(GetField(key), default_val);
151 }
152 
153 void CGuiRegistry::GetIntVec(const string& key, vector<int>& val) const
154 {
156 }
157 
158 void CGuiRegistry::GetRealVec(const string& key, vector<double>& val) const
159 {
161 }
162 
163 void CGuiRegistry::GetStringVec(const string& key, vector<string>& val) const
164 {
166 }
167 
168 void CGuiRegistry::GetStringList(const string& key, list<string>& val) const
169 {
170  vector<string> v;
172 
173  val.clear();
174  std::copy(v.begin(), v.end(), std::back_inserter(val));
175 }
176 
177 
178 void CGuiRegistry::Set(const string& key, int val)
179 {
180  Set(GetWritableSite(), key, val);
181 }
182 
183 
184 void CGuiRegistry::Set(const string& key, double val)
185 {
186  Set(GetWritableSite(), key, val);
187 }
188 
189 
190 void CGuiRegistry::Set(const string& key, bool val)
191 {
192  Set(GetWritableSite(), key, val);
193 }
194 
195 
196 void CGuiRegistry::Set(const string& key, const string& val)
197 {
198  Set(GetWritableSite(), key, val);
199 }
200 
201 
202 void CGuiRegistry::Set(const string& key, const char* val)
203 {
204  Set(key, string(val));
205 }
206 
207 
208 void CGuiRegistry::Set(const string& key, const vector<int>& val)
209 {
210  Set(GetWritableSite(), key, val);
211 }
212 
213 
214 void CGuiRegistry::Set(const string& key, const vector<double>& val)
215 {
216  Set(GetWritableSite(), key, val);
217 }
218 
219 
220 void CGuiRegistry::Set(const string& key, const vector<string>& val)
221 {
222  Set(GetWritableSite(), key, val);
223 }
224 
225 
226 void CGuiRegistry::Set(const string& key, const list<string>& val)
227 {
228  Set(GetWritableSite(), key, val);
229 }
230 
231 
232 void CGuiRegistry::Set(int site, const string& key, int val)
233 {
234  CRegistryFile& write_site = x_SetRegistry(site);
235  CRef<CUser_field> field = write_site.SetFieldToValue(key);
236  field->SetData().SetInt(val);
237 }
238 
239 
240 void CGuiRegistry::Set(int site, const string& key, double val)
241 {
242  CRegistryFile& write_site = x_SetRegistry(site);
243  CRef<CUser_field> field = write_site.SetFieldToValue(key);
244  field->SetData().SetReal(val);
245 }
246 
247 
248 void CGuiRegistry::Set(int site, const string& key, bool val)
249 {
250  CRegistryFile& write_site = x_SetRegistry(site);
251  CRef<CUser_field> field = write_site.SetFieldToValue(key);
252  field->SetData().SetBool(val);
253 }
254 
255 
256 void CGuiRegistry::Set(int site, const string& key, const string& val)
257 {
258  CRegistryFile& write_site = x_SetRegistry(site);
259  CRef<CUser_field> field = write_site.SetFieldToValue(key);
260  field->SetData().SetStr(val);
261 }
262 
263 
264 void CGuiRegistry::Set(int site, const string& key, const char* val)
265 {
266  Set(site, key, string(val));
267 }
268 
269 
270 void CGuiRegistry::Set(int site, const string& key, const vector<int>& val)
271 {
272  CRegistryFile& write_site = x_SetRegistry(site);
273  CRef<CUser_field> field = write_site.SetFieldToValue(key);
274  field->SetData().SetInts() = val;
275 }
276 
277 
278 void CGuiRegistry::Set(int site, const string& key, const vector<double>& val)
279 {
280  CRegistryFile& write_site = x_SetRegistry(site);
281  CRef<CUser_field> field = write_site.SetFieldToValue(key);
282  field->SetData().SetReals() = val;
283 }
284 
285 
286 void CGuiRegistry::Set(int site, const string& key, const vector<string>& val)
287 {
288  CRegistryFile& write_site = x_SetRegistry(site);
289  CRef<CUser_field> field = write_site.SetFieldToValue(key);
290 
291  CUser_field_Base::C_Data::TStrs& dest = field->SetData().SetStrs();
292  dest.clear();
293  ITERATE( vector<string>, v, val) {
294  dest.push_back(*v);
295  }
296 }
297 
298 
299 void CGuiRegistry::Set(int site, const string& key, const list<string>& val)
300 {
301  vector<string> v;
302  ITERATE(list<string>, it, val) {
303  v.push_back(*it);
304  }
305  Set(site, key, v);
306 }
307 
309 {
310  return SetField(GetWritableSite(), key);
311 }
312 
313 
315 {
316  CRegistryFile& write_site = x_SetRegistry(site);
317  return write_site.SetField(key);
318 }
319 
320 
322 {
324 }
325 
326 
328 {
329  CRegistryFile& write_site = x_SetRegistry(site);
330  return write_site.SetFieldToValue(key);
331 }
332 
333 bool CGuiRegistry::DeleteField(const string& key)
334 {
335  return DeleteField(GetWritableSite(), key);
336 }
337 
338 
339 bool CGuiRegistry::DeleteField(int site, const string& key)
340 {
341  CRegistryFile& write_site = x_SetRegistry(site);
342  return write_site.DeleteField(key);
343 }
344 
345 
347 {
348  list< CConstRef<CUser_field> > fields;
350 
351  ITERATE (TRegistries, iter, m_Registries) {
352  CConstRef<CUser_field> field = iter->second->GetField(key);
353  if (field)
354  fields.push_back(field);
355 
356  field = iter->second->GetField(key + "-meta");
357  if (field)
358  meta = field;
359  }
360 
361  return CRegistryReadView::ResolveField(fields, meta);
362 }
363 
364 
365 bool CGuiRegistry::HasField(const string& key) const
366 {
367  ITERATE (TRegistries, iter, m_Registries) {
368  if (iter->second->HasField(key)) {
369  return true;
370  }
371  }
372  return false;
373 }
374 
375 
376 /// return the registry at a particular priority.
377 /// Do not create if it does not exist.
379 {
381  if (it != m_Registries.end()) {
382  return it->second;
383  }
384  return CRef<CRegistryFile>();
385 }
386 
387 
388 /// return a registry at a particular priority, creating it if need be.
390 {
391  CRef<CRegistryFile> reg_ref = m_Registries[priority];
392  if (! reg_ref) {
393  reg_ref.Reset(new CRegistryFile);
394  m_Registries[priority] = reg_ref;
395  }
396  return *reg_ref;
397 }
398 
399 
401 {
403  if (!s_Registry)
404  s_Registry.Reset(new CGuiRegistry());
405  return *s_Registry;
406 }
407 
408 // get an exact snapshot of this registry
409 // it will be writable even if this registry is read-only
411  CRef<CGuiRegistry> new_reg(new CGuiRegistry);
412  for(auto sitereg: m_Registries) {
413  CRef<CRegistryFile> new_reg_file(new CRegistryFile);
414  new_reg_file->SetRegistry(sitereg.second->GetRegistry());
415  new_reg->m_Registries.emplace(sitereg.first, new_reg_file);
416  }
417  return new_reg;
418 }
419 
421  const string& section) const
422 {
424  view += iter->second->GetReadView(section);
425 }
426 
427 
428 CRegistryReadView CGuiRegistry::GetReadView(const string& section) const
429 {
430  CRegistryReadView view;
431  x_AppendToView(view, section);
432  return view;
433 }
434 
435 
437 CGuiRegistry::GetReadView(const string& section,
438  const vector<string>& subsections) const
439 {
440  CRegistryReadView view;
441 
442  // the section for a view made with subsections ends with
443  // the most significant subsection.
444  string str(section);
445  if ( !section.empty() ) {
446  str += kDecimalDot;
447  }
448 
449  vector<string>::const_reverse_iterator riter(subsections.end());
450  vector<string>::const_reverse_iterator rend (subsections.begin());
451  for ( ; riter != rend; ++riter) {
452  string key(*riter);
453  if ( !key.empty()) {
454  x_AppendToView(view, str + key);
455  }
456  }
457 
458  return view;
459 }
460 
461 
463 {
464  if(m_isReadOnly) {
465  NCBI_THROW(CException, eUnknown, "Attempt to write to read-only registry!");
466  }
467  return x_SetRegistry(GetWritableSite()).GetWriteView(section);
468 }
469 
470 CRegistryWriteView CGuiRegistry::GetWriteView(const string& section, int writeSite)
471 {
472  if(m_isReadOnly) {
473  NCBI_THROW(CException, eUnknown, "Attempt to write to read-only registry!");
474  }
475  return x_SetRegistry(writeSite).GetWriteView(section);
476 }
477 
479  const vector<string>& subsections)
480 {
481  if(m_isReadOnly) {
482  NCBI_THROW(CException, eUnknown, "Attempt to write to read-only registry!");
483  }
484  // the section for a view made with subsections ends with
485  // the most significant subsection.
486  string str(section);
487  if ( !section.empty() ) {
488  str += kDecimalDot;
489  }
490 
491  if ( ! subsections.empty()) {
492  str += subsections.back();
493  }
494 
496 }
497 
498 /////////////////////////////////////////////////////////////////////////////
499 ///
500 /// CGuiRegistryUtil
501 
502 
503 /// create a key from a section and a subkey
504 string CGuiRegistryUtil::MakeKey(const string& section, const string& key,
505  const string& delim)
506 {
507  return section + delim + key;
508 }
509 
510 
static string MakeKey(const string &section, const string &key, const string &delim=CGuiRegistry::kDecimalDot)
create a key from a section and a subkey
Definition: registry.cpp:504
CRef< CRegistryFile > x_GetRegistryRef(int) const
return the registry at a particular priority.
Definition: registry.cpp:378
void ToConfigFile(CNcbiOstream &ostr) const
Write the local policy to a specified stream, in INI format.
Definition: registry.cpp:68
CRef< objects::CUser_field > SetFieldToValue(const string &key)
same as SetField, but complain loudly if the field has subfields in it already.
Definition: registry.cpp:321
CRegistryWriteView GetWriteView(const string &section)
get a read-write view at a particular level.
Definition: registry.cpp:462
TRegistries m_Registries
Definition: registry.hpp:256
bool HasField(const string &key) const
Does a field with this section and key exist in this view?
Definition: registry.cpp:365
int GetWritableSite()
Get the priority of the site that Set methods modify.
Definition: registry.cpp:118
void Write(CNcbiOstream &ostr, int priority=ePriority_Local) const
Write the local policy to a specified stream.
Definition: registry.cpp:124
void SetWritableSite(int priority=ePriority_Local)
Set which site Set methods modify.
Definition: registry.cpp:110
CRef< CGuiRegistry > Clone()
Definition: registry.cpp:410
void GetIntVec(const string &key, vector< int > &val) const
Definition: registry.cpp:153
void SetGlobal(CNcbiIstream &istr)
Set the "global" repository.
Definition: registry.cpp:77
static const string kDecimalDot
Definition: registry.hpp:45
void GetRealVec(const string &key, vector< double > &val) const
Definition: registry.cpp:158
void GetStringVec(const string &key, vector< string > &val) const
Definition: registry.cpp:163
void FromConfigFile(CNcbiIstream &istr)
Initialize the local repository from an ini-format config file.
Definition: registry.cpp:61
CRegistryFile & x_SetRegistry(int)
return a registry at a particular priority, creating it if need be.
Definition: registry.cpp:389
double GetReal(const string &key, double default_val=0) const
Definition: registry.cpp:138
void GetStringList(const string &key, list< string > &val) const
Definition: registry.cpp:168
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
int m_WritableSite
The priority of the site that writes go to.
Definition: registry.hpp:259
bool m_isReadOnly
Definition: registry.hpp:261
void SetLocal(CNcbiIstream &istr)
establish our "local" repository.
Definition: registry.cpp:55
CConstRef< objects::CUser_field > GetField(const string &key) const
retrieve the best user field object for our key, taking into account our multiple stores.
Definition: registry.cpp:346
@ ePriority_Global
Definition: registry.hpp:51
void Set(const string &key, int val)
set specific values
Definition: registry.cpp:178
CRegistryReadView GetReadView(const string &section) const
get a read-only view at a particular level.
Definition: registry.cpp:428
string GetString(const string &key, const string &default_val=kEmptyStr) const
Definition: registry.cpp:148
bool DeleteField(const string &key)
delete the specified field (and any of its subfields).
Definition: registry.cpp:333
CRef< CRegistryFile > AddSite(CNcbiIstream &istr, int priority)
Add a site-specific repository.
Definition: registry.cpp:83
CRef< objects::CUser_field > SetField(const string &key)
retrieve the best user field object for our key, taking into account our multiple stores
Definition: registry.cpp:308
void x_AppendToView(CRegistryReadView &view, const string &section) const
Add a section to an existing view at a lower priority than any previously existing sections in that v...
Definition: registry.cpp:420
int GetInt(const string &key, int default_val=0) const
retrieve values by section and key.
Definition: registry.cpp:133
bool GetBool(const string &key, bool default_val=false) const
Definition: registry.cpp:143
CRef< objects::CUser_field > SetFieldToValue(const string &key)
Definition: reg_file.cpp:537
objects::CUser_object & SetRegistry()
Definition: reg_file.cpp:510
CRef< objects::CUser_field > SetField(const string &key)
Definition: reg_file.cpp:487
void Write(CNcbiOstream &istr) const
Definition: reg_file.cpp:457
bool DeleteField(const string &key)
Definition: reg_file.cpp:493
CRegistryWriteView GetWriteView(const string &section)
Definition: reg_file.cpp:70
class CRegistryReadView provides a nested hierarchical view at a particular key.
Definition: reg_view.hpp:58
int GetInt(const string &key, int default_val=0) const
access a named key at this level, with no recursion
Definition: reg_view.cpp:230
double GetReal(const string &key, double default_val=0) const
Definition: reg_view.cpp:235
bool GetBool(const string &key, bool default_val=false) const
Definition: reg_view.cpp:241
void GetIntVec(const string &key, vector< int > &val) const
Definition: reg_view.cpp:252
void GetRealVec(const string &key, vector< double > &val) const
Definition: reg_view.cpp:257
string GetString(const string &key, const string &default_val=kEmptyStr) const
Definition: reg_view.cpp:246
void GetStringVec(const string &key, vector< string > &val) const
Definition: reg_view.cpp:263
CRegistryReadView GetReadView(const string &section) const
Definition: reg_view.cpp:74
static CConstRef< objects::CUser_field > ResolveField(list< CConstRef< objects::CUser_field > > &fields, const objects::CUser_field *meta)
Definition: reg_view.cpp:368
const CUser_field & GetField(const string &str, const string &delim=".", NStr::ECase use_case=NStr::eCase) const
Access a named field in this user field.
Definition: User_field.cpp:211
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
virtual const char * what(void) const noexcept
Standard report (includes full backlog).
Definition: ncbiexpt.cpp:342
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
void SetData(TData &value)
Assign a value to Data data member.
vector< CStringUTF8 > TStrs
const struct ncbi::grid::netcache::search::fields::KEY key
ESERV_Site site
void copy(Njn::Matrix< S > *matrix_, const Njn::Matrix< T > &matrix0_)
Definition: njn_matrix.hpp:613
USING_SCOPE(objects)
static CNcbiRegistry * s_Registry
Definition: server_core.cpp:75
static const char * str(char *buf, int n)
Definition: stats.c:84
Modified on Wed Dec 06 07:12:47 2023 by modify_doxy.py rev. 669887