MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UUID.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  UUID.h ID System for Elements
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef UUID_H
28 #define UUID_H
29 
30 #include "itype.h"
32 #include "ZStringByteSource.h"
33 #include "ByteSerializable.h"
34 #include <string.h> /* For strlen, strncpy */
35 
36 #define MFM_UUID_FOR(label, apiVersion) UUID(label,(u32) apiVersion, (u32) MFM_BUILD_DATE, (u32) MFM_BUILD_TIME, UUID::ComputeConfigurationCode<CC>())
37 
38 namespace MFM {
39 
44  class UUID : public ByteSerializable {
45  public:
46 
47  UUID() : m_configurationCode(0), m_apiVersion(0), m_hexDate(0), m_hexTime(0)
48  {
49  m_label.Reset();
50  m_label.GetZString();
51  }
52 
53  template <class CC>
54  static u32 ComputeConfigurationCode() {
55  u32 val = 0;
56  val = (val<<4) + CC::ATOM_TYPE::ATOM_CATEGORY;
57  val = (val<<4) + CC::PARAM_CONFIG::EVENT_WINDOW_RADIUS;
58  val = (val<<4) + CC::PARAM_CONFIG::ELEMENT_TABLE_BITS;
59  val = (val<<4) + CC::PARAM_CONFIG::ELEMENT_DATA_SLOTS;
60  val = (val<<8) + CC::PARAM_CONFIG::TILE_WIDTH;
61  val = (val<<8) + CC::PARAM_CONFIG::BITS_PER_ATOM;
62  return val;
63  }
64 
65  UUID(const char * label, const u32 apiVersion, const u32 decDate, const u32 decTime, const u32 configCode)
66  : m_configurationCode(configCode),
67  m_apiVersion(apiVersion),
68  m_hexDate(decDate), m_hexTime(decTime)
69  {
70  if (!label)
71  FAIL(NULL_POINTER);
72  if (!LegalLabel(label))
73  FAIL(ILLEGAL_ARGUMENT);
74 
75  m_label.Reset();
76  m_label.Print(label);
77  m_label.GetZString(); // Ensure label is null-terminated
78  }
79 
85  static bool LegalLabel(const char * label)
86  {
87  if (!label)
88  return false;
89 
90  OString64 buf;
91 
92  ZStringByteSource zbs(label);
93  if (!zbs.ScanCamelIdentifier(buf))
94  return false;
95 
96  if (zbs.Read() >= 0) // Need EOF now
97  return false;
98 
99  if (buf.HasOverflowed())
100  return false;
101 
102  return true;
103  }
104 
105  static bool LegalFilename(const char* label)
106  {
107  ZStringByteSource zbs(label);
108 
109  UUID temp;
110  return 4==zbs.Scanf("%@.so",&temp);
111  }
112 
113  UUID(ByteSource & bs) ;
114 
115  const char * GetLabel() const {
116  return m_label.GetBuffer(); // m_label writers have ensured null-termination
117  }
118  u32 GetVersion() const { return m_apiVersion; }
119  u32 GetHexDate() const { return m_hexDate; }
120  u32 GetHexTime() const { return m_hexTime; }
121  u32 GetConfigurationCode() const { return m_configurationCode; }
122 
123  bool CompatibleConfigurationCode(const UUID & other) const ;
124  bool CompatibleLabel(const UUID & other) const ;
125  bool CompatibleAPIVersion(const UUID & other) const ;
126  bool CompatibleButStrictlyNewer(const UUID & other) const ;
127 
128  bool Compatible(const UUID & other) const ; //< CompatibleAPIVersion plus not older date
129 
130  void Print(ByteSink & bs) const ;
131 
132  Result PrintTo(ByteSink & bs, s32 argument = 0) {
133  Print(bs);
134  return SUCCESS;
135  }
136 
137  bool Read(ByteSource & bs) ;
138 
139  Result ReadFrom(ByteSource & bs, s32 argument = 0) {
140  if (Read(bs))
141  return SUCCESS;
142  return FAILURE;
143  }
144 
145  bool Equals(const UUID & other) const {
146  return *this == other;
147  }
148 
149  bool operator==(const UUID & other) const ;
150 
151  bool operator!=(const UUID & other) const {
152  return !(*this == other);
153  }
154 
155  virtual ~UUID() { }
156 
157  private:
158 
159  s32 CompareDateOnly(const UUID & other) const ;
160 
161  OString64 m_label;
162  u32 m_configurationCode;
163  u32 m_apiVersion;
164  u32 m_hexDate;
165  u32 m_hexTime;
166 
167  };
168 
169 }
170 
171 #endif /* UUID_H */
Definition: ZStringByteSource.h:40
bool ScanCamelIdentifier(ByteSink &result)
Definition: ByteSource.h:331
void Reset()
Definition: OverflowableCharBufferByteSink.h:192
Result PrintTo(ByteSink &bs, s32 argument=0)
Definition: UUID.h:132
const char * GetZString()
Definition: OverflowableCharBufferByteSink.h:143
Definition: ByteSource.h:44
bool HasOverflowed() const
Definition: OverflowableCharBufferByteSink.h:206
static bool LegalLabel(const char *label)
Definition: UUID.h:85
Definition: ByteSink.h:47
void Print(const char *str, s32 fieldWidth=-1, u8 padChar= ' ')
Definition: ByteSink.cpp:31
const char * GetBuffer() const
Definition: OverflowableCharBufferByteSink.h:158
Result ReadFrom(ByteSource &bs, s32 argument=0)
Definition: UUID.h:139
s32 Read()
Definition: ByteSource.h:66
Definition: UUID.h:44
Definition: ByteSerializable.h:45