xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/gdb_unique_ptr.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* std::unique_ptr specializations for GDB.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2016-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #ifndef COMMON_GDB_UNIQUE_PTR_H
217d62b00eSchristos #define COMMON_GDB_UNIQUE_PTR_H
227d62b00eSchristos 
237d62b00eSchristos #include <memory>
24*6881a400Schristos #include <string>
25*6881a400Schristos #include "gdbsupport/gdb-xfree.h"
267d62b00eSchristos 
277d62b00eSchristos namespace gdb
287d62b00eSchristos {
297d62b00eSchristos /* Define gdb::unique_xmalloc_ptr, a std::unique_ptr that manages
307d62b00eSchristos    xmalloc'ed memory.  */
317d62b00eSchristos 
327d62b00eSchristos /* The deleter for std::unique_xmalloc_ptr.  Uses xfree.  */
337d62b00eSchristos template <typename T>
347d62b00eSchristos struct xfree_deleter
357d62b00eSchristos {
367d62b00eSchristos   void operator() (T *ptr) const { xfree (ptr); }
377d62b00eSchristos };
387d62b00eSchristos 
397d62b00eSchristos /* Same, for arrays.  */
407d62b00eSchristos template <typename T>
417d62b00eSchristos struct xfree_deleter<T[]>
427d62b00eSchristos {
437d62b00eSchristos   void operator() (T *ptr) const { xfree (ptr); }
447d62b00eSchristos };
457d62b00eSchristos 
467d62b00eSchristos /* Import the standard unique_ptr to our namespace with a custom
477d62b00eSchristos    deleter.  */
487d62b00eSchristos 
497d62b00eSchristos template<typename T> using unique_xmalloc_ptr
507d62b00eSchristos   = std::unique_ptr<T, xfree_deleter<T>>;
517d62b00eSchristos 
527d62b00eSchristos /* A no-op deleter.  */
537d62b00eSchristos template<typename T>
547d62b00eSchristos struct noop_deleter
557d62b00eSchristos {
567d62b00eSchristos   void operator() (T *ptr) const { }
577d62b00eSchristos };
587d62b00eSchristos 
597d62b00eSchristos } /* namespace gdb */
607d62b00eSchristos 
617d62b00eSchristos /* Dup STR and return a unique_xmalloc_ptr for the result.  */
627d62b00eSchristos 
637d62b00eSchristos static inline gdb::unique_xmalloc_ptr<char>
647d62b00eSchristos make_unique_xstrdup (const char *str)
657d62b00eSchristos {
667d62b00eSchristos   return gdb::unique_xmalloc_ptr<char> (xstrdup (str));
677d62b00eSchristos }
687d62b00eSchristos 
69*6881a400Schristos /* Dup the first N characters of STR and return a unique_xmalloc_ptr
70*6881a400Schristos    for the result.  The result is always \0-terminated.  */
71*6881a400Schristos 
72*6881a400Schristos static inline gdb::unique_xmalloc_ptr<char>
73*6881a400Schristos make_unique_xstrndup (const char *str, size_t n)
74*6881a400Schristos {
75*6881a400Schristos   return gdb::unique_xmalloc_ptr<char> (xstrndup (str, n));
76*6881a400Schristos }
77*6881a400Schristos 
78*6881a400Schristos /* An overload of operator+= fo adding gdb::unique_xmalloc_ptr<char> to a
79*6881a400Schristos    std::string.  */
80*6881a400Schristos 
81*6881a400Schristos static inline std::string &
82*6881a400Schristos operator+= (std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
83*6881a400Schristos {
84*6881a400Schristos   return lhs += rhs.get ();
85*6881a400Schristos }
86*6881a400Schristos 
87*6881a400Schristos /* An overload of operator+ for adding gdb::unique_xmalloc_ptr<char> to a
88*6881a400Schristos    std::string.  */
89*6881a400Schristos 
90*6881a400Schristos static inline std::string
91*6881a400Schristos operator+ (const std::string &lhs, const gdb::unique_xmalloc_ptr<char> &rhs)
92*6881a400Schristos {
93*6881a400Schristos   return lhs + rhs.get ();
94*6881a400Schristos }
95*6881a400Schristos 
967d62b00eSchristos #endif /* COMMON_GDB_UNIQUE_PTR_H */
97