Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parameters_factory.cpp
Go to the documentation of this file.
1 
13 #include "parameters_factory.h"
14 using Bridge::vout;
15 
17 {
18  if (!s_instance) {
19  //
20  // acquire lock here
21  //
22  if (!s_instance) {
24  }
25  //
26  // release lock here
27  //
28  }
29 
30  return *s_instance;
31 }
32 
33 
35 {
36  static ParametersFactory instance_;
37 
38  s_instance = &instance_;
39 }
40 
41 
43 {
44  database_type::const_iterator q = m_map.find(realm);
45 
46  if (q == m_map.end()) {
47  vout.crucial("%s: key \'%s\' not found.\n", __func__, realm.c_str());
48  return 0;
49  }
50 
51  creator_callback_list cl = q->second;
52 
53  Parameters *param = new Parameters;
54  for (creator_callback_list::const_iterator p = cl.begin(); p != cl.end(); ++p) {
55  (*p)(*param);
56  }
57 
58  return param;
59 }
60 
61 
62 bool ParametersFactory::register_callback(const std::string& realm, const creator_callback& cb)
63 {
64  std::vector<std::string> v = string_tokenize(realm, '.');
65 
66  while (v.size() > 0)
67  {
68  std::string subrealm = string_concat(v, '.');
69 
70 #if 0
71  // for debug: use stdio because vout is not ready at this stage.
72  printf("%s: append to %s\n", __func__, subrealm.c_str());
73  printf("%s: realm \'%s\": callback append to %s\n", __func__, realm.c_str(), subrealm.c_str());
74 #endif
75 
76  m_map[subrealm].push_back(cb);
77 
78  v.pop_back();
79  }
80 
81  return true;
82 }
83 
84 
85 std::vector<std::string> ParametersFactory::string_tokenize(const std::string& src, const char delim)
86 {
87  std::vector<std::string> retv;
88 
89  size_t npos = src.length();
90  size_t p = 0;
91 
92  while (true)
93  {
94  size_t q = src.find(delim, p);
95 
96  if (q >= npos) { // not found
97  retv.push_back(src.substr(p)); // append rest
98  break;
99  }
100 
101  retv.push_back(src.substr(p, q - p));
102  p = q + 1; // skip delimiter.
103  }
104 
105  return retv;
106 }
107 
108 
109 std::string ParametersFactory::string_concat(const std::vector<std::string>& src, const char delim)
110 {
111  if (src.size() == 0) return std::string();
112 
113  std::string buf = src[0];
114  for (size_t i = 1; i < src.size(); ++i) {
115  buf += delim;
116  buf += src[i];
117  }
118 
119  return buf;
120 }
121 
122 
std::string string_concat(const std::vector< std::string > &src, const char delim= '.')
BridgeIO vout
Definition: bridgeIO.cpp:207
Parameters * create_parameters(const std::string &realm)
bool register_callback(const std::string &realm, const creator_callback &cb)
Class for parameters.
Definition: parameters.h:40
static ParametersFactory & Instance()
static ParametersFactory * s_instance
void crucial(const char *format,...)
Definition: bridgeIO.cpp:26
std::list< creator_callback > creator_callback_list
std::vector< std::string > string_tokenize(const std::string &src, const char delim= '.')
static void CreateInstance()