473,671 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Properties with non standard C++ (REALLY SORRY ABOUT THAT)

Hi,

first I would like to apologize about my question because usually I hate
specific implementation but in this case I really would like an answer.
Besides my example is an example of what's not to be done but I am aware
of that.
Don't loose time tell me that I should use standard implementation and
consider my question as theoretical.
So I am using Microsoft compiler and I have a class with lots of methods
with get and set :
class foo
{
public:
....

LPCTSTR GetName();
void SetName(LPCTSTR Value);
A GetABC();

void SetABC(A Value);
A GetABC();

void SetDEF(B Value);
B GetDEF();

....

};

So I would like to mix preprocessor tricks(it's bad I know) and template :
I am thinkinf of something like :
DECLARE_PROPERT Y(Name, LPCTSTR);
DECLARE_PROPERT Y(ABC, A);
DECLARE_PROPERT Y(DEF, B);
and after maybe
Property.Name = L"Vincent";
LPCTSTR tszName = Property.Name;


And here is what I have done for now :

template<class TYPE>
struct Property
{
TYPE m_Type;

TYPE Get()
{
g_pClient->Get ...( m_Type); // FILL BY MACRO
return TYPE;
}

void Set()
{
g_pClient->Set ...( m_Type); // FILL BY MACRO

}

__declspec(prop erty(get = Get, put = Put)) TYPE the_prop;
};

// template specialization for C strings
template<>
struct Property<LPCTST R>
{
... blabla
}..


I am sur there is something to do with all that but I still cannot find
the way.
I would try something like

DECLARE_PROPERT Y(PropName, TYPE) \
template<class TYPE\
struct Property \
{ \
TYPE m_Type; \
TYPE Get() \
{ \
g_pClient->Get ...( m_Type); \
return TYPE; \
} \

void Set() \
{ \
g_pClient->Set ...( m_Type); \
} \
__declspec(prop erty(get = Get, put = Put)) TYPE PropName; \
}; \

typedef something ...
Please give me your feedback
//------------------------ MS property ---------------------//
/ declspec_proper ty.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}

int getprop() {
return i;
}

__declspec(prop erty(get = getprop, put = putprop)) int the_prop;
};

int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}



Feb 15 '07 #1
8 2333
Vincent RICHOMME wrote:
Hi,

first I would like to apologize about my question because usually I
hate specific implementation but in this case I really would like an
answer. [..]
Is there any reason why you dind't post it to the newsgroup that
specifically caters to the needs of Visual Studio?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #2
Victor Bazarov a écrit :
Vincent RICHOMME wrote:
>Hi,

first I would like to apologize about my question because usually I
hate specific implementation but in this case I really would like an
answer. [..]

Is there any reason why you dind't post it to the newsgroup that
specifically caters to the needs of Visual Studio?

V
Yes and a good one : people here have better knowledge of C++ and are
more used to manipulate concept like template, OOP.
When I see people who can write boost class for instance, I must admit
I am impressed and you cannot find people like that on any newsgroup.

Feb 15 '07 #3
Vincent RICHOMME a écrit :
Victor Bazarov a écrit :
>Vincent RICHOMME wrote:
>>Hi,

first I would like to apologize about my question because usually I
hate specific implementation but in this case I really would like an
answer. [..]

Is there any reason why you dind't post it to the newsgroup that
specifically caters to the needs of Visual Studio?

V
Yes and a good one : people here have better knowledge of C++ and are
more used to manipulate concept like template, OOP.
When I see people who can write boost class for instance, I must admit
I am impressed and you cannot find people like that on any newsgroup.
But if you can give me a more standard approach I am curious.
The problem here is my class has lots of methods with different names
that almost do the same :

class foo
{
public:
....

LPCTSTR GetName();
void SetName(LPCTSTR Value);
A GetABC();

void SetABC(A Value);
A GetABC();

void SetDEF(B Value);
B GetDEF();

....
};
Feb 15 '07 #4
ok I found a solution to my problem.

