<commit_before>// Copyright 2012 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// @addtogroup wcore_platform
/// @{

/// @file

#include "wcore_platform.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <waffle/waffle_enum.h>
#include <waffle/glx/glx_platform.h>
#include <waffle/wayland/wayland_platform.h>
#include <waffle/x11_egl/xegl_platform.h>

#include "wcore_error.h"

static uint64_t id_counter = 0;

struct wcore_platform*
wcore_platform_create(int platform)
{
    struct wcore_platform *self;

    self = calloc(1, sizeof(*self));
    if (!self) {
        wcore_error(WAFFLE_OUT_OF_MEMORY);
        return NULL;
    }

    self->id = ++id_counter;
    if (self->id == 0) {
        fprintf(stderr, "waffle: error: internal counter wrapped around to 0");
        abort();
    }

    switch (platform) {
#ifdef WAFFLE_HAS_GLX
        case WAFFLE_PLATFORM_GLX:
            self->native = glx_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
#ifdef WAFFLE_HAS_WAYLAND
        case WAFFLE_PLATFORM_WAYLAND:
            self->native = wayland_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
#ifdef WAFFLE_HAS_X11_EGL
        case WAFFLE_PLATFORM_X11_EGL:
            self->native = xegl_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
        default:
            wcore_error_internal("bad value for platform (0x%x)", platform);
            goto error;
    }

    self->native_tag = platform;

    return self;

error:
    wcore_platform_destroy(self);
    return NULL;
}

bool
wcore_platform_destroy(struct wcore_platform *self)
{
    bool ok = true;

    if (!self)
        return true;

    switch (self->native_tag) {
#ifdef WAFFLE_HAS_GLX
        case WAFFLE_PLATFORM_GLX:
            ok &= glx_platform_destroy(self->native);
            break;
#endif
#ifdef WAFFLE_HAS_WAYLAND
        case WAFFLE_PLATFORM_WAYLAND:
            ok &= wayland_platform_destroy(self->native);
            break;
#endif
#ifdef WAFFLE_HAS_X11_EGL
        case WAFFLE_PLATFORM_X11_EGL:<commit_msg>core: Create and destroy cgl_platform<commit_after>// Copyright 2012 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// @addtogroup wcore_platform
/// @{

/// @file

#include "wcore_platform.h"

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <waffle/waffle_enum.h>
#include <waffle/cgl/cgl_platform.h>
#include <waffle/glx/glx_platform.h>
#include <waffle/wayland/wayland_platform.h>
#include <waffle/x11_egl/xegl_platform.h>

#include "wcore_error.h"

static uint64_t id_counter = 0;

struct wcore_platform*
wcore_platform_create(int platform)
{
    struct wcore_platform *self;

    self = calloc(1, sizeof(*self));
    if (!self) {
        wcore_error(WAFFLE_OUT_OF_MEMORY);
        return NULL;
    }

    self->id = ++id_counter;
    if (self->id == 0) {
        fprintf(stderr, "waffle: error: internal counter wrapped around to 0");
        abort();
    }

    switch (platform) {
#ifdef WAFFLE_HAS_CGL
        case WAFFLE_PLATFORM_CGL:
            self->native = cgl_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
#ifdef WAFFLE_HAS_GLX
        case WAFFLE_PLATFORM_GLX:
            self->native = glx_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
#ifdef WAFFLE_HAS_WAYLAND
        case WAFFLE_PLATFORM_WAYLAND:
            self->native = wayland_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
#ifdef WAFFLE_HAS_X11_EGL
        case WAFFLE_PLATFORM_X11_EGL:
            self->native = xegl_platform_create(&self->dispatch);
            if (!self->native)
                goto error;
            break;
#endif
        default:
            wcore_error_internal("bad value for platform (0x%x)", platform);
            goto error;
    }

    self->native_tag = platform;

    return self;

error:
    wcore_platform_destroy(self);
    return NULL;
}

bool
wcore_platform_destroy(struct wcore_platform *self)
{
    bool ok = true;

    if (!self)
        return true;

    switch (self->native_tag) {
#ifdef WAFFLE_HAS_CGL
        case WAFFLE_PLATFORM_CGL:
            ok &= cgl_platform_destroy(self->native);
            break;
#endif
#ifdef WAFFLE_HAS_GLX
        case WAFFLE_PLATFORM_GLX:
            ok &= glx_platform_destroy(self->native);
            break;
#endif
#ifdef WAFFLE_HAS_WAYLAND
        case WAFFLE_PLATFORM_WAYLAND:
            ok &= wayland_platform_destroy(self->native);
            break;
#endif
#ifdef WAFFLE_HAS_X11_EGL
        case WAFFLE_PLATFORM_X11_EGL:
{'fraction_non_alphanumeric': 0.10203395528660279, 'fraction_numerical': 0.008404773911581778, 'mean_word_length': 2.6458333333333335, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1664709', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1915, 'n_words': 481}