title: How to encapsulate a C API into RAII C++ classes?
body: <p>Given a C API to a library controlling sessions that owns items, what is the best design to encapsulate the C API into RAII C++ classes?</p>
  
  <p>The C API looks like:</p>
  
  <pre><code>HANDLE OpenSession(STRING sessionID);
  void CloseSession(HANDLE hSession);
  HANDLE OpenItem(HANDLE hSession, STRING itemID);
  void CloseItem(HANDLE hItem);
  </code></pre>
  
  <p>Plus other functions that are useful for one of these types (Session, or Item) and map directly to C++ member functions of the relevant object. But they are not needed here. My main interest is in the construction and destruction of these objects, using RAII to manage a correct opening and closing of these classes.</p>
  
  <p>My first idea for the design of my classes, is <em>pure</em> and direct RAII. The contained class accepts a container object as the constructor parameter.</p>
  
  <pre><code>class Session {
      HANDLE const m_hSession;
  public:
      Session(STRING sessionID): m_hSession(OpenSession(sessionID)) {}
      ~Session() { CloseSession(m_hSession); }
  };
  class Item {
      HANDLE const m_hItem;
  public:
      Item(HANDLE hSession, STRING itemID): m_hItem(OpenItem(hSession, itemID)) {}
      ~Item() { CloseItem(m_hItem); }
  };
  </code></pre>
  
  <p>This design have the disadvantage of allowing a bad behavior: a Session object can be destructed (and CloseSession function called) before all of its Item objects have been destructed. This is annoying, because it shouldn't happened. Even if this erroneous behavior is possible, hence not valid, using the C API, I'd like it to be avoided by design in the C++ API.</p>
  
  <p>That's why I am wondering about using the following design where the Session contains its Items (this shows the actual relationship), and is the sole class able to construct and destroy Items.</p>
  
  <pre><code>class Item {
      HANDLE const m_hItem;
      Item(HANDLE hSession, STRING itemID): m_hItem(OpenItem(hSession, itemID) {}
      ~Item() { CloseItem(m_hItem); }
      friend class Session;
  public:
  };
  class Session {
      HANDLE const m_hSession;
      typedef vector&lt;Item *&gt; VecItem;
      VecItem m_vecItem;
      Session(STRING sessionID): m_hSession(OpenSession(sessionID)) {}
      ~Session() {
          for (size_t n = 0 ; n &lt; m_vecItem.size() ; ++n) delete m_vecItem[n];
          m_vecItem.clear();
          CloseSession(m_hSession);
          }
  public:
      Item * OpenItem(STRING itemID) {
          Item *p = new Item(m_hSession, itemID);
          m_vecItem.push_back(p);
          return p;
          }
      void CloseItem(Item * item) {
          VecItem::iterator it = find(m_vecItem.begin(), m_vecItem.end(), item);
          if (it != m_vecItem.end()) {
              Item *p = *it; m_vecItem.erase(it); delete p;
              }
          }
  };
  </code></pre>
  
  <p>It looks to me as the only way to ensure a Session is not closed before its Items are closed: reflecting in the design that the Item objects are members of the 
  Session, and therefore will be destructed before the Session is destroyed.</p>
  
  <p>However, it looks a bit weird to me, as it leaves these functions OpenItem and CloseItem in the interface of the Session class. I was looking for something more in the line of RAII (for me, this means using a constructor for Item), but cannot imagine a way to encapsulate it that would ensure correct destruction order.</p>
  
  <p>Furthermore, using pointers, new and delete is too much old century C++. It should be possible to use a vector of Item (instead of Item*), at the price of defining correctly the move semantics for class Item, but it would be at the price of allowing a default constructor for Item that would create uninitialized second class citizen Item objects.</p>
  
  <p>Any better design ideas?</p>