Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooStringView.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN, Jan 2022
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef roofit_roofitcore_RooStringView_h
14#define roofit_roofitcore_RooStringView_h
15
16#include <string_view>
17#include <TString.h>
18
19#include <string>
20
21/// The RooStringView is a wrapper around a C-style string that can also be
22/// constructed from a `std::string` or a TString. As such, it serves as a
23/// drop-in replacement for `const char*` in public RooFit interfaces, keeping
24/// the possibility to pass a C-style string without copying but also accepting
25/// a `std::string`.
26
28public:
29 RooStringView(const char *str) : _cstr{str} {}
30 RooStringView(TString const &str) : _cstr{str} {}
31 RooStringView(std::string const &str) : _cstr{str.c_str()} {}
32 // If the string is a temporary, we have to store it ourselves, otherwise the C-style string would be invalid.
33 RooStringView(std::string &&str) : _strp{std::make_shared<std::string>(std::move(str))}, _cstr{_strp->c_str()} {}
34 const char * c_str() const { return _cstr; }
35 operator const char *() { return _cstr; }
36 operator std::string_view() { return _cstr; }
37
38private:
39 std::shared_ptr<std::string> _strp;
40 const char *_cstr;
41};
42
43#endif
The RooStringView is a wrapper around a C-style string that can also be constructed from a std::strin...
const char * c_str() const
RooStringView(std::string &&str)
RooStringView(const char *str)
std::shared_ptr< std::string > _strp
RooStringView(TString const &str)
const char * _cstr
RooStringView(std::string const &str)
Basic string class.
Definition TString.h:139