开发者

Aspect Oriented Programming in Qt

I'm trying to get my head around AOP and some Qt Code would really help.

From wikipedia here is some sample code (easy for a Qt/C++ programmer to read):

void transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger)
   throws Exception {
   logger.info("transferring money...");
   if (! checkUserPermission(user)){
     logger.info("User has no permission.");
     throw new UnauthorizedUserException();
   }
   if (fromAcc.getBalance() < amount) {
     logger.info("Insufficient Funds, sorry :( ");
     throw new InsufficientFundsException();
   }

   fromAcc.withdraw(amount);
   toAcc.deposit(amount);

   //get database connection

   //save transactions

   logger.info("Successful transaction. :) ");
 }

And then "aspectized":

void transfer(Account fromAcc, Account toAcc, int amount) throws Exception {


   if (fromAcc.getBalance() < amount) {
     throw new InsufficientFundsException();
   }

   fromAcc.withdraw(amount);
   toAcc.deposit(amount);
 }

aspect 开发者_如何学GoLogger 
{

    void Bank.transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger)
    {
    
        logger.info("transferring money...");
    }

    void Bank.getMoneyBack(User user, int transactionId, Logger logger)
    {
        logger.info("User requested money back");
    }

    // other crosscutting code...
}

Qt has signals and slots to decouple objects. But I still need to emit signals.

So: Can this be done with Qt or do I need some special framework/preprocessors as referenced in the wikipedia article?

I have a feeling that there must be some trick since Qt uses the Meta Object Compiler and some functionality might be "injected" with dynamic methods.... just spit-balling here ;)

Edit: To give a better context: I really like the dynamic aspects (power) of the Qt meta object with signals and slots and would like to keep a Qt feel to it. Thus, my idea is to make use of slots (or signals) as point cuts. For example:

If I define slot Bank::transfer(...) and then signal Bank::OnBeforeTranfer() and signal Bank::OnAfterTransfer(). If I then connect them to other aspects say Security::transfer() and Logger::transfer() (all QObjects) I can block calls (like fail OnBeforeTransfer).

But, if we then take it to the next evolution to get less and cleaner code I would like to get rid of the OnXXXX signals and connect the Bank::transfer slot to Security::transfer slot and Logger::transfer. Anything dynamic in Qt? : Like order of calling slots and and preventing next call in the "slot chain"?

This whole context can still be considered AOP right? I'm trying to stick to "method level point cuts" or am I totally beside the point here?


In what language are you planning to use Qt? I recently had to build a simple GUI in Qt around a python script and used the AOP python package Aspyct to do some quick before and after stuff. Qt is event-driven programming, I'd say get familiar with the Qt basics, many things are similar to AOP-style operations and then find some AOP libraries for the language you plan to use Qt in.


Another AOP framework you may consider using is AspectC++. I've played with it a bit and it seems to work quite well. They even have a whitepaper on the site that describes how AspectC++ can be used with Qt.


If you want to stay within the Qt framework, you could take a look at the State Machine Framework. (And get rid of the exceptions :)

Then you could just connect the Logger to state change events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