472,780 Members | 1,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Ugly C looking code

I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is exceptionally
ugly. For example, in my operator<< override:

template<typename T>
CMySQLTable& operator<<(const T& ClassTable)
{
/**/
}

I have a switch statement and code that looks like this:

switch ( ThisField.FieldType() )
{
case SQL_FieldType_Unknown:
std::cerr << " SQL_FieldType_Unknown: Error!" << std::endl;
assert(false);
break;

case SQL_FieldType_VarChar:
ThisField = *reinterpret_cast<const std::string*>( reinterpret_cast<const
char*>( &ClassTable ) + ThisOffset.Offset );
break;

case SQL_FieldType_Int:
ThisField = *reinterpret_cast<const int*>( reinterpret_cast<const
char*>( &ClassTable ) + ThisOffset.Offset );
break;

case SQL_FieldType_UnsignedInt:
ThisField = *reinterpret_cast<const unsigned int*>(
reinterpret_cast<const char*>( &ClassTable ) + ThisOffset.Offset );
break;

case SQL_FieldType_Bool:
ThisField = *reinterpret_cast<const bool*>( reinterpret_cast<const
char*>( &ClassTable ) + ThisOffset.Offset );
break;

/**/
}

It's the complicated casts that I find ugly. And in the operator>override
it's just as bad, if not worst.

template<typename T>
CMySQLTable& operator>>(T& ClassTable)
{
/**/
}

switch ( ThisField.FieldType() )
{
case SQL_FieldType_Unknown:
std::cerr << " SQL_FieldType_Unknown: Error!" << std::endl;
assert( false );
break;

case SQL_FieldType_VarChar:
*reinterpret_cast<std::string*>( reinterpret_cast<char*>( &ClassTable ) +
ThisOffset.Offset ) = ThisField.Value();
break;

case SQL_FieldType_Int:
*reinterpret_cast<int*>( reinterpret_cast<char*>( &ClassTable ) +
ThisOffset.Offset ) = StrmConvert<int>( ThisField.Value() );
break;

case SQL_FieldType_UnsignedInt:
*reinterpret_cast<unsigned int*>( reinterpret_cast<char*>( &ClassTable )
+ ThisOffset.Offset ) = StrmConvert<unsigned int>( ThisField.Value() );
break;

case SQL_FieldType_Bool:
*reinterpret_cast<bool*>( reinterpret_cast<char*>( &ClassTable ) +
ThisOffset.Offset ) = ThisField.Value() == "0" ? false : true;
break;
/**/
}

Can you think of a cleaner way to do this, or is this the way I'm stuck with
just because of what I'm doing (working with pointers and offsets).

What I'm doing, and it works in test, is I have a class include a subclass
in which I map the variables I want to pull from the database.

void CCharFieldMap::SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( "Version", Base->Version );
SetOffset( "Name", Base->Name );
SetOffset( "Password", Base->Password );
SetOffset( "Avatar", Base->Avatar );
SetOffset( "Race", *reinterpret_cast<unsigned int*>( &Base->Race ) );
SetOffset( "Sex", *reinterpret_cast<unsigned int*>( &Base->Sex ) );
SetOffset( "GM", Base->GM );
/**/
}

My overrides for << and >look at this instance that contains information
about the variables (Type of variable, offset into class) and transfer data
back and forth using the base address of the passed reference and the
offsets.

It's the only way I could find to do it, and right now it seems it will be
extremely easy to use for the user (me) for new classes. No longer do I
have to go through SQL schenanigans to pull data to/from databases, I can
simply do code like:

if ( ! PlayerTable.init( "192.168.1.100", "serpardum", "somepassword",
"abyssal", "players", 3307 ) )
{
std::cerr << "Initialization failed" << std::endl;
std::string wait;
std::getline( std::cin, wait );

return 1;
}

if ( ! PlayerTable.LoadTable( "Name", "Serpardum" ) )
{
std::cout << "Serpardum not found" << std::endl;
return 1;
}

PlayerTable >Player;

which would open my MySQL database, load the record where the Name ==
Serpardum, and load it into my class.

