wheee!
[asdcplib.git] / Identifier.h
1 //
2 // Identifier.h
3 //
4
5 #ifndef _IDENTIFIER_H_
6 #define _IDENTIFIER_H_
7
8 namespace ASDCP
9 {
10   // the base of all identifier classes
11   template <ui32_t SIZE>
12     class Identifier : public IArchive
13     {
14     protected:
15       byte_t m_Value[SIZE];
16
17     public:
18       Identifier() {
19         memset(m_Value, 0, SIZE);
20       }
21
22       //
23       inline Result_t Set(const byte_t* value) {
24         ASDCP_TEST_NULL(value);
25         memcpy(m_Value, value, SIZE);
26         return RESULT_OK;
27       }
28
29       //
30       inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
31         Reader.ReadRaw(m_Value, SIZE);
32         return RESULT_OK;
33       }
34
35       //
36       inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
37         Writer.WriteRaw(m_Value, SIZE);
38         return RESULT_OK;
39       }
40
41       inline const byte_t* Data() const { return m_Value; }
42
43       inline ui32_t Size() const { return SIZE; }
44
45       //
46       inline bool operator<(const Identifier& rhs) const
47         {
48           for ( ui32_t i = 0; i < SIZE; i++ )
49             {
50               if ( m_Value[i] != rhs.m_Value[i] )
51                 return m_Value[i] < rhs.m_Value[i];
52             }
53
54           return false;
55         }
56
57       //
58       inline bool operator==(const Identifier& rhs) const
59       {
60         if ( rhs.Size() != SIZE )
61           return false;
62
63         return ( memcmp(m_Value, rhs.m_Value, SIZE) == 0 );
64       }
65
66       //
67       // todo: refactor characrer insertion back to bin2hex()
68       const char* ToString(char* str_buf) const
69         {
70           char* p = str_buf;
71
72           for ( ui32_t i = 0; i < SIZE; i++ )
73             {
74               *p = (m_Value[i] >> 4) & 0x0f;
75               *p += *p < 10 ? 0x30 : 0x61 - 10;
76               p++;
77
78               *p = m_Value[i] & 0x0f;
79               *p += *p < 10 ? 0x30 : 0x61 - 10;
80               p++;
81
82               *p = ' ';
83               p++;
84             }
85
86           *p = 0;
87           return str_buf;
88         }
89     };
90
91
92   class UL;
93   class UUID;
94
95   // UID - either a UL or a UUID
96   class UID : public Identifier<SMPTE_UL_LENGTH>
97     {
98       friend class ASDCP::UL;
99       friend class ASDCP::UUID;
100
101     public:
102       UID() {}
103       UID(const UID& rhs) {
104         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
105       }
106     };
107
108   // Universal Label
109   class UL : public Identifier<SMPTE_UL_LENGTH>
110     {
111     public:
112       UL() {}
113       UL(const UL& rhs) {
114         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
115       }
116
117       UL(const UID& rhs) {
118         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
119       }
120
121       UL(const byte_t* value) {
122         assert(value);
123         memcpy(m_Value, value, SMPTE_UL_LENGTH);
124       }
125
126       bool operator==(const UL& rhs) const {
127         return ( memcmp(m_Value, rhs.m_Value, SMPTE_UL_LENGTH) == 0 ) ? true : false;
128       }
129     };
130
131   // UUID
132   class UUID : public Identifier<SMPTE_UL_LENGTH>
133     {
134     public:
135       UUID() {}
136       UUID(const UUID& rhs) {
137         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
138       }
139 #if 0
140       UUID(const UID& rhs) {
141         memcpy(m_Value, rhs.m_Value + 8, 8);
142         memcpy(m_Value + 8, rhs.m_Value, 8);
143       }
144 #endif
145       void GenRandomValue();
146     };
147
148   // UMID
149   class UMID : public Identifier<SMPTE_UMID_LENGTH>
150     {
151     public:
152       UMID() {}
153       UMID(const UMID &rhs) {
154         memcpy(m_Value, rhs.m_Value, SMPTE_UMID_LENGTH);
155       };
156
157       void MakeUMID(int Type);
158
159       void MakeUMID(int Type, const UUID& ID);
160
161       void SetMaterial(UL& aUL);
162
163       void SetInstance(int Instance, int Method = -1);
164
165       ui32_t GetInstance(void) const
166         {
167           assert(0);
168           return ( m_Value[13] << 16 ) | ( m_Value[14] << 8 ) | m_Value[15];
169         }
170     };
171
172 } // namespace mxflib
173
174 #endif // _IDENTIFIER_H_
175
176 //
177 // end Identifier.h
178 //