473,804 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mapping Class/Structure to use with DB

I am attempting to map the variables in a class or structure to use with
MySQL. I got something to work but I'm not happy with it. Here is a
snippet showing what I'm not happy with:

class COffsetMap
{
public:
COffsetMap() {}
virtual ~COffsetMap() {}
void SetBase( void* Base ) { Base_ = reinterpret_cas t<char*>( Base ); }

void SetOffset( const std::string& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var ) -
Base_, SQL_FieldType_V arChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var ) -
Base_, SQL_FieldType_I nt ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var ) -
Base_, SQL_FieldType_U nsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<uns igned int>( Offsets.size() )
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<uns igned int>( Offsets[i].Offset ) <<
std::endl;
}

private:
char* Base_;
};

Which is used like:

class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Name );
SetOffset( Base->Password );
SetOffset( Base->Avatar );
SetOffset( Base->GM );
// More of these
}
};

int main()
{
CCharacter Player;
CCharFieldMap CharFieldMap;
CharFieldMap.Se tMap( &Player );
CharFieldMap.Sh owOffsets();

// ...
}

Now, what I'm not happy with is const correctness. You may notice that
void SetOffset( int& Var )
is not const correct. The reason being if the user attempts to map a var
that is an enum, I do not want it to compile, they need to do something
special for those (not sure what yet). But, if this is declared as:
void SetOffset( const int& Var )
it compiles anyway and gives a wrong offset. Digging a little into it I
found it was creating a temporary int (converting enum to int) and passing
the reference to that, which is stored on the stack. If I remove the const
it won't compile (which is good).

I can just see myself, however, 4 months down the line refactoring code and
thinking, "oh, missed const correct on this one" and putting in the const
and breaking code. I could add a comment like
// Can not be const correct becuase of enum
but the fraility of this program depending on a const or not has me worried.

Has anyone else done this, or have a better way to do this? The whole
object is being able to do something like:
(psuedo code)
if ( ! PlayerTable.Loa dTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable >Player;

Which would use the internal offset map to load from the MySQL query
directly into the object. I've already tested if I can do this and it does
in fact work for my test case I did as a function.

Yes, I know it breaks encapsulation, but I figure if someone didn't want to
use this because it breaks encapsulation for their class, they could use the
other methods I'm building in such as
Player.name( PlayerTable["Name"] );
and other methods.
Aug 12 '06 #1
3 1232
Jim Langston wrote:
I am attempting to map the variables in a class or structure to use with
MySQL. I got something to work but I'm not happy with it. Here is a
snippet showing what I'm not happy with:

class COffsetMap
{
public:
COffsetMap() {}
virtual ~COffsetMap() {}
void SetBase( void* Base ) { Base_ = reinterpret_cas t<char*>( Base );
}

void SetOffset( const std::string& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_V arChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_I nt ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_U nsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<uns igned int>( Offsets.size()
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<uns igned int>( Offsets[i].Offset ) <<
std::endl;
}

private:
char* Base_;
};

Which is used like:

class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Name );
SetOffset( Base->Password );
SetOffset( Base->Avatar );
SetOffset( Base->GM );
// More of these
}
};

int main()
{
CCharacter Player;
CCharFieldMap CharFieldMap;
CharFieldMap.Se tMap( &Player );
CharFieldMap.Sh owOffsets();

// ...
}

Now, what I'm not happy with is const correctness. You may notice that
void SetOffset( int& Var )
is not const correct. The reason being if the user attempts to map a var
that is an enum, I do not want it to compile, they need to do something
special for those (not sure what yet). But, if this is declared as:
void SetOffset( const int& Var )
it compiles anyway and gives a wrong offset. Digging a little into it I
found it was creating a temporary int (converting enum to int) and passing
the reference to that, which is stored on the stack. If I remove the
const it won't compile (which is good).
You could try something like this:

template < typename T >
void dummy ( T const & );

template <>
void dummy<int( int const & i ) {}

enum E { a, b };

int main ( void ) {
int i;
dummy( i );
dummy( a ); // this yields a linker error.
}
or even:

template < typename T >
struct dummy_struct;

template <>
struct dummy_struct<in t{

static
void func ( int const & i ) {}

};

template < typename T >
void dummy ( T const & t ) {
dummy_struct<T> ::func( t );
}

enum E { a, b };

int main ( void ) {
int i;
dummy ( i );
dummy ( a ); // now this yields a compiler error
}
>
I can just see myself, however, 4 months down the line refactoring code
and thinking, "oh, missed const correct on this one" and putting in the
const
and breaking code. I could add a comment like
// Can not be const correct becuase of enum
but the fraility of this program depending on a const or not has me
worried.

