foobar2000 SDK  2015-08-03
config_object_impl.h
Go to the documentation of this file.
1 #ifndef _CONFIG_OBJECT_IMPL_H_
2 #define _CONFIG_OBJECT_IMPL_H_
3 
4 //template function bodies from config_object class
5 
6 template<class T>
8  if (get_data_raw(&p_out,sizeof(T)) != sizeof(T)) throw exception_io_data_truncation();
9 }
10 
11 template<class T>
13  return set_data_raw(&p_in,sizeof(T));
14 }
15 
16 template<class T>
17 void config_object::g_get_data_struct_t(const GUID & p_guid,T & p_out) {
19  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
20  return ptr->get_data_struct_t<T>(p_out);
21 }
22 
23 template<class T>
24 void config_object::g_set_data_struct_t(const GUID & p_guid,const T & p_in) {
26  if (!g_find(ptr,p_guid)) throw exception_service_not_found();
27  return ptr->set_data_struct_t<T>(p_in);
28 }
29 
30 
31 class config_object_impl : public config_object, private cfg_var
32 {
33 public:
34  GUID get_guid() const {return cfg_var::get_guid();}
35  void get_data(stream_writer * p_stream,abort_callback & p_abort) const ;
36  void set_data(stream_reader * p_stream,abort_callback & p_abort,bool p_notify);
37 
38  config_object_impl(const GUID & p_guid,const void * p_data,t_size p_bytes);
39 private:
40 
41  //cfg_var methods
42  void get_data_raw(stream_writer * p_stream,abort_callback & p_abort) {get_data(p_stream,p_abort);}
43  void set_data_raw(stream_reader * p_stream,t_size p_sizehint,abort_callback & p_abort) {set_data(p_stream,p_abort,false);}
44 
47 };
48 
50 
51 template<t_size p_size>
53 public:
54  config_object_fixed_const_impl_t(const GUID & p_guid, const void * p_data) : m_guid(p_guid) {memcpy(m_data,p_data,p_size);}
55  GUID get_guid() const {return m_guid;}
56 
57  void get_data(stream_writer * p_stream, abort_callback & p_abort) const { p_stream->write_object(m_data,p_size,p_abort); }
58  void set_data(stream_reader * p_stream, abort_callback & p_abort, bool p_notify) { PFC_ASSERT(!"Should not get here."); }
59 
60 private:
61  t_uint8 m_data[p_size];
62  const GUID m_guid;
63 };
64 
65 template<t_size p_size>
67 public:
68  GUID get_guid() const {return cfg_var::get_guid();}
69 
70  void get_data(stream_writer * p_stream,abort_callback & p_abort) const {
71  insync(m_sync);
72  p_stream->write_object(m_data,p_size,p_abort);
73  }
74 
75  void set_data(stream_reader * p_stream,abort_callback & p_abort,bool p_notify) {
77 
78  {
79  t_uint8 temp[p_size];
80  p_stream->read_object(temp,p_size,p_abort);
81  insync(m_sync);
82  memcpy(m_data,temp,p_size);
83  }
84 
86  }
87 
88  config_object_fixed_impl_t (const GUID & p_guid,const void * p_data)
89  : cfg_var(p_guid)
90  {
91  memcpy(m_data,p_data,p_size);
92  }
93 
94 private:
95  //cfg_var methods
96  void get_data_raw(stream_writer * p_stream,abort_callback & p_abort) {get_data(p_stream,p_abort);}
97  void set_data_raw(stream_reader * p_stream,t_size p_sizehint,abort_callback & p_abort) {set_data(p_stream,p_abort,false);}
98 
100  t_uint8 m_data[p_size];
101 
102 };
103 
104 template<t_size p_size, bool isConst> class _config_object_fixed_impl_switch;
105 template<t_size p_size> class _config_object_fixed_impl_switch<p_size,false> { public: typedef config_object_fixed_impl_t<p_size> type; };
106 template<t_size p_size> class _config_object_fixed_impl_switch<p_size,true> { public: typedef config_object_fixed_const_impl_t<p_size> type; };
107 
108 template<t_size p_size, bool isConst = false>
109 class config_object_fixed_factory_t : public service_factory_single_transparent_t< typename _config_object_fixed_impl_switch<p_size,isConst>::type >
110 {
111 public:
112  config_object_fixed_factory_t(const GUID & p_guid,const void * p_initval)
113  :
115  (p_guid,p_initval)
116  {}
117 };
118 
119 
121 {
122 public:
123  config_object_string_factory(const GUID & p_guid,const char * p_string,t_size p_string_length = ~0)
124  : config_object_factory(p_guid,p_string,pfc::strlen_max(p_string,~0)) {}
125 
126 };
127 
128 template<bool isConst = false>
130 public:
131  config_object_bool_factory_t(const GUID & p_guid,bool p_initval)
132  : config_object_fixed_factory_t<1,isConst>(p_guid,&p_initval) {}
133 };
135 
136 template<class T,bool isConst = false>
138 {
139 private:
140  struct t_initval
141  {
143  t_initval(T p_initval) : m_initval(p_initval) {byte_order::order_native_to_le_t(m_initval);}
144  T * get_ptr() {return &m_initval;}
145  };
146 public:
147  config_object_int_factory_t(const GUID & p_guid,T p_initval)
148  : config_object_fixed_factory_t<sizeof(T)>(p_guid,t_initval(p_initval).get_ptr() )
149  {}
150 };
151 
153 
154 
155 
157 {
158 public:
160  GUID get_watched_object(t_size p_index) {return m_guid;}
161  void on_watched_object_changed(const service_ptr_t<config_object> & p_object) {m_func(p_object);}
162 
163  typedef void (*t_func)(const service_ptr_t<config_object> &);
164 
165  config_object_notify_impl_simple(const GUID & p_guid,t_func p_func) : m_guid(p_guid), m_func(p_func) {}
166 private:
168  t_func m_func;
169 };
170 
172 
173 #endif //_CONFIG_OBJECT_IMPL_H_
critical_section m_sync
config_object_int_factory_t(const GUID &p_guid, T p_initval)
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)
uint8_t t_uint8
Definition: int_types.h:9
config_object_fixed_const_impl_t(const GUID &p_guid, const void *p_data)
Definition: pfc.h:71
void get_data(stream_writer *p_stream, abort_callback &p_abort) const
void get_data_raw(stream_writer *p_stream, abort_callback &p_abort)
Retrieves state of the variable. Called only from main thread, when writing configuration file...
config_object_fixed_factory_t(const GUID &p_guid, const void *p_initval)
void order_native_to_le_t(T &param)
void set_data(stream_reader *p_stream, abort_callback &p_abort, bool p_notify)
void set_data_raw(stream_reader *p_stream, t_size p_sizehint, abort_callback &p_abort)
Sets state of the variable. Called only from main thread, when reading configuration file...
virtual void get_data(stream_writer *p_stream, abort_callback &p_abort) const =0
GUID get_watched_object(t_size p_index)
config_object_fixed_impl_t< p_size > type
config_object_fixed_impl_t(const GUID &p_guid, const void *p_data)
config_object_int_factory_t< t_int32 > config_object_int32_factory
void get_data_raw(stream_writer *p_stream, abort_callback &p_abort)
Retrieves state of the variable. Called only from main thread, when writing configuration file...
config_object_fixed_const_impl_t< p_size > type
GUID get_guid() const
Definition: cfg_var.h:59
t_size get_data_raw(void *p_out, t_size p_bytes)
t_size strlen_max(const char *ptr, t_size max)
Definition: string_base.h:91
config_object_bool_factory_t config_object_bool_factory
size_t t_size
Definition: int_types.h:48
static void g_get_data_struct_t(const GUID &p_guid, T &p_out)
void on_watched_object_changed(const service_ptr_t< config_object > &p_object)
static void g_set_data_struct_t(const GUID &p_guid, const T &p_in)
service_factory_single_transparent_t< config_object_impl > config_object_factory
config_object_string_factory(const GUID &p_guid, const char *p_string, t_size p_string_length=~0)
void set_data(stream_reader *p_stream, abort_callback &p_abort, bool p_notify)
pfc::array_t< t_uint8 > m_data
GUID get_guid() const
void set_data_raw(stream_reader *p_stream, t_size p_sizehint, abort_callback &p_abort)
Sets state of the variable. Called only from main thread, when reading configuration file...
void get_data(stream_writer *p_stream, abort_callback &p_abort) const
service_factory_single_transparent_t< config_object_notify_impl_simple > config_object_notify_simple_factory
virtual void set_data(stream_reader *p_stream, abort_callback &p_abort, bool p_sendnotify=true)=0
Base class for configuration variable classes; provides self-registration mechaisms and methods to se...
Definition: cfg_var.h:54
config_object_notify_impl_simple(const GUID &p_guid, t_func p_func)
void set_data_struct_t(const T &p_in)
void get_data_struct_t(T &p_out)
config_object_bool_factory_t(const GUID &p_guid, bool p_initval)
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)