|
| 1 | +// |
| 2 | +// Graph.mm |
| 3 | +// OpenGraph_SPI |
| 4 | + |
| 5 | +#include "Graph.hpp" |
| 6 | +#include "Subgraph.hpp" |
| 7 | +#include "OGGraphDescription.h" |
| 8 | + |
| 9 | +#if OG_OBJC_FOUNDATION |
| 10 | +#include <dispatch/dispatch.h> |
| 11 | +#include <pthread.h> |
| 12 | +#include <Foundation/Foundation.h> |
| 13 | +#include <zlib.h> |
| 14 | + |
| 15 | +pthread_key_t OG::Graph::_current_update_key; |
| 16 | + |
| 17 | +OG::Graph::Graph() OG_NOEXCEPT { |
| 18 | + // TODO |
| 19 | + |
| 20 | + // libdispatch is not supported on WASI |
| 21 | + // Tracked via https://github.com/swiftwasm/swift/issues/5565 |
| 22 | + #if !OG_TARGET_OS_WASI |
| 23 | + static dispatch_once_t make_keys; |
| 24 | + dispatch_once_f(&make_keys, nullptr, [](void * _Nullable context){ |
| 25 | + pthread_key_create(&_current_update_key, nullptr); |
| 26 | + OG::Subgraph::make_current_subgraph_key(); |
| 27 | + }); |
| 28 | + #endif |
| 29 | + |
| 30 | + // TODO |
| 31 | +} |
| 32 | + |
| 33 | +const void OG::Graph::value_mark_all() const OG_NOEXCEPT { |
| 34 | + // TODO |
| 35 | +} |
| 36 | + |
| 37 | +void OG::Graph::all_start_profiling(uint32_t) OG_NOEXCEPT { |
| 38 | + // TODO |
| 39 | +} |
| 40 | +void OG::Graph::all_stop_profiling() OG_NOEXCEPT { |
| 41 | + // TODO |
| 42 | +} |
| 43 | +void OG::Graph::start_profiling(uint32_t) OG_NOEXCEPT { |
| 44 | + // TODO |
| 45 | +} |
| 46 | +void OG::Graph::stop_profiling() OG_NOEXCEPT { |
| 47 | + // TODO |
| 48 | +} |
| 49 | + |
| 50 | +void OG::Graph::write_to_file(const Graph * _Nullable graph, const char * _Nullable name, uint8_t options) OG_NOEXCEPT { |
| 51 | + @autoreleasepool { |
| 52 | + NSDictionary <NSString *, id> *options_dict = @{ |
| 53 | + (__bridge NSString *)OGDescriptionFormat: (__bridge NSString *)OGDescriptionFormatDictionary, |
| 54 | + (__bridge NSString *)OGDescriptionIncludeValues: @((options & 1) == 0), |
| 55 | + (__bridge NSString *)OGDescriptionAllGraphs: @(graph == nil) |
| 56 | + }; |
| 57 | + NSString *description = (__bridge NSString *)Graph::description(graph, options_dict); |
| 58 | + if (description == nil) { |
| 59 | + return; |
| 60 | + } |
| 61 | + const char *cstring_name = name ?: "graph.ag-gzon"; |
| 62 | + |
| 63 | + NSData *data = [NSJSONSerialization dataWithJSONObject:description options:0 error:nil]; |
| 64 | + NSString *file_path = [NSString stringWithUTF8String:cstring_name]; |
| 65 | + if (cstring_name[0] != '/') { |
| 66 | + file_path = [NSTemporaryDirectory() stringByAppendingPathComponent:file_path]; |
| 67 | + } |
| 68 | + NSError *error = nil; |
| 69 | + BOOL success = YES; |
| 70 | + if ([file_path.pathExtension isEqualToString:@"ag-gzon"]) { |
| 71 | + gzFile file = gzopen(file_path.fileSystemRepresentation, "wb"); |
| 72 | + if (file != NULL) { |
| 73 | + const char *bytes = (const char *)[data bytes]; |
| 74 | + NSUInteger remaining = [data length]; |
| 75 | + while (remaining != 0) { |
| 76 | + int bytes_written = gzwrite(file, bytes, (unsigned int)remaining); |
| 77 | + if (bytes_written <= 0) { |
| 78 | + success = NO; |
| 79 | + break; |
| 80 | + } |
| 81 | + bytes += bytes_written; |
| 82 | + remaining -= bytes_written; |
| 83 | + } |
| 84 | + gzclose(file); |
| 85 | + } |
| 86 | + } else { |
| 87 | + success = [data writeToFile:file_path options:0 error:&error]; |
| 88 | + } |
| 89 | + if (success) { |
| 90 | + fprintf(stderr, "Wrote graph data to \"%s\".\n", file_path.UTF8String); |
| 91 | + } else { |
| 92 | + fprintf(stderr, "Unable to write to \"%s\": %s\n", file_path.UTF8String, error.description.UTF8String); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +const bool OG::Graph::thread_is_updating() const OG_NOEXCEPT { |
| 98 | + void *current = pthread_getspecific(current_key()); |
| 99 | + if (!current) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + // TODO |
| 103 | + return false; |
| 104 | +} |
| 105 | + |
| 106 | +const bool OG::Graph::is_context_updating(const OG::Graph::Context&) const OG_NOEXCEPT { |
| 107 | + // TODO |
| 108 | + return false; |
| 109 | +} |
| 110 | +#endif /* OG_OBJC_FOUNDATION */ |
0 commit comments