Hello, Roman, one more time. :)

> The only problem I can see is that somebody uses or writes 
> initialization-unsafe internationalization function, but but in such a case 
> he/she should not enable
> internalization in fluid "project settings" in the first place  - although I 
> can not see how somebody would want to use non-safe i18n function, everybody 
> writes
> definitions like
>
>   const char * my_string = _("My text");
>
> so the function _() must work before main().
>
> So I do not believe this is fluid problem. I have about 30 internationalized 
> fluid files, I have looked into generated code (where widgets are constructed 
> both

Yes, it's quite obviously that the problem is hidden in implementation of 
function _(). You are right, it's a fault of structure of user application, but 
it must be admitted that FLTK was very unwise when invoked user function before 
main().

I have written simple program that has the same problem with initialization of 
global variables in different modules.
///////////////////
// file a.h
#include "stdio.h"
#define FLAG 5
typedef struct{
    int f1_;
    int f2_;
} struct_1;

class class_1 {
public:
    class_1(int a)
        :f1_(a)
    {
        printf("class_1()\n");
    }
    int f1_;
};
extern class_1 c1;
extern void print_s();
///////////////////
// file a.cpp
#include "a.h"
int foo(int a) {
    if (c1.f1_ == FLAG)
        return a;
    return 0;
}
static struct_1 s1 = {foo(4), foo(7)};
void print_s() {
    printf( "f1:%d, f2:%d", s1.f1_, s1.f2_ );
}
/////////////////
// file b.cpp
#include "a.h"
class_1 c1(FLAG);
static int a = c1.f1_;

int main(int argc, char* argv[]) {
    print_s();
    return 0;
}
After executing this sample (I use VC++ 2010 Express) the variable 'a' has 
correct value (5) but the structure 'struct_1' is filled by zeros :(

Nevertheless I think the current solution of this problem is not enough well (I 
had troubles too). There is much more smart solution: to wrap our static 
variable by some function that returns inner static variable (yes, it's a 
singleton).

In my case I need change lines in a.cpp:

-static struct_1 s1 = {foo(4), foo(7)};
+struct_1& s1() {
+  static struct_1 s1_ = {foo(4), foo(7)};
+  return s1_;
+}
void print_s() {
-   printf( "f1:%d, f2:%d", s1.f1_, s1.f2_ );
+   printf( "f1:%d, f2:%d", s1().f1_, s1().f2_ );
}
It works fine.

Now in FLTK-based application menu will be initialized only after first 
invoking of the wrapped function which appears in make_window() only.

Of course, in all places instead 'some_menu' now we must write 'some_menu()', 
but I think it is not a very big price for this safe, clear and flexible code. 
One of improvement: we can give this function to user (on demand) via refusing 
to declare it as static. It would be very usefully for images too.

I hope my idea is clear and will be used for next improving of FLTK.
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to