EAGlobal.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  5. <title>EAGlobal</title>
  6. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  7. <meta name="author" content="Paul Pedriana">
  8. </head>
  9. <body bgcolor="#FFFFFF">
  10. <h1>EAGlobal</h1>
  11. <h2>Introduction</h2>
  12. <p>EAGlobal provides the GlobalPtr class. GlobalPtr acts as a reference to a pointer which is global throughout the process (includes the application and any loaded DLLs). The object pointed to must define a unique 32-bit kGlobalID if one is not given. The GlobalPtr class works in a way similar to a smart pointer, but note that it is not the same as your typical auto_ptr or anything else provided by C++ library vendors. The pointer is set to NULL on creation.<br>
  13. <br>
  14. Global pointers may be used from multiple threads once initialized to point to an object, but are _not_ thread-safe when being set. If you have a situation where two threads may attempt to set a global pointer at the same time, you should use OS globals (See EAOSGlobal.h) instead to serialize the creators on the OS global lock and prevent race conditions.<br>
  15. <br>
  16. A GlobalPtr is not the same thing as simply declaring a pointer at
  17. a globally accessible scope, especially on platforms with dynamic libraries such as Windows with its DLLs. A GlobalPtr allows multiple pieces of code to declare independent pointers to an object, even if the pieced of code are in independent DLLs.<br>
  18. <br>
  19. The pointer assigned to a GlobalPointer need not be a pointer allocated
  20. dynamically on the heap. It can just as well be the address of some static or local variable.</p>
  21. <h2>Example usage </h2>
  22. <p>Here we provide some basic example usage.</p>
  23. <pre class="code-example">GlobalPtr&lt;int, 0x11111111&gt; pInteger;
  24. GlobalPtr&lt;int, 0x11111111&gt; pInteger2;
  25. assert(pInteger == NULL);
  26. pInteger = new int[2];
  27. pInteger[0] = 10;
  28. pInteger[1] = 20;
  29. assert(pInteger2 == pInteger);
  30. assert(pInteger2[0] == pInteger[0]);
  31. delete[] pInteger;
  32. pInteger = NULL;
  33. assert(pInteger2 == NULL);
  34. </pre>
  35. <h2>Interface</h2>
  36. <p>The GlobalPtr class works like a smart pointer, but note that it is not the same as your typical auto_ptr or anything else provided by C++ library vendors. </p>
  37. <pre class="code-example">template&lt;class t, uint32_t kGlobalId = T::kGlobalId&gt;
  38. class GlobalPtr
  39. {
  40. public:
  41. <span class="code-example-comment"> /// this_type
  42. /// This is an alias for this class.
  43. </span> typedef GlobalPtr&lt;T, kGlobalId&gt; this_type;
  44. <span class="code-example-comment"> /// GlobalPtr
  45. ///
  46. /// Default constructor. Sets member pointer to whatever the
  47. /// shared version is. If this is the first usage of the shared
  48. /// version, the pointer will be set to NULL.
  49. ///
  50. /// Example usage:
  51. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass;
  52. ///
  53. </span> GlobalPtr();
  54. <span class="code-example-comment"> /// GlobalPtr (copy constructor)
  55. ///
  56. /// Default constructor. Sets member pointer to whatever the
  57. /// shared version is. If this is the first usage of the shared
  58. /// version, the pointer will be set to NULL.
  59. ///
  60. /// Example usage:
  61. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass1;
  62. /// pSomeClass1 = new pSomeClass;
  63. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass2(pSomeClass1);
  64. /// pSomeClass2-&gt;DoSomething();
  65. ///
  66. </span> explicit GlobalPtr(const this_type& globalPtr);
  67. <span class="code-example-comment"> /// operator =
  68. ///
  69. /// Example usage:
  70. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass1;
  71. /// pSomeClass1 = new pSomeClass;
  72. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass2(pSomeClass1);
  73. /// pSomeClass2->DoSomething();
  74. ///
  75. </span> this_type& operator=(const this_type& /*globalPtr*/);
  76. <span class="code-example-comment"> /// operator =
  77. ///
  78. /// Example usage:
  79. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass1;
  80. /// pSomeClass1 = new pSomeClass;
  81. /// delete pSomeClass1;
  82. /// pSomeClass1 = new pSomeClass;
  83. ///
  84. </span> this_type& operator=(T* p);
  85. <span class="code-example-comment"> /// operator T*
  86. ///
  87. /// Example usage:
  88. /// GlobalPtrlt;SomeClass, 0x12345678&gt; pSomeClass;
  89. /// FunctionWhichUsesSomeClassPtr(pSomeClass);
  90. ///
  91. </span> operator T*() const;
  92. <span class="code-example-comment"> /// operator T*
  93. ///
  94. /// Example usage:
  95. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass;
  96. /// CallFunctionWhichUsesSomeClassPtr(pSomeClass);
  97. ///
  98. </span> T& operator*() const;
  99. <span class="code-example-comment"> /// operator -&gt;
  100. ///
  101. /// Example usage:
  102. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass;
  103. /// pSomeClass-&gt;DoSomething();
  104. ///
  105. </span> T* operator-&gt;() const;
  106. <span class="code-example-comment"> /// operator !
  107. ///
  108. /// Example usage:
  109. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass;
  110. /// if(!pSomeClass)
  111. /// pSomeClass = new SomeClass;
  112. ///
  113. </span> bool operator!() const;
  114. <span class="code-example-comment"> /// get
  115. ///
  116. /// Returns the owned pointer.
  117. ///
  118. /// Example usage:
  119. /// GlobalPtr&lt;SomeClass, 0x12345678&gt; pSomeClass = new SomeClass;
  120. /// SomeClass* pSC = pSomeClass.get();
  121. /// pSC->DoSomething();
  122. ///
  123. </span> T* get() const;
  124. };</pre>
  125. <p></p>
  126. <hr>
  127. <p>&nbsp;</p>
  128. <p>&nbsp;</p>
  129. <p>&nbsp;</p>
  130. <p>&nbsp;</p>
  131. <p>&nbsp;</p>
  132. <p>&nbsp;</p>
  133. <p>&nbsp;</p>
  134. <p>&nbsp;</p>
  135. <p>&nbsp;</p>
  136. <p>&nbsp;</p>
  137. <p>&nbsp;</p>
  138. <p>&nbsp;</p>
  139. <p>&nbsp;</p>
  140. <p> </p>
  141. </body></html>