📄 wzcomponenttest.html
字号:
<TITLE>WZ 1.0 - Component</TITLE>
<H1>Virtual Components</H1>
<PRE>
#include "<A HREF="wzcomponent.hxx">wzcomponent.hxx</A>"
</PRE>
<P>It is a common recommendation to make components private or
protected. This increases flexibility in the implementation. Our
position is different: The syntax we use in the application of some
package suggests a certain semantic context, has the character of
information.
<P>Moreover, it is much simpler to remember that there is a component
named xyz instead of remembering a number of methods like setXyz(),
getXyz(), with a number of variations from package to package of type
set_xyz(), xyz(), Xyz() and so on. Last not least, public components are
useful for fast prototyping.
<P>But what happens if we have used a public component in fast prototyping
and observe now that it is useful to replace the assignment with a call?
Here is a simple solution for this case. It is sufficient at least for
not very time-critical parts, that means, probably 95% of your code.
<P>This technique allows to leave the old access syntax
intact. Formally, we replace the type of the component. Now, the old
assignments now have to be replaced by implicit type-conversions. But
these type-conversions may be overwritten.
<PRE>
/*
class X{
public:
int a;
X(int i=0):a(i){;}
};
*/
</PRE>
has to be replaced by the code:
<PRE>
class X: public wzComponentClass{
public:
wzComponent<int> a; // thus, wzComponent<T> instead of T
X(int i=0):a(*this,i){;} // *this as the reference to a wzComponentClass
</PRE>
<P>That's the formal part. Let's see how you can use a inside your
implementation:
<PRE>
void geta(int& x){x = a.value;} // direct access - without call of readAccess
void getA(int& x){x = a;} // with call of readAccess
</PRE>
<P>Now, we have to define the virtual functions <B>readAccess</B> and
<B>writeAccess</B> of the abstract class wzComponentClass:
<PRE>
private:
void readAccess();
void writeAccess();
};
void X::readAccess()
{if (a.value>100) a.value = 100;} // or some other inconsistency correction;
</PRE>
<P>Another nice application is to define "constant" components which
are not really constant, but cannot be changed from outside:
<PRE>
class WriteAccessNowForbidden{int make_compiler_happy;};
void X::writeAccess()
{throw WriteAccessNowForbidden();}
</PRE>
<P>Internally, you can get the value of <B>a</B> by using
<B>a.value</B>. Before every read access, the function readAccess()
will be called and can check the internal state and compute all values
which may be inconsistent in the new implementation.
<PRE>
void test()
{
X x1(10), x2(150);
int b1,b2,b3;
x2.geta(b1); // gets the original, unmodified, current value 150;
x2.getA(b2); // gets the corrected value 100;
b3 = x2.a; // gets the corrected value 100;
now(b1==150); now(b2==100); now(b3==100);
try{
x1.a = 20;
should_fail();
}catch(WriteAccessNowForbidden){;}
</PRE>
<A NAME="errors"> <hr></A>
<H2><A HREF="errors.html">Errors</A></H2>
<PRE>
}
</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -