Notification casting causes compile error - c4

I am making a generic function to handle the stopping and starting of different media types in C4. I tried the following method to be called but it seems that the play method conflicts with the play method in the AVPlayer play method when called in this way. Is there a way to solve this problem?
-(void) StartStop: (NSNotification *) notification
{
if( [[notification object] isKindOfClass:[C4Movie class]] )
{
if( [[notification object] isPlaying])
[[notification object] pause];
else
[[notification object] play];
}
}

The solution is to cast the notifying object into the specific class so that the compiler knows which object to call. Objective-C uses dynamic binding. See this question for a detailed explanation: Late Binding vs Dynamic Binding
-(void) StartStop: (NSNotification *) notification
{
if( [[notification object] isKindOfClass:[C4Movie class]] )
{
C4Movie * temp = [notification object];
if( [temp isPlaying])
[temp pause];
else
[temp play];
}
}

Related

Qt QOpenGLWidget crash when call glewInit() with error : Missing GL version

I use Qt to create a 3D program with GLEW.
I have a problem when I call glewInit() from run-time creating Widget.
I create an inherited class , MyRender, based on QOpenGLWidget. Then, implemented initializeGL() with
GLenum err;
if( err = glewInit() )
{
printf( "error because: %s\n", glewGetErrorString( err ) );
exit(123);
}
Normally, I use class MyRender via Qt Designer and promote QOpenGLWidget to MyRender. Then I will have MyRender Object since the program starts. There is no problem.
However, when I create MyRender at Run-time. For example;
MyRender * myrender = new MyRender ( this );
The program will crash when calling glewInit()
Missing GL version // glewInit() problem
I found the people who have the same problem as me from this
However, in the post, people use either GLUT or SDL to create context. Since I use only GLEW how could I make the context from QOpenglWidget the same way as
glutInitDisplayMode(GLUT_RGB); //use GLUT
or
sf::Window App(sf::VideoMode(400, 400, 32), "Window"); //use SDL
or
glfwMakeContextCurrent // use glfw
Since I use none of them. I use only QOpenGLWidget and glew. I tried with
myrender->makeCurrent(); // fail
myrender->initializeGL(); // fail
before calling
glewInit()
however, the problem still persists.
About my machine: I use Windows 10 64-bit. Qt 5.11 GLEW 2.1.0
EDIT:
I test my code with
void initializeGL()
{
echo("inside initializeGL");
QOpenGLContext* current = this->context();
if( current == nullptr )
{
printf("current context is null\n");
exit(123);
}
else
{
printf("current context is good\n");
}
GLenum err = glewInit(); // error here
...
}
If I use Qt Designer to promote openGLWidget to MyRender . the context will be OK;
However, if I create MyRender in Run-time
MyRender* myrender = new MyRender( this );
The context will be null and leads glewInit() error.
I found that the problem is context is not created, and when the program call function initializeGL(). The solution that I did is I call show(); right after creating the widget.
myrender = new MyRender( ui->mdiArea );
myrender->show(); // this line make the context for QOpenGLWidget I think..
...
// note class MyRender is an inherited class from QOpenGLWidget

Sanitize untrusted CSS using Angular $sce.getTrustedCss

Trying to use $sce.getTrustedCss but always am getting an error for unsafe input.
Should such an example be safe or am I missing something.
$sce.getTrustedCss('.red {color: red;}');
Alternatively, are there other JS sanitizers that can work on CSS input?
google-caja only works for inline styling, it removes STYLE tags altogether.
Since Angular 2, use DomSanitizer:
import { DomSanitizer } from '#angular/platform-browser';
constructor(
private domSanitizer: DomSanitizer
) {}
// let legal = this.domSanitizer.bypassSecurityTrustStyle( styleAtrStr );
So far as I can tell, there is no actual CSS sanitization built in to Angular.
I think $sce.getTrustedCss(s) will always tell you that the input is unsafe unless you first do: s = $sce.trustAsCss(something). So if you pass your css through a sanitiser, or know it came from a trusted source you can mark it as safe for use.
Note also that the Angular documentation says that Angular doesn't actually use getTrustedCss() but you are free to use it in your own directives. I think that means if you do use it you would be responsible for ensuring the safe inputs were first passed through trustAsCss().
Here's the implementation of getTrusted():
function getTrusted(type, maybeTrusted) {
if (maybeTrusted === null || isUndefined(maybeTrusted) || maybeTrusted === '') {
return maybeTrusted;
}
var constructor = (byType.hasOwnProperty(type) ? byType[type] : null);
if (constructor && maybeTrusted instanceof constructor) {
return maybeTrusted.$$unwrapTrustedValue();
}
// If we get here, then we may only take one of two actions.
// 1. sanitize the value for the requested type, or
// 2. throw an exception.
if (type === SCE_CONTEXTS.RESOURCE_URL) {
if (isResourceUrlAllowedByPolicy(maybeTrusted)) {
return maybeTrusted;
} else {
throw $sceMinErr('insecurl',
'Blocked loading resource from url not allowed by $sceDelegate policy. URL: {0}',
maybeTrusted.toString());
}
} else if (type === SCE_CONTEXTS.HTML) {
return htmlSanitizer(maybeTrusted);
}
throw $sceMinErr('unsafe', 'Attempting to use an unsafe value in a safe context.');
}
return { trustAs: trustAs,
getTrusted: getTrusted,
valueOf: valueOf };
}];
Notice that urls and html have some extra checking, but other types (css, js) are only trusted when have been wrapped by the appropriate trustAs... function. Also trustAsCss() doesn't appear to be documented, but as the shorthand methods are automatically generated it should exist (or you could use trustAs($sce.CSS, ...) directly).

