www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RAII trouble

reply Spacen Jasset <spacen razemail.com> writes:
I have the following code in attempt to create an RAII object 
wrapper, but it seems that it might be impossible. The problem 
here is that FT_Init_FreeType is a static which is set at some 
previous time to a function entry point in the FreeType library.

I could use a scope block, but would rather have a self contained 
thing to do the work. Perhaps some sort of mixin is the other 
solution?

The ideal would be to have a struct that can be placed inside a 
function scope, or perhaps as a module global variable. Why does 
Ft_Init_FreeType have to be read at compile time?


text.d(143,15): Error: static variable FT_Init_FreeType cannot be 
read at compile time
text.d(174,43):        called from here: initialise()

struct FreeType
{
	 disable this();

	static FreeType initialise()
	{
		return FreeType(0);
	}

	this(int)
	{
		int error = FT_Init_FreeType(&library); /// ERROR
		enforce(!error);
	}
	
	alias library this;
	
	~this()
	{
		FT_Done_FreeType(library);
	}
	FT_Library library;
}

FreeType   freeType = FreeType.initialise();
Nov 20 2015
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/20/2015 02:56 PM, Spacen Jasset wrote:

 FT_Init_FreeType is a static which is set at some previous time to a
 function entry point in the FreeType library.
 text.d(143,15): Error: static variable FT_Init_FreeType cannot be read
 at compile time
The compiler seems to think that FT_Init_FreeType is a variable. Is that really a function pointer? Can you show how it's defined. My simple test works: extern (C) int init_func(double){ return 42; } static auto my_func = &init_func; struct S { static S init() { return S(0); } this(int) { my_func(1.5); } } void main() { auto s = S.init(); } Ali
Nov 20 2015
prev sibling parent reply anonymous <anonymous example.com> writes:
On 20.11.2015 23:56, Spacen Jasset wrote:
 The ideal would be to have a struct that can be placed inside a function
 scope, or perhaps as a module global variable. Why does Ft_Init_FreeType
 have to be read at compile time?


 text.d(143,15): Error: static variable FT_Init_FreeType cannot be read
 at compile time
 text.d(174,43):        called from here: initialise()

 struct FreeType
 {
       disable this();

      static FreeType initialise()
      {
          return FreeType(0);
      }

      this(int)
      {
          int error = FT_Init_FreeType(&library); /// ERROR
          enforce(!error);
      }

      alias library this;

      ~this()
      {
          FT_Done_FreeType(library);
      }
      FT_Library library;
 }

 FreeType   freeType = FreeType.initialise();
FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: ---- FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */ void main() {freeType = FreeType.initialise();} /* too */ ----
Nov 20 2015
parent reply Spacen Jasset <spacen razemail.com> writes:
On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote:

[...]
 FT_Init_FreeType must be read at compile time, because freeType 
 is a module level variable, and initializers for module 
 variables must be static values. The initializer is run through 
 CTFE (Compile Time Function Evaluation).
 Put the initialization/assignment in a function and it should 
 work. That function can be a static constructor or the main 
 function:
 ----
[...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks.
Nov 20 2015
parent Spacen Jasset <spacen razemail.com> writes:
On Friday, 20 November 2015 at 23:35:50 UTC, Spacen Jasset wrote:
 On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote:

 [...]
 FT_Init_FreeType must be read at compile time, because 
 freeType is a module level variable, and initializers for 
 module variables must be static values. The initializer is run 
 through CTFE (Compile Time Function Evaluation).
 Put the initialization/assignment in a function and it should 
 work. That function can be a static constructor or the main 
 function:
 ----
[...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks.
I Just noticed this trick you posted, that's interesting. Of course I couldn't get it working without the void to discard the initialization; FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */
Nov 20 2015