-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
154 lines (128 loc) · 3.83 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef OTEL_CLR_PROFILER_UTIL_H_
#define OTEL_CLR_PROFILER_UTIL_H_
#include <algorithm>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "string.h"
namespace trace
{
// Split splits a string by the given delimiter.
std::vector<WSTRING> Split(const WSTRING& s, wchar_t delim);
// Trim removes space from the beginning and end of a string.
WSTRING Trim(const WSTRING& str);
// GetEnvironmentValue returns the environment variable value for the given
// name. Space is trimmed.
WSTRING GetEnvironmentValue(const WSTRING& name);
// GetConfiguredSize returns the environment variable value for the given name, or default value
// if not configured, or misconfigured
size_t GetConfiguredSize(const WSTRING& name, size_t default_value);
// GetEnvironmentValues returns environment variable values for the given name
// split by the delimiter. Space is trimmed and empty values are ignored.
std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name, const wchar_t delim);
// GetEnvironmentValues calls GetEnvironmentValues with a semicolon delimiter.
std::vector<WSTRING> GetEnvironmentValues(const WSTRING& name);
// GetEnvironmentVariables returns list of all environment variable
std::vector<WSTRING> GetEnvironmentVariables(const std::vector<WSTRING> &prefixes);
// Convert Hex to string
WSTRING HexStr(const void* data, int len);
// Convert Token to string
WSTRING TokenStr(const mdToken* token);
// Convert HRESULT to a friendly string, e.g.: "0x80000002"
WSTRING HResultStr(const HRESULT hr);
WSTRING VersionStr(const USHORT major, const USHORT minor, const USHORT build, const USHORT revision);
WSTRING AssemblyVersionStr(const ASSEMBLYMETADATA& assembly_metadata);
template <class Container>
bool Contains(const Container& items, const typename Container::value_type& value)
{
return std::find(items.begin(), items.end(), value) != items.end();
}
// Singleton definition
class UnCopyable
{
protected:
UnCopyable(){};
~UnCopyable(){};
private:
UnCopyable(const UnCopyable&) = delete;
UnCopyable(const UnCopyable&&) = delete;
UnCopyable& operator=(const UnCopyable&) = delete;
UnCopyable& operator=(const UnCopyable&&) = delete;
};
template <typename T>
class Singleton : public UnCopyable
{
public:
static T* Instance()
{
static T instance_obj;
return &instance_obj;
}
};
template <typename T>
class BlockingQueue : public UnCopyable
{
private:
std::queue<T> queue_;
mutable std::mutex mutex_;
std::condition_variable condition_;
public:
T pop()
{
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty())
{
condition_.wait(mlock);
}
T value = queue_.front();
queue_.pop();
return value;
}
void push(const T& item)
{
{
std::lock_guard<std::mutex> guard(mutex_);
queue_.push(item);
}
condition_.notify_one();
}
};
template <typename T>
class UniqueBlockingQueue : public UnCopyable
{
private:
std::queue<std::unique_ptr<T>> queue_;
mutable std::mutex mutex_;
std::condition_variable condition_;
public:
std::unique_ptr<T> pop()
{
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty())
{
condition_.wait(mlock);
}
std::unique_ptr<T> value = std::move(queue_.front());
queue_.pop();
return value;
}
void push(std::unique_ptr<T>&& item)
{
{
std::lock_guard<std::mutex> guard(mutex_);
queue_.push(std::move(item));
}
condition_.notify_one();
}
};
} // namespace trace
#endif // OTEL_CLR_PROFILER_UTIL_H_