5#include <cgv/utils/file.h>
6#include <cgv/utils/tokenizer.h>
7#include <cgv/base/register.h>
8#include <cgv/reflect/find_reflection_handler.h>
9#include <cgv/reflect/reflect_enum.h>
21 bool operator < (
const config_file_view& cfv)
const {
return pos < cfv.pos; }
27 std::string file_name;
29 std::vector<config_file_view> views;
30 unsigned nr_out_of_date;
32 gui_config_file_observer(
const std::string& fn,
const std::string& c) : file_name(fn), content(c)
39 std::sort(views.begin(), views.end());
41 void inc_out_of_date_count() {
47 for (
unsigned i=0; i<views.size(); ++i)
48 if (views[i].view == v) {
49 if (!views[i].out_of_date) {
50 views[i].out_of_date =
true;
51 inc_out_of_date_count();
55 void update_config_file()
57 std::string new_content;
60 for (
unsigned i=0; i<views.size(); ++i) {
61 std::string new_value((
const char*)views[i].view->get_user_data());
62 std::size_t new_len = new_value.length();
63 new_content += content.substr(off, views[i].pos - off);
64 new_content += new_value;
65 off = views[i].pos+views[i].len;
66 views[i].pos += delta_pos;
67 delta_pos += (int)new_len - (
int)(views[i].len);
68 views[i].len = (unsigned)new_len;
69 views[i].out_of_date =
false;
71 new_content += content.substr(off);
72 cgv::utils::file::write(file_name, new_content.c_str(), new_content.size(),
true);
73 content = new_content;
90 static std::string last_value;
92 return const_cast<char*
>(last_value.c_str());
96template<>
class config_view<std::string> :
public cgv::gui::view<std::string>
107 static std::string last_value;
109 for (
unsigned i=0; i<last_value.size(); ++i)
110 if (last_value[i] ==
'"' || last_value[i] ==
'\\') {
111 last_value.insert(i,1,
'\\');
114 last_value = std::string(
"\"")+last_value+
"\"";
115 return const_cast<char*
>(last_value.c_str());
130 static std::string last_value;
131 last_value =
get_value() ?
"true" :
"false";
132 return const_cast<char*
>(last_value.c_str());
148 static std::string last_value;
149 last_value = enum_traits->get_enum_name(
get_value());
150 return const_cast<char*
>(last_value.c_str());
157 std::vector<token> toks;
158 bite_all(
tokenizer(property_assignments).set_skip(
"'\"",
"'\"",
"\\\\").set_ws(
";"),toks);
160 for (
size_t i=0; i<toks.size(); ++i) {
161 std::vector<token> sides;
163 if (sides.size() != 2) {
164 std::cerr <<
"property assignment >" <<
to_string(toks[i]).c_str() <<
"< does not match pattern lhs=rhs" << std::endl;
171 void* member_ptr = 0;
174 cfv.len = (unsigned) sides[1].get_length();
175 cfv.pos = (unsigned) (off+sides[1].begin-&property_assignments[0]);
176 cfv.out_of_date =
false;
197 std::cerr <<
"could not create config view for permanent attribute " << lhs << std::endl;
203 cfv.view->attach_to_reference(member_ptr);
207 std::cout <<
"did not find reference to member " << lhs << std::endl;
212 cfv.out_of_date =
true;
213 this->inc_out_of_date_count();
216 else if (rhs[0] ==
'"' || rhs[0] ==
'\'') {
217 size_t n = rhs.size()-1;
219 if (rhs[n] == rhs[0])
221 rhs = rhs.substr(1, n);
222 for (
size_t i=1; i<rhs.size(); ++i) {
223 if (rhs[i-1] ==
'\\' && (rhs[i] ==
'\\' || rhs[i] == open))
228 else if (rhs ==
"true" || rhs ==
"false") {
229 bool value = rhs ==
"true";
232 else if (
is_digit(rhs[0]) || rhs[0] ==
'.' || rhs[0] ==
'+' || rhs[0] ==
'-') {
235 bp->
set_void(lhs,
"int32", &int_value);
238 double value = atof(rhs.c_str());
251 static std::map<std::string,config_file_observer*>& ref_config_file_observer_map()
253 static std::map<std::string,config_file_observer*> cfom;
260 if (ref_config_file_observer_map().find(file_name) == ref_config_file_observer_map().end()) {
262 ref_config_file_observer_map()[file_name] = cfo;
265 cfo = ref_config_file_observer_map()[file_name];
273 return d->process_gui_file(file_name);
280 cfg_reg_type(
const char* dummy)
286#include "lib_begin.h"
virtual bool set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
abstract interface for the setter of a dynamic property.
virtual bool self_reflect(cgv::reflect::reflection_handler &)
used for simple self reflection
class for gui elements that view values of the type specified in the template argument
const T & get_value() const
return the current value
view(const std::string &_name, const std::string &_value)
The cgv::reflect::find_reflection_hander steers traversal to a specific member and calls the virtual ...
bool found_target() const
return whether target has been found
void * get_member_ptr() const
in case a valid target has been found, return a pointer to the member
abst_reflection_traits * get_reflection_traits() const
in case a valid target has been found, return a point to the reflection traits describing the type of...
the tokenizer allows to split text into tokens in a convenient way.
void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
void * get_user_data() const
this virtual method allows to pass application specific data for internal purposes
void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
void * get_user_data() const
this virtual method allows to pass application specific data for internal purposes
void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
void * get_user_data() const
this virtual method allows to pass application specific data for internal purposes
void * get_user_data() const
this virtual method allows to pass application specific data for internal purposes
void update()
this virtual update allows for example to ask a view to update the viewed value. The default implemen...
bool process_gui_file(const std::string &file_name)
process a gui file
config_file_observer * find_config_file_observer(const std::string &file_name, const std::string &content)
create or find a config_file_observer from the given file name and the read content of the config fil...
void multi_observe(base_ptr bp, const std::string &property_assignments, size_t off)
to be implemented method that adds permanent registration for a list of property assignments
the base namespace holds the base hierarchy, support for plugin registration and signals
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
void register_config_file_driver(config_file_driver *cfd)
method to register a config_file_driver
gui_driver_ptr get_gui_driver()
return the currently registered gui driver or an empty pointer if non has been registered
data::ref_ptr< abst_view > view_ptr
ref counted pointer to abst view
data::ref_ptr< gui_driver > gui_driver_ptr
ref counted pointer to driver
in this namespace reflection of types is implemented
@ TI_INT16
signed integer stored in 8 bits
@ TI_INT32
signed integer stored in 16 bits
@ TI_STRING
wide character type
@ TI_FLT32
floating point type stored in 16 bits
@ TI_UINT32
unsigned integer stored in 16 bits
@ TI_UINT8
signed integer stored in 64 bits
@ TI_INT64
signed integer stored in 32 bits
@ TI_UINT16
unsigned integer stored in 8 bits
@ TI_FLT64
floating point type stored in 32 bits
@ TI_UINT64
unsigned integer stored in 32 bits
double flt64_type
this type provides a 64 bit floating point type
signed char int8_type
this type provides an 8 bit signed integer type
int int32_type
this type provides an 32 bit signed integer type
short int16_type
this type provides an 16 bit signed integer type
unsigned short uint16_type
this type provides an 16 bit unsigned integer type
unsigned long long uint64_type
this type provides an 64 bit unsigned integer type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
long long int64_type
this type provides an 64 bit signed integer type
float flt32_type
this type provides a 32 bit floating point type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
namespace that holds tools that dont fit any other namespace
bool is_digit(char c)
check if char is a digit
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
bool is_integer(const char *begin, const char *end, int &value)
check if the text range (begin,end( defines an integer value. If yes, store the value in the passed r...
void bite_all(tokenizer &t, std::vector< token > &result)
bite all tokens into a token vector
Helper functions to process strings.
abstract interface for a config file driver that handles permanent registration and gui config files.
abstract interface for observers of config files.
type independent functionality for all enum fallback implementations
virtual abst_reflection_traits * clone()=0
clone function
virtual cgv::type::info::TypeId get_type_id() const =0
return the type id