C++'da İlginç bir template kullanımı

Başlatan yamak, 30 Ağustos 2016, 15:49:36

yamak

Touchgfx'i incelerken adamların template'lerle oluşturduğu meta fonksiyonlar çok hoşuma gitti.Ben de sizinle paylaşmak istedim.

Aşağıdaki dosya Meta.cpp.Touchgfx tarafından hazırlandı ve herhangi bir değişiklik yapmaya gerek yok.
/******************************************************************************
 *
 * @brief     This file is part of the TouchGFX 4.6.0 evaluation distribution.
 *
 * @author    Draupner Graphics A/S <http://www.touchgfx.com>
 *
 ******************************************************************************
 *
 * @section Copyright
 *
 * Copyright (C) 2014-2016 Draupner Graphics A/S <http://www.touchgfx.com>.
 * All rights reserved.
 *
 * TouchGFX is protected by international copyright laws and the knowledge of
 * this source code may not be used to write a similar product. This file may
 * only be used in accordance with a license and should not be re-
 * distributed in any way without the prior permission of Draupner Graphics.
 *
 * This is licensed software for evaluation use, any use must strictly comply
 * with the evaluation license agreement provided with delivery of the
 * TouchGFX software.
 *
 * The evaluation license agreement can be seen on [url=http://www.touchgfx.com]www.touchgfx.com[/url]
 *
 * @section Disclaimer
 *
 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Draupner Graphics A/S has
 * no obligation to support this software. Draupner Graphics A/S is providing
 * the software "AS IS", with no express or implied warranties of any kind,
 * including, but not limited to, any implied warranties of merchantability
 * or fitness for any particular purpose or warranties against infringement
 * of any proprietary rights of a third party.
 *
 * Draupner Graphics A/S can not be held liable for any consequential,
 * incidental, or special damages, or any other relief, or for any claim by
 * any third party, arising from your use of this software.
 *
 *****************************************************************************/
#ifndef META_HPP
#define META_HPP

namespace touchgfx
{

/**
 * Template meta-programming tools are grouped in this namespace
 */
namespace meta
{
/**
 * @struct Nil Meta.hpp common/Meta.hpp
 *
 * @brief Nil-type, indicates the end of a TypeList.
 *
 *        Nil-type, indicates the end of a TypeList.
 */
struct Nil { };

/**
* @struct TypeList Meta.hpp common/Meta.hpp
 *
 * @brief TypeList, used for generating compile-time lists of types.
 *
 *        TypeList, used for generating compile-time lists of types.
 *
 * @tparam First Type of the first.
 * @tparam Next  Type of the next.
 */
template< typename First, typename Next >
struct TypeList
{
    typedef First first; ///< The first element in the TypeList
    typedef Next  next;  ///< Remainder of the TypeList
};

/**
 * @fn template < typename T1, typename T2, bool choose1 = (sizeof(T1) > sizeof(T2)) > struct type_max
 *
 * @brief Meta-function, selects the "maximum" type, i.e. the largest type.
 *
 *        Meta-function, selects the "maximum" type, i.e. the largest type.
 *
 * @tparam T1          Generic type parameter.
 * @tparam T2          Generic type parameter.
 * @tparam choose1     True if sizeof(T1) is larger than sizeof(T2).
 */
template < typename T1, typename T2, bool choose1 = (sizeof(T1) > sizeof(T2)) >
struct type_max
{
    typedef T1 type; ///< The resulting type (default case: sizeof(T1)>sizeof(T2))
};

/**
 * @struct type_max<T1,T2,false> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization for the case where sizeof(T2) >= sizeof(T1).
 *
 *        Specialization for the case where sizeof(T2) >= sizeof(T1).
 *
 * @tparam T1 Generic type parameter.
 * @tparam T2 Generic type parameter.
 */
template<typename T1, typename T2>
struct type_max<T1, T2, false>
{
    typedef T2 type; ///< The resulting type (default case: sizeof(T2)>=sizeof(T1))
};

/**
 * @struct select_type_maxsize Meta.hpp common/Meta.hpp
 *
 * @brief Meta-function signature, selects maximum type from TypeList.
 *
 *        Meta-function signature, selects maximum type from TypeList.
 *
 * @tparam T Generic type parameter.
 */
template<typename T> struct select_type_maxsize;

/**
 * @struct select_type_maxsize<TypeList<First,Next>> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization to dive into the list (inherits result from type_max).
 *
 * @tparam First Type of the first.
 * @tparam Next  Type of the next.
 */
template< typename First, typename Next >
struct select_type_maxsize< TypeList< First, Next > > : public type_max< First, typename select_type_maxsize< Next >::type >
{ };

/**
 * @struct select_type_maxsize<TypeList<First,Nil>> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization for loop termination (when type Nil encountered).
 *
 *        Specialization for loop termination (when type Nil encountered).
 *
 * @tparam First Type of the first.
 */
template< typename First >
struct select_type_maxsize< TypeList< First, Nil > >
{
    typedef First type;
};

/**
 * @struct list_join Meta.hpp common/Meta.hpp
 *
 * @brief Meta-function signature, joins typelist with type (or another typelist).
 *
 *        Meta-function signature, joins typelist with type (or another typelist).
 *
 * @tparam TList Type of the list.
 * @tparam T     Generic type parameter.
 */
template< typename TList, typename T >
struct list_join;

/**
 * @struct list_join<Nil,Nil> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization for termination.
 *
 *        Specialization for termination.
 */
template<>
struct list_join< Nil, Nil>
{
    typedef Nil result;
};

/**
 * @struct list_join<Nil,T> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization for "end-of-LHS", with RHS as type.
 *
 *        Specialization for "end-of-LHS", with RHS as type.
 *
 * @tparam T Generic type parameter.
 */
template< typename T >
struct list_join< Nil, T>
{
    typedef TypeList< T, Nil> result;
};

/**
 * @struct list_join<Nil,TypeList<First,Next>> Meta.hpp common/Meta.hpp
 *
 * @brief Specialization for "end-of-LHS", with RHS as a TypeList.
 *
 *        Specialization for "end-of-LHS", with RHS as a TypeList.
 *
 * @tparam First Type of the first.
 * @tparam Next  Type of the next.
 */
template< typename First, typename Next>
struct list_join< Nil, TypeList< First, Next > >
{
    typedef TypeList< First, Next > result;
};

/**
 * @struct list_join<TypeList<First,Next>,T> Meta.hpp common/Meta.hpp
 *
 * @brief Recursively joins a typelist (LHS) with a type or a type-list (RHS).
 *
 *        Recursively joins a typelist (LHS) with a type or a type-list (RHS).
 *
 * @tparam First Type of the first.
 * @tparam Next  Type of the next.
 * @tparam T     Generic type parameter.
 */
template< typename First, typename Next, typename T>
struct list_join< TypeList< First, Next >, T >
{
    typedef TypeList< First, typename list_join<Next, T>::result > result;
};
} // namespace meta

} // namespace touchgfx