Vincent RICHOMME a écrit :
Vincent RICHOMME a écrit :
>Victor Bazarov a écrit :
>>Vincent RICHOMME wrote:
Hi,

first I would like to apologize about my question because usually I
hate specific implementation but in this case I really would like an
answer. [..]

Is there any reason why you dind't post it to the newsgroup that
specificall y caters to the needs of Visual Studio?

V
Yes and a good one : people here have better knowledge of C++ and are
more used to manipulate concept like template, OOP.
When I see people who can write boost class for instance, I must admit
I am impressed and you cannot find people like that on any newsgroup.
But if you can give me a more standard approach I am curious.
The problem here is my class has lots of methods with different names
that almost do the same :

class foo
{
public:
...

LPCTSTR GetName();
void SetName(LPCTSTR Value);
A GetABC();

void SetABC(A Value);
A GetABC();

void SetDEF(B Value);
B GetDEF();

...
};
Feb 15 '07 #5
Vincent RICHOMME wrote:
Hi,

first I would like to apologize about my question because usually I hate
specific implementation but in this case I really would like an answer.
Besides my example is an example of what's not to be done but I am aware
of that.
Don't loose time tell me that I should use standard implementation and
consider my question as theoretical.
So I am using Microsoft compiler and I have a class with lots of methods
with get and set :
In most cases, getters and setters are a bad design smell. Why
encapsulate something only to break the encapsulation?

--
Ian Collins.
Feb 15 '07 #6
Vincent RICHOMME wrote:
Vincent RICHOMME a écrit :
>Victor Bazarov a écrit :
>>Vincent RICHOMME wrote:
Hi,

first I would like to apologize about my question because usually I
hate specific implementation but in this case I really would like
an answer. [..]

Is there any reason why you dind't post it to the newsgroup that
specificall y caters to the needs of Visual Studio?

V
Yes and a good one : people here have better knowledge of C++ and are
more used to manipulate concept like template, OOP.
When I see people who can write boost class for instance, I must
admit I am impressed and you cannot find people like that on any
newsgroup.
But if you can give me a more standard approach I am curious.
The problem here is my class has lots of methods with different names
that almost do the same :

class foo
{
public:
...

LPCTSTR GetName();
void SetName(LPCTSTR Value);
A GetABC();

void SetABC(A Value);
A GetABC();

void SetDEF(B Value);
B GetDEF();

...
};
I don't know what to recommend you. There is no one "hard and fast"
solution that would be good for all. In many cases separating the
concepts (to reduce the individual number of "properties ") is good
enough a way to go:

class hasName {
whatevertypeyou like name;
public:
LPCTSTR GetName() const;
void SetName(LPCTSTR );
};
...
class foo : public hasName .... {
...
};

