Skip to content

Commit

Permalink
[C++] Refactor Guid to use std::array instead of std::vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking committed Oct 25, 2021
1 parent 23f93e0 commit 1662d2f
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 141 deletions.
2 changes: 2 additions & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,5 @@ YYYY/MM/DD, github id, Full name, email
2021/08/08, ansiemens, Yi-Hong Lin, ansiemens@gmail.com
2021/09/08, jmcken8, Joel McKenzie, joel.b.mckenzie@gmail.com
2021/10/10, tools4origins, Erwan Guyomarc'h, contact@erwan-guyomarch.fr
2021/10/25, jcking, Justin King, jcking@google.com

2 changes: 1 addition & 1 deletion runtime/Cpp/runtime/src/antlr4-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
#endif
#endif

#include "support/guid.h"
#include "support/Guid.h"
#include "support/Declarations.h"

#if !defined(HAS_NOEXCEPT)
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cpp/runtime/src/antlr4-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
#include "support/BitSet.h"
#include "support/CPPUtils.h"
#include "support/StringUtils.h"
#include "support/guid.h"
#include "support/Guid.h"
#include "tree/AbstractParseTreeVisitor.h"
#include "tree/ErrorNode.h"
#include "tree/ErrorNodeImpl.h"
Expand Down
30 changes: 15 additions & 15 deletions runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,32 @@ ATNDeserializer::~ATNDeserializer() {
* This value should never change. Updates following this version are
* reflected as change in the unique ID SERIALIZED_UUID.
*/
Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS() {
return Guid("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61");
antlrcpp::Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS() {
return antlrcpp::Guid("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61");
}

Guid ATNDeserializer::ADDED_LEXER_ACTIONS() {
return Guid("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E");
antlrcpp::Guid ATNDeserializer::ADDED_LEXER_ACTIONS() {
return antlrcpp::Guid("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E");
}

Guid ATNDeserializer::ADDED_UNICODE_SMP() {
return Guid("59627784-3BE5-417A-B9EB-8131A7286089");
antlrcpp::Guid ATNDeserializer::ADDED_UNICODE_SMP() {
return antlrcpp::Guid("59627784-3BE5-417A-B9EB-8131A7286089");
}

Guid ATNDeserializer::SERIALIZED_UUID() {
antlrcpp::Guid ATNDeserializer::SERIALIZED_UUID() {
return ADDED_UNICODE_SMP();
}

Guid ATNDeserializer::BASE_SERIALIZED_UUID() {
return Guid("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3");
antlrcpp::Guid ATNDeserializer::BASE_SERIALIZED_UUID() {
return antlrcpp::Guid("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3");
}

std::vector<Guid>& ATNDeserializer::SUPPORTED_UUIDS() {
static std::vector<Guid> singleton = { BASE_SERIALIZED_UUID(), ADDED_PRECEDENCE_TRANSITIONS(), ADDED_LEXER_ACTIONS(), ADDED_UNICODE_SMP() };
std::vector<antlrcpp::Guid>& ATNDeserializer::SUPPORTED_UUIDS() {
static std::vector<antlrcpp::Guid> singleton = { BASE_SERIALIZED_UUID(), ADDED_PRECEDENCE_TRANSITIONS(), ADDED_LEXER_ACTIONS(), ADDED_UNICODE_SMP() };
return singleton;
}

bool ATNDeserializer::isFeatureSupported(const Guid &feature, const Guid &actualUuid) {
bool ATNDeserializer::isFeatureSupported(const antlrcpp::Guid &feature, const antlrcpp::Guid &actualUuid) {
auto featureIterator = std::find(SUPPORTED_UUIDS().begin(), SUPPORTED_UUIDS().end(), feature);
if (featureIterator == SUPPORTED_UUIDS().end()) {
return false;
Expand Down Expand Up @@ -169,7 +169,7 @@ ATN ATNDeserializer::deserialize(const std::vector<uint16_t>& input) {
throw UnsupportedOperationException(reason);
}

Guid uuid = toUUID(data.data(), p);
antlrcpp::Guid uuid = toUUID(data.data(), p);
p += 8;
auto uuidIterator = std::find(SUPPORTED_UUIDS().begin(), SUPPORTED_UUIDS().end(), uuid);
if (uuidIterator == SUPPORTED_UUIDS().end()) {
Expand Down Expand Up @@ -628,8 +628,8 @@ void ATNDeserializer::checkCondition(bool condition, const std::string &message)
}
}

Guid ATNDeserializer::toUUID(const unsigned short *data, size_t offset) {
return Guid((uint16_t *)data + offset, true);
antlrcpp::Guid ATNDeserializer::toUUID(const unsigned short *data, size_t offset) {
return antlrcpp::Guid((uint16_t *)data + offset, true);
}

/* mem check: all created instances are freed in the d-tor of the ATNState they are added to. */
Expand Down
16 changes: 8 additions & 8 deletions runtime/Cpp/runtime/src/atn/ATNDeserializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ namespace atn {

/// This is the current serialized UUID.
// ml: defined as function to avoid the “static initialization order fiasco”.
static Guid SERIALIZED_UUID();
static antlrcpp::Guid SERIALIZED_UUID();

ATNDeserializer();
ATNDeserializer(const ATNDeserializationOptions& dso);
virtual ~ATNDeserializer();

static Guid toUUID(const unsigned short *data, size_t offset);
static antlrcpp::Guid toUUID(const unsigned short *data, size_t offset);

virtual ATN deserialize(const std::vector<uint16_t> &input);
virtual void verifyATN(const ATN &atn);
Expand All @@ -54,35 +54,35 @@ namespace atn {
/// <returns> {@code true} if the {@code actualUuid} value represents a
/// serialized ATN at or after the feature identified by {@code feature} was
/// introduced; otherwise, {@code false}. </returns>
virtual bool isFeatureSupported(const Guid &feature, const Guid &actualUuid);
virtual bool isFeatureSupported(const antlrcpp::Guid &feature, const antlrcpp::Guid &actualUuid);
void markPrecedenceDecisions(const ATN &atn);
Ref<LexerAction> lexerActionFactory(LexerActionType type, int data1, int data2);

private:
/// This is the earliest supported serialized UUID.
static Guid BASE_SERIALIZED_UUID();
static antlrcpp::Guid BASE_SERIALIZED_UUID();

/// This UUID indicates an extension of <seealso cref="BASE_SERIALIZED_UUID"/> for the
/// addition of precedence predicates.
static Guid ADDED_PRECEDENCE_TRANSITIONS();
static antlrcpp::Guid ADDED_PRECEDENCE_TRANSITIONS();

/**
* This UUID indicates an extension of ADDED_PRECEDENCE_TRANSITIONS
* for the addition of lexer actions encoded as a sequence of
* LexerAction instances.
*/
static Guid ADDED_LEXER_ACTIONS();
static antlrcpp::Guid ADDED_LEXER_ACTIONS();

/**
* This UUID indicates the serialized ATN contains two sets of
* IntervalSets, where the second set's values are encoded as
* 32-bit integers to support the full Unicode SMP range up to U+10FFFF.
*/
static Guid ADDED_UNICODE_SMP();
static antlrcpp::Guid ADDED_UNICODE_SMP();

/// This list contains all of the currently supported UUIDs, ordered by when
/// the feature first appeared in this branch.
static std::vector<Guid>& SUPPORTED_UUIDS();
static std::vector<antlrcpp::Guid>& SUPPORTED_UUIDS();

ATNDeserializationOptions deserializationOptions;
};
Expand Down
6 changes: 3 additions & 3 deletions runtime/Cpp/runtime/src/atn/ATNSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) {
throw UnsupportedOperationException("ATN Serializer" + reason);
}

Guid uuid = ATNDeserializer::toUUID(data.data(), p);
antlrcpp::Guid uuid = ATNDeserializer::toUUID(data.data(), p);
p += 8;
if (uuid != ATNDeserializer::SERIALIZED_UUID()) {
std::string reason = "Could not deserialize ATN with UUID " + uuid.toString() + " (expected " +
Expand Down Expand Up @@ -602,10 +602,10 @@ std::string ATNSerializer::getDecoded(ATN *atn, std::vector<std::string> &tokenN
return ATNSerializer(atn, tokenNames).decode(serialized);
}

void ATNSerializer::serializeUUID(std::vector<size_t> &data, Guid uuid) {
void ATNSerializer::serializeUUID(std::vector<size_t> &data, antlrcpp::Guid uuid) {
unsigned int twoBytes = 0;
bool firstByte = true;
for( std::vector<unsigned char>::const_reverse_iterator rit = uuid.rbegin(); rit != uuid.rend(); ++rit )
for(antlrcpp::Guid::const_reverse_iterator rit = uuid.rbegin(); rit != uuid.rend(); ++rit )
{
if (firstByte) {
twoBytes = *rit;
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cpp/runtime/src/atn/ATNSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace atn {
private:
std::vector<std::string> _tokenNames;

void serializeUUID(std::vector<size_t> &data, Guid uuid);
void serializeUUID(std::vector<size_t> &data, antlrcpp::Guid uuid);
};

} // namespace atn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
THE SOFTWARE.
*/

#include "guid.h"
#include <algorithm>

#include "Guid.h"

#ifdef GUID_LIBUUID
#include <uuid/uuid.h>
Expand All @@ -40,93 +42,90 @@
#include <jni.h>
#endif

using namespace std;
namespace antlrcpp {

// overload << so that it's easy to convert to a string
ostream &operator<<(ostream &s, const Guid &guid)
{
return s << hex << setfill('0')
<< setw(2) << (int)guid._bytes[0]
<< setw(2) << (int)guid._bytes[1]
<< setw(2) << (int)guid._bytes[2]
<< setw(2) << (int)guid._bytes[3]
<< "-"
<< setw(2) << (int)guid._bytes[4]
<< setw(2) << (int)guid._bytes[5]
<< "-"
<< setw(2) << (int)guid._bytes[6]
<< setw(2) << (int)guid._bytes[7]
<< "-"
<< setw(2) << (int)guid._bytes[8]
<< setw(2) << (int)guid._bytes[9]
<< "-"
<< setw(2) << (int)guid._bytes[10]
<< setw(2) << (int)guid._bytes[11]
<< setw(2) << (int)guid._bytes[12]
<< setw(2) << (int)guid._bytes[13]
<< setw(2) << (int)guid._bytes[14]
<< setw(2) << (int)guid._bytes[15];
std::ostream &operator<<(std::ostream &s, const Guid &guid) {
return s << std::hex << std::setfill('0')
<< std::setw(2) << static_cast<int>(guid.bytes_[0])
<< std::setw(2) << static_cast<int>(guid.bytes_[1])
<< std::setw(2) << static_cast<int>(guid.bytes_[2])
<< std::setw(2) << static_cast<int>(guid.bytes_[3])
<< "-"
<< std::setw(2) << static_cast<int>(guid.bytes_[4])
<< std::setw(2) << static_cast<int>(guid.bytes_[5])
<< "-"
<< std::setw(2) << static_cast<int>(guid.bytes_[6])
<< std::setw(2) << static_cast<int>(guid.bytes_[7])
<< "-"
<< std::setw(2) << static_cast<int>(guid.bytes_[8])
<< std::setw(2) << static_cast<int>(guid.bytes_[9])
<< "-"
<< std::setw(2) << static_cast<int>(guid.bytes_[10])
<< std::setw(2) << static_cast<int>(guid.bytes_[11])
<< std::setw(2) << static_cast<int>(guid.bytes_[12])
<< std::setw(2) << static_cast<int>(guid.bytes_[13])
<< std::setw(2) << static_cast<int>(guid.bytes_[14])
<< std::setw(2) << static_cast<int>(guid.bytes_[15]);
}

// create a guid from vector of bytes
Guid::Guid(const vector<unsigned char> &bytes)
{
_bytes = bytes;
Guid::Guid(const std::vector<uint8_t> &bytes) {
std::memcpy(bytes_.data(), bytes.data(), std::min(bytes.size(), bytes_.size()));
}

Guid::Guid(const std::array<uint8_t, 16> &bytes) : bytes_(bytes) {}

// create a guid from array of bytes
Guid::Guid(const unsigned char *bytes)
{
_bytes.assign(bytes, bytes + 16);
Guid::Guid(const uint8_t *bytes) {
std::memcpy(bytes_.data(), bytes, 16);
}

// create a guid from array of words
Guid::Guid(const uint16_t *bytes, bool reverse)
{
Guid::Guid(const uint16_t *bytes, bool reverse) {
size_t j = 0;
if (reverse) {
for (size_t i = 8; i > 0; --i)
{
_bytes.push_back(bytes[i - 1] >> 8);
_bytes.push_back(bytes[i - 1] & 0xFF);
for (size_t i = 8; i > 0; --i) {
bytes_[j++] = static_cast<uint8_t>(bytes[i - 1] >> 8);
bytes_[j++] = static_cast<uint8_t>(bytes[i - 1] & 0xFF);
}
} else {
for (size_t i = 0; i < 8; ++i)
{
_bytes.push_back(bytes[i] & 0xFF);
_bytes.push_back(bytes[i] >> 8);
for (size_t i = 0; i < 8; ++i) {
bytes_[j++] = static_cast<uint8_t>(bytes[i] & 0xFF);
bytes_[j++] = static_cast<uint8_t>(bytes[i] >> 8);
}
}
}

namespace {

// converts a single hex char to a number (0 - 15)
static unsigned char hexDigitToChar(char ch)
{
uint8_t hexDigitToChar(char ch) {
if (ch > 47 && ch < 58)
return (unsigned char)(ch - 48);
return (uint8_t)(ch - 48);

if (ch > 96 && ch < 103)
return (unsigned char)(ch - 87);
return (uint8_t)(ch - 87);

if (ch > 64 && ch < 71)
return (unsigned char)(ch - 55);
return (uint8_t)(ch - 55);

return 0;
}

// converts the two hexadecimal characters to an unsigned char (a byte)
static unsigned char hexPairToChar(char a, char b)
{
uint8_t hexPairToChar(char a, char b) {
return hexDigitToChar(a) * 16 + hexDigitToChar(b);
}

// create a guid from string
Guid::Guid(const string &fromString)
{
_bytes.clear();
}

// create a guid from string
Guid::Guid(const std::string &fromString) {
char charOne = 0, charTwo;
bool lookingForFirstChar = true;

size_t i = 0;
for (const char &ch : fromString)
{
if (ch == '-')
Expand All @@ -140,47 +139,13 @@ Guid::Guid(const string &fromString)
else
{
charTwo = ch;
auto byte = hexPairToChar(charOne, charTwo);
_bytes.push_back(byte);
bytes_[i++] = hexPairToChar(charOne, charTwo);
lookingForFirstChar = true;
}
}

}

// create empty guid
Guid::Guid()
{
_bytes = vector<unsigned char>(16, 0);
}

// copy constructor
Guid::Guid(const Guid &other)
{
_bytes = other._bytes;
}

// overload assignment operator
Guid &Guid::operator=(const Guid &other)
{
_bytes = other._bytes;
return *this;
}

// overload equality operator
bool Guid::operator==(const Guid &other) const
{
return _bytes == other._bytes;
}

// overload inequality operator
bool Guid::operator!=(const Guid &other) const
{
return !((*this) == other);
}

const std::string Guid::toString() const
{
std::string Guid::toString() const {
std::stringstream os;
os << *this;
return os.str();
Expand Down Expand Up @@ -301,3 +266,5 @@ Guid GuidGenerator::newGuid()
return bytes;
}
#endif

}
Loading

0 comments on commit 1662d2f

Please sign in to comment.