<commit_before>// ======================================================================== //
// Copyright 2009-2018 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.                                           //
// ======================================================================== //

#pragma once

// ospray
#include "ospray/common/OSPCommon.h"
// ospray::sg
#include "sg/common/TimeStamp.h"
#include "sg/common/Serialization.h"
#include "sg/common/RuntimeError.h"
// stl
#include <map>
// xml
#include "common/xml/XML.h"

namespace ospray {
  namespace sg {

    using intex_t = unsigned long long;

    struct OSPSG_INTERFACE DataBuffer : public Node
    {
      DataBuffer(OSPDataType type)
        : type(type), data(nullptr)
      {}

      virtual ~DataBuffer() override = default;

      virtual std::string toString() const override
      { return "DataBuffer<abstract>"; }

      virtual void postCommit(RenderContext &) override
      {
        if (hasParent()) {
          if (parent().value().is<OSPObject>())
            ospSetData(parent().valueAs<OSPObject>(), name().c_str(), getOSP());
        }
      }

      template <typename T>
      T get(index_t idx) const { return ((T*)base())[idx]; }

      virtual void*  base()  const = 0;
      virtual size_t size()  const = 0;
      virtual size_t bytesPerElement() const = 0;

      template <typename T>
      inline T* baseAs() const { return static_cast<T*>(base()); }

      OSPDataType getType()  const { return type; }
      OSPData     getOSP()
      {
        if (!empty() && !data) {
          if (type == OSP_RAW) {
            data = ospNewData(numBytes(), type, base(),
                              OSP_DATA_SHARED_BUFFER);
          }
          else
            data = ospNewData(size(), type, base(), OSP_DATA_SHARED_BUFFER);

          ospCommit(data);
        }

        return data;
      }

      bool   empty()    const { return size() == 0; }
      size_t numBytes() const { return size() * bytesPerElement(); }

      OSPDataType type;
      OSPData     data;
    };

    // -------------------------------------------------------
    // data *ARRAYS*
    // -------------------------------------------------------
    template<typename T, long int TID>
    struct DataArrayT : public DataBuffer
    {
      DataArrayT(T *base, size_t size, bool mine = true)
        : DataBuffer((OSPDataType)TID), numElements(size),
          mine(mine), base_ptr(base) {}<commit_msg>fix leaking OSPData objects in scene graph<commit_after>// ======================================================================== //
// Copyright 2009-2018 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.                                           //
// ======================================================================== //

#pragma once

// ospray
#include "ospray/common/OSPCommon.h"
// ospray::sg
#include "sg/common/TimeStamp.h"
#include "sg/common/Serialization.h"
#include "sg/common/RuntimeError.h"
// stl
#include <map>
// xml
#include "common/xml/XML.h"

namespace ospray {
  namespace sg {

    using intex_t = unsigned long long;

    struct OSPSG_INTERFACE DataBuffer : public Node
    {
      DataBuffer(OSPDataType type)
        : type(type)
      {}

      virtual ~DataBuffer() override
      {
        if(data) ospRelease(data);
      }

      virtual std::string toString() const override
      { return "DataBuffer<abstract>"; }

      virtual void postCommit(RenderContext &) override
      {
        if (hasParent()) {
          if (parent().value().is<OSPObject>())
            ospSetData(parent().valueAs<OSPObject>(), name().c_str(), getOSP());
        }
      }

      template <typename T>
      T get(index_t idx) const { return ((T*)base())[idx]; }

      virtual void*  base()  const = 0;
      virtual size_t size()  const = 0;
      virtual size_t bytesPerElement() const = 0;

      template <typename T>
      inline T* baseAs() const { return static_cast<T*>(base()); }

      OSPDataType getType()  const { return type; }
      OSPData     getOSP()
      {
        if (!empty() && !data) {
          if (type == OSP_RAW) {
            data = ospNewData(numBytes(), type, base(),
                              OSP_DATA_SHARED_BUFFER);
          }
          else
            data = ospNewData(size(), type, base(), OSP_DATA_SHARED_BUFFER);

          ospCommit(data);
        }

        return data;
      }

      bool   empty()    const { return size() == 0; }
      size_t numBytes() const { return size() * bytesPerElement(); }

      OSPDataType type;
      OSPData     data {nullptr};
    };

    // -------------------------------------------------------
    // data *ARRAYS*
    // -------------------------------------------------------
    template<typename T, long int TID>
    struct DataArrayT : public DataBuffer
    {
      DataArrayT(T *base, size_t size, bool mine = true)
        : DataBuffer((OSPDataType)TID), numElements(size),
          mine(mine), base_ptr(base) {}
{'fraction_non_alphanumeric': 0.18056371109806224, 'fraction_numerical': 0.004697592483852026, 'mean_word_length': 1.5242682474990736, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '788041', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1669, 'n_words': 485}