(yes

Essentially this employs the "policy" technique (see Alexandrescu,
"Modern C++ Design").

You can also create (and possibly pre-fill) a 'std::map' of all the
named values of your "properties ". You then have a slightly smaller
set of functions:

class foo {
public:
boost::any GetProp(LPCTSTR ) const;
void SetProp(LPCTSTR , boost::any);
};

There are plenty of other portable solutions, you just need to
search for them. Have you actually tried looking, say, on Google?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #7
In article <45************ ***********@new s.free.fr>, ri******@free.f r
says...
Hi,

first I would like to apologize about my question because usually I hate
specific implementation but in this case I really would like an answer.
Besides my example is an example of what's not to be done but I am aware
of that.
Don't loose time tell me that I should use standard implementation and
consider my question as theoretical.
So I am using Microsoft compiler and I have a class with lots of methods
with get and set :
A design with lots of accessors and mutators is usually a sign of a
problematic design -- you're usually better off fixing it than
facilitating it. In particular, this usually signals somebody who's
heard that public variables are bad, but hasn't grasped _why_ they're
bad, so he makes the variables themselves private, then then provides
public access to them via the get and set methods. The result is the
worst of all worlds -- the design is still one of public variables, and
all that's really changed is that the syntax has gotten really ugly.

The variables should be private because the interface should _usually_
be one of the class providing a higher-level abstraction, so clients do
NOT just get and set values. One basic principle is generally as "don't
ask -- tell". The idea is that code outside the class should NOT request
information about the class state before making a modification. e.g.
this would be bad:

some_class x;

if (x.getsomevalue () < 10)
x.setsomeotherv alue(12);

Instead, the class itself should contain that intelligence, and the
outside code should just do something like: x.dowhatever(12 );

Occassionally, however, you run into something that really makes sense
with an interface with what at least look and act like public variables.
In this case, (at least IMO) get/set methods are the wrong way to go. If
you really want unrestricted access to these variables, then just make
the public. If your get/set methods really do something (typically
range-checking) to assure the values are valid, then you should create
something that looks and acts like a public variable, but gives you the
control you need (e.g. carries out the aforementioned range-checking).

Since range-checking is so common, I've written a small template that
implements it:

#include <exception>
#include <iostream>
#include <functional>

template <class T, class less=std::less< T
class bounded {
const T lower_, upper_;
T val_;

bool check(T const &value) {
return less()(value, lower_) || less()(upper_, value);
}

void assign(T const &value) {
if (check(value))
throw std::domain_err or("Out of Range");
val_ = value;
}

public:
bounded(T const &lower, T const &upper)
: lower_(lower), upper_(upper) {}

bounded(bounded const &init) { assign(init); }

bounded &operator=(T const &v) { assign(v); return *this; }

operator T() const { return val_; }

friend std::istream &operator>>(std ::istream &is, bounded &b) {
T temp;
is >temp;

if (b.check(temp))
is.setstate(std ::ios::failbit) ;
else
b.val_ = temp;
return is;
}
};

This would be used like:

struct X {
bounded<intx;
bounded<inty;

X() : x(1, 100), y(0, 200) {}
};

and now code that uses these can simply treat x and y as public
variables (since they are) but you still have full control to assure
that only valid values are assigned to them. It's also possible to pass
the bounds as template parameters, so those would look like:

bounded<int, 1, 100x;
bounded<int, 0, 200y;

But this doesn't work for floating point ranges (OTOH, it has some
advantages, such as making different ranges different types, so
accidentally assigning from one to another is an error -- though an
explicit cast makes it possible when needed).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 16 '07 #8
Try out this code:
>>>>>>>>>>>>>>> >>>>>>>>>>>>> >>
#include <iostream>
#include <string>

using namespace std;

template<typena me class_T, typename T, typename const_reference _T =
const T&class property {
public:
typedef T value_type;
typedef const_reference _T const_reference _type;
typedef class_T class_type;

typedef const_reference _type (class_type::*g et_type)() const;
typedef void (class_type::*s et_type)(const_ reference_type) ;
typedef value_type& (class_type::*g et_ref_type)();

public:
property(class_ type* p_obj, get_type p_get_fun, set_type p_set_fun,
get_ref_type p_get_ref_fun) : m_p_obj(p_obj), m_p_get_fun(p_g et_fun),
m_p_set_fun(p_s et_fun), m_p_get_ref_fun (p_get_ref_fun) {}

T& operator =(const_referen ce_type value) {
(m_p_obj->*m_p_set_fun)( value);
return (m_p_obj->*m_p_get_ref_f un)();
}

operator const_reference _type() {
return (m_p_obj->*m_p_get_fun)( );
}

private:
class_type* m_p_obj;

get_type m_p_get_fun;
set_type m_p_set_fun;
get_ref_type m_p_get_ref_fun ;

};

