Jump to content

Typecasting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I'm creating a sort of GUI library for a programming language I'm working on, but I'm having a few problems typecasting pointers:

[HIGHLIGHT="C"]
#pragma once

#undef UNICODE
#include <windows.h>
using namespace System;

class P3L32Window
{
private:
static const char *defaultClass = "p3l32Window";
protected:
//WINDOW VARIABLES////////////////
HINSTANCE instance;
MSG msg;
int showCommand;
HWND window;
HWND parent;
DWORD windowStyle;
DWORD windowStyleEX;
//WINDOW SPECIFICATIONS///////////
LPCSTR windowClass;
WNDCLASSEX wc;
//EVENT HANDLERS//////////////////
P3LEventHandler<Win32Parameter> *closeHandler;
P3LEventHandler<Win32Parameter> *destroyHandler;
protected:
P3L32Window(HINSTANCE thisInstance,HWND parentWindow,LPCSTR thisClass,LPCSTR title, \
int cmdShow, int width, int height, int x = CW_USEDEFAULT, \
int y = CW_USEDEFAULT, DWORD style = 0, \
DWORD extendedStyle = 0, bool show = true)
{
//Irrelevant code removed

//set defaults for the window descriptor
/*PROBLEM - CAN'T DO A TYPECAST, GET THE FOLLOWING ERROR
"error C2440: '=' : cannot convert from
'LRESULT (__stdcall P3L32Window::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'
There is no context in which this conversion is possible."*/
wc.lpfnWndProc = &P3L32Window::P3L32DefaultWndProc;

//Irrelevant code removed
}
public:
//Irrelevant code removed
private:
LRESULT CALLBACK P3L32DefaultWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Win32Parameter parameters = {wParam,lParam};
switch(msg)
{
case WM_CLOSE:
closeHandler->execute(¶meters);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
destroyHandler->execute(¶meters);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

//Irrelevant code removed
};
[/HIGHLIGHT]
I know for a fact that I have all the arguments in the right order, and the return type is the same as the specification. I've had issues like this before, and I got around them, but this is different in some ways. Can someone help?

#2
vAC

vAC

    Newbie

  • Members
  • Pip
  • 7 posts
It's because P3L32Window::P3L32DefaultWndProc haven't that signature, this pointer additionally passed. There are 2 ways to solve problem:
1. define P3L32Window::P3L32DefaultWndProc as static method;
2. define P3L32DefaultWndProc as independent function.
Sorry for my poor English :pee:

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I figured as much. The problem with making P3L32DefaultWndProc a static function or a separate function is that it needs access to each window's individual P3LEventHandler objects. What I think I can do (correct me if I'm wrong) is the following:

In P3L32Window, I can create a static LinkedList object that will keep track of the windows created using their HWND as an identifier. Since each window has a different HWND value, it can be used distinguish which window is which. (I think that part might be wrong. If the HWND is the parent window, then this won't work.) So I can create an independant function that calls a static accessor method in P3L32Window to get a pointer to the desired P32L32Window object, thereby gaining access to the event handlers.


class P3L32Window

{

private:

    static LinkedList<P3L32Window *> *windows;

public:

    P3L32Window( /*args*/)

    {

        //irrelevant code removed

        windows->add(this);

    }

    //other methods...

    static P3L32Window *getWindow(HWND hwnd)

    {

        P3LComparator<P3L32Window *> *cmp= getWindowHWNDComparator();

        return windows->search(hwnd,cmp);

    }

};


LRESULT CALLBACK P3L32DefaultWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

    P3L32Window *thisWindow = P3L32Window::getWindow(hwnd);

    //handle messages here using thisWindow's event handlers.

}



#4
vAC

vAC

    Newbie

  • Members
  • Pip
  • 7 posts
Yes, that's rght. In MFC implemented such solution, but used map container, not a list for beter performance.
Sorry for my poor English :pee:

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Ah, well, I'll get around to writing that eventually. Since I'm creating my own programming language, I'm coding everything myself, so I know exactly how it works. I tend to be very mistrusting of others' code, no matter how reputable they are. Probably not a good thing. Any suggestions as to how I can make my programming language more coder-friendly as far as the simplicity of creating windows and stuff like that?

#6
vAC

vAC

    Newbie

  • Members
  • Pip
  • 7 posts
Yeah, remember myself. I had same opinion about "doing all myself" :) It's very desirable for experience, but in real projects it's beter to use standard solutions such a STL, boost, CGAL and so on.

Actualy i don't understand what are you doing...
You said that you are doing your own programming language, but you're using C++...Maybe it's your own UI library, not programming language? Can you explain it, please, maybe my English so poor? :)
Sorry for my poor English :pee:

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
I'm writing the libraries for the interpreter to use. For example, in P3L I could write the following code:


Window myWindow = new Window;

myWindow.show();


This would be compiled into P3LQCode, a bytecode format for the P3L virtual machine. This way, one program can be ported to any system, provided it has a P3L virtual machine program installed. The C++ code you see above is part of the standard P3L library, which will be used when executing P3L programs.

And contrary to what you say, your English is quite good.

#8
vAC

vAC

    Newbie

  • Members
  • Pip
  • 7 posts
Ah, understand - something like JVM :)
Very interesting idea. Good luck!
Sorry for my poor English :pee:

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Yes! Exactly that. Except, hopefully, better. Thanks for the help.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
Okay, I'm having another problem again. In the following code, I'm implementing a function as part of my Map class that gets rid of all null references in the map. My compiler is giving me the following rather large error:

lang\map.h(172) : error C2664:
'Comparator<T>::Comparator(int (__cdecl *)(T,T))' : cannot convert parameter 1 from
'int (__cdecl *)(void *,void *)' to 'int (__cdecl *)(T,T)'
with [ T=MapNode<P3LString *,LinkedList<P3LString *> *> * ]

This conversion requires a reinterpret_cast, a C-style cast or function-style cast.


I'm assuming this is because my Comparator class's standard pointer comparator is declared as comparePointer(void *,void *) and can't take MapNode<T,T> * pointers by default. However, the compiler says that I can typecast to fix it. How would I do that?

The relevant code for the function:
[HIGHLIGHT="C++"]
void garbageCollect()
{
LinkedList<MapNode<K,V>*> *ll = new LinkedList<MapNode<K,V>*>(map,size);
Comparator<MapNode<K,V> *> *cmp = \
new Comparator<MapNode<K,V> *>(&Comparator<MapNode<K,V> *>::comparePointer); //<---PROBLEM
//remove all nulls
while(ll->discard((MapNode<K,V> *)NULL))
--size;
//deallocate
free(map);
//reallocate to new array
map = ll->toArray();
//clean up
delete cmp;
delete ll;
}
[/HIGHLIGHT]

And yes, comparePointer is a static function.