Has anyone else done this, or have a better way to do this? The whole
object is being able to do something like:
(psuedo code)
if ( ! PlayerTable.Loa dTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable >Player;

Which would use the internal offset map to load from the MySQL query
directly into the object. I've already tested if I can do this and it
does in fact work for my test case I did as a function.

Yes, I know it breaks encapsulation, but I figure if someone didn't want
to use this because it breaks encapsulation for their class, they could
use the other methods I'm building in such as
Player.name( PlayerTable["Name"] );
and other methods.

Best

Kai-Uwe Bux
Aug 12 '06 #2
"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:eb******** **@murdoch.acc. Virginia.EDU...
Jim Langston wrote:
>I am attempting to map the variables in a class or structure to use with
MySQL. I got something to work but I'm not happy with it. Here is a
snippet showing what I'm not happy with:

class COffsetMap
{
public:
COffsetMap() {}
virtual ~COffsetMap() {}
void SetBase( void* Base ) { Base_ = reinterpret_cas t<char*>( Base );
}

void SetOffset( const std::string& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_V arChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_I nt ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var )
-
Base_, SQL_FieldType_U nsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<uns igned int>(
Offsets.size ()
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<uns igned int>( Offsets[i].Offset )
<<
std::endl;
}

private:
char* Base_;
};

Which is used like:

class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Name );
SetOffset( Base->Password );
SetOffset( Base->Avatar );
SetOffset( Base->GM );
// More of these
}
};

int main()
{
CCharacter Player;
CCharFieldMap CharFieldMap;
CharFieldMap.Se tMap( &Player );
CharFieldMap.Sh owOffsets();

// ...
}

Now, what I'm not happy with is const correctness. You may notice that
void SetOffset( int& Var )
is not const correct. The reason being if the user attempts to map a var
that is an enum, I do not want it to compile, they need to do something
special for those (not sure what yet). But, if this is declared as:
void SetOffset( const int& Var )
it compiles anyway and gives a wrong offset. Digging a little into it I
found it was creating a temporary int (converting enum to int) and
passing
the reference to that, which is stored on the stack. If I remove the
const it won't compile (which is good).

You could try something like this:

template < typename T >
void dummy ( T const & );

template <>
void dummy<int( int const & i ) {}

enum E { a, b };

int main ( void ) {
int i;
dummy( i );
dummy( a ); // this yields a linker error.
}
or even:

template < typename T >
struct dummy_struct;

template <>
struct dummy_struct<in t{

static
void func ( int const & i ) {}

};

template < typename T >
void dummy ( T const & t ) {
dummy_struct<T> ::func( t );
}

enum E { a, b };

int main ( void ) {
int i;
dummy ( i );
dummy ( a ); // now this yields a compiler error
}
Actually, its the var and not the enum value that I would have a concern
with. Something doing (in my code)

E MyEnum;
SetOffset( MyEnum );
>

>>
I can just see myself, however, 4 months down the line refactoring code
and thinking, "oh, missed const correct on this one" and putting in the
const
and breaking code. I could add a comment like
// Can not be const correct becuase of enum
but the fraility of this program depending on a const or not has me
worried.

Has anyone else done this, or have a better way to do this? The whole
object is being able to do something like:
(psuedo code)
if ( ! PlayerTable.Loa dTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable >Player;

Which would use the internal offset map to load from the MySQL query
directly into the object. I've already tested if I can do this and it
does in fact work for my test case I did as a function.

Yes, I know it breaks encapsulation, but I figure if someone didn't want
to use this because it breaks encapsulation for their class, they could
use the other methods I'm building in such as
Player.name( PlayerTable["Name"] );
and other methods.

Aug 12 '06 #3
Jim Langston wrote:
"Kai-Uwe Bux" <jk********@gmx .netwrote in message
news:eb******** **@murdoch.acc. Virginia.EDU...
>Jim Langston wrote:
>>I am attempting to map the variables in a class or structure to use with
MySQL. I got something to work but I'm not happy with it. Here is a
snippet showing what I'm not happy with:

class COffsetMap
{
public:
COffsetMap() {}
virtual ~COffsetMap() {}
void SetBase( void* Base ) { Base_ = reinterpret_cas t<char*>( Base
); }

void SetOffset( const std::string& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var
) -
Base_, SQL_FieldType_V arChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var
) -
Base_, SQL_FieldType_I nt ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_ba ck( COffset( reinterpret_cas t<const char*>( &Var
) -
Base_, SQL_FieldType_U nsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<uns igned int>(
Offsets.size( )
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<uns igned int>( Offsets[i].Offset )
<<
std::endl;
}

private:
char* Base_;
};

Which is used like:

class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Name );
SetOffset( Base->Password );
SetOffset( Base->Avatar );
SetOffset( Base->GM );
// More of these
}
};

int main()
{
CCharacter Player;
CCharFieldMap CharFieldMap;
CharFieldMap.Se tMap( &Player );
CharFieldMap.Sh owOffsets();

// ...
}