class example {
public:
example() : width(this, &example::get_w idth, &example::set_w idth,
&example::get_r ef_width), title(this, &example::get_t itle,
&example::set_t itle, &example::get_r ef_title), m_width(0), m_title()
{};

property<exampl e, int, intwidth;
property<exampl e, stringtitle;

private:
int m_width;

int get_width() const {
return m_width;
}

void set_width(int width) {
m_width = width;
}

int& get_ref_width() {
return m_width;
}
string m_title;

const string& get_title() const {
return m_title;
}

void set_title(const string& title) {
m_title = title;
}

string& get_ref_title() {
return m_title;
}

};

void test(int val) {
cout << "test(int): " << val << endl;
}

void test(const string& val) {
cout << "test(strin g): " << val << endl;
}

int main(int, char* []) {
example ex;
cout << "ex.width: " << static_cast<int >(ex.width) << endl;
cout << "ex.title: " << static_cast<con st string&>(ex.tit le) << endl;
ex.width = 1;
ex.title = "title";
cout << "ex.width: " << static_cast<int >(ex.width) << endl;
cout << "ex.title: " << static_cast<con st string&>(ex.tit le) << endl;
test(ex.width);
test(ex.title);
return 0;
}
<<<<<<<<<<<<<<< <<<<<<<<<<<<< <<

The code compiles and workes fine on GCC (however I do not remebrer
which version) and MS VS 2005.
Note that I pass pointer "this" in constructor - which is not a
good idea to do. However this code does not use this value before the
construction completes so no problem - bu if you want to remove this
you can always allow late initialization of this pointer (but then you
would HAVE to remember to do this).

The idea behind this is to make a tamplate class which will allow
property-like behavior. You must pass some arguments as you can read
from the code. const_reference _T is used to allow the behavior with
basic types where you do not use reference but a simple type.

As you can see using to << operator requires explicit cast. You can
remove this by decalring the operator for property class.

Naturally the class can be easly modified to react on other needs
or to simulate get-only or set-only properties. I think that using
template metaprograming could improve the code. This was just a quick
try to give you some ideas.

Adam Badura

Feb 16 '07 #9

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

Similar topics

1
1742
by: Pavils Jurjans | last post by:
Hallo, I yesterday was browsing the book, JavaScript The Definitive Guide (4th ed), which is, unquestionably, the best reference book for JS. To my surprise, I didn't find any documentation about the static properties of global RegExp object, ie, RegExp.lastMatch, RegExp.leftContext, RegExp.rightContext, and all those RegExp.$x properties. I lloked up the ECMA-262 PDF and to my surprise realized that they are not included into the...
43
4966
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
18
18364
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
10
7360
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
4
3372
by: Richard Abraham | last post by:
I have created a new class ServiceTimer based upon the .NET System.Timers.Timer class. This has a new property CollectionIntervalMinutes which I expected to be added to the 'Behavior' section of the class properties and also to be available as a 'Dynamic' property so that I could control it from the application config file. The new property is not shown, but the properties inherited from the base class are I cannot find any reference to how...
45
2404
by: Brett | last post by:
If I do this without declaring a corresponding field, is it considered bad design? What are the advantages or disadvantages to either method? Notice there is not set. public string URL { get { return "www.somewhere.com/test.aspx"; }
0
872
by: Keith Chadwick | last post by:
I have been building a class around a xml document that is the representation of several tables from our sql2k database. The class has your standard methods such as load and save but I have also been adding in properties to allow for easy access to the the underlying xml document. My problem is this. I would like to strongly type the properties to match as closely with the sql2k types they are store in. For instance I have an integer...
4
2014
by: XNoDE | last post by:
Hi all, VS.Net 2002. Is there a way to set default properties for controls in the Toolbox? There a some properties that I implement as standard in my apps and would like to get around setting these same basic things each time a use a control.
22
1546
by: Peter | last post by:
Hi I was wondering, why use properties instead of get and set methods? I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)" methods for example. Are there any arguments as to why properties are better? Or is it just a matter or preference? Personally I like them - I think they "look nice", but that's about as good as I can do :-|
0
8390
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
8909
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
8819
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...
0
7428
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
5690
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
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2806
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
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
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.