Browse Source

Reference Counting zu threads hinzugefügt

kolja 5 years ago
parent
commit
d128ead01e
7 changed files with 99 additions and 48 deletions
  1. 2 1
      AsynchronCall.cpp
  2. 3 18
      RenderThread.cpp
  3. 1 8
      RenderThread.h
  4. 27 0
      Thread.cpp
  5. 7 0
      Thread.h
  6. 54 19
      UIMLView.cpp
  7. 5 2
      UIMLView.h

+ 2 - 1
AsynchronCall.cpp

@@ -35,7 +35,8 @@ void AsynchronCall::thread()
 
 void AsynchronCall::threadEnd()
 {
+    Thread::threadEnd();
     if( finished )
         *finished = 1;
-    delete this;
+    release();
 }

+ 3 - 18
RenderThread.cpp

@@ -7,7 +7,8 @@ using namespace Framework;
 // Inhalt der RenderTh Klasse aus RenderThread.h
 // Konstruktor 
 RenderTh::RenderTh()
-    : stoppen( 0 ),
+    : Thread(),
+    stoppen( 0 ),
     bildschirm( 0 ),
     zeit( new ZeitMesser() ),
     renderTickZeit( 1 / 60 ),
@@ -16,8 +17,7 @@ RenderTh::RenderTh()
     renderFunktion( 0 ),
     tickFunktion( 0 ),
     pause( 0 ),
-    maxFps( 30 ),
-    ref( 1 )
+    maxFps( 30 )
 {
 }
 
@@ -141,19 +141,4 @@ Bildschirm *RenderTh::zBildschirm() const
 double RenderTh::getRenderTickZeit() const // gibt die Zeit zurück, die zum Rendern und zum Tick benötigt wird
 {
     return renderTickZeit;
-}
-
-// Reference Counting
-RenderTh *RenderTh::getThis()
-{
-    ++ref;
-    return this;
-}
-
-RenderTh *RenderTh::release()
-{
-    --ref;
-    if( !ref )
-        delete this;
-    return 0;
 }

+ 1 - 8
RenderThread.h

@@ -12,7 +12,7 @@ namespace Framework
     class Bild; // Bild.h
 
     // Ein Thread, der ein Bildschirm verwaltet. Er ruft die render() und tick() funktionen automatisch auf
-    class RenderTh : private Thread
+    class RenderTh : public Thread
     {
     private:
         bool stoppen;
@@ -26,7 +26,6 @@ namespace Framework
         bool pause;
         Critical cs;
         int maxFps;
-        int ref;
 
     public:
         // Konstruktor 
@@ -71,12 +70,6 @@ namespace Framework
         __declspec( dllexport ) Bildschirm *zBildschirm() const;
         // Gibt die Zeit zurück, mit der die tick() Funktion des Bildschirms als letztes aufgerufen wurde
         __declspec( dllexport ) double getRenderTickZeit() const;
-        // Erhöht den Reference Counter um 1
-        //  Return: Ein zeiger auf diesen Shader
-        __declspec( dllexport ) RenderTh *getThis();
-        // Verringert den Reference Counter und löscht den Shader, falls der Refeence Counter auf 0 ist
-        //  Return: 0
-        __declspec( dllexport ) RenderTh *release();
     };
 }
 

+ 27 - 0
Thread.cpp

@@ -13,6 +13,7 @@ Thread::Thread()
     thRegister->add( this );
     run = 0;
     lockCount = 0;
+    ref = 1;
 }
 
 // Destruktor 
@@ -39,6 +40,7 @@ void Thread::start() // startet den Thread
     {
 #ifdef WIN32
         threadHandle = CreateThread( 0, 0, threadStart, this, 0, &threadId );
+        ref++;
 #else
         pthread_create( &threadHandle, NULL, threadStart, this );
 #endif
@@ -66,6 +68,7 @@ void Thread::ende() // beendet den Thread
     {
         while( lockCount > 0 )
             Sleep( 100 );
+        bool rel = run;
 #ifdef WIN32
 #pragma warning(suppress: 6258)
         TerminateThread( threadHandle, 0 );
@@ -77,6 +80,12 @@ void Thread::ende() // beendet den Thread
         }
         pthread_cancel( threadHandle );
 #endif
+        if( rel )
+        {
+            run = 0;
+            release();
+            return;
+        }
     }
     run = 0;
 }
@@ -87,6 +96,7 @@ void Thread::thread() // Thread
 void Thread::threadEnd()
 {
     run = 0;
+    release();
 }
 
 // constant 
@@ -139,6 +149,23 @@ void Thread::removeCriticalLock()
     lockCount--;
 }
 
+// Erhöht den Reference Counting Zähler.
+//  return: this.
+Thread *Thread::getThis()
+{
+    ref++;
+    return this;
+}
+
+// Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
+//  return: 0.
+Thread *Thread::release()
+{
+    if( !--ref )
+        delete this;
+    return 0;
+}
+
 // funktionen 
 #ifdef WIN32
 unsigned long __stdcall Framework::threadStart( void *param )