#endif // META_HPP


Aşağıdaki dosya ise FrontEndHeap.cpp ve programcı tarafından doldurulması gerekiyor.
/******************************************************************************
 *
 * @brief     This file is part of the TouchGFX 4.6.0 evaluation distribution.
 *
 * @author    Draupner Graphics A/S <http://www.touchgfx.com>
 *
 ******************************************************************************
 *
 * @section Copyright
 *
 * This file is free software and is provided for example purposes. You may
 * use, copy, and modify within the terms and conditions of the license
 * agreement.
 *
 * This is licensed software for evaluation use, any use must strictly comply
 * with the evaluation license agreement provided with delivery of the
 * TouchGFX software.
 *
 * The evaluation license agreement can be seen on [url=http://www.touchgfx.com]www.touchgfx.com[/url]
 *
 * @section Disclaimer
 *
 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Draupner Graphics A/S has
 * no obligation to support this software. Draupner Graphics A/S is providing
 * the software "AS IS", with no express or implied warranties of any kind,
 * including, but not limited to, any implied warranties of merchantability
 * or fitness for any particular purpose or warranties against infringement
 * of any proprietary rights of a third party.
 *
 * Draupner Graphics A/S can not be held liable for any consequential,
 * incidental, or special damages, or any other relief, or for any claim by
 * any third party, arising from your use of this software.
 *
 *****************************************************************************/
#ifndef FRONTENDHEAP_HPP
#define FRONTENDHEAP_HPP

#include <common/Meta.hpp>
#include <common/Partition.hpp>
#include <mvp/MVPHeap.hpp>
#include <gui/common/FrontendApplication.hpp>
#include <gui/model/Model.hpp>
#include <gui/first_screen/FirstView.hpp>
#include <gui/first_screen/FirstPresenter.hpp>
#include <gui/second_screen/SecondView.hpp>
#include <gui/second_screen/SecondPresenter.hpp>
#include <gui/common/CustomTransition.hpp>
#include <touchgfx/transitions/NoTransition.hpp>

/**
 * This class provides the memory that shall be used for memory allocations
 * in the frontend. A single instance of the FrontendHeap is allocated once (in heap
 * memory), and all other frontend objects such as views, presenters and data model are
 * allocated within the scope of this FrontendHeap. As such, the RAM usage of the entire
 * user interface is sizeof(FrontendHeap).
 *
 * @note The FrontendHeap reserves memory for the most memory-consuming presenter and
 * view only. The largest of these classes are determined at compile-time using template
 * magic. As such, it is important to add all presenters, views and transitions to the
 * type lists in this class.
 *
 */
class FrontendHeap : public MVPHeap
{
public:
    /**
     * A list of all view types. Must end with meta::Nil.
     * @note All view types used in the application MUST be added to this list!
     */
    typedef meta::TypeList< FirstView,
            meta::TypeList< SecondView,
            meta::Nil
            > > ViewTypes;

	
    /**
     * Determine (compile time) the View type of largest size.
     */
    typedef meta::select_type_maxsize< ViewTypes >::type MaxViewType;

    /**
     * A list of all presenter types. Must end with meta::Nil.
     * @note All presenter types used in the application MUST be added to this list!
     */
    typedef meta::TypeList< FirstPresenter,
            meta::TypeList< SecondPresenter,
            meta::Nil
            > > PresenterTypes;

    /**
     * Determine (compile time) the Presenter type of largest size.
     */
    typedef meta::select_type_maxsize< PresenterTypes >::type MaxPresenterType;

    /**
     * A list of all transition types. Must end with meta::Nil.
     * @note All transition types used in the application MUST be added to this list!
     */
    typedef meta::TypeList< CustomTransition,
            meta::TypeList< NoTransition,
            meta::Nil
            > > TransitionTypes;

    /**
     * Determine (compile time) the Transition type of largest size.
     */
    typedef meta::select_type_maxsize< TransitionTypes >::type MaxTransitionType;

    static FrontendHeap& getInstance()
    {
        static FrontendHeap instance;
        return instance;
		
    }

    Partition< PresenterTypes, 1 > presenters;
    Partition< ViewTypes, 1 > views;
    Partition< TransitionTypes, 1 > transitions;
    FrontendApplication app;
    Model model;
private:
    FrontendHeap()
        : MVPHeap(presenters, views, transitions, app),
          presenters(),
          views(),
          transitions(),
          app(model, *this)
    {
        // Goto start screen
        app.gotoFirstScreenNoTrans();
    }

};

#endif

Bu dosyada doldurulması gereken kısımlar;
ViewTypes ve ViewTypes kısımları.Mesela burda ViewTypes:
    typedef meta::TypeList< FirstView,
            meta::TypeList< SecondView,
            meta::Nil
            > > ViewTypes;

Şeklinde doldurulmuş.Burada FirstView ve SecondView ekrandaki herbir menunun ismi ve herbiri bir class.
Aşağıdaki meta fonksiyonla da compile time'da size ı en büyük olan class bulunuyor.
typedef meta::select_type_maxsize< PresenterTypes >::type MaxPresenterType;

Böylece compile time'da heap alanını hesaplamış oluyo.

F493

Selam,
  Hocam TouchGFX ile ilgili çalışmalar yapıyorsunuz sanırım, RTOS da kullanıyor musunuz uygulamada? Çalışmalarınız ne düzeyde, Yurt içi  ve Yurt dışı ile görüşmelerim oldu. Güzel bir yazılım hakikatten. Çok ilgimi çekiyor.

Esen kalın.

yamak

Hocam şu an inceleme aşamasındayım ama çok hoşuma gitti.Adamlar gerçekten çok güzel yazmışlar. Zaten gördüğüm kadarıyla touchgfx e tek rakip embedded wizard var galiba.

F493

Hocam TouchGFX ile ilgili GUI Designer eksiği var sanırım. PC tarafında Tasarım arayüzü zayıf, her şeyi kod ile yapıyorsunuz. Direk FreeRTOS ile geliyor. Ama muhteşem sonuçlar çıkıyor ortaya. EmWin ile kapsamlı projeler yaptım fakat arayüzü zayıf kalıyor, animasyon vs.  TouchGFX çok hoşuma gitti ama C++ ve FreeRTOS olması hızlı başlamaya engel oldu açıkçası. Ama bir okadar da güvenli ve güçlü kılıyor. Özellikle C++ olduğundan Exception Handling olması çok çok iyi ve FreeRTOS olması da bunu Catch etmek için iyi. İşletim sistemi olması çok hoş. Yükten kurtarır insanı. Henüz bir butonla hello world yapma şansım olmadı. Belki siz bir şeyler paylaşırsınız :-).

Esen kalın.

oyaz

Touchgfx gerçekten çok güzel grafiklere sahip. Designer ın beta sürümü çıktı bildiğim kadarıyla.
Become a learning machine...

yamak

Evet hocam designer in beta si cikti ama indirilemiyor. Ancak tester olara basvuruda bulunulursa oyle olabiliyor.Ama ben basvurdum link falan gondermediler.
Ayrica C++ olmasa bu kadar guclu olmazdi. Excetion handling ten ziyade gui tasarimlarinda nesne yonelimli bi dille tasarim yapmak avantajli oluyo.

F493

Bu konunun üzerine gitmek gerekli. Projenin en önemli kısımlarından biri, User Interface ve bu yazılım oldukça yeterli.