Now, what I'm not happy with is const correctness. You may notice that
void SetOffset( int& Var )
is not const correct. The reason being if the user attempts to map a
var that is an enum, I do not want it to compile, they need to do
something
special for those (not sure what yet). But, if this is declared as:
void SetOffset( const int& Var )
it compiles anyway and gives a wrong offset. Digging a little into it I
found it was creating a temporary int (converting enum to int) and
passing
the reference to that, which is stored on the stack. If I remove the
const it won't compile (which is good).

You could try something like this:

template < typename T >
void dummy ( T const & );

template <>
void dummy<int( int const & i ) {}

enum E { a, b };

int main ( void ) {
int i;
dummy( i );
dummy( a ); // this yields a linker error.
}
or even:

template < typename T >
struct dummy_struct;

template <>
struct dummy_struct<in t{

static
void func ( int const & i ) {}

};

template < typename T >
void dummy ( T const & t ) {
dummy_struct<T> ::func( t );
}

enum E { a, b };

int main ( void ) {
int i;
dummy ( i );
dummy ( a ); // now this yields a compiler error
}

Actually, its the var and not the enum value that I would have a concern
with. Something doing (in my code)

E MyEnum;
SetOffset( MyEnum );
You want compilation of those lines to fail, right? Well, then the code I
gave should give you a hint because, it is not the value that triggers the
error message: it is the type. The following also fails to compile:

int main ( void ) {
int i;
E e;
dummy ( i );
dummy ( e ); // this yields an error
}

Maybe, I am misunderstandin g you.
>>

>>>
I can just see myself, however, 4 months down the line refactoring code
and thinking, "oh, missed const correct on this one" and putting in the
const
and breaking code. I could add a comment like
// Can not be const correct becuase of enum
but the fraility of this program depending on a const or not has me
worried.

Has anyone else done this, or have a better way to do this? The whole
object is being able to do something like:
(psuedo code)
if ( ! PlayerTable.Loa dTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable >Player;

Which would use the internal offset map to load from the MySQL query
directly into the object. I've already tested if I can do this and it
does in fact work for my test case I did as a function.

Yes, I know it breaks encapsulation, but I figure if someone didn't want
to use this because it breaks encapsulation for their class, they could
use the other methods I'm building in such as
Player.name( PlayerTable["Name"] );
and other methods.
Aug 12 '06 #4

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

Similar topics

0
1274
by: Olivier Jullian | last post by:
Hi, I'm new to .NET and am trying to take advantage of the object structure while accessing relational databases. I started a small project for managing "projects". Here is a description of my data structure : A project would be made of tasks, and each task can contain sub-tasks, so I would have something like
1
1324
by: Flare | last post by:
Hi. Im have asked for recommendation regarding the best production ready O/R mapping tools for .net. I got that. Since im an old, ok maybe not old (only 27), Java programmer patterns and developement with tiers is of great importance for me. I have a problem with the famours anti-pattern Tier leakage. In the Java world I used POJO´s (simple java classes) as value objects. These value
6
6546
by: naruto | last post by:
Hi all, I have the following being defined in a A.cxx file. // define in source file. Not exported to the outside world (this cannot be // moved to the header file ) #define CHANNEL_0 0 #define CHANNEL_1 1 #define CHANNEL_2 2
2
2794
by: Danny Miller | last post by:
Hi there, I'm facing a problem that is driving me nuts. The scenario is as follows: 1) Create an empty directory structure e.g. C:\Dev1\TestWebApp 2) Map a virtual directory e.g. TestWebApp to this new directory 3) Under C:\Dev1 create a blank visual studio solution and add a new web project called TestWebApp (which will be created in C:\Dev1\TestWebApp and
3
2670
by: ripster | last post by:
Hi, I am working on a project which has a wsdl file which contains two elements (lets call then the Name element) with same name but under different targetNamespaces. When I generate the client proxy file I end up with two ..NET classes, one called Name and the other called Name1. The Name1 class has the appropriate attributes to serialise the content to an element called name with the correct xml namespace. The wsdl2java tool that...
1
2562
by: Ram | last post by:
Hey, I'm having a trouble mapping a connecting between 2 of my tables. We have 2 tables - the simplest "dept", "emp" tables which are mapped to 2 classes. Class Dept contains 2 properties for emps - 1 for manager and the second for workers (Collection). How can I map this? We can add additional fields in the emp table that indicates the property in the depts?
1
2594
by: Vikas | last post by:
Hi I have been browsing C struct to C# mapping emails on the newsgroups, but I haven't been able to find a solution to my problem. My C structure looks like this: struct myCstruct { char * data; size_t size; };
5
2577
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
1
4560
by: magikminox | last post by:
Hi Guys I have a table that contains codes for commodities.Some of the codes in this table have changed and some of them have not.So now i want to design a solution that enable me to map the new codes in a different mapping table to the old ones in the other table.I also want to retain the old codes because most of the archived data used the old codes. Where there is no new code, the current code is being retained.How do i design my table...
0
9711
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
10343
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
10335
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
10088
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
9169
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
6862
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
5529
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
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
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.