Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce ROOT::ROwningPtr
  • Loading branch information
vepadulano authored and guitargeek committed Dec 7, 2023
commit b8d5795be69eb810193ee0afbb72a1934b87cd4b
1 change: 1 addition & 0 deletions core/foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set_property(TARGET Core APPEND PROPERTY DICT_HEADERS
ThreadLocalStorage.h
ROOT/RLogger.hxx
ROOT/RNotFn.hxx
ROOT/ROwningPtr.hxx
ROOT/RRangeCast.hxx
ROOT/RSpan.hxx
ROOT/RStringView.hxx
Expand Down
43 changes: 43 additions & 0 deletions core/foundation/inc/ROOT/ROwningPtr.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
\file ROOT/ROwningPtr.hxx
\ingroup core
\author Jonas Rembser
\author Vincenzo Eduardo Padulano
\date 2022-08
*/

/*************************************************************************
* Copyright (C) 1995-2022, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/

#ifndef ROOT_ROWNINGPTR
#define ROOT_ROWNINGPTR

#include <memory>
namespace ROOT {

template <typename T, typename = std::enable_if_t<std::is_pointer<T>::value>>
class ROwningPtr {
public:
using pointer = T;
using element_type = typename std::remove_pointer<pointer>::type;
using reference = typename std::add_lvalue_reference<element_type>::type;

ROwningPtr(T ptr) : _ptr{ptr} {}

operator pointer() { return _ptr; }
operator std::unique_ptr<element_type>() { return std::unique_ptr<element_type>{_ptr}; }

reference operator*() const { return *_ptr; }
pointer operator->() const { return _ptr; }

private:
T _ptr;
};

} // namespace ROOT
#endif
7 changes: 4 additions & 3 deletions io/io/inc/TFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "TDirectoryFile.h"
#include "TUrl.h"
#include "ROOT/RConcurrentHashColl.hxx"
#include "ROOT/ROwningPtr.hxx"

// Not a part of TFile interface; provide a forward declaration instead of #include.
// #ifndef R__LESS_INCLUDES
Expand Down Expand Up @@ -296,9 +297,9 @@ class TFile : public TDirectoryFile {
*AsyncOpen(const char *name, Option_t *option = "",
const char *ftitle = "", Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault,
Int_t netopt = 0);
static TFile *Open(const char *name, Option_t *option = "",
const char *ftitle = "", Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault,
Int_t netopt = 0);
static ROOT::ROwningPtr<TFile *> Open(const char *name, Option_t *option = "", const char *ftitle = "",
Int_t compress = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault,
Int_t netopt = 0);
static TFile *Open(TFileOpenHandle *handle);

static EFileType GetType(const char *name, Option_t *option = "", TString *prefix = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion io/io/src/TDirectoryFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ Int_t TDirectoryFile::SaveObjectAs(const TObject *obj, const char *filename, Opt
nbytes = TBufferJSON::ExportToFile(fname, obj, option);
} else {
TContext ctxt; // The TFile::Open will change the current directory.
auto *local = TFile::Open(fname.Data(), opt.Contains("a") ? "update" : "recreate");
TFile *local = TFile::Open(fname.Data(), opt.Contains("a") ? "update" : "recreate");
if (!local) return 0;
nbytes = obj->Write();
delete local;
Expand Down
4 changes: 2 additions & 2 deletions io/io/src/TFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4068,8 +4068,8 @@ TFile *TFile::OpenFromCache(const char *name, Option_t *, const char *ftitle,
/// In RECREATE mode, a nullptr is returned if the file can not be created.
/// In UPDATE mode, a nullptr is returned if the file cannot be created or opened.

TFile *TFile::Open(const char *url, Option_t *options, const char *ftitle,
Int_t compress, Int_t netopt)
ROOT::ROwningPtr<TFile *>
TFile::Open(const char *url, Option_t *options, const char *ftitle, Int_t compress, Int_t netopt)
{
TPluginHandler *h;
TFile *f = nullptr;
Expand Down