moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / guid.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file guid.h
3 **      \brief Template Header
4 **
5 **      $Id: guid.h,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SYNFIG_GUID_H
25 #define __SYNFIG_GUID_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include "string.h"
30 #include <stdint.h>
31 #include <cassert>
32
33 /* === M A C R O S ========================================================= */
34
35 /* === T Y P E D E F S ===================================================== */
36
37 /* === C L A S S E S & S T R U C T S ======================================= */
38
39 namespace synfig {
40         
41 class GUID
42 {
43         union {
44                 struct {
45                         unsigned int a;
46                         unsigned int b;
47                         unsigned int c;
48                         unsigned int d;
49                 } u_32;
50                 struct {
51                         uint64_t a;
52                         uint64_t b;
53                 } u_64;
54                         
55         } data;
56         
57 public:
58         GUID()
59                 { make_unique(); }      
60         GUID(const GUID& x):data(x.data)
61                 { }
62         GUID(const int i){assert(!i); data.u_64.a=0;data.u_64.b=0;}
63
64         GUID(const String& str);
65         
66         static GUID zero() { return GUID(0); }
67         static GUID hasher(const String& str);
68         static GUID hasher(int i);
69
70         operator bool()const { return data.u_32.a||data.u_32.b||data.u_32.c||data.u_32.d; }
71         
72         uint64_t get_hi()const { return data.u_64.a; }
73         uint64_t get_lo()const { return data.u_64.b; }
74
75         uint64_t get_hi_hi()const { return data.u_32.a; }
76         uint64_t get_hi_lo()const { return data.u_32.b; }
77         uint64_t get_lo_hi()const { return data.u_32.c; }
78         uint64_t get_lo_lo()const { return data.u_32.d; }
79         
80         void make_unique();
81         String get_string()const;
82         
83         bool operator==(const GUID& rhs)const
84                 { return data.u_64.a==rhs.data.u_64.a && data.u_64.b==rhs.data.u_64.b; }
85         bool operator!=(const GUID& rhs)const
86                 { return data.u_64.a!=rhs.data.u_64.a || data.u_64.b!=rhs.data.u_64.b; }
87         bool operator<(const GUID& rhs)const
88                 { return (data.u_64.a==rhs.data.u_64.a)?(data.u_64.b<rhs.data.u_64.b):(data.u_64.a<rhs.data.u_64.a); }
89         bool operator>(const GUID& rhs)const
90                 { return (data.u_64.a==rhs.data.u_64.a)?(data.u_64.b>rhs.data.u_64.b):(data.u_64.a>rhs.data.u_64.a); }
91         bool operator<=(const GUID& rhs)const
92                 { return operator<(rhs) || operator==(rhs); }
93         bool operator>=(const GUID& rhs)const
94                 { return operator>(rhs) || operator==(rhs); }
95
96         //! Operator '^' (xor)
97         /*! If A ^ B == C, then C ^ B == A and B ^ A == C.
98         **      Also keep in mind that A ^ A == 0 and A ^ B ^ B = A. */
99         GUID& operator^=(const GUID& rhs)
100         {
101                 data.u_32.a^=rhs.data.u_32.a;
102                 data.u_32.b^=rhs.data.u_32.b;
103                 data.u_32.c^=rhs.data.u_32.c;
104                 data.u_32.d^=rhs.data.u_32.d;
105                 return *this;
106         }
107         GUID operator^(const GUID& rhs)const { return GUID(*this)^=rhs; }
108         
109         //! Operator '%' (alt-xor)
110         /*! A % B != B % A. */
111         GUID& operator%=(const GUID& rhs)
112         {
113                 data.u_32.a^=rhs.data.u_32.b;
114                 data.u_32.b^=rhs.data.u_32.c;
115                 data.u_32.c^=rhs.data.u_32.d;
116                 data.u_32.d^=rhs.data.u_32.a;
117                 return *this;
118         }
119         GUID operator%(const GUID& rhs)const { return GUID(*this)%=rhs; }
120
121 };
122
123 class GUIDHash
124 {
125 public:
126         size_t operator()(const GUID& guid)const
127         {
128                 return
129                         guid.get_hi_hi()+
130                         guid.get_hi_lo()+
131                         guid.get_lo_hi()+
132                         guid.get_lo_lo();
133         }
134 };
135
136 };
137
138 /* === E N D =============================================================== */
139
140 #endif