473,698 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed with design of generic class to handle multiple types

I need to design data storage classes and operators for an image
processing system that must support a range of basic data types of
different lengths i.e. float, int, char, double. I have a template
class that stores the data. The problem with this design is the
inability to treat image data generically- I have a set of specialized
classes with no connection. I'm considering deriving all ImageData<T>
classes from a image data abstract base class with virtual methods for
operations where possible. ImageData methods would then be called on
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const ImageDataBase&
r2)

In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.

Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.

I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.

Jul 23 '05 #1
6 1802
> I need to design data storage classes and operators for an image
processing system that must support a range of basic data types of
different lengths i.e. float, int, char, double. I have a template
class that stores the data. The problem with this design is the
inability to treat image data generically- I have a set of specialized
classes with no connection. I'm considering deriving all ImageData<T>
classes from a image data abstract base class with virtual methods for
operations where possible.
That's not a bad idea. If you need to put ImageData's into a collection
for example, you'll have to use polymorphism.
ImageData methods would then be called on
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const
ImageDataBas e& r2)
Why not a

template <class T>
ImageData<T> operator+(const ImageData<T> &r1, const ImageData<T> &r2);
In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.
What I wrote only accepts same types (two ImageData<int> for example).
You may use two different parameters for the types and the compiler
will give errors if the types cannot be converted.

If you want to disallow legal type conversions, you'll have to do some
checking manually.
Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.
Don't.
I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.


Maybe a bit more informations would be useful.
Jonathan

Jul 23 '05 #2

Code4u wrote:
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const ImageDataBase&
r2)

In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.

Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.

I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.


The second approach would be better wherein all the operations can be
accomplished using that I suppose.

Jul 23 '05 #3

Code4u wrote:
I need to design data storage classes and operators for an image
processing system that must support a range of basic data types of
different lengths i.e. float, int, char, double. I have a template
class that stores the data. The problem with this design is the
inability to treat image data generically- I have a set of specialized
classes with no connection. I'm considering deriving all ImageData<T>
classes from a image data abstract base class with virtual methods for
operations where possible. ImageData methods would then be called on
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const ImageDataBase&
r2)

In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.

Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.

I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.


You are of course describing a variant type, that is a an object that
represents a single value of any one of a number of different types.
Needless to say, implementing a variant type poses some challenges and
common pitfalls: dynamic_casts are inefficient, and using void * is
often dangerous.

For a good reference implementation that avoids these sorts of
problems, I would start with the boost variant library. Here's a link
to its documentation: http://boost.org/doc/html/variant.html

Greg

Jul 23 '05 #4
You might want different classes for highly-typed collections vs. a
polymorphic one. One that only takes one type of image data only
should not be allowed to have one of the other ones, but the
polymorphic collection may want more flexibility.

If this is on PC (MFC VS6) beware of dynamic_cast. Extremely expensive
call.

Stuart

Jul 23 '05 #5
On 13 Jul 2005 06:03:55 -0700, "Greg" <gr****@pacbell .net> wrote:

Code4u wrote:
I need to design data storage classes and operators for an image
processing system that must support a range of basic data types of
different lengths i.e. float, int, char, double. I have a template
class that stores the data. The problem with this design is the
inability to treat image data generically- I have a set of specialized
classes with no connection. I'm considering deriving all ImageData<T>
classes from a image data abstract base class with virtual methods for
operations where possible. ImageData methods would then be called on
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const ImageDataBase&
r2)

In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.

Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.

I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.


You are of course describing a variant type, that is a an object that
represents a single value of any one of a number of different types.
Needless to say, implementing a variant type poses some challenges and
common pitfalls: dynamic_casts are inefficient, and using void * is
often dangerous.

For a good reference implementation that avoids these sorts of
problems, I would start with the boost variant library. Here's a link
to its documentation: http://boost.org/doc/html/variant.html

Greg


Greg, the data to be stored is for image processing, a single frame
might be 4k x 4k pixels. Within a single image all the data will be
the same type, so to use a variant for each and every pixel is a
horrible waste of memory. A variant that uses a union would have a
100% overhead when storing an int in a union of int & double. For
Boost variant is looks a good deal worse-

boost::variant< int, double>

has a sizeof()=16

