home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / macros / element2.tde < prev    next >
Encoding:
Text File  |  1996-02-22  |  7.8 KB  |  217 lines

  1. --
  2. --  Define the class for both key and element type
  3. --
  4. <code DEFINE_COLLECTION_CLASS>
  5. <define _MEMBER_TYPE_>IString\</define>
  6.  
  7. class $ELEMENT_CLASS$
  8. {
  9. //-----------------------------------------------------------------------------
  10. // Required members
  11. //-----------------------------------------------------------------------------
  12. public:
  13. <if ($DEFAULT_CONSTRUCTOR$)>
  14.      //------------------------------------------------------------------------
  15.      // Default constructor
  16.      //------------------------------------------------------------------------
  17.      $ELEMENT_CLASS$()
  18. <if ($KEY_PTR$)>
  19.           :iSelf(this)
  20. </if>
  21.      {
  22.      }
  23. </if>
  24. <if ($COPY_CONSTRUCTOR$)>
  25.      //------------------------------------------------------------------------
  26.      // Copy constructor
  27.      //------------------------------------------------------------------------
  28.      $ELEMENT_CLASS$($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
  29.         : iName(a$ELEMENT_CLASS$.iName)
  30. <if ($KEY_PTR$)>
  31.         , iSelf(this)
  32. </if>
  33.      {
  34.      }
  35. </if>
  36. <if ($ASSIGNMENT$)>
  37.      //------------------------------------------------------------------------
  38.      // Assignment operator
  39.      //------------------------------------------------------------------------
  40.      $ELEMENT_CLASS$& operator = ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
  41.      {
  42.         iName = a$ELEMENT_CLASS$.iName;
  43. <if  ($KEY_PTR$)>
  44.         iSelf = this;
  45. </if>
  46.         return *this;
  47.      }
  48. </if>
  49. <if (($K_EQUALITY$) | ($EQUALITY$)) >
  50.      //------------------------------------------------------------------------
  51.      // Equality test
  52.      //------------------------------------------------------------------------
  53.      IBoolean operator== ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$) const
  54.      {
  55.         return (iName == a$ELEMENT_CLASS$.iName);
  56.      }
  57. </if>
  58. <if ( ($ORDERING$) | ($K_ORDERING$) )>
  59.      //------------------------------------------------------------------------
  60.      // Ordering relation between $ELEMENT_CLASS$ objects.
  61.      //------------------------------------------------------------------------
  62.      IBoolean operator< ($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$) const
  63.      {
  64.         return (iName < a$ELEMENT_CLASS$.iName);
  65.      }
  66. </if>
  67. //-----------------------------------------------------------------------------
  68. // Additional members (customization)
  69. //-----------------------------------------------------------------------------
  70.      //------------------------------------------------------------------------
  71.      // Constructor to initialize the private memebers
  72.      //------------------------------------------------------------------------
  73.      $ELEMENT_CLASS$($_MEMBER_TYPE_$ name) :
  74.           iName(name)
  75. <if ($KEY_PTR$)>
  76.          ,iSelf(this)
  77. </if>
  78.      {
  79.      }
  80.  
  81.      //------------------------------------------------------------------------
  82.      // Overload the output operator to print out the contents of the
  83.      // collection. The following is an example of printing out the
  84.      // name member function of the $ELEMENT_CLASS$.
  85.      //------------------------------------------------------------------------
  86.      friend
  87.      ostream& operator<< (ostream& os, $ELEMENT_CLASS$ const& a$ELEMENT_CLASS$)
  88.      {
  89.        return os << a$ELEMENT_CLASS$.iName;
  90.      }
  91.  
  92.      //------------------------------------------------------------------------
  93.      // Define access function to the private members of the class
  94.      //------------------------------------------------------------------------
  95.      $_MEMBER_TYPE_$ const& name() const
  96.      {
  97.         return iName;
  98.      }
  99. <if ($KEY_ACCESS_COLLECTION$)>
  100.  
  101.      //------------------------------------------------------------------------
  102.      // Obtain the key from a given element.
  103.      //------------------------------------------------------------------------
  104.      $KEY_TYPE$ const& getKey() const
  105.      {
  106.           \<if ($KEY_PTR$)>
  107.           // We cannot return "this" because it'll break the c++ rules, so
  108.           // iSelf is used instead.
  109.           return iSelf;
  110.           \<else>
  111.           return *this;
  112.           \</if>
  113.      }
  114. </if>
  115.  
  116. private:
  117.      //------------------------------------------------------------------------
  118.      // Private members of the element type.
  119.      //------------------------------------------------------------------------
  120.      $_MEMBER_TYPE_$ iName;
  121. <if ($KEY_PTR$)>
  122.      $KEY_TYPE$ iSelf;
  123. </if>
  124.  
  125. }; // End of class $ELEMENT_CLASS$
  126. <if ( (($HASH_FUNCTION$) & ($K_HASH_FUNCTION$)) & \
  127.       ( (($KEY_PTR$) & (!$ELEMENT_PTR$)) | ((!$KEY_PTR$) & ($ELEMENT_PTR$)) ) )>
  128.  
  129. //-----------------------------------------------------------------------------
  130. // Need a two hash functions
  131. //-----------------------------------------------------------------------------
  132. inline unsigned long hash($ELEMENT_CLASS$ const& a$ELEMENT_CLASS$, unsigned long hashInput)
  133. {
  134.    return hash((const char*)a$ELEMENT_CLASS$.name(), hashInput);
  135. }
  136.  
  137. inline unsigned long hash($ELEMENT_CLASS$* const& a$ELEMENT_CLASS$, unsigned long hashInput)
  138. {
  139.    return hash((const char*)a$ELEMENT_CLASS$->name(), hashInput);
  140. }
  141. <else>\<if ($HASH_FUNCTION$)>
  142.  
  143. //-----------------------------------------------------------------------------
  144. // Need a hash function
  145. //-----------------------------------------------------------------------------
  146. inline unsigned long hash($ELEMENT_TYPE$ const& a$ELEMENT_CLASS$, unsigned long hashInput)
  147. {
  148. <if ($ELEMENT_PTR$)>
  149.    return hash((const char*)a$ELEMENT_CLASS$->name(), hashInput);
  150. <else>
  151.    return hash((const char*)a$ELEMENT_CLASS$.name(), hashInput);
  152. </if>
  153. }
  154. <else>\<if ($K_HASH_FUNCTION$)>
  155.  
  156. //-----------------------------------------------------------------------------
  157. // Need a hash function
  158. //-----------------------------------------------------------------------------
  159. inline unsigned long hash($KEY_TYPE$ const& a$KEY_CLASS$, unsigned long hashInput)
  160. {
  161. <if ($KEY_PTR$)>
  162.    return hash((const char*)a$KEY_CLASS$->name(), hashInput);
  163. <else>
  164.    return hash((const char*)a$KEY_CLASS$.name(), hashInput);
  165. </if>
  166. }
  167. </if>\</if>\</if>
  168.  
  169.  
  170. <if ($KEY_ACCESS_COLLECTION$)>
  171.  
  172. //-----------------------------------------------------------------------------
  173. // Define the Key access function. This function must be defined outside
  174. // the element class. It has one argument, whose type is the element type.
  175. // The return value of this function must be a const reference.
  176. //-----------------------------------------------------------------------------
  177. inline $KEY_TYPE$ const& key($ELEMENT_TYPE$ const& t)
  178. {
  179. // This function must call a member function that returns the key. In
  180. // this case the function is named get_key().
  181. // You can change always change it to call a different function.
  182. <if ($ELEMENT_PTR$)>
  183.    return t->getKey();
  184. <else>
  185.    return t.getKey();
  186. </if>
  187. }
  188. </if>
  189. <if (($ELEMENT_PTR$) & ($ORDERING$)) >
  190.  
  191. //-----------------------------------------------------------------------------
  192. // Define a comparison function
  193. //-----------------------------------------------------------------------------
  194. inline long compare($ELEMENT_TYPE$ const& e1, $ELEMENT_TYPE$ const& e2)
  195. {
  196.    if      (*e1 < *e2) return -1;
  197.    else if (*e2 < *e1) return 1;
  198.    else                return 0;
  199. }
  200. </if>
  201. <if (($ELEMENT_PTR$) & ($EQUALITY$)) >
  202.  
  203. //-----------------------------------------------------------------------------
  204. // Need an equality function
  205. //-----------------------------------------------------------------------------
  206. inline IBoolean equal($ELEMENT_TYPE$ const& e1, $ELEMENT_TYPE$ const& e2)
  207. {
  208.    return (*e1 == *e2);
  209. }
  210. </if>
  211.  
  212. //-----------------------------------------------------------------------------
  213. //  Collection Class declaration
  214. //-----------------------------------------------------------------------------
  215. typedef $CLTN_TYPE$<$ELEMENT_TYPE$, $KEY_TYPE$> $CLTN_NAME$;
  216. </code DEFINE_COLLECTION_CLASS>
  217.