]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/support/CPPUtils.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / support / CPPUtils.h
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.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlrcpp {
11
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);
20
21   // Using RAII + a lambda to implement a "finally" replacement.
22   struct FinalAction {
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.
27     }
28     ~FinalAction() { if (_enabled) _cleanUp(); }
29
30     void disable() { _enabled = false; }
31   private:
32     std::function<void ()> _cleanUp;
33     bool _enabled {true};
34   };
35
36   ANTLR4CPP_PUBLIC FinalAction finally(std::function<void ()> f);
37
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;
42   }
43
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;
47   }
48
49   template <typename T>
50   std::string toString(const T &o) {
51     std::stringstream ss;
52     // typeid gives the mangled class name, but that's all what's possible
53     // in a portable way.
54     ss << typeid(o).name() << "@" << std::hex << reinterpret_cast<uintptr_t>(&o);
55     return ss.str();
56   }
57
58   // Get the error text from an exception pointer or the current exception.
59   std::string what(std::exception_ptr eptr = std::current_exception());
60
61   class SingleWriteMultipleReadLock {
62   public:
63     void readLock();
64     void readUnlock();
65     void writeLock();
66     void writeUnlock();
67
68   private:
69     std::condition_variable _readerGate;
70     std::condition_variable _writerGate;
71
72     std::mutex _mutex;
73     size_t _activeReaders = 0;
74     size_t _waitingWriters = 0;
75     size_t _activeWriters = 0;
76   };
77
78 } // namespace antlrcpp