📄 mainwzcomponenttest.html
字号:
<TITLE>WZ 1.1: Components</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 orprotected. This increases flexibility in the implementation. Ourposition is different: The syntax we use in the application of somepackage suggests a certain semantic context, has the character ofinformation. <P>Moreover, it is much simpler to remember that there is a componentnamed xyz instead of remembering a number of methods like setXyz(),getXyz(), with a number of variations from package to package of typeset_xyz(), xyz(), Xyz() and so on. Last not least, public components areuseful for fast prototyping. <P>But what happens if we have used a public component in fast prototypingand 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 fornot very time-critical parts, that means, probably 95% of your code. <P>This technique allows to leave the old access syntaxintact. Formally, we replace the type of the component. Now, the oldassignments now have to be replaced by implicit type-conversions. Butthese 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 the component<I>a</I> 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 whichare 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 valueswhich may be inconsistent in the new implementation.<PRE>void main(){ 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<A NAME="End"> <hr></A><PRE> fine();}</PRE> <P>You can test this code with: <BR> <BR><B>>cog main wzcomponent</B> <BR> <BR>The output should be simply: <BR> <BR><B>success</B>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -