Improving C++ Encapsulation with the Pimpl Idiom: Listing 2

A message queue "pImpling" a primitive array.

/**********************************************
 my_queue.h
**********************************************/
#include <memory>
#include <string>

using namespace std;

typedef string element_t;

class my_queue {
  struct queue_impl; // forward-declared nested struct

  unique_ptr<queue_impl> pImpl;
public:
  my_queue();
  my_queue(const my_queue&);
  my_queue(my_queue&&);

  size_t enqueue(const element_t&);
  element_t dequeue();
  size_t get_lenght() const;
  bool is_empty() const;
  my_queue& operator=(const my_queue&);
  my_queue& operator=(my_queue&&);

  ~my_queue();
};



/**********************************************
 my_queue.cpp
**********************************************/
#include "my_queue.h"
#include <stdexcept>
#include <algorithm>
#include <utility>

using namespace std;

const size_t ARRAY_LIMIT = 1000;

struct my_queue::queue_impl {
  element_t* array;
  size_t top, bottom;

  queue_impl() : array{new element_t[ARRAY_LIMIT]}, top{0}, bottom{0} {}

  ~queue_impl() { delete[] array; }
};

my_queue::my_queue() : pImpl{new queue_impl{}} {}

my_queue::my_queue(const my_queue& other) : pImpl{new queue_impl{}} {
  for (size_t i = other.pImpl->top;i<other.pImpl->bottom;++i)
    enqueue(other.pImpl->array[i]);
}

my_queue::my_queue(my_queue&& other)=default;

size_t my_queue::enqueue(const element_t& e) {
  if (pImpl->bottom < ARRAY_LIMIT)
    pImpl->array[pImpl->bottom++] = e;
  else
    throw overflow_error("The queue ran out of space.");

  return pImpl->bottom;
}

element_t my_queue::dequeue() {
  if (pImpl->top == pImpl->bottom)
    throw runtime_error("The queue is empty.");

  if (pImpl->top > pImpl->bottom)
    throw runtime_error("The queue is an inconsistent state.");

  return pImpl->array[pImpl->top++];
}

size_t my_queue::get_lenght() const {
  return pImpl->bottom - pImpl->top;
}

bool my_queue::is_empty() const {
  return pImpl->top == pImpl->bottom;
}

my_queue& my_queue::operator=(const my_queue& other) {
  if (this!=&other) {
    pImpl->top = pImpl->bottom = 0;
    for (size_t i = other.pImpl->top;i<other.pImpl->bottom;++i)
      enqueue(other.pImpl->array[i]);

  }

  return *this;
}

my_queue& my_queue::operator=(my_queue&& other)=default;

my_queue::~my_queue()=default;



/**********************************************
 main.cpp
**********************************************/
#include "my_queue.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[]) {
  my_queue q1, q2;
  element_t lincoln_quote[] = {
    "Give", "me", "six", "hours", "to", "chop", "down", "a", "tree", "and",
    "I", "will", "spend", "the", "first", "four", "sharpening", "the", "axe"
  };

  for (int i = 0; i<10; ++i)
    q1.enqueue(lincoln_quote[i]);

  for (int i = 0; i<5; ++i) {
    cout << "Queue 1: " << (q1.dequeue()) << endl;
  }
  // prints
  // Queue 1: Give
  // Queue 1: me
  // Queue 1: six
  // Queue 1: hours
  // Queue 1: to

  q2 = q1;

  for (int i = 10; i<19; ++i)
    q1.enqueue(lincoln_quote[i]);

  while (!q2.is_empty()) {
    cout << "Queue 2: " << (q2.dequeue()) << endl;
  }
  // Queue 2: chop
  // Queue 2: down
  // Queue 2: a
  // Queue 2: tree
  // Queue 2: and

  while (!q1.is_empty()) {
    cout << "Queue 1: " << (q1.dequeue()) << endl;
  }
  // Queue 1: chop
  // Queue 1: down
  // Queue 1: a
  // Queue 1: tree
  // Queue 1: and
  // Queue 1: I
  // Queue 1: will
  // Queue 1: spend
  // Queue 1: the
  // Queue 1: first
  // Queue 1: four
  // Queue 1: sharpening
  // Queue 1: the
  // Queue 1: axe
}

About the Author

Diego Dagum is a software architect and developer with more than 20 years of experience. He can be reached at [email protected].

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events