Resource Management in C++: Listing 2

Shared_resource implementation.

// shared_resource.cpp
#include "shared_resource.hpp"
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
#include <sstream>

using namespace std;

string _compose_msg_about_resource(shared_resource*, const char*);

shared_resource* create_resource()
{
  static int id = 0;

  shared_resource* ret = static_cast<shared_resource*>(malloc(sizeof(shared_resource)));
  if (ret)
  {
    ret->id = ++id;
    cout << _compose_msg_about_resource(ret, "reserved for application");
    ret->status = resource_status::available;
    reset_resource(ret);
  }
  else
    throw runtime_error("Resource couldn't be created.");

  return ret;
}

void recycle_resource(shared_resource* resource)
{
  if (resource)
  {
    shut_resource(resource);
    cout << _compose_msg_about_resource(resource, "recycled");
    free(resource);
  }
  // no else: recycling a null resource has no effect.
}

void shut_resource(shared_resource* resource)
{
  if ((resource)&&(resource->status==resource_status::in_use))
  {
    resource->status = resource_status::available;
    // other code that shuts the resource
    // ...
    cout << _compose_msg_about_resource(resource, "shut down (still allocated)");
  }
  // no else: turning off a null resource, or an already shut one has no effect.
}

// whatever its status, reinitiates the resource.
void reset_resource(shared_resource* resource)
{
  if (resource)
  {
    if (resource->status==resource_status::available)
    {
      resource->status = resource_status::in_use;
      // other code that reinitiates the resource
      // ...
      cout << _compose_msg_about_resource(resource, "ready for use");
    }
    else
    {
      throw runtime_error(_compose_msg_about_resource(resource, "already in use"));
    }
  }
  else
    throw invalid_argument("Resource is null.");
}

void do_alpha_with_resource(shared_resource* resource)
{
  if (resource)
  {
    if (resource->status==resource_status::in_use)
      cout << _compose_msg_about_resource(resource, "used for <alpha>");
    // some code that does <alpha> with resource...
    // ...
    else
      throw runtime_error(_compose_msg_about_resource(resource, "not ready"));
  }
  else
    throw invalid_argument("Resource is null.");
}

void do_beta_with_resource(shared_resource* resource)
{
  if (resource)
  {
    if (resource->status==resource_status::in_use)
    {
      cout << _compose_msg_about_resource(resource, "used for <beta>");
      // some code that does <beta> with resource...
      // ...
      cout << _compose_msg_about_resource(resource, "... oops! Something's wrong. An exception must be issued");
      throw runtime_error(_compose_msg_about_resource(resource, "error while doing <beta>"));
    }
    else
      throw runtime_error(_compose_msg_about_resource(resource, "not ready"));
  }
  else
    throw invalid_argument("Resource is null.");
}



string _compose_msg_about_resource(shared_resource* resource, const char* msg)
{
  stringstream ss;

  if (resource)
    ss << "Resource " << resource->id << " " << string(msg) << "." << endl;

  return ss.str();
}

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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube