forked from microsoft/MSIX-PackageSupportFramework
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpsf_config.h
More file actions
624 lines (527 loc) · 19.1 KB
/
psf_config.h
File metadata and controls
624 lines (527 loc) · 19.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include <typeinfo>
#include "utilities.h"
namespace psf
{
struct json_null;
struct json_string;
struct json_number;
struct json_boolean;
struct json_object;
struct json_array;
#define JSON_TYPES \
JSON_TYPE(null), \
JSON_TYPE(string), \
JSON_TYPE(number), \
JSON_TYPE(boolean), \
JSON_TYPE(object), \
JSON_TYPE(array)
#define JSON_TYPE(t) t
enum class json_type { JSON_TYPES };
#undef JSON_TYPE
#define JSON_TYPE(t) #t
static constexpr std::string_view json_type_names[] = { JSON_TYPES };
namespace details
{
template <typename Interface>
struct json_type_from_interface;
template <> struct json_type_from_interface<json_null> { static constexpr json_type value = json_type::null; };
template <> struct json_type_from_interface<json_string> { static constexpr json_type value = json_type::string; };
template <> struct json_type_from_interface<json_number> { static constexpr json_type value = json_type::number; };
template <> struct json_type_from_interface<json_boolean> { static constexpr json_type value = json_type::boolean; };
template <> struct json_type_from_interface<json_object> { static constexpr json_type value = json_type::object; };
template <> struct json_type_from_interface<json_array> { static constexpr json_type value = json_type::array; };
template <typename Interface>
constexpr json_type json_type_v = json_type_from_interface<Interface>::value;
}
struct json_value
{
virtual ~json_value() {}
virtual const json_type type() const noexcept = 0;
virtual const json_null* try_as_null() const noexcept = 0;
virtual const json_string* try_as_string() const noexcept = 0;
virtual const json_number* try_as_number() const noexcept = 0;
virtual const json_boolean* try_as_boolean() const noexcept = 0;
virtual const json_object* try_as_object() const noexcept = 0;
virtual const json_array* try_as_array() const noexcept = 0;
// Convenience functions
template <typename T>
const T* try_as() const noexcept
{
using type = std::remove_const_t<T>;
if constexpr (std::is_same_v<type, json_null>)
{
return try_as_null();
}
else if constexpr (std::is_same_v<type, json_string>)
{
return try_as_string();
}
else if constexpr (std::is_same_v<type, json_number>)
{
return try_as_number();
}
else if constexpr (std::is_same_v<type, json_boolean>)
{
return try_as_boolean();
}
else if constexpr (std::is_same_v<type, json_object>)
{
return try_as_object();
}
else if constexpr (std::is_same_v<type, json_array>)
{
return try_as_array();
}
else
{
// static_assert(false, "Invalid JSON type");
}
}
const json_null& as_null() const
{
return as<json_null>();
}
const json_string& as_string() const
{
return as<json_string>();
}
const json_number& as_number() const
{
return as<json_number>();
}
const json_boolean& as_boolean() const
{
return as<json_boolean>();
}
const json_object& as_object() const
{
return as<json_object>();
}
const json_array& as_array() const
{
return as<json_array>();
}
template <typename T>
const T& as() const
{
using namespace std::literals;
auto result = try_as<T>();
if (!result)
{
std::string source{ json_type_names[static_cast<std::underlying_type_t<json_type>>(json_type())] };
std::string target{ json_type_names[static_cast<std::underlying_type_t<json_type>>(details::json_type_v<T>)] };
auto error{ "Invalid JSON cast: cannot convert from "s + source + " to "s + target };
throw std::runtime_error(error);
}
return *result;
}
};
namespace details
{
template <typename Interface>
struct json_value_base : json_value
{
virtual const json_type type() const noexcept override final
{
return json_type_v<Interface>;
}
virtual const json_null* try_as_null() const noexcept override final
{
return try_as_base<json_null>();
}
virtual const json_string* try_as_string() const noexcept override final
{
return try_as_base<json_string>();
}
virtual const json_number* try_as_number() const noexcept override final
{
return try_as_base<json_number>();
}
virtual const json_boolean* try_as_boolean() const noexcept override final
{
return try_as_base<json_boolean>();
}
virtual const json_object* try_as_object() const noexcept override final
{
return try_as_base<json_object>();
}
virtual const json_array* try_as_array() const noexcept override final
{
return try_as_base<json_array>();
}
private:
template <typename T>
const T* try_as_base() const noexcept
{
if constexpr (std::is_same_v<T, Interface>)
{
return static_cast<const Interface*>(this);
}
else
{
static_assert(json_type_v<T> != json_type_v<Interface>);
return nullptr;
}
}
};
}
struct json_null : details::json_value_base<json_null>
{
};
struct json_string : details::json_value_base<json_string>
{
virtual const char* narrow(_Out_opt_ unsigned* length = nullptr) const noexcept = 0;
virtual const wchar_t* wide(_Out_opt_ unsigned* length = nullptr) const noexcept = 0;
// Convenience functions
std::string_view string() const noexcept
{
unsigned size;
auto str = narrow(&size);
return { str, size };
}
std::wstring_view wstring() const noexcept
{
unsigned size;
auto str = wide(&size);
return { str, size };
}
};
struct json_number : details::json_value_base<json_number>
{
virtual std::uint64_t get_unsigned() const noexcept = 0;
virtual std::int64_t get_signed() const noexcept = 0;
virtual double get_float() const noexcept = 0;
// Convenience functions
template <
typename T,
std::enable_if_t<std::disjunction_v<std::is_floating_point<T>, std::is_arithmetic<T>>, int> = 0>
auto get() const noexcept
{
if constexpr (std::is_floating_point_v<T>)
{
return static_cast<T>(get_float());
}
else if constexpr (std::is_signed_v<T>)
{
return static_cast<T>(get_signed());
}
else
{
return static_cast<T>(get_unsigned());
}
}
};
struct json_boolean : details::json_value_base<json_boolean>
{
virtual bool get() const noexcept = 0;
// Convenience functions
explicit operator bool() const noexcept
{
// E.g. `if (*jsonBool) { ... }`
return get();
}
};
struct json_object : details::json_value_base<json_object>
{
virtual const json_value* try_get(_In_ const char* key) const noexcept = 0;
// Iteration support. All values have been exhausted when either begin_enumeration or advance return null. Early
// termination of enumeration must call cancel_enumeration to free resources. Example:
// enumeration_data data;
// for (auto it = obj->begin_enumeration(&data); it; it = obj->advance(it, &data))
// {
// if (some_condition(data))
// {
// obj->cancel_enumeration(it);
// break;
// }
// }
struct enumeration_handle;
struct enumeration_data
{
const char* key;
unsigned key_length;
json_value* value;
};
virtual enumeration_handle* begin_enumeration(_Out_ enumeration_data* data) const noexcept = 0;
virtual enumeration_handle* advance(_In_ enumeration_handle* handle, _Inout_ enumeration_data* data) const noexcept = 0;
virtual void cancel_enumeration(_In_ enumeration_handle* handle) const noexcept = 0;
// Convenience functions
const json_value& get(_In_ const char* key) const
{
auto result = try_get(key);
if (!result)
{
auto message = std::string{ "Key '" } + key + "' does not exist in the JSON object";
throw std::out_of_range(message);
}
return *result;
}
struct iterator
{
using difference_type = int;
using value_type = const std::pair<std::string_view, const json_value&>;
using pointer = value_type*;
using reference = value_type;
using iterator_category = std::input_iterator_tag;
iterator(const json_object* object) noexcept :
object(object)
{
}
iterator(const json_object* object, enumeration_handle* handle, const enumeration_data& data) noexcept :
object(object),
handle(handle),
data(data)
{
}
iterator(iterator&& other) noexcept :
iterator(other.object, other.handle, other.data)
{
other.handle = nullptr;
other.data = {};
}
iterator& operator=(iterator&& other) noexcept
{
std::swap(object, other.object);
std::swap(handle, other.handle);
std::swap(data, other.data);
}
// NOTE: enumeration_handles are not copy-able, so for the time being we must remain, at best, an
// InputIterator
iterator(const iterator&) = delete;
iterator& operator=(const iterator&) = delete;
~iterator()
{
if (handle)
{
object->cancel_enumeration(handle);
}
}
struct proxy_result
{
value_type value;
reference operator*() const noexcept
{
return value;
}
pointer operator->() const noexcept
{
return &value;
}
};
// InputIterator
iterator& operator++() noexcept
{
handle = object->advance(handle, &data);
return *this;
}
auto operator++(int) noexcept
{
proxy_result result{ **this };
++(*this);
return result;
}
reference operator*() const noexcept
{
return { std::string_view(data.key, data.key_length), *data.value };
}
auto operator->() const noexcept
{
return proxy_result{ **this };
}
friend bool operator==(const iterator& lhs, const iterator& rhs) noexcept
{
// NOTE: handles might not compare equal, but the memory they point to will
assert(lhs.object == rhs.object);
return lhs.data.key == rhs.data.key;
}
friend bool operator!=(const iterator& lhs, const iterator& rhs) noexcept
{
return !(lhs == rhs);
}
const json_object* object;
enumeration_handle* handle = nullptr;
enumeration_data data = {};
};
using const_iterator = iterator;
iterator begin() const noexcept
{
enumeration_data data;
auto handle = begin_enumeration(&data);
return iterator{ this, handle, data };
}
const_iterator cbegin() const noexcept
{
return begin();
}
iterator end() const noexcept
{
return iterator{ this };
}
const_iterator cend() const noexcept
{
return end();
}
};
struct json_array : details::json_value_base<json_array>
{
virtual unsigned size() const noexcept = 0;
virtual const json_value* try_get_at(unsigned index) const noexcept = 0;
// Convenience functions
const json_value& get_at(unsigned index) const
{
auto result = try_get_at(index);
if (!result)
{
throw std::out_of_range("Index out of range");
}
return *result;
}
const json_value& operator[](unsigned index) const
{
return get_at(index);
}
struct iterator
{
using difference_type = int;
using value_type = const json_value;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::random_access_iterator_tag;
// InputIterator
iterator& operator++() noexcept
{
return (*this) += 1;
}
reference operator*() const noexcept
{
return (*this)[0];
}
pointer operator->() const noexcept
{
return &(**this);
}
friend bool operator==(const iterator& lhs, const iterator& rhs) noexcept
{
assert(lhs.array == rhs.array);
return lhs.index == rhs.index;
}
friend bool operator!=(const iterator& lhs, const iterator& rhs) noexcept
{
return !(lhs == rhs);
}
// ForwardIterator
iterator operator++(int) noexcept
{
auto copy = *this;
++(*this);
return copy;
}
// BidirectionalIterator
iterator& operator--() noexcept
{
return (*this) -= 1;
}
iterator operator--(int) noexcept
{
auto copy = *this;
--(*this);
return copy;
}
// RandomAccessIterator
iterator& operator+=(difference_type diff) noexcept
{
index += diff;
assert(index <= array->size());
return *this;
}
iterator& operator-=(difference_type diff) noexcept
{
return (*this) += -diff;
}
friend iterator operator+(iterator lhs, difference_type rhs) noexcept
{
return lhs += rhs;
}
friend iterator operator-(iterator lhs, difference_type rhs) noexcept
{
return lhs -= rhs;
}
friend iterator operator+(difference_type lhs, iterator rhs) noexcept
{
return rhs += lhs;
}
friend difference_type operator-(const iterator& lhs, const iterator& rhs) noexcept
{
return static_cast<difference_type>(lhs.index) - static_cast<difference_type>(rhs.index);
}
reference operator[](difference_type offset) const noexcept
{
assert((index + offset) < array->size());
return array->get_at(index + offset);
}
friend bool operator<(const iterator& lhs, const iterator& rhs) noexcept
{
assert(lhs.array == rhs.array);
return lhs.index < rhs.index;
}
friend bool operator>(const iterator& lhs, const iterator& rhs) noexcept
{
assert(lhs.array == rhs.array);
return lhs.index > rhs.index;
}
friend bool operator<=(const iterator& lhs, const iterator& rhs) noexcept
{
return !(lhs > rhs);
}
friend bool operator>=(const iterator& lhs, const iterator& rhs) noexcept
{
return !(lhs < rhs);
}
const json_array* array;
unsigned index;
};
using const_iterator = iterator;
iterator begin() const noexcept
{
return iterator{ this, 0 };
}
const_iterator cbegin() const noexcept
{
return begin();
}
iterator end() const noexcept
{
return iterator{ this, size() };
}
const_iterator cend() const noexcept
{
return end();
}
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = reverse_iterator;
reverse_iterator rbegin() const noexcept
{
return reverse_iterator(end());
}
const_reverse_iterator crbegin() const noexcept
{
return rbegin();
}
reverse_iterator rend() const noexcept
{
return reverse_iterator(begin());
}
const_reverse_iterator crend() const noexcept
{
return rend();
}
};
}