Boost logo

Boost Users :

From: Stephen torri (storri_at_[hidden])
Date: 2004-12-28 21:11:11


I want to expand the File_Base virtual interface here to allow me to
pass the 'file_data' from the File class without have two sets of code:

if (size == 32)
  32 bite code
else
  64 bite code

How do I expand the File_base interface? Is there a way to grab the type
of a variable passed into a function and reuse it at run time?

So that one time doSomething would have the following if the 'file_data'
variable was passed in the File_Base interface:

void doSomething (boost::shared_ptr<File_Base> const& f)
{
  File<32> a = f->get_File();
  // do 32 bit code
}

While another time the function code be:

void doSomething (boost::shared_ptr<File_Base> const& f)
{
  File<64> a = f->get_File();
  // do 64 bit code
}

I would like to be generic so I don't have to repeat the same code.

Stephen

--------- SAMPLE PROGRAM --------

#include <iostream>
#include <boost/shared_ptr.hpp>

/*! \brief Type Mapper type */
template <int size> struct Type_Mapper;

/*! \brief 32-bit Type Mapper */
template <>
struct Type_Mapper<32> {

  /*! \brief 32-bit type */
  typedef uint32_t arch_t;
};

/*! \brief 64-bit Type Mapper */
template <>
struct Type_Mapper<64> {

  /*! \brief 64-bit type */
  typedef uint64_t arch_t;
};

class File_Base {
public:
  virtual uint32_t const get_Size() = 0;
};

template <uint32_t size>
class File : public File_Base
{
public:
  virtual uint32_t const get_Size()
  {
    return size;
  }

  typename Type_Mapper<size>::arch_t file_data;
};

void doSomething (boost::shared_ptr<File_Base> const& f)
{
  // Needs to access the variable 'file_data' in File<size>
  // Problem: How to expand File_Base interface so that
  // the variable 'file_data' can be returned without have
  // code like:

  if (f->get_Size() == 32)
    {
      // do 32 bit code
    }
  else
    {
      // do 64 bit code
    }
}

void create (void)
{
  boost::shared_ptr<File_Base> f (new File<32>());
  doSomething (f);
}

int main (int, char*) {

  create ();

  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net