Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:27

0001 /*
0002  * Project: RooFit
0003  * Authors:
0004  *   Jonas Rembser, CERN, Jan 2022
0005  *
0006  * Copyright (c) 2022, CERN
0007  *
0008  * Redistribution and use in source and binary forms,
0009  * with or without modification, are permitted according to the terms
0010  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
0011  */
0012 
0013 #ifndef roofit_roofitcore_RooStringView_h
0014 #define roofit_roofitcore_RooStringView_h
0015 
0016 #include <string_view>
0017 #include <TString.h>
0018 
0019 #include <string>
0020 
0021 /// The RooStringView is a wrapper around a C-style string that can also be
0022 /// constructed from a `std::string` or a TString. As such, it serves as a
0023 /// drop-in replacement for `const char*` in public RooFit interfaces, keeping
0024 /// the possibility to pass a C-style string without copying but also accepting
0025 /// a `std::string`.
0026 
0027 class RooStringView {
0028 public:
0029    RooStringView(const char *str) : _cstr{str} {}
0030    RooStringView(TString const &str) : _cstr{str} {}
0031    RooStringView(std::string const &str) : _cstr{str.c_str()} {}
0032    // If the string is a temporary, we have to store it ourselves, otherwise the C-style string would be invalid.
0033    RooStringView(std::string &&str) : _strp{std::make_shared<std::string>(std::move(str))}, _cstr{_strp->c_str()} {}
0034    const char * c_str() const { return _cstr; }
0035    operator const char *() { return _cstr; }
0036    operator std::string_view() { return _cstr; }
0037 
0038 private:
0039    std::shared_ptr<std::string> _strp;
0040    const char *_cstr;
0041 };
0042 
0043 #endif