home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1996 November / VPR9611B.ISO / vabasic / ntclnt.exe / DISK8 / data.8 / datab / INCLUDE / VATOM.H < prev    next >
C/C++ Source or Header  |  1996-07-29  |  8KB  |  369 lines

  1. /*****************************************************************************
  2.  ***   $Source: /rcs/crcs/general/vatom.h,v $
  3.  ***   Checked int by: $Author: alaind $
  4.  ***   $Date: 1996/03/19 15:32:58 $
  5.  ***   $Revision: 1.12 $
  6.  *****************************************************************************
  7.  ***                                       ***
  8.  ***          Copyright (c) 1993, Visual Edge Software Ltd.           ***
  9.  ***                                        ***
  10.  ***   All rights reserved.  This notice is  intended  as  a  precaution   ***
  11.  ***   against    inadvertent publication, and shall not be deemed to con-   ***
  12.  ***   stitute an acknowledgment that publication has  occurred     nor  to   ***
  13.  ***   imply  any  waiver  of confidentiality.    The year included in the   ***
  14.  ***   notice is the year of the creation of the work.               ***
  15.  ***                                        ***
  16.  *****************************************************************************/
  17.  
  18. #ifndef VATOM_H
  19. #define VATOM_H
  20.  
  21. #ifdef VSYS_WIN16
  22. // Shorten the names of these enums and typedefs so that MSVC
  23. // IMPLIB does not crash because of identifiers longer than
  24. // 128 chars.
  25. #define VTSearchCaseEnum    VTSCaseE
  26. #endif
  27.  
  28. #ifndef __cplusplus
  29.  
  30. struct VAtomStruct;
  31. typedef VAtomStruct *VTAtomP;
  32.  
  33. #else
  34.  
  35. #include <dllclass.hh>
  36. #include <vwcstr.hh>
  37.  
  38. class VeAtom;
  39. typedef VeAtom *VTAtomP;
  40.  
  41. //=========================================================================
  42. // VTSearchCase
  43. //     This enumeration is used to specify how to perform name
  44. // searches when dealing with atoms.  This enumeration allows
  45. // hash tables, arrays, etc. to take the search type as an argument.
  46. //-------------------------------------------------------------------------
  47.  
  48. typedef enum VTSearchCaseEnum
  49. {
  50.     kVCaseSensitive,    // Name search is case sensitive.
  51.     kVCaseInsensitive    // Name search is case insensitive.
  52. } VTSearchCase;
  53.  
  54. //-----------------------------------------------------------------------------
  55. // Internal atom representation
  56. // These objects should only be used as atom pointers by
  57. // outside code.
  58.  
  59. class VeAtom : public VeDllBasedClass
  60. {
  61.     public:
  62.  
  63.     bool_t        Equal(VTAtomP);
  64.     bool_t        CaselessEqual(VTAtomP);
  65.  
  66.     bool_t        Equal(VTAtomP, VTSearchCase);
  67.  
  68.     VTAtomP        Caseless();
  69.     const char    *String();
  70.     const wchar_t    *WideString();
  71.  
  72.     private:
  73.  
  74.     VeAtom(    const char    *str,
  75.         VTAtomP        caseless,
  76.         unsigned    len,
  77.         unsigned    hash);
  78.  
  79.     VeAtom(    const wchar_t    *str,
  80.         VTAtomP        caseless,
  81.         unsigned    len,
  82.         unsigned    hash);
  83.  
  84.     // Put an inline destructor so that nobody can delete
  85.     // a VeAtom except those who are allowed to do so.
  86.     ~VeAtom() { }
  87.  
  88.     bool_t        SameAs(const char *str, unsigned len);
  89.     bool_t        SameAs(const wchar_t *str, unsigned len);
  90.  
  91.     unsigned    HashIndex();
  92.  
  93.     friend class    VeAtomManager;
  94.     friend class    VeAtomHashTable;
  95.  
  96.     unsigned    itsLength;
  97.  
  98.     char        *itsString;
  99.     wchar_t        *itsWideString;
  100.  
  101.     VTAtomP        itsCaselessAtom;
  102.     VTAtomP        itsNext;
  103.  
  104.     unsigned    itsHashIndex;
  105. };
  106.  
  107. //-----------------------------------------------------------------------------
  108. // C++ castable atom class.
  109. // This should be used for return values and arguments to functions
  110. // This class is able to cast to/from VeString and const char *.
  111.  
  112. class VeAtomRef : public VeDllBasedClass
  113. {
  114.     public:
  115.  
  116.     VeAtomRef();
  117.     VeAtomRef(const char *);
  118.     VeAtomRef(const wchar_t *);
  119.     VeAtomRef(const VeString&);
  120.     VeAtomRef(const VwcString&);
  121.     VeAtomRef(VTAtomP);
  122.     VeAtomRef(const VeAtomRef &);
  123.  
  124.     ~VeAtomRef();
  125.  
  126.     VeAtomRef &operator=(const VeAtomRef &other);
  127.  
  128.     operator const char *() const;
  129.     operator const wchar_t *() const;
  130.     operator VTAtomP() const;
  131.  
  132.     bool_t        Equal(const VeAtomRef&) const;
  133.     bool_t        CaselessEqual(const VeAtomRef&) const;
  134.  
  135.     bool_t        Equal(const VeAtomRef&, VTSearchCase) const;
  136.  
  137.     VeAtomRef    Caseless() const;
  138.  
  139.     bool_t        IsValid() const;
  140.  
  141.     protected:
  142.  
  143.     VTAtomP        itsAtom;
  144. };
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Class which manages collections of atoms.
  148. // There should be only one instance of this class per process and it
  149. // should never be deleted (until the process dies).
  150. // The instance is controlled by LookupAtom.
  151.  
  152. class VeAtomManager : public VeDllBasedClass
  153. {
  154.     public:
  155.  
  156.     // only LookupAtom should use these public methods.
  157.  
  158.     VeAtomManager();
  159.  
  160.     VTAtomP Lookup(const char *);
  161.     VTAtomP Lookup(const wchar_t *);
  162.  
  163.     protected:
  164.  
  165.     VTAtomP    Install(VTAtomP        caseless,
  166.             const char    *str,
  167.             unsigned    len,
  168.             unsigned    hash);
  169.  
  170.     VTAtomP    Install(VTAtomP        caseless,
  171.             const wchar_t    *str,
  172.             unsigned    len,
  173.             unsigned    hash);
  174.  
  175.     unsigned    Hash(    const char    *str,
  176.                 unsigned    &len);
  177.  
  178.     unsigned    Hash(    const wchar_t    *str,
  179.                 unsigned    &len);
  180.  
  181.     VTAtomP        Find(    const char    *str,
  182.                 unsigned    &len,
  183.                 unsigned    &hash);
  184.  
  185.     VTAtomP        Find(    const wchar_t    *str,
  186.                 unsigned    &len,
  187.                 unsigned    &hash);
  188.  
  189.     VeAtom        **itsAtoms;
  190. };
  191.  
  192. extern "C"
  193. {
  194.  
  195. #endif /* __cplusplus */
  196.  
  197. /* C callable entry points for using Atoms */
  198.  
  199. #define VaAtomsEqual(a, b)    ((a) == (b))
  200.  
  201. #define VaAtomsCaseEqual(a, b, cs)            \
  202.     ( ((cs) == kVCaseSensitive) ?            \
  203.         ((a) == (b)) :                \
  204.         (((a) ? VeCaselessAtom(a) : 0) ==    \
  205.          ((b) ? VeCaselessAtom(b) : 0)) )
  206.  
  207. VFUNCDECL(VTAtomP)        VeLookupAtom(const char *);
  208. VFUNCDECL(VTAtomP)        VeLookupWideAtom(const wchar_t *);
  209.  
  210. VFUNCDECL(VTAtomP)        VeCaselessAtom(VTAtomP);
  211.  
  212. VFUNCDECL(const char*)        VeGetAtomString(VTAtomP);
  213. VFUNCDECL(const wchar_t*)    VeGetAtomWideString(VTAtomP);
  214.  
  215. #ifdef __cplusplus
  216.  
  217. }
  218.  
  219. //=============================================================================
  220. // Inline methods of VeAtom
  221. //-----------------------------------------------------------------------------
  222.  
  223. inline bool_t VeAtom::Equal(VTAtomP cmp)
  224. {
  225.     return this == cmp;
  226. }
  227.  
  228. inline bool_t VeAtom::CaselessEqual(VTAtomP cmp)
  229. {
  230.     return    (this ? this->itsCaselessAtom : 0) ==
  231.         (cmp ? cmp->itsCaselessAtom : 0);
  232. }
  233.  
  234. inline bool_t VeAtom::Equal(VTAtomP cmp, VTSearchCase scase)
  235. {
  236.     if(scase == kVCaseSensitive)
  237.         return Equal(cmp);
  238.     else
  239.         return CaselessEqual(cmp);
  240. }
  241.  
  242. inline VTAtomP VeAtom::Caseless()
  243. {
  244.     return itsCaselessAtom;
  245. }
  246.  
  247. inline unsigned VeAtom::HashIndex()
  248. {
  249.     return itsHashIndex;
  250. }
  251.  
  252. inline const char *VeAtom::String()
  253. {
  254.     return itsString;
  255. }
  256.  
  257. inline const wchar_t *VeAtom::WideString()
  258. {
  259.     return itsWideString;
  260. }
  261.  
  262. //=============================================================================
  263. // Inline methods of VeAtomRef
  264. //-----------------------------------------------------------------------------
  265.  
  266. inline VeAtomRef::VeAtomRef()
  267. {
  268.     itsAtom = 0;
  269. }
  270.  
  271. inline VeAtomRef::VeAtomRef(const char *string)
  272. {
  273.     itsAtom = VeLookupAtom(string);
  274. }
  275.  
  276. inline VeAtomRef::VeAtomRef(const wchar_t *string)
  277. {
  278.     itsAtom = VeLookupWideAtom(string);
  279. }
  280.  
  281. inline VeAtomRef::VeAtomRef(const VeString& string)
  282. {
  283.     itsAtom = VeLookupAtom((const char *)string);
  284. }
  285.  
  286. inline VeAtomRef::VeAtomRef(const VwcString& string)
  287. {
  288.     itsAtom = VeLookupWideAtom((const wchar_t *)string);
  289. }
  290.  
  291. inline VeAtomRef::VeAtomRef(VTAtomP atom)
  292. {
  293.     itsAtom = atom;
  294. }
  295.  
  296. inline VeAtomRef::VeAtomRef(const VeAtomRef &from)
  297. {
  298.     itsAtom = from.itsAtom;
  299. }
  300.  
  301. inline VeAtomRef::~VeAtomRef()
  302. {
  303. }
  304.  
  305. inline VeAtomRef &VeAtomRef::operator=(const VeAtomRef &other)
  306. {
  307.     itsAtom = other.itsAtom;
  308.     return *this;
  309. }
  310.  
  311. inline VeAtomRef::operator VTAtomP() const
  312. {
  313.     return itsAtom;
  314. }
  315.  
  316. inline VeAtomRef::operator const char *() const
  317. {
  318.     if(itsAtom)
  319.         return itsAtom->String();
  320.     else
  321.         return "";
  322. }
  323.  
  324. inline VeAtomRef::operator const wchar_t *() const
  325. {
  326.     if(itsAtom)
  327.         return itsAtom->WideString();
  328.     else
  329.         return kVwcEmpty;
  330. }
  331.  
  332. inline bool_t VeAtomRef::Equal(const VeAtomRef &atom) const
  333. {
  334.     // Note that Equal is able to a NULL 'this'
  335.     return itsAtom->Equal(atom);
  336. }
  337.  
  338. inline bool_t VeAtomRef::CaselessEqual(const VeAtomRef &atom) const
  339. {
  340.     // Note that CaselessEqual is able to a NULL 'this'
  341.     return itsAtom->CaselessEqual(atom);
  342. }
  343.  
  344. inline bool_t VeAtomRef::Equal(const VeAtomRef &atom, VTSearchCase scase) const
  345. {
  346.     // Note that Equal is able to a NULL 'this'
  347.     return itsAtom->Equal(atom, scase);
  348. }
  349.  
  350. inline VeAtomRef VeAtomRef::Caseless() const
  351. {
  352.     if(itsAtom)
  353.         return itsAtom->Caseless();
  354.     else
  355.         return VeAtomRef();
  356. }
  357.  
  358. inline bool_t VeAtomRef::IsValid() const
  359. {
  360.     if(itsAtom)
  361.         return TRUE;
  362.     else
  363.         return FALSE;
  364. }
  365.  
  366. #endif /* __cplusplus */
  367.  
  368. #endif /* VATOM_H */
  369.