RProperty 1

Pulish及Subscribe:使用RProperty进行subscribe操作

详细描述

下列代码片段演示了如何使用RProperty 来订阅用户使用Symbian IPC发布及订阅机制定义的属性。下列步骤用来订阅并接收属性更新状态。

  • 使用RProperty::Attach()标注需要的属性,然后调用RProperty::Subscribe()订阅
  • 通过TRequestStatus信号通知RProperty::Subscribe()完成。
  • 调用RProperty::Get()获得更新后的属性值
  • 通过RProperty::Subscribe重新提交请求来监听属性变化
  • 最终调用RProperty::Cancel来取消订阅,以及通过RProperty::Close

Publish及Subscribe:使用Rproperty进行publish操作 中的代码片段演示了用户定义属性如何被发布,这些属性定义将通过ExampleProperties.h在发布者和订阅者之间共享。

#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
 
const Tint KMaxStrLen = 10;
const Tuid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
 
#endif /*EXAMPLEPROPERTIES_H_*/

This snippet can be self-signed.

MMP文件

需要下列链接库

LIBRARY euser.lib

前言

发布者或订阅者线程都可以调用Rproperty::Define() 来定义属性。用户定义的属性将一直保存到操作系统重启或被删除。

Tint ret=Rproperty::Define(KExampleProperty,EIntProperty,Rproperty::Eint);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
 
ret= Rproperty::Define(KExampleProperty,EStrProperty,Rproperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}

头文件

