473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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_cast<char*>( Base ); }

void SetOffset( const std::string& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var ) -
Base_, SQL_FieldType_VarChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var ) -
Base_, SQL_FieldType_Int ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var ) -
Base_, SQL_FieldType_UnsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<unsigned int>( Offsets.size() )
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<unsigned 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.SetMap( &Player );
CharFieldMap.ShowOffsets();

// ...
}

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.LoadTable( "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 1214
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_cast<char*>( Base );
}

void SetOffset( const std::string& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_VarChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_Int ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_UnsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<unsigned int>( Offsets.size()
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<unsigned 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.SetMap( &Player );
CharFieldMap.ShowOffsets();

// ...
}

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<int{

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.LoadTable( "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_cast<char*>( Base );
}

void SetOffset( const std::string& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_VarChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_Int ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var )
-
Base_, SQL_FieldType_UnsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<unsigned int>(
Offsets.size()
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<unsigned 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.SetMap( &Player );
CharFieldMap.ShowOffsets();

// ...
}

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<int{

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.LoadTable( "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_cast<char*>( Base
); }

void SetOffset( const std::string& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var
) -
Base_, SQL_FieldType_VarChar ) );
}
void SetOffset( int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var
) -
Base_, SQL_FieldType_Int ) );
}
void SetOffset( unsigned int& Var )
{
Offsets.push_back( COffset( reinterpret_cast<const char*>( &Var
) -
Base_, SQL_FieldType_UnsignedInt ) );
}
// More of these

std::vector< COffset Offsets;

void ShowOffsets()
{
std::cout << "Count:" << static_cast<unsigned int>(
Offsets.size()
)
<< std::endl;
for ( unsigned int i = 0; i < Offsets.size(); ++i )
std::cout << static_cast<unsigned 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.SetMap( &Player );
CharFieldMap.ShowOffsets();

// ...
}

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<int{

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 misunderstanding 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.LoadTable( "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
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...
1
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...
6
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...
2
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...
3
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...
1
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...
1
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 *...
5
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:...
1
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.