I am wondering if there a way to do something like this in c++. What I want to do works similar to how a display list in opengl works, but I would like to be able to do this for a general case.
BeginCachingCode();
a bunch of lines of code blah
...
...
...
...
EndCachingCode();
Quote:Original post by SiCrane Sounds like a job for a function.
What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function, if anyone is familiar with opengl display lists then this should make sense.
I was thinking that BeginCaching could perhaps store the memory address of the next instruction, and that EndCaching could mark the memory address of the previous instruction as an endpoint to use when RunCachedCode is called. I am not sure if something like this is possible.
The method I describe here is not caching, but merely just stores and start and end addresses of the instructions in the code block.
Quote:Original post by SiCrane Sounds like a job for a function.
What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function
Quote:if anyone is familiar with opengl display lists then this should make sense.
Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:
There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).
If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.
JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.
Quote:Original post by SiCrane Sounds like a job for a function.
What do you mean? I am trying to keep things in this case as general as possible without forcing users to be constrained to defining a specific callback function
Quote:if anyone is familiar with opengl display lists then this should make sense.
Ask yourself - what is it that makes gl's display lists faster than the alternative? What compromises are made in order to acheive this speed up? Then ask yourself (and please tell me because I'm baffled) how you'd apply this as a C++ optimisation. Because as far as I can see it, all you've asked for is:
Hmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.
Quote:Original post by hplus0603 There is no language-specific feature that lets you do this in C++. There are higher-level constructs, like closures with bound parameters, that let you do this in various dynamic languages (i e LISP, LUA).
If you really want to do that (and sometimes, like in software rasterizers, you really do), then you can generate assembly code into a chunk of memory and jump to it. You have to make sure you allocate that memory in the way that the OS needs to mark that memory as executable, though, or you'll just get an exception.
JIT bytecode interpreters, and various pixel format converters and scanline rasterizers have been known to do this.
I have not used assembly much. However is it possible within assembly to get the address of the current instruction pointer?
Quote:Original post by Khaos Dragon Hmm I suppose I should have been more general, my aim is not to necessarily cache code but mainly to be able to store it in some form. I am not doing this for speed, but for organizational reasons.
'Organisational reasons' is so horribly vauge that it could mean anything, but at a guess I'd suggest you look into function pointers and/or the listener pattern.
I currently am using function pointers, but this requires me to force the user to define a function with specific class parameters where I like him to be able to pass whatever paramteres possible.
I suppose a function pointer with a void pointer as an argument would be good here?