Przechowywanie instancji klasy

0

Witam,
Mój problem wygląda tak:

  1. mam klasę, która zajmuje się rejestracją callbacków i ich obsługą.
  2. chcę, żeby po wywołaniu danego callbacka mieć dostęp do składowych tej klasy, więc przy rejestracji przypisuję instancję klasy do globalnej zmiennej instance
  3. przy jednej instancji klasy wszystko działa poprawnie, lecz jak mam dwie instancje tej klasy, to mam dostęp do zmiennych tylko z tej drugiej klasy, ponieważ to w jej instancja została ostatnio przypisana do zmiennej instance.

Mój cel jest taki, żeby każdy callback był wywoływany w kontekście swojej klasy. Jak to zrealizować z poziomu kodu, niestety callbacki nie dostają żadnych informacji w parametrach, a ja nie mogę zmienić parametrów callbacka.

Trochę kodu:

/*******************************************************************************
 *                       Copyright © 2016 Rafał Babiarz                      *
 *                                                                             *
 * This file is part of GG plugin                                              *
 *                                                                             *
 * GG plugin is free software: you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by        *
 * the Free Software Foundation; either version 3, or (at your option)         *
 * any later version.                                                          *
 *                                                                             *
 * GG is distributed in the hope that it will be useful,                       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with GNU Radio. If not, see <http://www.gnu.org/licenses/>.           *
 ******************************************************************************/

#include "GaduStateChangeService.h"
#include "SDK\PluginLink.h"
#include "SDK\IconManager.h"
#include <map>

void* instance;

CGaduStateChangeService::CGaduStateChangeService(CGaduStateChange *gaduStateChange)
{
	this->gaduStateChange = gaduStateChange;

	this->CreateServices();
}

CGaduStateChangeService::~CGaduStateChangeService()
{
	this->DestroyServices();
}

void CGaduStateChangeService::CreateServices()
{
	instance = this;
	this->hOnline = this->RegisterService(gaduStateChange->GetPopupServiceName("Online"), this->Online);
	this->hFFC = this->RegisterService(gaduStateChange->GetPopupServiceName("FFC"), this->FFC);
	this->hAway = this->RegisterService(gaduStateChange->GetPopupServiceName("Away"), this->Away);
	this->hDND = this->RegisterService(gaduStateChange->GetPopupServiceName("DND"), this->DND);
	this->hInvisible = this->RegisterService(gaduStateChange->GetPopupServiceName("Invisible"), this->Invisible);
	this->hOffline = this->RegisterService(gaduStateChange->GetPopupServiceName("Offline"), this->Offline);
	this->hChangeDesc = this->RegisterService(gaduStateChange->GetPopupServiceName("ChangeDesc"), this->ChangeDesc);
}

void CGaduStateChangeService::DestroyServices()
{
	this->UnregisterService(this->hOnline);
	this->UnregisterService(this->hFFC);
	this->UnregisterService(this->hAway);
	this->UnregisterService(this->hDND);
	this->UnregisterService(this->hInvisible);
	this->UnregisterService(this->hOffline);
	this->UnregisterService(this->hChangeDesc);
	instance = NULL;

}

HANDLE CGaduStateChangeService::RegisterService(UnicodeString name, TAQQService serviceProc)
{
	return CPluginLink::instance()->getLink().CreateServiceFunction(name.c_str(), serviceProc);
}

INT_PTR CGaduStateChangeService::UnregisterService(HANDLE hService)
{
	return CPluginLink::instance()->getLink().DestroyServiceFunction(hService);
}

INT_PTR CGaduStateChangeService::Online(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int onlineIcon = CIconManager::instance()->GetIconByName("GG_ONLINE");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = onlineIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(onlineIcon);
}

INT_PTR CGaduStateChangeService::FFC(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int ffcIcon = CIconManager::instance()->GetIconByName("GG_FFC");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = ffcIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(ffcIcon);
}

INT_PTR CGaduStateChangeService::Away(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int awayIcon = CIconManager::instance()->GetIconByName("GG_AWAY");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = awayIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(awayIcon);
}

INT_PTR CGaduStateChangeService::DND(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int dndIcon = CIconManager::instance()->GetIconByName("GG_DND");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = dndIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(dndIcon);
}

INT_PTR CGaduStateChangeService::Invisible(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int invisibleIcon = CIconManager::instance()->GetIconByName("GG_INVISIBLE");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = invisibleIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(invisibleIcon);
}

INT_PTR CGaduStateChangeService::Offline(WPARAM wParam, LPARAM lParam)
{
	CGaduStateChangeService *gaduStateChangeService = (CGaduStateChangeService*)instance;

	int offlineIcon = CIconManager::instance()->GetIconByName("GG_OFFLINE");
	gaduStateChangeService->gaduStateChange->stateButton->blinkStopIcon = offlineIcon;
	gaduStateChangeService->gaduStateChange->stateButton->setIcon(offlineIcon);
}

INT_PTR CGaduStateChangeService::ChangeDesc(WPARAM wParam, LPARAM lParam)
{
	//ShowMessage("Desc");
}
0

Sprawdź czy przypadkiem callback'i nie przekazują ci tego w WParam lub LParam

0

W lParam dostaję jakiś adres, ale konwersja tego na tekst lub na instancję klasy nic mi nie daje.

0

Trzeba jakoś wymusić w CreateServices() jakiegoś z tych callback'ów
Sprawdzasz jeżeli nie ma LParam w mapie to mapujesz LParam na instance tylko przynajmniej przenieś ją jako składową statyczną.

Jednak mi się wydaje że jakieś podpięcie się do użytkownika powinno zwrócić to samo co dostajesz w LParam.

0

Niestety lParam dla każdego wywołania callbacka jest zawsze to samo :/

0

Sprawdź jeszcze czy przypadkiem nie są różne wątki.

0

W sensie? Bo nie rozumiem.

Edit: Tak teraz pomyślałem, czy nie dałoby się tego zrobić za pomocą std::bind i std::function.

1 użytkowników online, w tym zalogowanych: 0, gości: 1