+ 7 - 0
Thread.h

@@ -18,6 +18,7 @@ namespace Framework
         unsigned long threadId;
         pthread_t threadHandle;
         bool run;
+        int ref;
 
     public:
         // Konstruktor 
@@ -50,6 +51,12 @@ namespace Framework
         void setSystemHandlePointer( pthread_t *ths );
         // Gibt ein Handle auf den Thread zurück
         __declspec( dllexport ) pthread_t getThreadHandle() const;
+        // Erhöht den Reference Counting Zähler.
+        //  return: this.
+        __declspec( dllexport ) Thread *getThis();
+        // Verringert den Reference Counting Zähler. Wenn der Zähler 0 erreicht, wird das Zeichnung automatisch gelöscht.
+        //  return: 0.
+        __declspec( dllexport ) virtual Thread *release();
 
     private:
         void addCriticalLock();

+ 54 - 19
UIMLView.cpp

@@ -127,8 +127,15 @@ Zeichnung *UIMLView::parseElement( XML::Element *e )
                     break;
             }
         }
+        if( e->hasAttribute( "text-align" ) )
+        {
+            if( !e->hasAttribute( "text-align-horizontal" ) )
+                e->setAttribute( "text-align-horizontal", e->getAttributeValue( "text-align" ) );
+            if( !e->hasAttribute( "text-align-vertical" ) )
+                e->setAttribute( "text-align-vertical", e->getAttributeValue( "text-align" ) );
+        }
         // create objects
-        if( e->getName().istGleich( "textfield" ) || 
+        if( e->getName().istGleich( "textfield" ) ||
             e->getName().istGleich( "text" ) ||
             e->getName().istGleich( "textarea" ) )
         {
@@ -142,6 +149,16 @@ Zeichnung *UIMLView::parseElement( XML::Element *e )
             t->setText( e->getText() );
             if( e->hasAttribute( "font-size" ) )
                 t->setSchriftSize( (int)e->getAttributeValue( "font-size" ) );
+            if( e->hasAttribute( "text-align-horizontal" ) )
+            {
+                if( e->getAttributeValue( "text-align-horizontal" ).istGleich( "center" ) )
+                    t->addStyle( TextFeld::Style::HCenter );
+            }
+            if( e->hasAttribute( "text-align-vertical" ) )
+            {
+                if( e->getAttributeValue( "text-align-vertical" ).istGleich( "center" ) )
+                    t->addStyle( TextFeld::Style::VCenter );
+            }
             z = t;
         }
         if( e->getName().istGleich( "button" ) )
@@ -169,7 +186,7 @@ Zeichnung *UIMLView::parseElement( XML::Element *e )
     return z;
 }
 
-void UIMLView::layout( XML::Element *e )
+void UIMLView::layout( XML::Element *e, Zeichnung *parent )
 {
     Text id = e->getAttributeValue( "id" );
     Zeichnung *z = members->z( id );
@@ -182,14 +199,14 @@ void UIMLView::layout( XML::Element *e )
             Text w = e->getAttributeValue( "width" );
             width = w;
             if( w.getText()[ w.getLength() - 1 ] == '%' )
-                width = ( getBreite() / 100 ) * width;
+                width = (int)( ( parent->getBreite() / 100.0 ) * width );
         }
         if( e->hasAttribute( "height" ) )
         {
             Text h = e->getAttributeValue( "height" );
             height = h;
             if( h.getText()[ h.getLength() - 1 ] == '%' )
-                height = ( getHeight() / 100 ) * height;
+                height = (int)( ( parent->getHeight() / 100.0 ) * height );
         }
         z->setSize( width, height );
         if( e->hasAttribute( "align-left" ) )
@@ -199,12 +216,12 @@ void UIMLView::layout( XML::Element *e )
             if( la.istGleich( "start" ) )
                 x = 0;
             else if( la.istGleich( "end" ) )
-                x = getBreite();
+                x = parent->getBreite();
             else
             {
                 XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", la );
                 for( auto i = ed.getIterator(); i; i++ )
-                    layout( i );
+                    layout( i, parent );
                 Zeichnung *laz = members->z( la );
                 if( laz )
                     x = laz->getX() + laz->getBreite();
@@ -214,7 +231,7 @@ void UIMLView::layout( XML::Element *e )
                 Text mt = e->getAttributeValue( "margin-left" );
                 int m = mt;
                 if( mt.getText()[ mt.getLength() - 1 ] == '%' )
-                    m = ( getBreite() / 100 ) * m;
+                    m = (int)( ( parent->getBreite() / 100.0 ) * m );
                 x += m;
             }
             z->setX( x );
@@ -226,12 +243,12 @@ void UIMLView::layout( XML::Element *e )
             if( ra.istGleich( "start" ) )
                 x = -z->getBreite();
             else if( ra.istGleich( "end" ) )
-                x = getBreite() - z->getBreite();
+                x = parent->getBreite() - z->getBreite();
             else
             {
                 XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ra );
                 for( auto i = ed.getIterator(); i; i++ )