F493

gerbay kullanıyor musunuz bu proğramı?

F493

Süper Hocam, 

  Temsilci firma G3TEK  , firmada Celal Bey var onunla irtibata geçmiştik. Hocam konuyla alakalı çizeceğiniz bir harita olur mu?. Mesela döküman olabilir, hiç ekrana buton koyma gibi basit bir işlem yaptınız mı?. Valla EmWin'in dibine indim ama çok fakir, C++ bilgim biraz eskidir uzun yıllar kod yazmadım ama kuralları mantığı, OOP, class vs iyi bildiğim bir yapı. Fakat işin içine FreeRTOS da girince öğrenme süreci uzuyor, zaman sorunu olmasa mesele değil.  Ben bu konuya açım. Öneri ve yardımlarınızı bekliyorum.

Bu arada şuan ARÇELİK yazılımı alma niyetindeymiş.

Esen kalın.

F493


yamak

Alıntı yapılan: gerbay - 30 Ağustos 2016, 20:21:29
exception handling embedded için çok istenen birşey değil aslında. maaliyeti biraz fazla.

TouchGfx in grafiklerinin gücü ise "Anti-Grain Geometry" den geliyor..    http://www.antigrain.com/about/index.html    u inceleyebilirsiniz.

antigrain geometry yi çıkaran rus eleman bayaa iyi iş yapmış.. zaten sonrasında AutoCad de işe girmişti diye hatırlıyorum.

şu an kaliteli grafiği olan bir çok yazılım anti-grain geometry kullanıyor..  hatta en çok da coğrafi bilgi sistemi ve haritalama yazılımları kullanıyor.

TouchGfx in C++ tasarımı da güzel olmuş ancak bizim forumda kullanabilecek az kişi çıkar. Embedded için de artık C++ öğrenmeye başlayın diyeli yıllar geçti ama bizim ahali kitaptan biraz C öğrenince her şeyi yazabileceğini düşünüp atlıyor proje yapmaya.. Ne veri yapısı var ne algoritma.. template falan filan ise ancak rüyada olur..

Neyse, TouchGfx güzeldir, yaptığınız işi PC ortamında da derleyip görebiliyor olmanız da bayaa bir güzeldir.
@gerbay hocam artık mikrolar gelişti ve içerisine yazılacak kodun boyutu ve projenin büyüklüğü de aynı oranda büyümüş oluyo.O sebep günümüz projelerini C++ ile yazmak şart.Yakında herkes yavaş yavaş C++ a geçmek zorunda kalacaktır.
Alıntı yapılan: F493 - 30 Ağustos 2016, 21:06:22
Süper Hocam, 

  Temsilci firma G3TEK  , firmada Celal Bey var onunla irtibata geçmiştik. Hocam konuyla alakalı çizeceğiniz bir harita olur mu?. Mesela döküman olabilir, hiç ekrana buton koyma gibi basit bir işlem yaptınız mı?. Valla EmWin'in dibine indim ama çok fakir, C++ bilgim biraz eskidir uzun yıllar kod yazmadım ama kuralları mantığı, OOP, class vs iyi bildiğim bir yapı. Fakat işin içine FreeRTOS da girince öğrenme süreci uzuyor, zaman sorunu olmasa mesele değil.  Ben bu konuya açım. Öneri ve yardımlarınızı bekliyorum.

Bu arada şuan ARÇELİK yazılımı alma niyetindeymiş.

Esen kalın.
Hocam aslında başlangıç projesi çok zor değil. Çünkü adamlar zaten template bir proje oluşturmuşlar ve visual studio da simülator için tasarım yapabiliyosunuz.İlk olarak template proje üstünden ilerleyebilirsiniz.Ayrıca gerçek ortamda denemenize de gerek yok.Simülatorde denemeniz yeterli.Aşağıdaki video (rastlamışsınızdır belki) başlangıç için ideal aslında.

https://www.youtube.com/watch?v=W68VFFUmrb0

tekosis

hocalarım bu yazdıklarınız nedir? nerelerde kullanıyorsunuz biraz açar mısınız?
İlim ilim bilmektir, ilim kendin bilmektir, sen kendin bilmezsin, bu nice okumaktır.

yamak

Alıntı yapılan: tekosis - 31 Ağustos 2016, 00:24:37
hocalarım bu yazdıklarınız nedir? nerelerde kullanıyorsunuz biraz açar mısınız?
Hocam touchgfx embedded platformlar için hazırlanmış bir gui kütüphanesi.Örneğin aşağıdaki videoki örnek stm32f429'a yazılmış
https://www.youtube.com/watch?v=j-fgE1hOlbo

tekosis

İlim ilim bilmektir, ilim kendin bilmektir, sen kendin bilmezsin, bu nice okumaktır.