qmlRegisterSingletonType and multiple top-level windows

I have a QGuiAppplication derived class (called Sy_application) that I've wrapped with another (called Sy_application_qml, with extra some QML specific features) and then registered that as a singleton with the QML engine.
This all works fine from the first opened top-level window, but the second (and presumably any more) only gets null from the singleton. Adding some debug:
// The static factory method used in the qmlRegisterSingletonType call.
QObject* Sy_application_qml::factory( QQmlEngine* engine,
QJSEngine* scriptEngine )
{
Q_UNUSED( engine )
Q_UNUSED( scriptEngine )
qDebug() << "Creating";
return new Sy_application_qml();
}
// SY_APP is the qApp macro casted to my Sy_application type.
QObject* Sy_application_qml::get()
{
qDebug() << "Getting:" << SY_APP;
return SY_APP;
}
// This is an example of it's use within QML
onPositiveClicked: {
console.log( Sy_application_qml );
Sy_application_qml.get().newProject( sampleRate.value, frameRate.value );
close();
}
And the debug output:
// Opening first window.
Creating
// Creating second
qml: Sy_application_qml(0x1895b30)
Getting: Sy_application(0x7fff80f051c0)
// Attempting to create third
qml: Sy_application_qml(0x1895b30)
Getting: Sy_application(0x7fff80f051c0)
qrc:/qml/gui/dialogs/Sy_newProjectDialog.qml:62: TypeError: Cannot call method 'newProject' of null
As you can see the singleton is still present and the C++ Sy_application instance is being returned correctly, but is appearing as null on the QML side. Any reason why it works for one window and not another?
So I finally worked it out, and it had nothing to do with QML singletons or having multiple top-level windows - but it had everything to do with passing QObject pointers to the QML engine from Q_INVOKABLE methods (the Sy_application_qml::get() method in my case).
From the docs:
Objects not-created by QML have CppOwnership by default. The exception
to this is objects returned from C++ method calls; in these cases, the
ownership of the returned objects will be set to JavaScriptOwnerShip.
Note this applies only to explicit invocations of Q_INVOKABLE methods
or slots, and not to property getter invocations.
So the QML engine had deleted my Sy_application pointer. The solution was nice and simple:
QObject* Sy_application_qml::factory( QQmlEngine* engine,
QJSEngine* scriptEngine )
{
Q_UNUSED( engine )
Q_UNUSED( scriptEngine )
QQmlEngine::setObjectOwnership( SY_APP, QQmlEngine::CppOwnership );
return new Sy_application_qml();
}

In ActionScript, is there a way to test for existence of variable with datatype "Function"

So I have a class where I instantiate a variable callback like so:
public var callback:Function;
So far so good. Now, I want to add an event listener to this class and test for existence of the callback. I'm doing like so:
this.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent) : void {
if (callback) {
// do some things
}
});
This works great, doesn't throw any errors, but everywhere I test for callback I get the following warning:
3553: Function value used where type Boolean was expected.
Possibly the parentheses () are missing after this function reference.
That bugged me, so I tried to get rid of the warning by testing for null and undefined. Those caused errors. I can't instantiate a Function as null, either.
I know, I know, real programmers only care about errors, not warnings. I will survive if this situation is not resolved. But it bothers me! :) Am I just being neurotic, or is there actually some way to test whether a real Function has been created without the IDE bitching about it?
Similar to using typeof:
if(callback is Function){
}
I believe should evaluate to true if the function exists and is a function and false if it is null or is not a function. (although if that doesn't work try if(callback && callback is function){}
if( !(callback == null)){
// do something
}
There's already an answer that works, but I thought I'd mention that you can also stop the warning from occurring by explicitly casting the result to a Boolean.
if (Boolean(callback)) {
// do something
}
Have you tried:
if (typeof callback == "function") {
// do some things
}
?
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/operators.html#typeof

Does Flex have an onclose/onunload event for the main movie?

I'm looking to dispose of my localconnection when the movie is closed, or unloaded, what event should i do this with?
I do not believe that there is, but I think you can force the closure anyway.
Not entirely certain how simple this will be, but here would be my best guess. When instantiating your LocalConnection, be sure to include this in the client (or as a property in AS2):
function close()
{
myConnection.close();
}
In addition to that, I would include this while attempting to have the connection connect:
var commName:String = "MY_CONNECTION";
var myConnection:LocalConnection = new LocalConnection();
// Try to tell any open LocalConnection on this channel to close.
// This may cause an AsyncErrorEvent, so be sure to add the appropriate
// Error handling.
myConnection.send( commName, "close" );
try
{
myConnection.connect( commName );
}
catch( error:Error )
{
// If there is another connection already open on the same channel,
// that will cause an Error. I have had some luck catching that
// Error and then calling connect again. That said, you would be
// best to take precautions anyway.
try
{
myConnection.connect( commName );
}
catch( error:Error )
{
// Your connection cannot connect!!!
// DO SOMETHING!!!
}
}
myConnection.client = this;
I guess you could react to the unload event in JavaScript and call some cleanup function in the flex app through the ExternalInterface. Haven't worked much with ExternalInterface though, so I'm not really sure.

Resources