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

  • See Prompts Microsoft Engineers Use for Bleeding-Edge Multimodal RAG AI Research

    Vision-centric queries show how front-line experts are prompting LLMs these days.

  • AI Explains Expressions in Update to Java on VS Code

    "The Spring Tools now show code lenses above these expressions that allow you to quickly let GitHub Copilot explain those statements for you."

  • Microsoft Eases Integration with Semantic Kernel AI SDK

    The basic idea is to provide unified API abstractions, especially for idiomatic C# code, to help platform developers and others work with any provider with standard implementations for caching, telemetry, tool calling and other common tasks.

  • Final .NET 9 Preview Ships with Go-Live License

    Visual Studio developers can now download the SDK for .NET 9 Release Candidate 2 with a go-live license, meaning devs get Microsoft support for production applications even before the framework reaches general availability next month.

Subscribe on YouTube