SHARE
TWEET

Untitled

a guest Jun 15th, 2012 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef CSTRING_VECTOR_HPP
  2. #define CSTRING_VECTOR_HPP
  3.  
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <string>
  7. #include <vector>
  8.  
  9. namespace utility {
  10.  
  11.     /// This is like vector<string> except with a C-friendly API. Or you can
  12.     /// think of it as like vector<char const*> except it automatically
  13.     /// cleans up its pointers.
  14.     //
  15.     // Inspired by http://forums.xkcd.com/viewtopic.php?p=3029979#p3029979
  16.    class cstring_vector
  17.    {
  18.       /// This vector manages the lifetimes and automatic deletions of the
  19.       /// strings themselves.
  20.       std::vector<std::string> std_string_buffer;
  21.  
  22.       /// This vector provides a view of std_string_buffer that's
  23.       /// C-compatible
  24.       std::vector<char const *> cstring_buffer;
  25.  
  26.       /// This sets up the cstring_buffer to mirror std_string_buffer. This
  27.       /// needs to be done after any of the std_string_buffer strings are
  28.       /// changed. In the public API of this class, that means only after
  29.       /// init.alization
  30.       void make_c_buffer()
  31.       {
  32.          std::transform(std_string_buffer.begin(),
  33.                         std_string_buffer.end(),
  34.                         std::inserter(cstring_buffer, cstring_buffer.begin()),
  35.                         [](std::string const & str) {
  36.                            return str.c_str();
  37.                         });
  38.          cstring_buffer.push_back(nullptr);
  39.       }
  40.    
  41.    public:
  42.       ///
  43.       /// Constructs this cstring_vector with a copy of 'orig'
  44.       cstring_vector(std::vector<std::string> orig)
  45.          : std_string_buffer(orig)
  46.       {
  47.          make_c_buffer();
  48.       }
  49.  
  50.       /// Copies the given cstring_vector. The copy gets a new copy of all
  51.       /// the strings, and they will be located at a new place in memory.
  52.       cstring_vector(cstring_vector const & other )
  53.          : std_string_buffer( other.std_string_buffer)
  54.       {
  55.          make_c_buffer();
  56.       }
  57.  
  58.  
  59.       ///
  60.       /// Move constructor; sets this to be 'other' and destroys 'other'.
  61.       cstring_vector(cstring_vector && other )
  62.          : std_string_buffer(std::move(other.std_string_buffer))
  63.          , cstring_buffer(std::move(other.cstring_buffer))
  64.       {}
  65.    
  66.       /// Returns a C-compatible view into this buffer, as a null-terminated
  67.       /// array of char const * strings.
  68.       char const * const *
  69.       get_c_buffer() const
  70.       {
  71.          return &cstring_buffer.front();
  72.       }
  73.  
  74.       /// Returns a C-compatible view into this buffer, as a null-terminated
  75.       /// array of char const * strings.
  76.       char const * *
  77.       get_c_buffer()
  78.       {
  79.          return &cstring_buffer.front();
  80.       }
  81.  
  82.       /// Returns the number of strings in the array (not counting the final
  83.       /// terminating NULL).
  84.       size_t count_strings() const
  85.       {
  86.          return std_string_buffer.size();
  87.       }
  88.    };
  89.  
  90. }
  91.  
  92. // Yo, Emacs!
  93. // Local Variables:
  94. //     c-file-style: "ellemtel"
  95. //     c-basic-offset: 4
  96. //     indent-tabs-mode: nil
  97. // End:
  98.  
  99. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top