I like it, but as I said, I find some of it ugly. Any suggestions?
Aug 25 '06 #1
5 3252
Jim Langston wrote:
I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is exceptionally
ugly. For example, in my operator<< override:
Why don't you just save all the fields as strings and convert them as
required?
--
Ian Collins.
Aug 26 '06 #2
Ian Collins wrote:
Jim Langston wrote:
>I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is
exceptionally
ugly. For example, in my operator<< override:
Why don't you just save all the fields as strings and convert them as
required?
Why not store a table of pointers into the MySQL result itself, and fetch
them out on demand, typesafely?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 26 '06 #3

Ian Collins wrote:
Jim Langston wrote:
I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is exceptionally
ugly. For example, in my operator<< override:
Why don't you just save all the fields as strings and convert them as
required?
I don't quite understand what you mean. All the fields are strings in
the CField class. The operator>and operator<< overrides are doing
the conversion. So what did you mean beyond this?

Aug 26 '06 #4

Phlip wrote:
Ian Collins wrote:
Jim Langston wrote:
I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is
exceptionally
ugly. For example, in my operator<< override:
Why don't you just save all the fields as strings and convert them as
required?

Why not store a table of pointers into the MySQL result itself, and fetch
them out on demand, typesafely?
Please explain? Currently when I read the table it goes into a CField
map which contains the string that MySQL returns and the data type. So
in my CMySQLTable class I have a map of <std::string, CFieldwith the
std::string being the field name, and CField being the value (as a
string) and data type as stored in MySQL.

Oh, I think I see what you are saying, and I even thought about this.
Instead of storing them as std::string in the CField, store them as the
data type itself. I couldnt' figure out a good way to do this in a
map. A union wouldn't work as the data would be different sizes. And
even if I used a union of pointers (pointing to std::string, int,
unsigned int, etc...) I would still wind up having to use a switch
based on type.

Even if I came up with a way to store the data as it's type
transparently, the only thing this would save me is not having to run
StrmConvert</* type */>( ) on the strings. I don't see how it could
save me on the casting into the class they're being loaded into.

Aug 26 '06 #5
Se*******@gmail.com wrote:
Ian Collins wrote:
>>Jim Langston wrote:
>>>I've been working on a project to map a MySQL database to a C++ class.
Well, I actually got it to work, but some if it I just feel is exceptionally
ugly. For example, in my operator<< override:

Why don't you just save all the fields as strings and convert them as
required?


I don't quite understand what you mean. All the fields are strings in
the CField class. The operator>and operator<< overrides are doing
the conversion. So what did you mean beyond this?
I might have misunderstood your code, I though you were converting each
field value in a result as it was copied.

Looking at my own MySQL wrappers, I do pretty much what Phlip said and
store the query result and use the subscript operator to index rows and
again within the row to access fields. The field access converts the
value to std::string on read and from the base type on write.

--
Ian Collins.
Aug 26 '06 #6

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

Similar topics

9
by: Pedro Graca | last post by:
<?php function ugly_array() { return array(1, 2, 3, 4, 5); } $arr = ugly_array(); echo $arr; ?> not so ugly :) now ... I want to get rid of the $arr temporary variable.
0
by: S. Staats | last post by:
I'm having to use google groups to post and I was cutting and pasting text - and ended up with an ugly-looking subject in my previous posting.
6
by: Quick Function | last post by:
Hi, I developed a site and used css. I put almost all style information in the css and used a lot of "id=my_css_class" in the html. They is little style specification in the html. I found that on...
1
by: ngoc | last post by:
Hi I want to make homepage with javascript for drop down navigation menu. Looking around some webpages, I see javascript code is embedded in html code. It is ugly and unmaintainable. Are there any...
5
by: yxq | last post by:
Hello I am using VS.Net 2002, my icons of XP type look ugly, and i have saw the article. i have added the manifest file. Sometime the icons show very good, but sometime the icons look very...
7
by: teo | last post by:
I have to validate the user input to prvent HTML injection. I use validateRequest=True and when a potentially malicious input occurs AspNet immediately sends its ugly page about the...
7
by: Yarco | last post by:
Well, friend. Yes, I'm doing PHP. But exception is a traditional machinism in programming languages. I'm very doubt about this. In procedure language, we return error code when we meet an error. So...
20
by: benhoyt | last post by:
Hi guys, I've been using Python for some time now, and am very impressed with its lack of red tape and its clean syntax -- both probably due to the BDFL's ability to know when to say "no". ...
1
by: JAM | last post by:
I'm trying to code some directory structure as my output / input file using XML. I would like to see formatiing with indentations. mimicking directory tree structure. Unfortunately the code such as...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.