Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Designing a Volume Class

Reply
Thread Tools

Designing a Volume Class

 
 
E G
Guest
Posts: n/a
 
      01-22-2004
Hi!

I am having problems in designing a class. First, I have a base class
that allocates a 3D data set and allows some other mathematical
operations with it, something like this:

template <typename T> class BasicArray
{
 
Reply With Quote
 
 
 
 
Donovan Rebbechi
Guest
Posts: n/a
 
      01-22-2004
In article <bupk61$hmv$(E-Mail Removed)>, E G wrote:
> Hi!
>
> I am having problems in designing a class. First, I have a base class
> that allocates a 3D data set and allows some other mathematical
> operations with it,


What mathematical operations do you need to do ? Think about this carefully,
it's important.

> template <typename T> class BasicArray
> {
> .
> .
> .
> private:
> T ***Array;


Why use T*** and not a T* ? What are the advantages of this approach ? Have you
read the FAQ for this forum ?

> public:
> T operator()(unsigned,unsigned,unsigned);


Should be:

T& operator()(unsigned,unsigned,unsigned);
const T& operator() const(unsigned,unsigned,unsigned);

> This class is not useful alone since I have to read the data from files.
> The files may contain the information in an 3D array of any C basic type
> (i.e., char, short, int, float, etcetera). This is the reason of
> creating a template class. I decided to create a second class "Volume"
> that handles the I/O and header information and uses BasicArray somehow.


Here's the problem: you don't know what data type is stored in the file at
compile time. So you need to handle this dynamically.

For example:

class FileReader;

class DoubleReader : public FileReader
{
....
};

....

Then the question is, do you always convert the data into a certain type of
BasicArray (BasicArray<double> or BasicArray<int> or even BasicArray<char>
depending on what you need to do with the data) or do you leave the data in
the same format that it's stored on disk ?

You need to think about how your classes are going to be used. Flexibility
may seem like a good thing, but gratuitous flexibility is often actually
just a nuisance. For example, having a "data set of any type" class could
result in worse performance, and create all sorts of nasty problems down
the road when you write your math ops.

> Since the information of the basic type in which the information is
> represented is stored in the file I cannot inherit Volume from
> BasicArray.


Templates and inheritance don't mix that well. If you really did want to
handle this data polymorphically, you would need to derive BasicArray from
some other class:

class BaseArray
{
double operator() ( ..... );

....
};

class BasicArray<T> : public BaseArray
{


};

But then you're going to have one hell of a time doing things like
performing arithmatic ops on mixed types. For example, suppose you "add"
a BasicArray<double> to a BasicArray<int>. What type should the result
be ? You need a double dispatch. Also, if you are to make a variable return
type, it needs to be a polymorphic BaseArray* pointer, not a value.

But that's ugly. So now you need to wrap your BaseArray class in a handle.

You should read a book that discusses handle classes if you haven't yet done
so. Accelerated C++ would be a good start.

Have I convinced you that using an "any-datatype" class for general use is
probably not as good an idea as it may at first seem ?

> I was thinking of creating the class Volume with a void *
> pointer.


No! That's a horrible way to do it.

You would need

class ArrayHandle
{
BaseArray* impl;
public:
....

};

class BaseArray
{

....
};

template <typename T> class BasicArray : public BaseArray
{
...
};

Then you have to decide how the handle manages your array. Does it reference
count ? Does it deep copy ? Etc.

Then you need to think about how you handle mixed type arithmatic. For example,
does it make sense for the sum of two BasicArray<short> objects to be a
BasicArray<double> object ? You need to write "double dispatch" code which
in itself is a terrible mess.

Now the simplest solution would be to avoid the "any type" nonsense for
anything more complicated than element access and file reading. This way,
you only use "any type" for the limited and probablyt necessary task of
being able to make small changes to large files without changing the data
type of the whole file.

For mathematical operations, the appropriate model will usually be to convert
the on-disk data to an appropriate type. What an "appropriate type" is depends
on what is being done with the data. char is appropriate for a binary mask.
int is appropriate for discrete valued data, and for results of computations
which preserve discreteness (e.g. spatial masking) floating point types are
appropriate for storing the results of floating point computation on int data.

The person using the code for a particular application is the right person
to decide what type of Array is the appropriate one to use.

Sometimes, there may not be a sensible conversion (for example, double->char).
In this case, there should be some way to throw an exception or something,
to tell the programmer using the class that the file open "failed" because
the file was "unsuitable" for their intended use. Again, this must be handled
at runtime since you don't know what will be in the file at compile time.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-22-2004
E G wrote:

> Hi!
>
> I am having problems in designing a class. First, I have a base class
> that allocates a 3D data set and allows some other mathematical
> operations with it, something like this:
>
> template <typename T> class BasicArray
> {
> .
> .
> .
> private:
> T ***Array;
> .
> .
> .
> public:
> T operator()(unsigned,unsigned,unsigned);
> BasicArray<T> operator+(T op);
> .
> .
> .
> }
>
> This class is not useful alone since I have to read the data from files.
> The files may contain the information in an 3D array of any C basic type
> (i.e., char, short, int, float, etcetera). This is the reason of
> creating a template class. I decided to create a second class "Volume"
> that handles the I/O and header information and uses BasicArray somehow.
> Since the information of the basic type in which the information is
> represented is stored in the file I cannot inherit Volume from
> BasicArray. I was thinking of creating the class Volume with a void *
> pointer. This pointer would point to the appropiate instance of the
> BasicArray<type>. However, I am not sure that this is the best approach.
> Any ideas are greatly appreciated.
>
> Regards!
>


containment.

template <typename T>
class Volume {
private:
BasicArray<T> data;
public:
BasicArray<T>& get_array();
const BasicArray<T>& get_array() const;
// yada yada yada
};
 
Reply With Quote
 
Donovan Rebbechi
Guest
Posts: n/a
 
      01-23-2004
In article <4_YPb.4687$(E-Mail Removed)> , red floyd wrote:
> E G wrote:


> containment.
>
> template <typename T>
> class Volume {
> private:
> BasicArray<T> data;
> public:
> BasicArray<T>& get_array();
> const BasicArray<T>& get_array() const;
> // yada yada yada
> };


And how does this help ?

--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-23-2004
Donovan Rebbechi wrote:
> In article <4_YPb.4687$(E-Mail Removed)> , red floyd wrote:
>
>>E G wrote:

>
>
>
>>containment.
>>
>>template <typename T>
>>class Volume {
>>private:
>> BasicArray<T> data;
>>public:
>> BasicArray<T>& get_array();
>> const BasicArray<T>& get_array() const;
>> // yada yada yada
>>};

>
>
> And how does this help ?
>


He wanted to know how he could use BasicArray<> with Volume, but said
that he had problems with inheritance. It seems to me that containment
is a much better solution.

DISCLAIMER: I may have misread the OP.
 
Reply With Quote
 
Donovan Rebbechi
Guest
Posts: n/a
 
      01-23-2004
In article <q2_Pb.4715$(E-Mail Removed)> , red floyd wrote:
> Donovan Rebbechi wrote:
>> In article <4_YPb.4687$(E-Mail Removed)> , red floyd wrote:
>>
>>>E G wrote:

>>
>>
>>
>>>containment.
>>>
>>>template <typename T>
>>>class Volume {
>>>private:
>>> BasicArray<T> data;
>>>public:
>>> BasicArray<T>& get_array();
>>> const BasicArray<T>& get_array() const;
>>> // yada yada yada
>>>};

>>
>>
>> And how does this help ?
>>

>
> He wanted to know how he could use BasicArray<> with Volume, but said
> that he had problems with inheritance. It seems to me that containment
> is a much better solution.


Your example is not a solution because it doesn't actually solve anything.

The problems he was dealing with were associated with handling data (read via
input files) whose type is not known at compile time. Your volume class doesn't
address this (because your class still has a template parameter which must be
presented at compile time), and in fact it, as you've presented it, your
code doesn't really solve anything else either.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
Jumbo
Guest
Posts: n/a
 
      01-23-2004

"E G" <(E-Mail Removed)> wrote in message
news:bupk61$hmv$(E-Mail Removed)...
> Hi!
>
> I am having problems in designing a class. First, I have a base class
> that allocates a 3D data set and allows some other mathematical
> operations with it, something like this:
>
> template <typename T> class BasicArray
> {
> .
> .
> .
> private:
> T ***Array;
> .
> .
> .
> public:
> T operator()(unsigned,unsigned,unsigned);
> BasicArray<T> operator+(T op);
> .
> .
> .
> }
>
> This class is not useful alone since I have to read the data from files.
> The files may contain the information in an 3D array of any C basic type
> (i.e., char, short, int, float, etcetera). This is the reason of
> creating a template class. I decided to create a second class "Volume"
> that handles the I/O and header information and uses BasicArray somehow.
> Since the information of the basic type in which the information is
> represented is stored in the file I cannot inherit Volume from
> BasicArray. I was thinking of creating the class Volume with a void *
> pointer. This pointer would point to the appropiate instance of the
> BasicArray<type>. However, I am not sure that this is the best approach.
> Any ideas are greatly appreciated.
>
> Regards!
>


What you've got there looks good already. A custom array class specifically
designed to suit your project.
Stick some logic in your volume class i.e:
if(type=="int")
BasicArray<int> name;
else if(type=="char")
BasicArray<char> name;
etc etc.

Then your array is initialised with the correct type. What you might want to
do is keep then seperate. One class for the business logic and one for the
data. So design the array class so the volume class can communicate with the
it easily.
For example say your volume class had a function named getfiledata(8 bytes)
then you make that function not only get the data but also fill the array
with the data. Thus you need a good interface so make your array classes
accessor methods suitable.

The array class doens't need to be inherintely related to I don't think but
I don't know enough about your project.
You could always nest the array class inside the volume class too remember.









 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD Verdict reviews: MADLAX: REALITY (VOLUME 7), STARSHIP OPERATORS: MEMORIES (VOLUME 2), and more! DVD Verdict DVD Video 0 04-20-2006 08:36 AM
DVD Verdict reviews: SPEED RACER: VOLUME 4, MERMAID FOREST: UNQUENCHABLE THIRST (VOLUME 3), and more! DVD Verdict DVD Video 0 04-13-2006 08:22 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
DVD Verdict reviews: SAMURAI CHAMPLOO: VOLUME 3, NEW FIST OF THE NORTH STAR: WHEN A MAN CARRIES SORROW (VOLUME 3), and more! DVD Verdict DVD Video 2 08-04-2005 07:32 AM
DVD Verdict reviews: SAMURAI CHAMPLOO (VOLUME 2), CHRONO CRUSADE: THE DEVIL TO PAY (VOLUME 4), and more! DVD Verdict DVD Video 0 05-26-2005 08:12 AM



Advertisments