pre-release commit
[asdcplib.git] / Identifier.h
1 /*
2 Copyright (c) 2005-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    Identifier.h
28     \version $Id$
29     \brief   strings of bytes that identify things
30 */
31
32 #ifndef _IDENTIFIER_H_
33 #define _IDENTIFIER_H_
34
35 namespace ASDCP
36 {
37   // the base of all identifier classes
38   template <ui32_t SIZE>
39     class Identifier : public IArchive
40     {
41     protected:
42       byte_t m_Value[SIZE];
43
44     public:
45       Identifier() {
46         memset(m_Value, 0, SIZE);
47       }
48
49       //
50       inline Result_t Set(const byte_t* value) {
51         ASDCP_TEST_NULL(value);
52         memcpy(m_Value, value, SIZE);
53         return RESULT_OK;
54       }
55
56       //
57       inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
58         Reader.ReadRaw(m_Value, SIZE);
59         return RESULT_OK;
60       }
61
62       //
63       inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
64         Writer.WriteRaw(m_Value, SIZE);
65         return RESULT_OK;
66       }
67
68       inline const byte_t* Data() const { return m_Value; }
69
70       inline ui32_t Size() const { return SIZE; }
71
72       //
73       inline bool operator<(const Identifier& rhs) const
74         {
75           for ( ui32_t i = 0; i < SIZE; i++ )
76             {
77               if ( m_Value[i] != rhs.m_Value[i] )
78                 return m_Value[i] < rhs.m_Value[i];
79             }
80
81           return false;
82         }
83
84       //
85       inline bool operator==(const Identifier& rhs) const
86       {
87         if ( rhs.Size() != SIZE )
88           return false;
89
90         return ( memcmp(m_Value, rhs.m_Value, SIZE) == 0 );
91       }
92
93       //
94       // todo: refactor characrer insertion back to bin2hex()
95       const char* ToString(char* str_buf) const
96         {
97           char* p = str_buf;
98
99           for ( ui32_t i = 0; i < SIZE; i++ )
100             {
101               *p = (m_Value[i] >> 4) & 0x0f;
102               *p += *p < 10 ? 0x30 : 0x61 - 10;
103               p++;
104
105               *p = m_Value[i] & 0x0f;
106               *p += *p < 10 ? 0x30 : 0x61 - 10;
107               p++;
108
109               *p = ' ';
110               p++;
111             }
112
113           *p = 0;
114           return str_buf;
115         }
116     };
117
118
119   class UL;
120   class UUID;
121
122   // UID - either a UL or a UUID
123   class UID : public Identifier<SMPTE_UL_LENGTH>
124     {
125       friend class ASDCP::UL;
126       friend class ASDCP::UUID;
127
128     public:
129       UID() {}
130       UID(const UID& rhs) {
131         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
132       }
133     };
134
135   // Universal Label
136   class UL : public Identifier<SMPTE_UL_LENGTH>
137     {
138     public:
139       UL() {}
140       UL(const UL& rhs) {
141         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
142       }
143
144       UL(const UID& rhs) {
145         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
146       }
147
148       UL(const byte_t* value) {
149         assert(value);
150         memcpy(m_Value, value, SMPTE_UL_LENGTH);
151       }
152
153       bool operator==(const UL& rhs) const {
154         return ( memcmp(m_Value, rhs.m_Value, SMPTE_UL_LENGTH) == 0 ) ? true : false;
155       }
156     };
157
158   // UUID
159   class UUID : public Identifier<SMPTE_UL_LENGTH>
160     {
161     public:
162       UUID() {}
163       UUID(const UUID& rhs) {
164         memcpy(m_Value, rhs.m_Value, SMPTE_UL_LENGTH);
165       }
166 #if 0
167       UUID(const UID& rhs) {
168         memcpy(m_Value, rhs.m_Value + 8, 8);
169         memcpy(m_Value + 8, rhs.m_Value, 8);
170       }
171 #endif
172       void GenRandomValue();
173     };
174
175   // UMID
176   class UMID : public Identifier<SMPTE_UMID_LENGTH>
177     {
178     public:
179       UMID() {}
180       UMID(const UMID &rhs) {
181         memcpy(m_Value, rhs.m_Value, SMPTE_UMID_LENGTH);
182       };
183
184       void MakeUMID(int Type);
185
186       void MakeUMID(int Type, const UUID& ID);
187
188       void SetMaterial(UL& aUL);
189
190       void SetInstance(int Instance, int Method = -1);
191
192       ui32_t GetInstance(void) const
193         {
194           assert(0);
195           return ( m_Value[13] << 16 ) | ( m_Value[14] << 8 ) | m_Value[15];
196         }
197     };
198
199 } // namespace mxflib
200
201 #endif // _IDENTIFIER_H_
202
203 //
204 // end Identifier.h
205 //