-                    layout( i );
+                    layout( i, parent );
                 Zeichnung *raz = members->z( ra );
                 if( raz )
                     x = raz->getX() - z->getBreite();
@@ -241,7 +258,7 @@ void UIMLView::layout( XML::Element *e )
                 Text mt = e->getAttributeValue( "margin-right" );
                 int m = mt;
                 if( mt.getText()[ mt.getLength() - 1 ] == '%' )
-                    m = ( getBreite() / 100 ) * m;
+                    m = (int)( ( parent->getBreite() / 100.0 ) * m );
                 x -= m;
             }
             z->setX( x );
@@ -253,12 +270,12 @@ void UIMLView::layout( XML::Element *e )
             if( ta.istGleich( "start" ) )
                 y = 0;
             else if( ta.istGleich( "end" ) )
-                y = getHeight();
+                y = parent->getHeight();
             else
             {
                 XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ta );
                 for( auto i = ed.getIterator(); i; i++ )
-                    layout( i );
+                    layout( i, parent );
                 Zeichnung *taz = members->z( ta );
                 if( taz )
                     y = taz->getY() + taz->getHeight();
@@ -268,7 +285,7 @@ void UIMLView::layout( XML::Element *e )
                 Text mt = e->getAttributeValue( "margin-top" );
                 int m = mt;
                 if( mt.getText()[ mt.getLength() - 1 ] == '%' )
-                    m = ( getHeight() / 100 ) * m;
+                    m = (int)( ( parent->getHeight() / 100.0 ) * m );
                 y += m;
             }
             z->setY( y );
@@ -280,12 +297,12 @@ void UIMLView::layout( XML::Element *e )
             if( ba.istGleich( "start" ) )
                 y = -z->getHeight();
             else if( ba.istGleich( "end" ) )
-                y = getHeight() - z->getHeight();
+                y = parent->getHeight() - z->getHeight();
             else
             {
                 XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ba );
                 for( auto i = ed.getIterator(); i; i++ )
-                    layout( i );
+                    layout( i, parent );
                 Zeichnung *baz = members->z( ba );
                 if( baz )
                     y = baz->getY() - z->getHeight();
@@ -295,7 +312,7 @@ void UIMLView::layout( XML::Element *e )
                 Text mt = e->getAttributeValue( "margin-bottom" );
                 int m = mt;
                 if( mt.getText()[ mt.getLength() - 1 ] == '%' )
-                    m = ( getHeight() / 100 ) * m;
+                    m = (int)( ( parent->getHeight() / 100.0 ) * m );
                 y -= m;
             }
             z->setY( y );
@@ -307,17 +324,35 @@ void UIMLView::layout( XML::Element *e )
             Text xt = e->getAttributeValue( "x" );
             x = xt;
             if( xt.getText()[ xt.getLength() - 1 ] == '%' )
-                x = ( getBreite() / 100 ) * x;
+                x = (int)( ( parent->getBreite() / 100.0 ) * x );
         }
         if( e->hasAttribute( "y" ) )
         {
             Text yt = e->getAttributeValue( "y" );
             y = yt;
             if( yt.getText()[ yt.getLength() - 1 ] == '%' )
-                y = ( getHeight() / 100 ) * y;
+                y = (int)( ( parent->getHeight() / 100.0 ) * y );
         }
         z->setPosition( x, y );
     }
+    // recursive layout
+    for( auto i = e->getChilds(); i; i++ )
+        layout( i, z ? z : parent );
+    if( z )
+    {
+        if( e->getName().istGleich( "table" ) )
+        {
+            ObjTabelle *objT = (ObjTabelle*)z;
+            if( objT->getZeilenAnzahl() > 0 )
+            {
+                for( int i = 0; i < objT->getSpaltenAnzahl(); i++ )
+                {
+                    if( objT->zZeichnung( i, 0 ) )
+                        objT->setSpaltenBreite( i, objT->zZeichnung( i, 0 )->getBreite() );
+                }
+            }
+        }
+    }
 }
 
 // setzt den inhalt der view
@@ -359,7 +394,7 @@ void UIMLView::layout()
     {
         for( auto i = dom->getChilds(); i; i++ )
         {
-            layout( i._ );
+            layout( i._, this );
         }
     }
 }

+ 5 - 2
UIMLView.h

@@ -40,7 +40,10 @@
     align-top / align-bottom
 
   spezific attributes:
-     
+     font-size (textfield, text, textarea, button)
+     text-align (textfield, text, textarea)
+     text-align-horizontal (textfield, text, textarea)
+     text-align-vertical (textfield, text, textarea)
 
   example:
     <view>
@@ -75,7 +78,7 @@ namespace Framework
         int nextId;
         void parseTable( Iterator<XML::Element*> childs, ObjTabelle *table );
         Zeichnung *parseElement( XML::Element *e );
-        void layout( XML::Element *e );
+        void layout( XML::Element *e, Zeichnung *parent );
 
     public:
         // Erstellt eine UIML View