473,786 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A simple validity check. But need your kind help!

Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below

class Domain
{
private:
double mLowerCorner[3];
double mUpperCorner[3];

public:
double* GetLowerCorner( ) {return mLowerCorner;}
double* GetUpperCorner( ) {return mUpperCorner;}

void SetLowerCorner( double lowerCorner[3])
{
if (mUpperCorner[0] < lowerCorner[0] || // Validity check
mUpperCorner[1] < lowerCorner[1] ||
mUpperCorner[2] < lowerCorner[2] )
{
throw out_of_range("E rror");
}
mLowerCorner[0] = lowerCorner[0];
mLowerCorner[1] = lowerCorner[1];
mLowerCorner[2] = lowerCorner[2];
}

// My question is here: I need similar functions
// void SetUpperCorner( double upperCorner[3]),
// void SetDomain(doubl e lowerCorner[3], double upperCorner[3]),
// and constructors.
// and so on. The check make me very uncomfortable.
};

Anyone can give me a good suggestion to solve the problem?

Thanks a lot!

Shuisheng

Feb 16 '07 #1
2 1931
shuisheng wrote:
Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below

class Domain
{
private:
double mLowerCorner[3];
double mUpperCorner[3];

public:
double* GetLowerCorner( ) {return mLowerCorner;}
double* GetUpperCorner( ) {return mUpperCorner;}
These methods expose the arrays. Client code could say:

(GetLowerCorner ())[1] = 2.0;

and change the entries. In that case, the client will bypass all validity
checks whatsoever. At least, you need:

double const * GetLowerCorner( ) const {return mLowerCorner;}
double const * GetUpperCorner( ) const {return mUpperCorner;}
void SetLowerCorner( double lowerCorner[3])
{
if (mUpperCorner[0] < lowerCorner[0] || // Validity check
mUpperCorner[1] < lowerCorner[1] ||
mUpperCorner[2] < lowerCorner[2] )
{
throw out_of_range("E rror");
}
mLowerCorner[0] = lowerCorner[0];
mLowerCorner[1] = lowerCorner[1];
mLowerCorner[2] = lowerCorner[2];
}

// My question is here: I need similar functions
// void SetUpperCorner( double upperCorner[3]),
// void SetDomain(doubl e lowerCorner[3], double upperCorner[3]),
// and constructors.
// and so on. The check make me very uncomfortable.
};
What should make you uncomfortable is not the checks but the sheer
complexity you are inviting just to deal with two points in space.

What about making domains immutable and putting a little more intelligence
into the points:

#include <tr1/array>
#include <stdexcept>

// warning: [bad hack, there is no guarantee that we can derive from array]
/*
(a) We use derivation rather than a typedef so that operator- only applies
to this type.
(b) Some people will prefer to derive privately and import all methods via
using directives.
*/
struct vector3d : public std::tr1::array <double,3{

vector3d ( double x = 0.0,
double y = 0.0,
double z = 0.0 )
{
(*this)[0] = x;
(*this)[1] = y;
(*this)[2] = z;
}

};

vector3d operator- ( vector3d const & lhs, vector3d const & rhs ) {
vector3d result;
for ( unsigned i = 0; i < 3; ++i ) {
result[i] = lhs[i] - rhs[i];
}
return ( result );
}
bool is_strictly_pos itive ( vector3d const & v ) {
for ( unsigned i = 0; i < 3; ++i ) {
if ( v[i] <= 0 ) {
return ( false );
}
}
return ( true );
}

bool does_define_box ( vector3d const & bot,
vector3d const & top ) {
return ( is_strictly_pos itive( top - bot ) );
}

class immutableDomain {

vector3d bot;
vector3d top;

public:

immutableDomain ( vector3d const & b, vector3d t )
: bot(b)
, top(t)
{
if ( ! does_define_box ( bot, top ) ) {
throw std::out_of_ran ge( "Error" );
}
}

vector3d const & lowerCorner ( void ) const {
return ( bot );
}

vector3d const & upperCorner ( void ) const {
return ( top );
}

};

If you want to, you can define a mutable Domain class wrapping around the
immutable one:

class Domain {

immutableDomain the_data;

public:

Domain ( vector3d const & b, vector3d const & t )
: the_data( b, t )
{}

vector3d const & getLowerCorner ( void ) const {
return ( the_data.lowerC orner() );
}

vector3d const & getUpperCorner ( void ) const {
return ( the_data.upperC orner() );
}

void setLowerCorner ( vector3d const & bot ) {
the_data = immutableDomain ( bot, the_data.upperC orner() );
}

void setUpperCorner ( vector3d const & top ) {
the_data = immutableDomain ( the_data.lowerC orner(), top );
}

};

The constructor of immutableDomain will be the only place where the
invariant is checked.
Best

Kai-Uwe Bux
Feb 16 '07 #2
In article <11************ **********@v45g 2000cwv.googleg roups.com>,
sh*********@yah oo.com says...
Dear All,

Assume I have a class for a cuboid domain. The domain is defined by
the cuboid's lower corner, such as (0, 0, 0), and upper corner, such
as (1, 1, 1). The upper corner should be always higher than the lower
corner. I write a code as below
I'd add private functions to check the condition, and do the assignment.
Using this, the rest become pretty trivial:

class Domain {
double mLowerCorner[3];
double mUpperCorner[3];

bool check(double *lower, double *upper) {
if (upper[0] < lower[0] ||
upper[1] < lower[1] ||
upper[2] < lower[2])
throw out_of_range("E rror");
}

void assign(double &a, double const &b) {
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
}

public:
void SetLowerCorner( double lower[3]) {
check(lower, mUpperCorner);
assign(mLowerCo rner, lower);
}

void SetUpperCorner( double upper[3]) {
check(mLowerCor ner, upper);
assign(mUpperCo rner, upper);
}

void SetDomain(doubl e *lower, double *upper) {
check(lower, upper);
assign(mLowerCo rner, lower);
assign(mUpperCo rner, upper);
}

Domain(double *lower, double *upper) {
SetDomain(lower , upper);
}
// ...
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 16 '07 #3

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

Similar topics

38
3546
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
6
2158
by: francisco lopez | last post by:
ok , first of all sorry if my english is not so good, I do my best. here is my problem: I don´t know much javascript so I wrote a very simple one to validate a form I have on my webpage. could you please have a look at the following script: ------------------------------------------------------------
5
7248
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
16
3387
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data of the program till the last byte. Here we find static tables, global context pointers, etc. This are the global variables of the program.
33
4569
by: a | last post by:
Hi, I have a pointer that points to an unknown heap memory block, is it possible to check the pointer + 3 is valid or not? If it is impossible, how can I do the check? Thanks
9
2979
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r"); if(!$fp) {
14
2989
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
6
11883
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with so far: However I have found that it does not check it correctly. I just need to check the 9x9 array, which I am passing to this function against the classic sudoku rules and then return true for
4
3295
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of arguments. For example, #define MAX_SIZE 100000 int foo(double *x, unsigned ling sz, int option) {
0
9647
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
10360
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
10163
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...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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
7510
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
6744
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();...
1
4064
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.