#ifndef EXAMPLESUBSCRIBER_H_
#define EXAMPLESUBSCRIBER_H_
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
#include "exampleproperties.h"
 
 
class MExampleSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(Tint aValue) = 0;
virtual void StrPropertyUpdatedL(Tdes& aValue) = 0;
};
 
 
class CExampleSubscriber : public Cactive
{
enum {Epriority=0};
public:
static CExampleSubscriber* NewL( const Tuid aUid,
const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
virtual ~CExampleSubscriber();
 
private:
CExampleSubscriber( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
Rproperty iProperty;
const Tuid iUid;
const TUint32 iKey;
MExampleSubscriberObserver& iObserver;
};
 
#endif /*EXAMPLESUBSCRIBER_H_*/

源文件

#include "ExampleSubscriber.h"
 
CExampleSubscriber::CExampleSubscriber(
const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
: Cactive(Epriority), iUid( aUid ),
iKey( aKey ), iObserver( aObserver)
{
}
 
CExampleSubscriber* CExampleSubscriber::NewL( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
{
CExampleSubscriber* self=
new(Eleave) CExampleSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CExampleSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CExampleSubscriber::~CExampleSubscriber()
{
Cancel();
iProperty.Close();
}
 
void CExampleSubscriber::DoCancel()
{
iProperty.Cancel();
}
 
void CExampleSubscriber::RunL()
{
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
Tint intValue;
Tbuf<KMaxStrLen> strValue;
 
switch( iKey )
{
case EIntProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EIntProperty,intValue);
if( iProperty.Get( intValue ) == KErrNotFound )
{
// Int property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.IntPropertyUpdatedL(intValue);
}
break;
case EStrProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EStrProperty,strValue);
if( iProperty.Get( strValue ) == KErrNotFound )
{
// Str property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.StrPropertyUpdatedL(strValue);
}
break;
 
default:
break;
}
}
 
// End of File

使用CExampleSubscriber类

在头文件里:

#include "examplesubscriber.h"
 
class CPropertyExampleAppUi : public CAknAppUi, MExampleSubscriberObserver
{
//…
public:
void IntPropertyUpdatedL(Tint aValue);
void StrPropertyUpdatedL(Tdes& aValue);
//…
CExampleSubscriber* iIntSubscriber;
CExampleSubscriber* iStrSubscriber;
};

在源文件中:

void CPropertyExampleAppUi::ConstructL()
{
//…
iIntSubscriber = CExampleSubscriber::NewL(KExampleProperty,EIntProperty,*this);
iStrSubscriber = CExampleSubscriber::NewL(KExampleProperty,EStrProperty,*this);
}
 
CPropertyExampleAppUi::~CPropertyExampleAppUi()
{
//…
delete iIntSubscriber;
delete iStrSubscriber;
}
 
void CPropertyExampleAppUi::IntPropertyUpdatedL(Tint aValue)
{
//do something with the updated property
}
 
void CPropertyExampleAppUi::StrPropertyUpdatedL(Tdes& aValue)
{
//do something with the updated property
}

后记

使用Rproperty来订阅两个示例属性

如果订阅线程定义了属性,那么也可以使用Rproperty::Delete() 来删除掉。

Tint err(KErrNone);
 
err = Rproperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = Rproperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}

当属性都被删除掉后,任何未完成的订阅将被告知KErrNotFound完成 (来自:Forum Nokia Wiki)

 

详细描述

下列代码片段演示了如何使用RProperty 来订阅用户使用Symbian IPC发布及订阅机制定义的属性。下列步骤用来订阅并接收属性更新状态。

  • 使用RProperty::Attach()标注需要的属性,然后调用RProperty::Subscribe()订阅
  • 通过TRequestStatus信号通知RProperty::Subscribe()完成。
  • 调用RProperty::Get()获得更新后的属性值
  • 通过RProperty::Subscribe重新提交请求来监听属性变化
  • 最终调用RProperty::Cancel来取消订阅,以及通过RProperty::Close

Publish及Subscribe:使用Rproperty进行publish操作 中的代码片段演示了用户定义属性如何被发布,这些属性定义将通过ExampleProperties.h在发布者和订阅者之间共享。

#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
 
const Tint KMaxStrLen = 10;
const Tuid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
 
#endif /*EXAMPLEPROPERTIES_H_*/

This snippet can be self-signed.

MMP文件

需要下列链接库

LIBRARY euser.lib

前言

发布者或订阅者线程都可以调用Rproperty::Define() 来定义属性。用户定义的属性将一直保存到操作系统重启或被删除。

Tint ret=Rproperty::Define(KExampleProperty,EIntProperty,Rproperty::Eint);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
 
ret= Rproperty::Define(KExampleProperty,EStrProperty,Rproperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}

头文件

#ifndef EXAMPLESUBSCRIBER_H_
#define EXAMPLESUBSCRIBER_H_
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
#include "exampleproperties.h"
 
 
class MExampleSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(Tint aValue) = 0;
virtual void StrPropertyUpdatedL(Tdes& aValue) = 0;
};
 
 
class CExampleSubscriber : public Cactive
{
enum {Epriority=0};
public:
static CExampleSubscriber* NewL( const Tuid aUid,
const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
virtual ~CExampleSubscriber();
 
private:
CExampleSubscriber( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
Rproperty iProperty;
const Tuid iUid;
const TUint32 iKey;
MExampleSubscriberObserver& iObserver;
};
 
#endif /*EXAMPLESUBSCRIBER_H_*/

源文件

#include "ExampleSubscriber.h"
 
CExampleSubscriber::CExampleSubscriber(
const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
: Cactive(Epriority), iUid( aUid ),
iKey( aKey ), iObserver( aObserver)
{
}
 
CExampleSubscriber* CExampleSubscriber::NewL( const Tuid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
{
CExampleSubscriber* self=
new(Eleave) CExampleSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CExampleSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CExampleSubscriber::~CExampleSubscriber()
{
Cancel();
iProperty.Close();
}
 
void CExampleSubscriber::DoCancel()
{
iProperty.Cancel();
}
 
void CExampleSubscriber::RunL()
{
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
Tint intValue;
Tbuf<KMaxStrLen> strValue;
 
switch( iKey )
{
case EIntProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EIntProperty,intValue);
if( iProperty.Get( intValue ) == KErrNotFound )
{
// Int property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.IntPropertyUpdatedL(intValue);
}
break;
case EStrProperty:
// Int property updated, get new value
//see also: Rproperty::Get(KExampleProperty,EStrProperty,strValue);
if( iProperty.Get( strValue ) == KErrNotFound )
{
// Str property deleted, do necessary actions here…
}
else
{
// use new value …
iObserver.StrPropertyUpdatedL(strValue);
}
break;
 
default:
break;
}
}
 
// End of File

使用CExampleSubscriber类

在头文件里:

#include "examplesubscriber.h"
 
class CPropertyExampleAppUi : public CAknAppUi, MExampleSubscriberObserver
{
//…
public:
void IntPropertyUpdatedL(Tint aValue);
void StrPropertyUpdatedL(Tdes& aValue);
//…
CExampleSubscriber* iIntSubscriber;
CExampleSubscriber* iStrSubscriber;
};

在源文件中:

void CPropertyExampleAppUi::ConstructL()
{
//…
iIntSubscriber = CExampleSubscriber::NewL(KExampleProperty,EIntProperty,*this);
iStrSubscriber = CExampleSubscriber::NewL(KExampleProperty,EStrProperty,*this);
}
 
CPropertyExampleAppUi::~CPropertyExampleAppUi()
{
//…
delete iIntSubscriber;
delete iStrSubscriber;
}
 
void CPropertyExampleAppUi::IntPropertyUpdatedL(Tint aValue)
{
//do something with the updated property
}
 
void CPropertyExampleAppUi::StrPropertyUpdatedL(Tdes& aValue)
{
//do something with the updated property
}

后记

使用Rproperty来订阅两个示例属性

如果订阅线程定义了属性,那么也可以使用Rproperty::Delete() 来删除掉。

Tint err(KErrNone);
 
err = Rproperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = Rproperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}

当属性都被删除掉后,任何未完成的订阅将被告知KErrNotFound完成 (来自:Forum Nokia Wiki)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值