First commit
This commit is contained in:
16
runtime-linux/antlr4-runtime/support/Any.cpp
Normal file
16
runtime-linux/antlr4-runtime/support/Any.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#include "Any.h"
|
||||
|
||||
using namespace antlrcpp;
|
||||
|
||||
Any::~Any()
|
||||
{
|
||||
delete _ptr;
|
||||
}
|
||||
|
||||
Any::Base::~Base() {
|
||||
}
|
||||
142
runtime-linux/antlr4-runtime/support/Any.h
Normal file
142
runtime-linux/antlr4-runtime/support/Any.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
// A standard C++ class loosely modeled after boost::Any.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable: 4521) // 'antlrcpp::Any': multiple copy constructors specified
|
||||
#endif
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
template<class T>
|
||||
using StorageType = typename std::decay<T>::type;
|
||||
|
||||
struct ANTLR4CPP_PUBLIC Any
|
||||
{
|
||||
bool isNull() const { return _ptr == nullptr; }
|
||||
bool isNotNull() const { return _ptr != nullptr; }
|
||||
|
||||
Any() : _ptr(nullptr) {
|
||||
}
|
||||
|
||||
Any(Any& that) : _ptr(that.clone()) {
|
||||
}
|
||||
|
||||
Any(Any&& that) : _ptr(that._ptr) {
|
||||
that._ptr = nullptr;
|
||||
}
|
||||
|
||||
Any(const Any& that) : _ptr(that.clone()) {
|
||||
}
|
||||
|
||||
Any(const Any&& that) : _ptr(that.clone()) {
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Any(U&& value) : _ptr(new Derived<StorageType<U>>(std::forward<U>(value))) {
|
||||
}
|
||||
|
||||
template<class U>
|
||||
bool is() const {
|
||||
typedef StorageType<U> T;
|
||||
|
||||
auto derived = dynamic_cast<Derived<T> *>(_ptr);
|
||||
|
||||
return derived != nullptr;
|
||||
}
|
||||
|
||||
template<class U>
|
||||
StorageType<U>& as() {
|
||||
typedef StorageType<U> T;
|
||||
|
||||
auto derived = dynamic_cast<Derived<T>*>(_ptr);
|
||||
|
||||
if (!derived)
|
||||
throw std::bad_cast();
|
||||
|
||||
return derived->value;
|
||||
}
|
||||
|
||||
template<class U>
|
||||
operator U() {
|
||||
return as<StorageType<U>>();
|
||||
}
|
||||
|
||||
Any& operator = (const Any& a) {
|
||||
if (_ptr == a._ptr)
|
||||
return *this;
|
||||
|
||||
auto old_ptr = _ptr;
|
||||
_ptr = a.clone();
|
||||
|
||||
if (old_ptr)
|
||||
delete old_ptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Any& operator = (Any&& a) {
|
||||
if (_ptr == a._ptr)
|
||||
return *this;
|
||||
|
||||
std::swap(_ptr, a._ptr);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual ~Any();
|
||||
|
||||
virtual bool equals(Any other) const {
|
||||
return _ptr == other._ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Base {
|
||||
virtual ~Base();
|
||||
virtual Base* clone() const = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Derived : Base
|
||||
{
|
||||
template<typename U> Derived(U&& value_) : value(std::forward<U>(value_)) {
|
||||
}
|
||||
|
||||
T value;
|
||||
|
||||
Base* clone() const {
|
||||
return new Derived<T>(value);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Base* clone() const
|
||||
{
|
||||
if (_ptr)
|
||||
return _ptr->clone();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Base *_ptr;
|
||||
|
||||
};
|
||||
|
||||
template<> inline
|
||||
Any::Any(std::nullptr_t&& ) : _ptr(nullptr) {
|
||||
}
|
||||
|
||||
|
||||
} // namespace antlrcpp
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
43
runtime-linux/antlr4-runtime/support/Arrays.cpp
Normal file
43
runtime-linux/antlr4-runtime/support/Arrays.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#include "tree/ParseTree.h"
|
||||
#include "Exceptions.h"
|
||||
|
||||
#include "support/Arrays.h"
|
||||
|
||||
using namespace antlrcpp;
|
||||
|
||||
std::string Arrays::listToString(const std::vector<std::string> &list, const std::string &separator)
|
||||
{
|
||||
std::stringstream ss;
|
||||
bool firstEntry = true;
|
||||
|
||||
ss << '[';
|
||||
for (auto &entry : list) {
|
||||
ss << entry;
|
||||
if (firstEntry) {
|
||||
ss << separator;
|
||||
firstEntry = false;
|
||||
}
|
||||
}
|
||||
|
||||
ss << ']';
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string Arrays::toString(const std::vector<antlr4::tree::ParseTree*> &source) {
|
||||
std::string result = "[";
|
||||
bool firstEntry = true;
|
||||
for (auto value : source) {
|
||||
result += value->toStringTree();
|
||||
if (firstEntry) {
|
||||
result += ", ";
|
||||
firstEntry = false;
|
||||
}
|
||||
}
|
||||
return result + "]";
|
||||
}
|
||||
110
runtime-linux/antlr4-runtime/support/Arrays.h
Normal file
110
runtime-linux/antlr4-runtime/support/Arrays.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
class ANTLR4CPP_PUBLIC Arrays {
|
||||
public:
|
||||
|
||||
static std::string listToString(const std::vector<std::string> &list, const std::string &separator);
|
||||
|
||||
template <typename T>
|
||||
static bool equals(const std::vector<T> &a, const std::vector<T> &b) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < a.size(); ++i)
|
||||
if (!(a[i] == b[i]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool equals(const std::vector<T *> &a, const std::vector<T *> &b) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < a.size(); ++i) {
|
||||
if (a[i] == b[i])
|
||||
continue;
|
||||
if (!(*a[i] == *b[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static bool equals(const std::vector<Ref<T>> &a, const std::vector<Ref<T>> &b) {
|
||||
if (a.size() != b.size())
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < a.size(); ++i) {
|
||||
if (!a[i] && !b[i])
|
||||
continue;
|
||||
if (!a[i] || !b[i])
|
||||
return false;
|
||||
if (a[i] == b[i])
|
||||
continue;
|
||||
|
||||
if (!(*a[i] == *b[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::string toString(const std::vector<T> &source) {
|
||||
std::string result = "[";
|
||||
bool firstEntry = true;
|
||||
for (auto &value : source) {
|
||||
result += value.toString();
|
||||
if (firstEntry) {
|
||||
result += ", ";
|
||||
firstEntry = false;
|
||||
}
|
||||
}
|
||||
return result + "]";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::string toString(const std::vector<Ref<T>> &source) {
|
||||
std::string result = "[";
|
||||
bool firstEntry = true;
|
||||
for (auto &value : source) {
|
||||
result += value->toString();
|
||||
if (firstEntry) {
|
||||
result += ", ";
|
||||
firstEntry = false;
|
||||
}
|
||||
}
|
||||
return result + "]";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::string toString(const std::vector<T *> &source) {
|
||||
std::string result = "[";
|
||||
bool firstEntry = true;
|
||||
for (auto value : source) {
|
||||
result += value->toString();
|
||||
if (firstEntry) {
|
||||
result += ", ";
|
||||
firstEntry = false;
|
||||
}
|
||||
}
|
||||
return result + "]";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <>
|
||||
std::string Arrays::toString(const std::vector<antlr4::tree::ParseTree *> &source);
|
||||
}
|
||||
76
runtime-linux/antlr4-runtime/support/BitSet.h
Normal file
76
runtime-linux/antlr4-runtime/support/BitSet.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
class ANTLR4CPP_PUBLIC BitSet : public std::bitset<1024> {
|
||||
public:
|
||||
size_t nextSetBit(size_t pos) const {
|
||||
for (size_t i = pos; i < size(); i++){
|
||||
if (test(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return INVALID_INDEX;
|
||||
}
|
||||
|
||||
// Prints a list of every index for which the bitset contains a bit in true.
|
||||
friend std::wostream& operator << (std::wostream& os, const BitSet& obj)
|
||||
{
|
||||
os << "{";
|
||||
size_t total = obj.count();
|
||||
for (size_t i = 0; i < obj.size(); i++){
|
||||
if (obj.test(i)){
|
||||
os << i;
|
||||
--total;
|
||||
if (total > 1){
|
||||
os << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
os << "}";
|
||||
return os;
|
||||
}
|
||||
|
||||
static std::string subStringRepresentation(const std::vector<BitSet>::iterator &begin,
|
||||
const std::vector<BitSet>::iterator &end) {
|
||||
std::string result;
|
||||
std::vector<BitSet>::iterator vectorIterator;
|
||||
|
||||
for (vectorIterator = begin; vectorIterator != end; vectorIterator++) {
|
||||
result += vectorIterator->toString();
|
||||
}
|
||||
// Grab the end
|
||||
result += end->toString();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string toString(){
|
||||
std::stringstream stream;
|
||||
stream << "{";
|
||||
bool valueAdded = false;
|
||||
for (size_t i = 0; i < size(); ++i){
|
||||
if (test(i)){
|
||||
if (valueAdded) {
|
||||
stream << ", ";
|
||||
}
|
||||
stream << i;
|
||||
valueAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
stream << "}";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
243
runtime-linux/antlr4-runtime/support/CPPUtils.cpp
Executable file
243
runtime-linux/antlr4-runtime/support/CPPUtils.cpp
Executable file
@@ -0,0 +1,243 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#include "support/CPPUtils.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
std::string join(std::vector<std::string> strings, const std::string &separator) {
|
||||
std::string str;
|
||||
bool firstItem = true;
|
||||
for (std::string s : strings) {
|
||||
if (!firstItem) {
|
||||
str.append(separator);
|
||||
}
|
||||
firstItem = false;
|
||||
str.append(s);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
std::map<std::string, size_t> toMap(const std::vector<std::string> &keys) {
|
||||
std::map<std::string, size_t> result;
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
result.insert({ keys[i], i });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string escapeWhitespace(std::string str, bool escapeSpaces) {
|
||||
std::string result;
|
||||
for (auto c : str) {
|
||||
switch (c) {
|
||||
case '\n':
|
||||
result += "\\n";
|
||||
break;
|
||||
|
||||
case '\r':
|
||||
result += "\\r";
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
result += "\\t";
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
if (escapeSpaces) {
|
||||
result += "·";
|
||||
break;
|
||||
}
|
||||
// else fall through
|
||||
|
||||
default:
|
||||
result += c;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string toHexString(const int t) {
|
||||
std::stringstream stream;
|
||||
stream << std::uppercase << std::hex << t;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::string arrayToString(const std::vector<std::string> &data) {
|
||||
std::string answer;
|
||||
for (auto sub: data) {
|
||||
answer += sub;
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
std::string replaceString(const std::string &s, const std::string &from, const std::string &to) {
|
||||
std::string::size_type p;
|
||||
std::string ss, res;
|
||||
|
||||
ss = s;
|
||||
p = ss.find(from);
|
||||
while (p != std::string::npos) {
|
||||
if (p > 0)
|
||||
res.append(ss.substr(0, p)).append(to);
|
||||
else
|
||||
res.append(to);
|
||||
ss = ss.substr(p + from.size());
|
||||
p = ss.find(from);
|
||||
}
|
||||
res.append(ss);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<std::string> split(const std::string &s, const std::string &sep, int count) {
|
||||
std::vector<std::string> parts;
|
||||
std::string ss = s;
|
||||
|
||||
std::string::size_type p;
|
||||
|
||||
if (s.empty())
|
||||
return parts;
|
||||
|
||||
if (count == 0)
|
||||
count= -1;
|
||||
|
||||
p = ss.find(sep);
|
||||
while (!ss.empty() && p != std::string::npos && (count < 0 || count > 0)) {
|
||||
parts.push_back(ss.substr(0, p));
|
||||
ss = ss.substr(p+sep.size());
|
||||
|
||||
--count;
|
||||
p = ss.find(sep);
|
||||
}
|
||||
parts.push_back(ss);
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
// Debugging helper. Adds indentation to all lines in the given string.
|
||||
std::string indent(const std::string &s, const std::string &indentation, bool includingFirst) {
|
||||
std::vector<std::string> parts = split(s, "\n", -1);
|
||||
for (size_t i = 0; i < parts.size(); ++i) {
|
||||
if (i == 0 && !includingFirst)
|
||||
continue;
|
||||
parts[i].insert(0, indentation);
|
||||
}
|
||||
|
||||
return join(parts, "\n");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
// Recursively get the error from a, possibly nested, exception.
|
||||
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023026
|
||||
// No nested exceptions before VS 2015.
|
||||
template <typename T>
|
||||
std::exception_ptr get_nested(const T &/*e*/) {
|
||||
try {
|
||||
return nullptr;
|
||||
}
|
||||
catch (const std::bad_cast &) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
#else
|
||||
template <typename T>
|
||||
std::exception_ptr get_nested(const T &e) {
|
||||
try {
|
||||
auto nested = dynamic_cast<const std::nested_exception&>(e);
|
||||
return nested.nested_ptr();
|
||||
}
|
||||
catch (const std::bad_cast &) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string what(std::exception_ptr eptr) {
|
||||
if (!eptr) {
|
||||
throw std::bad_exception();
|
||||
}
|
||||
|
||||
std::string result;
|
||||
std::size_t nestCount = 0;
|
||||
|
||||
next: {
|
||||
try {
|
||||
std::exception_ptr yeptr;
|
||||
std::swap(eptr, yeptr);
|
||||
std::rethrow_exception(yeptr);
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
result += e.what();
|
||||
eptr = get_nested(e);
|
||||
}
|
||||
catch (const std::string &e) {
|
||||
result += e;
|
||||
}
|
||||
catch (const char *e) {
|
||||
result += e;
|
||||
}
|
||||
catch (...) {
|
||||
result += "cannot be determined";
|
||||
}
|
||||
|
||||
if (eptr) {
|
||||
result += " (";
|
||||
++nestCount;
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
|
||||
result += std::string(nestCount, ')');
|
||||
return result;
|
||||
}
|
||||
|
||||
//----------------- FinallyAction ------------------------------------------------------------------------------------
|
||||
|
||||
FinalAction finally(std::function<void ()> f) {
|
||||
return FinalAction(f);
|
||||
}
|
||||
|
||||
//----------------- SingleWriteMultipleRead --------------------------------------------------------------------------
|
||||
|
||||
void SingleWriteMultipleReadLock::readLock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
while (_waitingWriters != 0)
|
||||
_readerGate.wait(lock);
|
||||
++_activeReaders;
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void SingleWriteMultipleReadLock::readUnlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
--_activeReaders;
|
||||
lock.unlock();
|
||||
_writerGate.notify_one();
|
||||
}
|
||||
|
||||
void SingleWriteMultipleReadLock::writeLock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
++_waitingWriters;
|
||||
while (_activeReaders != 0 || _activeWriters != 0)
|
||||
_writerGate.wait(lock);
|
||||
++_activeWriters;
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void SingleWriteMultipleReadLock::writeUnlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
--_waitingWriters;
|
||||
--_activeWriters;
|
||||
if (_waitingWriters > 0)
|
||||
_writerGate.notify_one();
|
||||
else
|
||||
_readerGate.notify_all();
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
} // namespace antlrcpp
|
||||
78
runtime-linux/antlr4-runtime/support/CPPUtils.h
Normal file
78
runtime-linux/antlr4-runtime/support/CPPUtils.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
std::string join(std::vector<std::string> strings, const std::string &separator);
|
||||
std::map<std::string, size_t> toMap(const std::vector<std::string> &keys);
|
||||
std::string escapeWhitespace(std::string str, bool escapeSpaces);
|
||||
std::string toHexString(const int t);
|
||||
std::string arrayToString(const std::vector<std::string> &data);
|
||||
std::string replaceString(const std::string &s, const std::string &from, const std::string &to);
|
||||
std::vector<std::string> split(const std::string &s, const std::string &sep, int count);
|
||||
std::string indent(const std::string &s, const std::string &indentation, bool includingFirst = true);
|
||||
|
||||
// Using RAII + a lambda to implement a "finally" replacement.
|
||||
struct FinalAction {
|
||||
FinalAction(std::function<void ()> f) : _cleanUp { f } {}
|
||||
FinalAction(FinalAction &&other) :
|
||||
_cleanUp(std::move(other._cleanUp)), _enabled(other._enabled) {
|
||||
other._enabled = false; // Don't trigger the lambda after ownership has moved.
|
||||
}
|
||||
~FinalAction() { if (_enabled) _cleanUp(); }
|
||||
|
||||
void disable() { _enabled = false; }
|
||||
private:
|
||||
std::function<void ()> _cleanUp;
|
||||
bool _enabled {true};
|
||||
};
|
||||
|
||||
ANTLR4CPP_PUBLIC FinalAction finally(std::function<void ()> f);
|
||||
|
||||
// Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
|
||||
template <typename T1, typename T2>
|
||||
inline bool is(T2 *obj) { // For pointer types.
|
||||
return dynamic_cast<typename std::add_const<T1>::type>(obj) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline bool is(Ref<T2> const& obj) { // For shared pointers.
|
||||
return dynamic_cast<T1 *>(obj.get()) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string toString(const T &o) {
|
||||
std::stringstream ss;
|
||||
// typeid gives the mangled class name, but that's all what's possible
|
||||
// in a portable way.
|
||||
ss << typeid(o).name() << "@" << std::hex << reinterpret_cast<uintptr_t>(&o);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// Get the error text from an exception pointer or the current exception.
|
||||
std::string what(std::exception_ptr eptr = std::current_exception());
|
||||
|
||||
class SingleWriteMultipleReadLock {
|
||||
public:
|
||||
void readLock();
|
||||
void readUnlock();
|
||||
void writeLock();
|
||||
void writeUnlock();
|
||||
|
||||
private:
|
||||
std::condition_variable _readerGate;
|
||||
std::condition_variable _writerGate;
|
||||
|
||||
std::mutex _mutex;
|
||||
size_t _activeReaders = 0;
|
||||
size_t _waitingWriters = 0;
|
||||
size_t _activeWriters = 0;
|
||||
};
|
||||
|
||||
} // namespace antlrcpp
|
||||
163
runtime-linux/antlr4-runtime/support/Declarations.h
Normal file
163
runtime-linux/antlr4-runtime/support/Declarations.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace antlr4 {
|
||||
class ANTLRErrorListener;
|
||||
class ANTLRErrorStrategy;
|
||||
class ANTLRFileStream;
|
||||
class ANTLRInputStream;
|
||||
class BailErrorStrategy;
|
||||
class BaseErrorListener;
|
||||
class BufferedTokenStream;
|
||||
class CharStream;
|
||||
class CommonToken;
|
||||
class CommonTokenFactory;
|
||||
class CommonTokenStream;
|
||||
class ConsoleErrorListener;
|
||||
class DefaultErrorStrategy;
|
||||
class DiagnosticErrorListener;
|
||||
class EmptyStackException;
|
||||
class FailedPredicateException;
|
||||
class IllegalArgumentException;
|
||||
class IllegalStateException;
|
||||
class InputMismatchException;
|
||||
class IntStream;
|
||||
class InterpreterRuleContext;
|
||||
class Lexer;
|
||||
class LexerInterpreter;
|
||||
class LexerNoViableAltException;
|
||||
class ListTokenSource;
|
||||
class NoSuchElementException;
|
||||
class NoViableAltException;
|
||||
class NullPointerException;
|
||||
class ParseCancellationException;
|
||||
class Parser;
|
||||
class ParserInterpreter;
|
||||
class ParserRuleContext;
|
||||
class ProxyErrorListener;
|
||||
class RecognitionException;
|
||||
class Recognizer;
|
||||
class RuleContext;
|
||||
class Token;
|
||||
template<typename Symbol> class TokenFactory;
|
||||
class TokenSource;
|
||||
class TokenStream;
|
||||
class TokenStreamRewriter;
|
||||
class UnbufferedCharStream;
|
||||
class UnbufferedTokenStream;
|
||||
class WritableToken;
|
||||
|
||||
namespace misc {
|
||||
class InterpreterDataReader;
|
||||
class Interval;
|
||||
class IntervalSet;
|
||||
class MurmurHash;
|
||||
class Utils;
|
||||
class Predicate;
|
||||
}
|
||||
namespace atn {
|
||||
class ATN;
|
||||
class ATNConfig;
|
||||
class ATNConfigSet;
|
||||
class ATNDeserializationOptions;
|
||||
class ATNDeserializer;
|
||||
class ATNSerializer;
|
||||
class ATNSimulator;
|
||||
class ATNState;
|
||||
enum class ATNType;
|
||||
class AbstractPredicateTransition;
|
||||
class ActionTransition;
|
||||
class ArrayPredictionContext;
|
||||
class AtomTransition;
|
||||
class BasicBlockStartState;
|
||||
class BasicState;
|
||||
class BlockEndState;
|
||||
class BlockStartState;
|
||||
class DecisionState;
|
||||
class EmptyPredictionContext;
|
||||
class EpsilonTransition;
|
||||
class LL1Analyzer;
|
||||
class LexerAction;
|
||||
class LexerActionExecutor;
|
||||
class LexerATNConfig;
|
||||
class LexerATNSimulator;
|
||||
class LexerMoreAction;
|
||||
class LexerPopModeAction;
|
||||
class LexerSkipAction;
|
||||
class LookaheadEventInfo;
|
||||
class LoopEndState;
|
||||
class NotSetTransition;
|
||||
class OrderedATNConfigSet;
|
||||
class ParseInfo;
|
||||
class ParserATNSimulator;
|
||||
class PlusBlockStartState;
|
||||
class PlusLoopbackState;
|
||||
class PrecedencePredicateTransition;
|
||||
class PredicateTransition;
|
||||
class PredictionContext;
|
||||
enum class PredictionMode;
|
||||
class PredictionModeClass;
|
||||
class RangeTransition;
|
||||
class RuleStartState;
|
||||
class RuleStopState;
|
||||
class RuleTransition;
|
||||
class SemanticContext;
|
||||
class SetTransition;
|
||||
class SingletonPredictionContext;
|
||||
class StarBlockStartState;
|
||||
class StarLoopEntryState;
|
||||
class StarLoopbackState;
|
||||
class TokensStartState;
|
||||
class Transition;
|
||||
class WildcardTransition;
|
||||
}
|
||||
namespace dfa {
|
||||
class DFA;
|
||||
class DFASerializer;
|
||||
class DFAState;
|
||||
class LexerDFASerializer;
|
||||
class Vocabulary;
|
||||
}
|
||||
namespace tree {
|
||||
class AbstractParseTreeVisitor;
|
||||
class ErrorNode;
|
||||
class ErrorNodeImpl;
|
||||
class ParseTree;
|
||||
class ParseTreeListener;
|
||||
template<typename T> class ParseTreeProperty;
|
||||
class ParseTreeVisitor;
|
||||
class ParseTreeWalker;
|
||||
class SyntaxTree;
|
||||
class TerminalNode;
|
||||
class TerminalNodeImpl;
|
||||
class Tree;
|
||||
class Trees;
|
||||
|
||||
namespace pattern {
|
||||
class Chunk;
|
||||
class ParseTreeMatch;
|
||||
class ParseTreePattern;
|
||||
class ParseTreePatternMatcher;
|
||||
class RuleTagToken;
|
||||
class TagChunk;
|
||||
class TextChunk;
|
||||
class TokenTagToken;
|
||||
}
|
||||
|
||||
namespace xpath {
|
||||
class XPath;
|
||||
class XPathElement;
|
||||
class XPathLexerErrorListener;
|
||||
class XPathRuleAnywhereElement;
|
||||
class XPathRuleElement;
|
||||
class XPathTokenAnywhereElement;
|
||||
class XPathTokenElement;
|
||||
class XPathWildcardAnywhereElement;
|
||||
class XPathWildcardElement;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
runtime-linux/antlr4-runtime/support/StringUtils.cpp
Normal file
36
runtime-linux/antlr4-runtime/support/StringUtils.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#include "support/StringUtils.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
void replaceAll(std::string& str, std::string const& from, std::string const& to)
|
||||
{
|
||||
if (from.empty())
|
||||
return;
|
||||
|
||||
size_t start_pos = 0;
|
||||
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
|
||||
str.replace(start_pos, from.length(), to);
|
||||
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'.
|
||||
}
|
||||
}
|
||||
|
||||
std::string ws2s(std::wstring const& wstr) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
std::string narrow = converter.to_bytes(wstr);
|
||||
|
||||
return narrow;
|
||||
}
|
||||
|
||||
std::wstring s2ws(const std::string &str) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
std::wstring wide = converter.from_bytes(str);
|
||||
|
||||
return wide;
|
||||
}
|
||||
|
||||
} // namespace antrlcpp
|
||||
54
runtime-linux/antlr4-runtime/support/StringUtils.h
Normal file
54
runtime-linux/antlr4-runtime/support/StringUtils.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
* Use of this file is governed by the BSD 3-clause license that
|
||||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "antlr4-common.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
|
||||
// For all conversions utf8 <-> utf32.
|
||||
// VS 2015 and VS 2017 have different bugs in std::codecvt_utf8<char32_t> (VS 2013 works fine).
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
|
||||
typedef std::wstring_convert<std::codecvt_utf8<__int32>, __int32> UTF32Converter;
|
||||
#else
|
||||
typedef std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> UTF32Converter;
|
||||
#endif
|
||||
|
||||
// The conversion functions fails in VS2017, so we explicitly use a workaround.
|
||||
template<typename T>
|
||||
inline std::string utf32_to_utf8(T const& data)
|
||||
{
|
||||
// Don't make the converter static or we have to serialize access to it.
|
||||
UTF32Converter converter;
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
|
||||
auto p = reinterpret_cast<const int32_t *>(data.data());
|
||||
return converter.to_bytes(p, p + data.size());
|
||||
#else
|
||||
return converter.to_bytes(data);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline UTF32String utf8_to_utf32(const char* first, const char* last)
|
||||
{
|
||||
UTF32Converter converter;
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
|
||||
auto r = converter.from_bytes(first, last);
|
||||
i32string s = reinterpret_cast<const int32_t *>(r.data());
|
||||
#else
|
||||
std::u32string s = converter.from_bytes(first, last);
|
||||
#endif
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void replaceAll(std::string &str, std::string const& from, std::string const& to);
|
||||
|
||||
// string <-> wstring conversion (UTF-16), e.g. for use with Window's wide APIs.
|
||||
ANTLR4CPP_PUBLIC std::string ws2s(std::wstring const& wstr);
|
||||
ANTLR4CPP_PUBLIC std::wstring s2ws(std::string const& str);
|
||||
}
|
||||
303
runtime-linux/antlr4-runtime/support/guid.cpp
Executable file
303
runtime-linux/antlr4-runtime/support/guid.cpp
Executable file
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "guid.h"
|
||||
|
||||
#ifdef GUID_LIBUUID
|
||||
#include <uuid/uuid.h>
|
||||
#endif
|
||||
|
||||
#ifdef GUID_CFUUID
|
||||
#include <CoreFoundation/CFUUID.h>
|
||||
#endif
|
||||
|
||||
#ifdef GUID_WINDOWS
|
||||
#include <objbase.h>
|
||||
#endif
|
||||
|
||||
#ifdef GUID_ANDROID
|
||||
#include <jni.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
// create a guid from vector of bytes
|
||||
Guid::Guid(const vector<unsigned char> &bytes)
|
||||
{
|
||||
_bytes = bytes;
|
||||
}
|
||||
|
||||
// create a guid from array of bytes
|
||||
Guid::Guid(const unsigned char *bytes)
|
||||
{
|
||||
_bytes.assign(bytes, bytes + 16);
|
||||
}
|
||||
|
||||
// create a guid from array of words
|
||||
Guid::Guid(const uint16_t *bytes, bool reverse)
|
||||
{
|
||||
if (reverse) {
|
||||
for (size_t i = 8; i > 0; --i)
|
||||
{
|
||||
_bytes.push_back(bytes[i - 1] >> 8);
|
||||
_bytes.push_back(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// converts a single hex char to a number (0 - 15)
|
||||
static unsigned char hexDigitToChar(char ch)
|
||||
{
|
||||
if (ch > 47 && ch < 58)
|
||||
return (unsigned char)(ch - 48);
|
||||
|
||||
if (ch > 96 && ch < 103)
|
||||
return (unsigned char)(ch - 87);
|
||||
|
||||
if (ch > 64 && ch < 71)
|
||||
return (unsigned char)(ch - 55);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// converts the two hexadecimal characters to an unsigned char (a byte)
|
||||
static unsigned char hexPairToChar(char a, char b)
|
||||
{
|
||||
return hexDigitToChar(a) * 16 + hexDigitToChar(b);
|
||||
}
|
||||
|
||||
// create a guid from string
|
||||
Guid::Guid(const string &fromString)
|
||||
{
|
||||
_bytes.clear();
|
||||
|
||||
char charOne = 0, charTwo;
|
||||
bool lookingForFirstChar = true;
|
||||
|
||||
for (const char &ch : fromString)
|
||||
{
|
||||
if (ch == '-')
|
||||
continue;
|
||||
|
||||
if (lookingForFirstChar)
|
||||
{
|
||||
charOne = ch;
|
||||
lookingForFirstChar = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
charTwo = ch;
|
||||
auto byte = hexPairToChar(charOne, charTwo);
|
||||
_bytes.push_back(byte);
|
||||
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::stringstream os;
|
||||
os << *this;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
// This is the linux friendly implementation, but it could work on other
|
||||
// systems that have libuuid available
|
||||
#ifdef GUID_LIBUUID
|
||||
Guid GuidGenerator::newGuid()
|
||||
{
|
||||
uuid_t id;
|
||||
uuid_generate(id);
|
||||
return id;
|
||||
}
|
||||
#endif
|
||||
|
||||
// this is the mac and ios version
|
||||
#ifdef GUID_CFUUID
|
||||
Guid GuidGenerator::newGuid()
|
||||
{
|
||||
auto newId = CFUUIDCreate(NULL);
|
||||
auto bytes = CFUUIDGetUUIDBytes(newId);
|
||||
CFRelease(newId);
|
||||
|
||||
const unsigned char byteArray[16] =
|
||||
{
|
||||
bytes.byte0,
|
||||
bytes.byte1,
|
||||
bytes.byte2,
|
||||
bytes.byte3,
|
||||
bytes.byte4,
|
||||
bytes.byte5,
|
||||
bytes.byte6,
|
||||
bytes.byte7,
|
||||
bytes.byte8,
|
||||
bytes.byte9,
|
||||
bytes.byte10,
|
||||
bytes.byte11,
|
||||
bytes.byte12,
|
||||
bytes.byte13,
|
||||
bytes.byte14,
|
||||
bytes.byte15
|
||||
};
|
||||
return byteArray;
|
||||
}
|
||||
#endif
|
||||
|
||||
// obviously this is the windows version
|
||||
#ifdef GUID_WINDOWS
|
||||
Guid GuidGenerator::newGuid()
|
||||
{
|
||||
GUID newId;
|
||||
CoCreateGuid(&newId);
|
||||
|
||||
const unsigned char bytes[16] =
|
||||
{
|
||||
(newId.Data1 >> 24) & 0xFF,
|
||||
(newId.Data1 >> 16) & 0xFF,
|
||||
(newId.Data1 >> 8) & 0xFF,
|
||||
(newId.Data1) & 0xff,
|
||||
|
||||
(newId.Data2 >> 8) & 0xFF,
|
||||
(newId.Data2) & 0xff,
|
||||
|
||||
(newId.Data3 >> 8) & 0xFF,
|
||||
(newId.Data3) & 0xFF,
|
||||
|
||||
newId.Data4[0],
|
||||
newId.Data4[1],
|
||||
newId.Data4[2],
|
||||
newId.Data4[3],
|
||||
newId.Data4[4],
|
||||
newId.Data4[5],
|
||||
newId.Data4[6],
|
||||
newId.Data4[7]
|
||||
};
|
||||
|
||||
return bytes;
|
||||
}
|
||||
#endif
|
||||
|
||||
// android version that uses a call to a java api
|
||||
#ifdef GUID_ANDROID
|
||||
GuidGenerator::GuidGenerator(JNIEnv *env)
|
||||
{
|
||||
_env = env;
|
||||
_uuidClass = env->FindClass("java/util/UUID");
|
||||
_newGuidMethod = env->GetStaticMethodID(_uuidClass, "randomUUID", "()Ljava/util/UUID;");
|
||||
_mostSignificantBitsMethod = env->GetMethodID(_uuidClass, "getMostSignificantBits", "()J");
|
||||
_leastSignificantBitsMethod = env->GetMethodID(_uuidClass, "getLeastSignificantBits", "()J");
|
||||
}
|
||||
|
||||
Guid GuidGenerator::newGuid()
|
||||
{
|
||||
jobject javaUuid = _env->CallStaticObjectMethod(_uuidClass, _newGuidMethod);
|
||||
jlong mostSignificant = _env->CallLongMethod(javaUuid, _mostSignificantBitsMethod);
|
||||
jlong leastSignificant = _env->CallLongMethod(javaUuid, _leastSignificantBitsMethod);
|
||||
|
||||
unsigned char bytes[16] =
|
||||
{
|
||||
(mostSignificant >> 56) & 0xFF,
|
||||
(mostSignificant >> 48) & 0xFF,
|
||||
(mostSignificant >> 40) & 0xFF,
|
||||
(mostSignificant >> 32) & 0xFF,
|
||||
(mostSignificant >> 24) & 0xFF,
|
||||
(mostSignificant >> 16) & 0xFF,
|
||||
(mostSignificant >> 8) & 0xFF,
|
||||
(mostSignificant) & 0xFF,
|
||||
(leastSignificant >> 56) & 0xFF,
|
||||
(leastSignificant >> 48) & 0xFF,
|
||||
(leastSignificant >> 40) & 0xFF,
|
||||
(leastSignificant >> 32) & 0xFF,
|
||||
(leastSignificant >> 24) & 0xFF,
|
||||
(leastSignificant >> 16) & 0xFF,
|
||||
(leastSignificant >> 8) & 0xFF,
|
||||
(leastSignificant) & 0xFF,
|
||||
};
|
||||
return bytes;
|
||||
}
|
||||
#endif
|
||||
112
runtime-linux/antlr4-runtime/support/guid.h
Executable file
112
runtime-linux/antlr4-runtime/support/guid.h
Executable file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef GUID_ANDROID
|
||||
#include <jni.h>
|
||||
#endif
|
||||
|
||||
// Class to represent a GUID/UUID. Each instance acts as a wrapper around a
|
||||
// 16 byte value that can be passed around by value. It also supports
|
||||
// conversion to string (via the stream operator <<) and conversion from a
|
||||
// string via constructor.
|
||||
class Guid
|
||||
{
|
||||
public:
|
||||
|
||||
// create a guid from vector of bytes
|
||||
Guid(const std::vector<unsigned char> &bytes);
|
||||
|
||||
// create a guid from array of bytes
|
||||
Guid(const unsigned char *bytes);
|
||||
|
||||
// Create a guid from array of words.
|
||||
Guid(const uint16_t *bytes, bool reverse);
|
||||
|
||||
// create a guid from string
|
||||
Guid(const std::string &fromString);
|
||||
|
||||
// create empty guid
|
||||
Guid();
|
||||
|
||||
// copy constructor
|
||||
Guid(const Guid &other);
|
||||
|
||||
// overload assignment operator
|
||||
Guid &operator=(const Guid &other);
|
||||
|
||||
// overload equality and inequality operator
|
||||
bool operator==(const Guid &other) const;
|
||||
bool operator!=(const Guid &other) const;
|
||||
|
||||
const std::string toString() const;
|
||||
std::vector<unsigned char>::const_iterator begin() { return _bytes.begin(); }
|
||||
std::vector<unsigned char>::const_iterator end() { return _bytes.end(); }
|
||||
std::vector<unsigned char>::const_reverse_iterator rbegin() { return _bytes.rbegin(); }
|
||||
std::vector<unsigned char>::const_reverse_iterator rend() { return _bytes.rend(); }
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// actual data
|
||||
std::vector<unsigned char> _bytes;
|
||||
|
||||
// make the << operator a friend so it can access _bytes
|
||||
friend std::ostream &operator<<(std::ostream &s, const Guid &guid);
|
||||
};
|
||||
|
||||
// Class that can create new guids. The only reason this exists instead of
|
||||
// just a global "newGuid" function is because some platforms will require
|
||||
// that there is some attached context. In the case of android, we need to
|
||||
// know what JNIEnv is being used to call back to Java, but the newGuid()
|
||||
// function would no longer be cross-platform if we parameterized the android
|
||||
// version. Instead, construction of the GuidGenerator may be different on
|
||||
// each platform, but the use of newGuid is uniform.
|
||||
class GuidGenerator
|
||||
{
|
||||
public:
|
||||
#ifdef GUID_ANDROID
|
||||
GuidGenerator(JNIEnv *env);
|
||||
#else
|
||||
GuidGenerator() { }
|
||||
#endif
|
||||
|
||||
Guid newGuid();
|
||||
|
||||
#ifdef GUID_ANDROID
|
||||
private:
|
||||
JNIEnv *_env;
|
||||
jclass _uuidClass;
|
||||
jmethodID _newGuidMethod;
|
||||
jmethodID _mostSignificantBitsMethod;
|
||||
jmethodID _leastSignificantBitsMethod;
|
||||
#endif
|
||||
};
|
||||
Reference in New Issue
Block a user