foobar2000 SDK  2015-08-03
config_object.cpp
Go to the documentation of this file.
1 #include "foobar2000.h"
2 
4 {
6  {
9  while(e.next(ptr))
10  ptr->on_changed(p_object);
11  }
12 }
13 
15 {
18  while(e.next(ptr))
19  {
20  if (ptr->get_guid() == p_guid)
21  {
22  p_out = ptr;
23  return true;
24  }
25  }
26  return false;
27 }
28 
30 {
32  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
33  ptr->get_data_string(p_out);
34 }
35 
36 void config_object::g_set_data_string(const GUID & p_guid,const char * p_data,t_size p_length)
37 {
39  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
40  ptr->set_data_string(p_data,p_length);
41 }
42 
44 {
45  t_int32 temp;
46  get_data_struct_t<t_int32>(temp);
48  p_out = temp;
49 }
50 
52 {
53  t_int32 temp = p_val;
55  set_data_struct_t<t_int32>(temp);
56 }
57 
59  try {
60  bool ret = p_default;
61  get_data_bool(ret);
62  return ret;
63  } catch(...) {return p_default;}
64 }
65 
67  try {
68  t_int32 ret = p_default;
69  get_data_int32(ret);
70  return ret;
71  } catch(...) {return p_default;}
72 }
73 
74 void config_object::g_get_data_int32(const GUID & p_guid,t_int32 & p_out) {
76  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
77  ptr->get_data_int32(p_out);
78 }
79 
80 void config_object::g_set_data_int32(const GUID & p_guid,t_int32 p_val) {
82  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
83  ptr->set_data_int32(p_val);
84 }
85 
86 bool config_object::g_get_data_bool_simple(const GUID & p_guid,bool p_default)
87 {
89  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
90  return ptr->get_data_bool_simple(p_default);
91 }
92 
94 {
96  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
97  return ptr->get_data_int32_simple(p_default);
98 }
99 
100 void config_object::get_data_bool(bool & p_out) {get_data_struct_t<bool>(p_out);}
101 void config_object::set_data_bool(bool p_val) {set_data_struct_t<bool>(p_val);}
102 
103 void config_object::g_get_data_bool(const GUID & p_guid,bool & p_out) {g_get_data_struct_t<bool>(p_guid,p_out);}
104 void config_object::g_set_data_bool(const GUID & p_guid,bool p_val) {g_set_data_struct_t<bool>(p_guid,p_val);}
105 
106 namespace {
107  class stream_writer_string : public stream_writer {
108  public:
109  void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
110  m_out.add_string((const char*)p_buffer,p_bytes);
111  }
112  stream_writer_string(pfc::string_base & p_out) : m_out(p_out) {m_out.reset();}
113  private:
114  pfc::string_base & m_out;
115  };
116 
117  class stream_writer_fixedbuffer : public stream_writer {
118  public:
119  void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
120  if (p_bytes > 0) {
121  if (p_bytes > m_bytes - m_bytes_read) throw pfc::exception_overflow();
122  memcpy((t_uint8*)m_out,p_buffer,p_bytes);
123  m_bytes_read += p_bytes;
124  }
125  }
126  stream_writer_fixedbuffer(void * p_out,t_size p_bytes,t_size & p_bytes_read) : m_out(p_out), m_bytes(p_bytes), m_bytes_read(p_bytes_read) {m_bytes_read = 0;}
127  private:
128  void * m_out;
129  t_size m_bytes;
130  t_size & m_bytes_read;
131  };
132 
133 
134 
135  class stream_writer_get_length : public stream_writer {
136  public:
137  void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
138  m_length += p_bytes;
139  }
140  stream_writer_get_length(t_size & p_length) : m_length(p_length) {m_length = 0;}
141  private:
142  t_size & m_length;
143  };
144 };
145 
147  t_size ret = 0;
148  stream_writer_fixedbuffer stream(p_out,p_bytes,ret);
149  abort_callback_dummy aborter;
150  get_data(&stream,aborter);
151  return ret;
152 }
153 
155  t_size ret = 0;
156  stream_writer_get_length stream(ret);
157  abort_callback_dummy aborter;
158  get_data(&stream,aborter);
159  return ret;
160 }
161 
162 void config_object::set_data_raw(const void * p_data,t_size p_bytes, bool p_notify) {
163  stream_reader_memblock_ref stream(p_data,p_bytes);
164  abort_callback_dummy aborter;
165  set_data(&stream,aborter,p_notify);
166 }
167 
168 void config_object::set_data_string(const char * p_data,t_size p_length) {
169  set_data_raw(p_data,pfc::strlen_max(p_data,p_length));
170 }
171 
173  stream_writer_string stream(p_out);
174  abort_callback_dummy aborter;
175  get_data(&stream,aborter);
176 }
177 
178 
179 //config_object_impl stuff
180 
181 
183  insync(m_sync);
184  p_stream->write_object(m_data.get_ptr(),m_data.get_size(),p_abort);
185 }
186 
187 void config_object_impl::set_data(stream_reader * p_stream,abort_callback & p_abort,bool p_notify) {
189 
190  {
191  insync(m_sync);
192  m_data.set_size(0);
193  enum {delta = 1024};
194  t_uint8 buffer[delta];
195  for(;;)
196  {
197  t_size delta_done = p_stream->read(buffer,delta,p_abort);
198 
199  if (delta_done > 0)
200  {
201  m_data.append_fromptr(buffer,delta_done);
202  }
203 
204  if (delta_done != delta) break;
205  }
206  }
207 
208  if (p_notify) config_object_notify_manager::g_on_changed(this);
209 }
210 
211 config_object_impl::config_object_impl(const GUID & p_guid,const void * p_data,t_size p_bytes) : cfg_var(p_guid)
212 {
213  m_data.set_data_fromptr((const t_uint8*)p_data,p_bytes);
214 }
void set_data_string(const char *p_data, t_size p_length)
t_size get_data_raw_length()
void ensure_main_thread()
Triggers a bug check if the calling thread is not the main app thread.
static void g_on_changed(const service_ptr_t< config_object > &p_object)
void set_data_int32(t_int32 p_val)
uint8_t t_uint8
Definition: int_types.h:9
void get_data_int32(t_int32 &p_out)
void get_data_string(pfc::string_base &p_out)
Definition: pfc.h:71
abort_callback_impl abort_callback_dummy
bool get_data_bool_simple(bool p_default)
void order_native_to_le_t(T &param)
t_int32 get_data_int32_simple(t_int32 p_default)
void get_data(stream_writer *p_stream, abort_callback &p_abort) const
static bool g_get_data_bool_simple(const GUID &p_guid, bool p_default)
static void g_set_data_int32(const GUID &p_guid, t_int32 p_val)
int32_t t_int32
Definition: int_types.h:4
t_size get_data_raw(void *p_out, t_size p_bytes)
void order_le_to_native_t(T &param)
t_size strlen_max(const char *ptr, t_size max)
Definition: string_base.h:91
static void g_set_data_string(const GUID &p_guid, const char *p_data, t_size p_length=~0)
size_t t_size
Definition: int_types.h:48
config_object_impl(const GUID &p_guid, const void *p_data, t_size p_bytes)
void write(const service_ptr_t< file > &p_file, abort_callback &p_abort, const char *p_string, bool is_utf8)
static void g_get_data_bool(const GUID &p_guid, bool &p_out)
pfc::array_t< t_uint8 > m_data
bool assert_main_thread()
Tests whether calling thread is main app thread, and shows diagnostic message in debugger output if i...
void set_data_fromptr(const t_source *p_buffer, t_size p_count)
Warning: buffer pointer must not point to buffer allocated by this array (fixme). ...
Definition: array.h:139
void set_data(stream_reader *p_stream, abort_callback &p_abort, bool p_notify)
void set_data_bool(bool p_val)
static void g_get_data_string(const GUID &p_guid, pfc::string_base &p_out)
static void g_get_data_int32(const GUID &p_guid, t_int32 &p_out)
Base class for configuration variable classes; provides self-registration mechaisms and methods to se...
Definition: cfg_var.h:54
bool next(service_ptr_t< t_query > &p_out)
Definition: service.h:587
static void g_set_data_bool(const GUID &p_guid, bool p_val)
static t_int32 g_get_data_int32_simple(const GUID &p_guid, t_int32 p_default)
void set_data_raw(const void *p_data, t_size p_bytes, bool p_sendnotify=true)
static bool g_find(service_ptr_t< config_object > &p_out, const GUID &p_guid)
void get_data_bool(bool &p_out)