GenericResource.h
上传用户:smh666999
上传日期:2007-01-14
资源大小:553k
文件大小:3k
源码类别:

BREW编程

开发平台:

Visual C++

  1. #ifndef _gresource_h_
  2. #define _gresource_h_
  3. #include "BrewString.h"
  4. #include "AEEStdlib.h"
  5. #include "AEEAppGen.h"
  6. #include "AEEFile.h"
  7. #include "CbkDispatcher.h"
  8. //A few utility macros; We shall eventually move this into the AEE Services.
  9. #define ISDIGIT(c)  ( (unsigned) ((c) - '0') < 10)
  10. #define ISALPHA(c)  ( (unsigned) ( ((c)|32) - 'a') < 26 )
  11. #define ISALNUM(c)  ( ISDIGIT(c) || ISALPHA(c) )
  12. struct GDS: public IRelease
  13. {
  14. virtual void releaseResource( ) 
  15. {
  16. delete this;
  17. }
  18. virtual int getUID( ) 
  19. {
  20. return 555;
  21. }
  22. static int getID()
  23. {
  24. return 555;
  25. }
  26. template <class R>
  27. static GDS* initDataStruct(R* rr, char* address, const String& params )
  28. {
  29. GDS* ds=static_cast<GDS*>(rr->getRegistered(GDS::getID()));
  30. GDS::createGDS(ds);
  31. ds->address = address;
  32. ds->parameters = params;
  33. if (!ds->data.isEmpty())
  34. ds->data="";
  35. rr->registerResource(ds);
  36. return ds;
  37. }
  38. char* address;
  39. String parameters;
  40. String data;
  41. int error;
  42. UINT bufSize;
  43. private:
  44. GDS() :  bufSize(7)
  45. {}
  46. static void createGDS(GDS*& ds)
  47. {
  48. if (ds==0)
  49. ds = new GDS();
  50. }
  51. GDS( const GDS &Value );
  52. const GDS &operator = ( const GDS &Rhs );
  53. };
  54. template <class Y, class F>
  55. struct pkCBK;
  56. class GResource 
  57. {
  58. public:
  59. GResource(IShell* shell,  IExecute* pp, GDS* ds) :
  60. shell_(shell),
  61. processPolicy_(pp), 
  62. resource_("Address "),
  63. ds_(ds), 
  64. idx_(0)
  65. {
  66. }
  67. ~GResource();
  68. int init();
  69. int initConnection();
  70. void reset();
  71. private:
  72. void write();
  73. void read();
  74. void onError( int errorCode) ;
  75. void onData( ) ;
  76. void releaseResources();
  77. void releaseResource();
  78. private:
  79. IShell* shell_;
  80. String resource_;
  81. IExecute* processPolicy_; 
  82. int idx_;
  83. typedef pkAEECBK<GResource> A;
  84. A a_;
  85. GDS* ds_;
  86. private:
  87. GResource( const GResource &Value );
  88. const GResource &operator = ( const GResource &Rhs );
  89. };
  90. GResource::~GResource()
  91. {
  92. releaseResources();
  93. }
  94. void GResource::releaseResource()
  95. {
  96. idx_ = 0;
  97. resource_ =("Address ");
  98. }
  99. void GResource::reset()
  100. {
  101. releaseResource();
  102. }
  103. void GResource::releaseResources()
  104. {
  105. releaseResource();
  106. }
  107. int GResource::init()
  108. int err;
  109. resource_ += ds_->address;
  110. //do specific creation if any
  111. return SUCCESS;
  112. }
  113. int GResource::initConnection()
  114. {
  115. resource_ += " Param: ";
  116. resource_ += ds_->parameters;
  117. a_.setCallback(this, read, shell_);
  118. a_();
  119. return SUCCESS;
  120. }
  121. void GResource::read ()
  122. {
  123. int end = idx_ + ds_->bufSize;
  124. end = end>resource_.length()? resource_.length(): end;
  125. String s = resource_.substring(idx_, end);
  126. if (s.length() > 0) 
  127. {
  128. ds_->data += s;
  129. a_.setCallback(this, read, shell_);
  130. a_();
  131. idx_ = end;
  132. return;
  133. }
  134. else 
  135. {
  136. onData();
  137. return;
  138. }
  139. }
  140. void GResource::onError( int errorCode) {
  141. ds_->error = errorCode;
  142. ds_->data = "";
  143. processPolicy_->onCbk() ;
  144. }
  145. void GResource::onData( ) {
  146. ds_->error = SUCCESS;
  147. processPolicy_->onCbk() ;
  148. }
  149. #endif