473,769 Members | 3,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design - class to represent a variable

Hello,

I've been pondering with this for quite some time now, and finally
decided to ask here for suggestions. I'm kind of confused, actually...
Maybe I'm thinking too much...

Brain dump follows...

I need a class to represent a variable, with an associated data type
and/or value. Though, I don't want it to be a variant type, and not a
'convenience' class either.

The variable objects will be used in variable lists (a'la symbol tables),
so they are going to be polymorphic.

Now to the point...

The variable type is determined by the following factors:
1. raw data type (int, float, string, etc...)
2. array width (if it's an array of p.1)
3. storage class modifier

My dilemma concerns the actual design of the way how to access those
variables in different parts of the system.

The lists of variables are used in quite different situations.
For example, in generating function prototypes (type declarations),
carrying actual data around the system, translating data coming from
external sources.

The above requirements are pushing me towards a consequent use of the
visitor pattern. Though, I'm completely puzzled on how to apply visitor
to a type which is determined by 3 factors (as shown above).

It all seems as a total maintenance disaster at the moment...

THE Question:
Is there a simple way (or a way to simplify) such a case of three-way-
visitation?

Should I go for explicit definition of each type?

E.g.

Given types Int and Float, and storage classes Uniform and Varying,
define every possible combination of data type, array specifier, and
storage class modifier. ouch!

class Variable ... // the root

class UniformInt : public Variable ...
class UniformIntArray : public Variable ...
class VaryingInt : public Variable ...
class VaryingIntArray : public Variable ...
class UniformFloat : public Variable ...
etc...

Maybe I could nest it in some way?

E.g.

class StorageSpec ... // root

class Uniform : public StorageSpec ...
class Varying : public StorageSpec ...

class Variable ... // root

class Float : public Variable {
StorageSpec* m_storage_spec;
};
etc...

Or maybe the other way around, that is - embed data type in a storage
class?

I'm aware that it's impossible to help me with so small amount of
information given. Though I hope you can give me some references to
whatever medium, where a remotely similar problem is solved.

Thanks for your time.

Cheers.
Jul 22 '05 #1
7 1823
What exactly are you writing? I'm curious because I'm
working on a very similar problem.
Jul 22 '05 #2
Derek <us**@nospam.or g> wrote in news:2h******** ****@uni-berlin.de:
What exactly are you writing? I'm curious because I'm
working on a very similar problem.


Actually it's going to be a part of an application utilizing Renderman
interface. I'd like to represent RI variable specifications in C++ classes
following consequent OO practices. *cough* *cough*

Jul 22 '05 #3
> > What exactly are you writing? I'm curious because I'm
working on a very similar problem.


Actually it's going to be a part of an application
utilizing Renderman interface. I'd like to represent
RI variable specifications in C++ classes following
consequent OO practices. *cough* *cough*


That's what I thought. I just started a little RenderMan
project myself (yet another RenderMan-compliant renderer).
It's very new and I can barely render a sphere at this
point, but I've been thinking ahead about how to implement
RI variables.

I've started peeking at open-source renderers to see how
they handle variables (Aqsis and Pixe are both C++), but I
haven't come up with a definitive design yet.

Let me know if you come up with a cool design. My email
address is "derek AT networld DOT com" if you want to
compare notes sometime.
Jul 22 '05 #4
I'm not sure you want to deal with the combinatorial
explosion of using the visitor pattern. After all,
there are 5 storage classes (constant, uniform,
varying, facevarying, and vertex) and 10 types
(float, integer, vector, color, normal, point, matrix,
double, hpoint, and string). And each combination
can be a scalar or an array. Yikes!

I propose a very simple strucutre (off the top of my
head):

class Variable
{
public:
//...
private:
std::string m_varName;
StorageType m_storageType;
DataType m_dataType;
int m_arrayLength;
int m_elementLength ;
std::vector<flo at> m_data;
};

Now your algorithms can 'switch' based on type type if
they have to. I think you will find that many operations
(e.g., linear interpolation) don't care about the type
at all.

Perhaps the only wrinkle is that string variables can't
be stored very efficiently in a vector<float> and should
probably be treated separately.
Jul 22 '05 #5
Derek <us**@nospam.or g> wrote in news:2h******** ****@uni-berlin.de:
I'm not sure you want to deal with the combinatorial
explosion of using the visitor pattern. After all,
there are 5 storage classes (constant, uniform,
varying, facevarying, and vertex) and 10 types
(float, integer, vector, color, normal, point, matrix,
double, hpoint, and string). And each combination
can be a scalar or an array. Yikes!
That's exactly the point.
Having such huge visitors seemed like a bad idea.
On the other hand, visitor guarantees that every subtype will be serviced
individually in the code.
I propose a very simple strucutre (off the top of my
head):

class Variable
{
public:
//...
private:
std::string m_varName;
StorageType m_storageType;
DataType m_dataType;
int m_arrayLength;
int m_elementLength ;
std::vector<flo at> m_data;
};

Now your algorithms can 'switch' based on type type if
they have to. I think you will find that many operations
(e.g., linear interpolation) don't care about the type
at all.


I've been considering a similar design, except that the main data was
separated into respective derived classes.

That is:

class Variable {
std::string m_name;
StorageType m_storage_type;
int m_size; // ==0 : scalar, >0 - array
public:
virtual void AcceptVisitor(V isitor&) const = 0;
// ... accessors, etc.
};

class FloatVariable : public Variable {
std::vector<flo at> m_data;
public:
//...
};

class StringVariable : public Variable {
std::vector<std ::string> m_data;
public:
//...
};

// etc...

This way, I could dispatch based on the actual data type, and use
conditionals for the storage type and/or array specification.

But then ... I'd probably end up with such conditionals in every visitor,
which is kind of contradictory to the whole design. Why bother with
visitor at all when there will be switches everywhere, anyway?

On the other hand, considering the fact that the storage specifier is
only giving a new meaning for array dimensions, I thought about turning
it into a kind of templated policy class, which would only determine the
algorithms for operations on the data regardless of the type...

E.g.

// only one element to copy
struct UniformStorage {
template <class IterT>
static void Copy(IterT to, IterT from)
{ *to = *from; } // just one value to copy here
// ...
};

// nvertices elements to copy
class VaryingStorage {
int m_nvertices;
public:
VaryingStorage( int nvertices) : m_nvertices(nve rtices) { }
template <class IterT>
static void Copy(IterT to, IterT from) {
for (int i=0; i!=m_nvertices; ++i) {
*to = *from;
++to; ++from;
}
}
};

And then use those policies as template arguments for FloatVariable et
al.

This technique, however, rules out the GOF style visitation, because
virtual functions cannot be templates.
Unless all templates are given explicitly...

// in visitor
virtual void Visit(FloatVari able<UniformSto rage> const&)
//...

So here we go back to the huge-visitor maintenance problem, because every
possible combination of variable with storage class must be given
explicitly...

Uhh too much coffee I guess...

Cheers
Jul 22 '05 #6
Your logic is all too familiar; I've gone 'round and
'round with the same thoughts. The biggest problem is
that I'm not really sure what operations I will need
to perform on RI variables. That is, I'm not convinced
that I will really have to switch based on type in all
THAT many places. I really think in many cases all
numerical data can be treated uniformly regardless of
type. In that case a simple one-class solution with a
few switch statements here and there is probably the
best way to go. If I'm wrong, however, and I need to
switch all over the place, I will revisit a polymorphic
solution with visituation. Either way, I'm still a few
weeks away from having to worry about it. Good luck.
Jul 22 '05 #7
Derek <us**@nospam.or g> wrote in news:2h******** ****@uni-berlin.de:
Your logic is all too familiar; I've gone 'round and
'round with the same thoughts. The biggest problem is
that I'm not really sure what operations I will need
to perform on RI variables. That is, I'm not convinced
that I will really have to switch based on type in all
THAT many places. (...)


You're right. I guess I'm just trying too hard.

But still, I'm curious how do others handle similar design issues.

Cheers.
Jul 22 '05 #8

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

Similar topics

43
4867
by: grz02 | last post by:
Hi, Im an experienced database+software designer and developer, but, unfortunately, anything to do with web-programming and web-systems designs is still a pretty new area to me... (been working mostly with "legacy" environments the last 10 years) So I am writing this, hoping to get some useful advise and feedback... I have done some pretty trivial, small websites with html/PHP,
11
3185
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
5
8732
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
24
2017
by: Kalpesh | last post by:
Hello All, Please help validate this design problem Assume that I have several entities in my project (eg Supplier, Customer etc). All of them save several common properties - name, address, city, state, zipcode etc I thought of making a base class - BusinessEntity (with all of the
29
3577
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
11
2363
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure similar to a database or XML representation. Rules to modify the stored data will be executed, then the data will be transformed into an output format. Think something similar to FormatA -> XML -> Manipulate XML -> FormatB
4
1771
by: nw | last post by:
Hi, I was wondering if someone would be able to give me some comments on the following class structure, it feels to me as if there must be a better way, but I'm unsure what it is, perhaps I should be using multiple inheritance? Basically I have a pure virtual class called Body, this contains a number of integration algorithms which are applied to the Body. Generally only one integration algorithm will be used with a
4
14729
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns – State Often computer software operates based on a condition called a state. These states traditionally have been implemented using a switch statement. The cases of the switch represent the various states. The example below might be used to read a disc file int state = 1; switch (state) { case 1: //open the file
8
2235
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address, port number, or both. How should we design the classes to represent these filters? My answer was: class FilterRule {
0
9423
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
10211
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
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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...
0
6672
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
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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.