Jul 23 '05 #6
Code4u wrote:
On 13 Jul 2005 06:03:55 -0700, "Greg" <gr****@pacbell .net> wrote:

Code4u wrote:
I need to design data storage classes and operators for an image
processing system that must support a range of basic data types of
different lengths i.e. float, int, char, double. I have a template
class that stores the data. The problem with this design is the
inability to treat image data generically- I have a set of specialized
classes with no connection. I'm considering deriving all ImageData<T>
classes from a image data abstract base class with virtual methods for
operations where possible. ImageData methods would then be called on
the base class and be implemented in the specialized template. The
code gets a little ugly when implementing operations with two image
data operands, i.e.

ImageDataBase& operator+(const ImageDataBase& r1, const ImageDataBase&
r2)

In this function I would need to write code to check for all the legal
operand types, using dynamic_cast, and then peform the calculation and
create the result ImageData or throw an exception.

Alternatively I could have an ImageData class that holds a void
pointer and an enum describing the data type.

I'm interested in getting input on the pros and cons of each, also if
there's a better pattern I would like to hear about it.


You are of course describing a variant type, that is a an object that
represents a single value of any one of a number of different types.
Needless to say, implementing a variant type poses some challenges and
common pitfalls: dynamic_casts are inefficient, and using void * is
often dangerous.

For a good reference implementation that avoids these sorts of
problems, I would start with the boost variant library. Here's a link
to its documentation: http://boost.org/doc/html/variant.html

Greg


Greg, the data to be stored is for image processing, a single frame
might be 4k x 4k pixels. Within a single image all the data will be
the same type, so to use a variant for each and every pixel is a
horrible waste of memory. A variant that uses a union would have a
100% overhead when storing an int in a union of int & double. For
Boost variant is looks a good deal worse-

boost::variant< int, double>

has a sizeof()=16


As the example of the + operator above makes clear, the variant types
in question are on the order of an instance of an ImageData<int>
versus an ImageData<doubl e> and not on the level of ints and doubles.
It's also unlikely that objects with significant storage requirements
will be passed around by value in any case, and the size of the variant
type is not all that important. After all, there is no requirement that
a variant type be value-based, it could just as easily reference the
stored type via a reference wrapper for example. Nor do varant types
that store by value always have constant storage needs. A
template-based variant class can vary its compile-time size based on
the type it is actually storing.

The most important issue though is the question of a compile-time
versus a run-time implementation. The strength of the Boost variant
type is that it requires that each cross pairing of types be either
explicitly handled (or explicitly ignored) by the programmer or the
program will fail to compile.

By covering all the possible combinations at compile-time, there is
little room left for undefined behavior to happen at runtime. And by
performing the computations up front and before they are needed, it has
the added advantage of lower overhead than a runtime implementation
relying on dynamic-casts and polymorphism. In other words, a
compile-time implementation is not only safer than a runtime version -
it is faster as well.

Greg

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
4258
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
10
2049
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
15
1861
by: James | last post by:
Hello Everyone! I'm trying to design a database for a library that stocks a range of media. (see link) http://homepage.ntlworld.com/james.merrie/private/library.gif Each user can make many enquiries / reservations / loans (if I can get enquiries right, I can do the rest). Every Enquiry can be made on each item of stock, every item of stock is
4
2428
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO application I will be making, also my first .NET application, so im looking for any help and guidance. Ok, my problems are todo with object and database abstraction, what should i do.
9
1719
by: Marc De Schrijver | last post by:
I'm designing an OO Model for a large application, and I have some question on how to model a particular situation; it's not directly related to C# but rather to general OO. The applicaiton will be developed in C# 2.0 though, which may have some influence on the OO model. Here's what I'm trying to model: I have a class Company, a class Publisher, a class Manufacturer and a class Distributor. Their relationships are as follows: 1. A...
0
5569
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
3
2726
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this OLE Object (load a picture from a given path). Something like this I would expect: _reports.crImage local_Report = new _reports.crImage();
3
1298
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The current non-generic implementation is functional, but not as clean as I would like. Although not sure, I believe my problems stem from lacking support of co- and contravariance in C# (which I'm desperately hoping will make it in the next version)....
2
10030
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.