1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "antlr4-common.h"
12 std::string join(std::vector<std::string> strings, const std::string &separator);
13 std::map<std::string, size_t> toMap(const std::vector<std::string> &keys);
14 std::string escapeWhitespace(std::string str, bool escapeSpaces);
15 std::string toHexString(const int t);
16 std::string arrayToString(const std::vector<std::string> &data);
17 std::string replaceString(const std::string &s, const std::string &from, const std::string &to);
18 std::vector<std::string> split(const std::string &s, const std::string &sep, int count);
19 std::string indent(const std::string &s, const std::string &indentation, bool includingFirst = true);
21 // Using RAII + a lambda to implement a "finally" replacement.
23 FinalAction(std::function<void ()> f) : _cleanUp { f } {}
24 FinalAction(FinalAction &&other) :
25 _cleanUp(std::move(other._cleanUp)), _enabled(other._enabled) {
26 other._enabled = false; // Don't trigger the lambda after ownership has moved.
28 ~FinalAction() { if (_enabled) _cleanUp(); }
30 void disable() { _enabled = false; }
32 std::function<void ()> _cleanUp;
36 ANTLR4CPP_PUBLIC FinalAction finally(std::function<void ()> f);
38 // Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
39 template <typename T1, typename T2>
40 inline bool is(T2 *obj) { // For pointer types.
41 return dynamic_cast<typename std::add_const<T1>::type>(obj) != nullptr;
44 template <typename T1, typename T2>
45 inline bool is(Ref<T2> const& obj) { // For shared pointers.
46 return dynamic_cast<T1 *>(obj.get()) != nullptr;
50 std::string toString(const T &o) {
52 // typeid gives the mangled class name, but that's all what's possible
54 ss << typeid(o).name() << "@" << std::hex << reinterpret_cast<uintptr_t>(&o);
58 // Get the error text from an exception pointer or the current exception.
59 std::string what(std::exception_ptr eptr = std::current_exception());
61 class SingleWriteMultipleReadLock {
69 std::condition_variable _readerGate;
70 std::condition_variable _writerGate;
73 size_t _activeReaders = 0;
74 size_t _waitingWriters = 0;
75 size_t _activeWriters = 0;
78 } // namespace antlrcpp