<commit_before>#include <cstring>

#include "SampleAndControl.h"
#include "chprintf.h"

#include "MPU6050.h"
#include "rear_motor_controller.h"
#include "fork_motor_controller.h"
#include "SampleBuffer.h"
#include "SystemState.h"

SampleAndControl::SampleAndControl()
  : tp_control_(0), state_(0)
{
}

void SampleAndControl::controlThread(const char * filename)
{
  chRegSetThreadName("Control");
  enableSensorsMotors();
  MPU6050 & imu = MPU6050::Instance();
  hardware::RearMotorController rear_motor_controller;
  hardware::ForkMotorController fork_motor_controller;
  hardware::Encoder front_wheel_encoder(STM32_TIM4, 800);
  front_wheel_encoder.set_count(0);

  SampleBuffer & sb = SampleBuffer::Instance();
  sb.initialize(filename);

  // zero out system timer
  STM32_TIM5->CNT = 0;

  // Create a sample to populate
  Sample s;
  memset(&s, 0, sizeof(s));

  systime_t time = chTimeNow();     // Initial time
  systime_t sleep_time;
  for (uint32_t i = 0; !chThdShouldTerminate(); ++i) {
    time += MS2ST(con::T_ms);       // Next deadline

    // Begin pre control data collection
    sampleTimers(s);  // sample system time/encoder counts/PWM duty cycle
    imu.Acquire(s);   // sample rate gyro, accelerometer and temperature sensors
    sampleSetPoints(s); // sample rear wheel and yaw rate commands
    // End pre control data collection

    // Begin control
    // if (rwc.isEnabled() && (i % con::RW_N == 0))
    rear_motor_controller.update(s);
    // if (yrc.isEnabled() && (i % con::YC_N == 0))
    fork_motor_controller.update(s);
    // End control

    // Begin post control data collection
    sampleMotorState(s);
    s.system_state |= systemstate::CollectionEnabled;
    // End post control data collection

    // Put the sample in to the buffer
    bool encode_failure = false;
    if (!sb.insert(s))
      encode_failure = true;

    // Clear the sample for the next iteration
    // The first time through the loop, computation_time will be logged as zero,
    // subsequent times will be accurate but delayed by one sample period
    uint32_t ti = s.system_time;
    memset(&s, 0, sizeof(s));
    s.computation_time = STM32_TIM5->CNT - ti;
    if (encode_failure)
      s.system_state |= systemstate::SampleBufferEncodeError;

    // Go to sleep until next interval
    chSysLock();
    sleep_time = time - chTimeNow();
    if (static_cast<int32_t>(sleep_time) > 0)
      chThdSleepS(sleep_time);
    chSysUnlock();
  } // for

  // Clean up
  disableSensorsMotors();
  msg_t write_errors = sb.deinitialize();
  // End cleanup

  chThdExit(write_errors);
}

// Caller: Shell thread
void SampleAndControl::shellcmd(BaseSequentialStream *chp, int argc, char *argv[])
{
  if (argc > 1 || argc < 0) {
    chprintf(chp, "Invalid usage.\r\n");
    return;
  }

  msg_t m;
  if (tp_control_) {
    m = Stop();
    if (argc == 0) {
      chprintf(chp, "Data collection and control terminated with %d errors.\r\n", m);
      return;
    }
  } else { // control thread not running
    if (argc == 0)
      m = Start("samples.dat");// Start data collection to default file "samples.dat"
    else
      m = Start(argv[0]);// Start data collection to file in argv[0]

    if (m == 0) {
      chprintf(chp, "Data collection and control initiated.\r\n");
      return;
    }
  }
  chprintf(chp, "Errors starting threads with error:  %d.\r\n", m);
}

msg_t SampleAndControl::Start(const char * filename)
{
  tp_control_ = chThdCreateStatic(SampleAndControl::waControlThread,
                                 sizeof(waControlThread),
                                 NORMALPRIO + 3,
                                 reinterpret_cast<tfunc_t>(controlThread_),
                                 const_cast<char *>(filename));
  if (!tp_control_)
    return 1;

  return 0;
}

msg_t SampleAndControl::Stop()
{
  chThdTerminate(tp_control_);
  msg_t m = chThdWait(tp_control_);
  tp_control_ = 0;
  return m;
}

<commit_msg>WIP: Update main loop to make use of new interfaces.<commit_after>#include <cstring>

#include "SampleAndControl.h"
#include "chprintf.h"

#include "MPU6050.h"
#include "rear_motor_controller.h"
#include "fork_motor_controller.h"
#include "SampleBuffer.h"
#include "SystemState.h"

SampleAndControl::SampleAndControl()
  : tp_control_(0), state_(0)
{
}

void SampleAndControl::controlThread(const char * filename)
{
  chRegSetThreadName("Control");
  enableSensorsMotors();
  MPU6050 & imu = MPU6050::Instance();
  hardware::RearMotorController rear_motor_controller;
  hardware::ForkMotorController fork_motor_controller;
  hardware::Encoder front_wheel_encoder(STM32_TIM4, 800);
  front_wheel_encoder.set_count(0);

  SampleBuffer & sb = SampleBuffer::Instance();
  sb.initialize(filename);

  // zero out system timer
  STM32_TIM5->CNT = 0;

  // Create a sample to populate
  Sample s;
  memset(&s, 0, sizeof(s));

  systime_t time = chTimeNow();     // Initial time
  systime_t sleep_time;
  for (uint32_t i = 0; !chThdShouldTerminate(); ++i) {
    time += MS2ST(con::T_ms);       // Next deadline

    // Begin pre control data collection
    s.loop_count = i;
    s.system_time = STM32_TIM5->CNT;
    s.encoder.front_wheel = front_wheel_encoder.get_angle();
    imu.Acquire(s);     // acquire gyro, accelerometer, and temperature data
    // End pre control data collection

    // Begin control
    rear_motor_controller.update(s);
    fork_motor_controller.update(s);
    // End control

    // Begin post control data collection
    // TODO: Push sampleMotorState functionality down into motor_controller
    // class or subclasses.
    // sampleMotorState(s);
    s.system_state |= systemstate::CollectionEnabled;
    // End post control data collection

    // Put the sample in to the buffer
    bool encode_failure = false;
    if (!sb.insert(s))
      encode_failure = true;

    // Clear the sample for the next iteration
    // The first time through the loop, computation_time will be logged as zero,
    // subsequent times will be accurate but delayed by one sample period
    uint32_t ti = s.system_time;
    memset(&s, 0, sizeof(s));
    s.computation_time = STM32_TIM5->CNT - ti;
    if (encode_failure)
      s.system_state |= systemstate::SampleBufferEncodeError;

    // Go to sleep until next interval
    chSysLock();
    sleep_time = time - chTimeNow();
    if (static_cast<int32_t>(sleep_time) > 0)
      chThdSleepS(sleep_time);
    chSysUnlock();
  } // for

  // Clean up
  // disableSensorsMotors();
  // TODO: change samplebuffer to be a non-singleton class and implement
  // cleanup in destructor
  msg_t write_errors = sb.deinitialize();
  // End cleanup

  chThdExit(write_errors);
}

// Caller: Shell thread
void SampleAndControl::shellcmd(BaseSequentialStream *chp, int argc, char *argv[])
{
  if (argc > 1 || argc < 0) {
    chprintf(chp, "Invalid usage.\r\n");
    return;
  }

  msg_t m;
  if (tp_control_) {
    m = Stop();
    if (argc == 0) {
      chprintf(chp, "Data collection and control terminated with %d errors.\r\n", m);
      return;
    }
  } else { // control thread not running
    if (argc == 0)
      m = Start("samples.dat");// Start data collection to default file "samples.dat"
    else
      m = Start(argv[0]);// Start data collection to file in argv[0]

    if (m == 0) {
      chprintf(chp, "Data collection and control initiated.\r\n");
      return;
    }
  }
  chprintf(chp, "Errors starting threads with error:  %d.\r\n", m);
}

msg_t SampleAndControl::Start(const char * filename)
{
  tp_control_ = chThdCreateStatic(SampleAndControl::waControlThread,
                                 sizeof(waControlThread),
                                 NORMALPRIO + 3,
                                 reinterpret_cast<tfunc_t>(controlThread_),
                                 const_cast<char *>(filename));
  if (!tp_control_)
    return 1;

  return 0;
}

msg_t SampleAndControl::Stop()
{
  chThdTerminate(tp_control_);
  msg_t m = chThdWait(tp_control_);
  tp_control_ = 0;
  return m;
}

<|endoftext|>


<commit_before>#pragma once

#include "blackhole/cpp17/string_view.hpp"

namespace blackhole {

using cpp17::string_view;

class record_t;

class sink_t {
    virtual ~sink_t() {}

    virtual auto filter(const record_t& record) -> bool = 0;

    virtual auto execute(const record_t& record, const string_view& formatted) -> void = 0;
};

}  // namespace blackhole
<commit_msg>fix(sink): make sink API public<commit_after>#pragma once

#include "blackhole/cpp17/string_view.hpp"

namespace blackhole {

using cpp17::string_view;

class record_t;

class sink_t {
public:
    virtual ~sink_t() {}

    virtual auto filter(const record_t& record) -> bool = 0;

    virtual auto execute(const record_t& record, const string_view& formatted) -> void = 0;
};

}  // namespace blackhole
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: Filter.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: hjs $ $Date: 2004-06-28 17:09:14 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef FORMS_COMPONENT_FILTER_HXX
#define FORMS_COMPONENT_FILTER_HXX

#ifndef _COM_SUN_STAR_LANG_XSERVICEINFO_HPP_
#include <com/sun/star/lang/XServiceInfo.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XINITIALIZATION_HPP_
#include <com/sun/star/lang/XInitialization.hpp>
#endif
#ifndef _COM_SUN_STAR_FORM_XBOUNDCOMPONENT_HPP_
#include <com/sun/star/form/XBoundComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTER_HPP_
#include <com/sun/star/util/XNumberFormatter.hpp>
#endif
#ifndef _COM_SUN_STAR_AWT_XTEXTCOMPONENT_HPP_
#include <com/sun/star/awt/XTextComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
#include <com/sun/star/sdb/SQLContext.hpp>
#endif

#ifndef _TOOLKIT_CONTROLS_UNOCONTROL_HXX_
#include <toolkit/controls/unocontrol.hxx>
#endif

#ifndef _TOOLKIT_AWT_LISTENERMULTIPLEXER_HXX_
#include <toolkit/helper/listenermultiplexer.hxx>
#endif

#ifndef _CPPUHELPER_IMPLBASE5_HXX_
#include <cppuhelper/implbase5.hxx>
#endif

#ifndef _COMPHELPER_UNO3_HXX_
#include <comphelper/uno3.hxx>
#endif
#ifndef _CPPUHELPER_IMPLBASE4_HXX_
#include <cppuhelper/implbase4.hxx>
#endif
#ifndef _CONNECTIVITY_SQLPARSE_HXX
#include <connectivity/sqlparse.hxx>
#endif

#ifndef SVX_QUERYDESIGNCONTEXT_HXX
#include <svx/ParseContext.hxx>
#endif

class Window;

//.........................................................................
namespace frm
{
//.........................................................................

    //=====================================================================
    // OFilterControl
    //=====================================================================
    typedef ::cppu::ImplHelper5 <   ::com::sun::star::awt::XTextComponent
                                ,   ::com::sun::star::awt::XFocusListener
                                ,   ::com::sun::star::awt::XItemListener
                                ,   ::com::sun::star::form::XBoundComponent
                                ,   ::com::sun::star::lang::XInitialization
                                >   OFilterControl_BASE;

    class OFilterControl    :public UnoControl
                            ,public OFilterControl_BASE
                            ,public ::svxform::OParseContextClient
    {
        TextListenerMultiplexer     m_aTextListeners;

        ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >        m_xORB;
        ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >               m_xField;
        ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >            m_xFormatter;
        ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >                 m_xConnection;
        ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >           m_xMetaData;
        ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >                      m_xMessageParent;

        ::rtl::OUString                 m_aText;
        ::connectivity::OSQLParser      m_aParser;
        sal_Int16                       m_nControlClass;        // which kind of control do we use?
        sal_Bool                        m_bFilterList : 1;
        sal_Bool                        m_bMultiLine : 1;
        sal_Bool                        m_bFilterListFilled : 1;

    private:
//      OFilterControl(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB);

        void implInitFilterList();

    public:
        OFilterControl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB );

        DECLARE_UNO3_AGG_DEFAULTS(OFilterControl,OWeakAggObject);
        ::com::sun::star::uno::Any  SAL_CALL queryAggregation( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);

        static const ::com::sun::star::uno::Sequence< sal_Int8 > & getUnoTunnelId();
        sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 > & rId );

        virtual ::rtl::OUString GetComponentServiceName();
        virtual void SAL_CALL   createPeer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit > & rxToolkit, const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer >  & rParentPeer ) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::lang::XComponent
        virtual void SAL_CALL   dispose(void) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::awt::XTextComponent
        virtual void            SAL_CALL addTextListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextListener > & l ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL removeTextListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextListener > & l ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setText( const ::rtl::OUString& aText ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL insertText( const ::com::sun::star::awt::Selection& rSel, const ::rtl::OUString& aText ) throw( ::com::sun::star::uno::RuntimeException );
        virtual ::rtl::OUString SAL_CALL getText() throw( ::com::sun::star::uno::RuntimeException );
        virtual ::rtl::OUString SAL_CALL getSelectedText() throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setSelection( const ::com::sun::star::awt::Selection& aSelection ) throw( ::com::sun::star::uno::RuntimeException );
        virtual ::com::sun::star::awt::Selection SAL_CALL getSelection() throw( ::com::sun::star::uno::RuntimeException );
        virtual sal_Bool        SAL_CALL isEditable() throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setEditable( sal_Bool bEditable ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setMaxTextLen( sal_Int16 nLength ) throw( ::com::sun::star::uno::RuntimeException );
        virtual sal_Int16       SAL_CALL getMaxTextLen() throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::form::XBoundComponent
        virtual void            SAL_CALL addUpdateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XUpdateListener > & l) throw( ::com::sun::star::uno::RuntimeException ) {}
        virtual void            SAL_CALL removeUpdateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XUpdateListener > & l) throw( ::com::sun::star::uno::RuntimeException ) {}
        virtual sal_Bool        SAL_CALL commit() throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::lang::XEventListener
        virtual void            SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::awt::XFocusListener
        virtual void            SAL_CALL focusGained(const ::com::sun::star::awt::FocusEvent& e) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL focusLost(const ::com::sun::star::awt::FocusEvent& e) throw( ::com::sun::star::uno::RuntimeException ){}

    // ::com::sun::star::awt::XItemListener
        virtual void            SAL_CALL itemStateChanged(const ::com::sun::star::awt::ItemEvent& rEvent) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::util::XInitialization
        virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);

        // XServiceInfo
        virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
        virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
        virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);

        // com::sun::star::lang::XServiceInfo - static version
        static  ::rtl::OUString SAL_CALL getImplementationName_Static();
        static  ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_Static();
        static  ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL Create( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory );

    protected:
        virtual void PrepareWindowDescriptor( ::com::sun::star::awt::WindowDescriptor& rDesc );
        virtual void ImplSetPeerProperty( const ::rtl::OUString& rPropName, const ::com::sun::star::uno::Any& rVal );

        sal_Bool ensureInitialized( );

        void displayException( const ::com::sun::star::sdb::SQLContext& _rExcept );
    };
//.........................................................................
}   // namespace frm
//.........................................................................

#endif // FORMS_COMPONENT_FILTER_HXX
<commit_msg>INTEGRATION: CWS ooo19126 (1.4.130); FILE MERGED 2005/09/05 13:50:18 rt 1.4.130.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: Filter.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-08 22:39:35 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef FORMS_COMPONENT_FILTER_HXX
#define FORMS_COMPONENT_FILTER_HXX

#ifndef _COM_SUN_STAR_LANG_XSERVICEINFO_HPP_
#include <com/sun/star/lang/XServiceInfo.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XINITIALIZATION_HPP_
#include <com/sun/star/lang/XInitialization.hpp>
#endif
#ifndef _COM_SUN_STAR_FORM_XBOUNDCOMPONENT_HPP_
#include <com/sun/star/form/XBoundComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_XNUMBERFORMATTER_HPP_
#include <com/sun/star/util/XNumberFormatter.hpp>
#endif
#ifndef _COM_SUN_STAR_AWT_XTEXTCOMPONENT_HPP_
#include <com/sun/star/awt/XTextComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
#include <com/sun/star/sdb/SQLContext.hpp>
#endif

#ifndef _TOOLKIT_CONTROLS_UNOCONTROL_HXX_
#include <toolkit/controls/unocontrol.hxx>
#endif

#ifndef _TOOLKIT_AWT_LISTENERMULTIPLEXER_HXX_
#include <toolkit/helper/listenermultiplexer.hxx>
#endif

#ifndef _CPPUHELPER_IMPLBASE5_HXX_
#include <cppuhelper/implbase5.hxx>
#endif

#ifndef _COMPHELPER_UNO3_HXX_
#include <comphelper/uno3.hxx>
#endif
#ifndef _CPPUHELPER_IMPLBASE4_HXX_
#include <cppuhelper/implbase4.hxx>
#endif
#ifndef _CONNECTIVITY_SQLPARSE_HXX
#include <connectivity/sqlparse.hxx>
#endif

#ifndef SVX_QUERYDESIGNCONTEXT_HXX
#include <svx/ParseContext.hxx>
#endif

class Window;

//.........................................................................
namespace frm
{
//.........................................................................

    //=====================================================================
    // OFilterControl
    //=====================================================================
    typedef ::cppu::ImplHelper5 <   ::com::sun::star::awt::XTextComponent
                                ,   ::com::sun::star::awt::XFocusListener
                                ,   ::com::sun::star::awt::XItemListener
                                ,   ::com::sun::star::form::XBoundComponent
                                ,   ::com::sun::star::lang::XInitialization
                                >   OFilterControl_BASE;

    class OFilterControl    :public UnoControl
                            ,public OFilterControl_BASE
                            ,public ::svxform::OParseContextClient
    {
        TextListenerMultiplexer     m_aTextListeners;

        ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >        m_xORB;
        ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >               m_xField;
        ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >            m_xFormatter;
        ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >                 m_xConnection;
        ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >           m_xMetaData;
        ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow >                      m_xMessageParent;

        ::rtl::OUString                 m_aText;
        ::connectivity::OSQLParser      m_aParser;
        sal_Int16                       m_nControlClass;        // which kind of control do we use?
        sal_Bool                        m_bFilterList : 1;
        sal_Bool                        m_bMultiLine : 1;
        sal_Bool                        m_bFilterListFilled : 1;

    private:
//      OFilterControl(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB);

        void implInitFilterList();

    public:
        OFilterControl( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB );

        DECLARE_UNO3_AGG_DEFAULTS(OFilterControl,OWeakAggObject);
        ::com::sun::star::uno::Any  SAL_CALL queryAggregation( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);

        static const ::com::sun::star::uno::Sequence< sal_Int8 > & getUnoTunnelId();
        sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 > & rId );

        virtual ::rtl::OUString GetComponentServiceName();
        virtual void SAL_CALL   createPeer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit > & rxToolkit, const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer >  & rParentPeer ) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::lang::XComponent
        virtual void SAL_CALL   dispose(void) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::awt::XTextComponent
        virtual void            SAL_CALL addTextListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextListener > & l ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL removeTextListener( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextListener > & l ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setText( const ::rtl::OUString& aText ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL insertText( const ::com::sun::star::awt::Selection& rSel, const ::rtl::OUString& aText ) throw( ::com::sun::star::uno::RuntimeException );
        virtual ::rtl::OUString SAL_CALL getText() throw( ::com::sun::star::uno::RuntimeException );
        virtual ::rtl::OUString SAL_CALL getSelectedText() throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setSelection( const ::com::sun::star::awt::Selection& aSelection ) throw( ::com::sun::star::uno::RuntimeException );
        virtual ::com::sun::star::awt::Selection SAL_CALL getSelection() throw( ::com::sun::star::uno::RuntimeException );
        virtual sal_Bool        SAL_CALL isEditable() throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setEditable( sal_Bool bEditable ) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL setMaxTextLen( sal_Int16 nLength ) throw( ::com::sun::star::uno::RuntimeException );
        virtual sal_Int16       SAL_CALL getMaxTextLen() throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::form::XBoundComponent
        virtual void            SAL_CALL addUpdateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XUpdateListener > & l) throw( ::com::sun::star::uno::RuntimeException ) {}
        virtual void            SAL_CALL removeUpdateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::form::XUpdateListener > & l) throw( ::com::sun::star::uno::RuntimeException ) {}
        virtual sal_Bool        SAL_CALL commit() throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::lang::XEventListener
        virtual void            SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::awt::XFocusListener
        virtual void            SAL_CALL focusGained(const ::com::sun::star::awt::FocusEvent& e) throw( ::com::sun::star::uno::RuntimeException );
        virtual void            SAL_CALL focusLost(const ::com::sun::star::awt::FocusEvent& e) throw( ::com::sun::star::uno::RuntimeException ){}

    // ::com::sun::star::awt::XItemListener
        virtual void            SAL_CALL itemStateChanged(const ::com::sun::star::awt::ItemEvent& rEvent) throw( ::com::sun::star::uno::RuntimeException );

    // ::com::sun::star::util::XInitialization
        virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);

        // XServiceInfo
        virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
        virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
        virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);

        // com::sun::star::lang::XServiceInfo - static version
        static  ::rtl::OUString SAL_CALL getImplementationName_Static();
        static  ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_Static();
        static  ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL Create( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory );

    protected:
        virtual void PrepareWindowDescriptor( ::com::sun::star::awt::WindowDescriptor& rDesc );
        virtual void ImplSetPeerProperty( const ::rtl::OUString& rPropName, const ::com::sun::star::uno::Any& rVal );

        sal_Bool ensureInitialized( );

        void displayException( const ::com::sun::star::sdb::SQLContext& _rExcept );
    };
//.........................................................................
}   // namespace frm
//.........................................................................

#endif // FORMS_COMPONENT_FILTER_HXX
<|endoftext|>


<commit_before>// Copyright 2017 Global Phasing Ltd.
//
// Structure -> cif::Document -> mmcif (PDBx/mmCIF) file

#ifndef GEMMI_TO_MMCIF_HPP_
#define GEMMI_TO_MMCIF_HPP_

#include <string>
#include <utility>  // std::pair
#include "sprintf.hpp"
#include "cifdoc.hpp"
#include "model.hpp"

namespace gemmi {

namespace impl {

inline void add_cif_atoms(const Structure& st, cif::Block& block) {
  // atom list
  cif::Loop& atom_loop = block.clear_or_add_loop("_atom_site.", {
      "id",
      "type_symbol",
      "label_atom_id",
      "label_alt_id",
      "label_comp_id",
      "label_asym_id",
      "label_seq_id",
      "pdbx_PDB_ins_code",
      "Cartn_x",
      "Cartn_y",
      "Cartn_z",
      "occupancy",
      "B_iso_or_equiv",
      "pdbx_formal_charge",
      "auth_seq_id",
      "auth_asym_id",
      "pdbx_PDB_model_num"});
  std::vector<std::string>& vv = atom_loop.values;
  vv.reserve(count_atom_sites(st) * atom_loop.tags.size());
  std::vector<std::pair<int, const Atom*>> aniso;
  int serial = 0;
  for (const Model& model : st.models) {
    for (const Chain& chain : model.chains) {
      for (const Residue& res : chain.residues) {
        std::string seq_id = std::to_string(res.seq_id);
        std::string auth_seq_id = std::to_string(res.auth_seq_id);
        std::string ins_code(1, res.ins_code ? res.ins_code : '?');
        for (const Atom& a : res.atoms) {
          vv.emplace_back(std::to_string(++serial));
          vv.emplace_back(a.element.uname());
          vv.emplace_back(a.name);
          vv.emplace_back(1, a.altloc ? a.altloc : '.');
          vv.emplace_back(res.name);
          vv.emplace_back(chain.name);
          vv.emplace_back(seq_id);
          vv.emplace_back(ins_code);
          vv.emplace_back(to_str(a.pos.x));
          vv.emplace_back(to_str(a.pos.y));
          vv.emplace_back(to_str(a.pos.z));
          vv.emplace_back(to_str(a.occ));
          vv.emplace_back(to_str(a.b_iso));
          vv.emplace_back(a.charge == 0 ? "?" : std::to_string(a.charge));
          vv.emplace_back(auth_seq_id);
          vv.emplace_back(chain.auth_name);
          vv.emplace_back(model.name);
          if (a.u11 != 0.f)
            aniso.emplace_back(serial, &a);
        }
      }
    }
  }
  if (aniso.empty()) {
    block.delete_loop("_atom_site_anisotrop.id");
  } else {
    cif::Loop& aniso_loop = block.clear_or_add_loop("_atom_site_anisotrop.", {
                                    "id", "U[1][1]", "U[2][2]", "U[3][3]",
                                    "U[1][2]", "U[1][3]", "U[2][3]"});
    std::vector<std::string>& aniso_val = aniso_loop.values;
    aniso_val.reserve(aniso_loop.tags.size() * aniso.size());
    for (const auto& a : aniso) {
      aniso_val.emplace_back(std::to_string(a.first));
      aniso_val.emplace_back(to_str(a.second->u11));
      aniso_val.emplace_back(to_str(a.second->u22));
      aniso_val.emplace_back(to_str(a.second->u33));
      aniso_val.emplace_back(to_str(a.second->u12));
      aniso_val.emplace_back(to_str(a.second->u13));
      aniso_val.emplace_back(to_str(a.second->u23));
    }
  }
}

} // namespace impl

inline void update_cif_block(const Structure& st, cif::Block& block) {
  if (st.models.empty())
    return;
  block.name = st.name;
  auto e_id = st.info.find("_entry.id");
  std::string id = cif::quote(e_id != st.info.end() ? e_id->second : st.name);
  block.update_value("_entry.id", id);
  auto initial_date =
         st.info.find("_pdbx_database_status.recvd_initial_deposition_date");
  if (initial_date != st.info.end()) {
    block.update_value("_pdbx_database_status.entry_id", id);
    block.update_value(initial_date->first, initial_date->second);
  }

  // unit cell and symmetry
  block.update_value("_cell.entry_id", id);
  block.update_value("_cell.length_a",    to_str(st.cell.a));
  block.update_value("_cell.length_b",    to_str(st.cell.b));
  block.update_value("_cell.length_c",    to_str(st.cell.c));
  block.update_value("_cell.angle_alpha", to_str(st.cell.alpha));
  block.update_value("_cell.angle_beta",  to_str(st.cell.beta));
  block.update_value("_cell.angle_gamma", to_str(st.cell.gamma));
  auto z_pdb = st.info.find("_cell.Z_PDB");
  if (z_pdb != st.info.end())
    block.update_value(z_pdb->first, z_pdb->second);
  block.update_value("_symmetry.entry_id", id);
  block.update_value("_symmetry.space_group_name_H-M", cif::quote(st.sg_hm));

  // _entity
  cif::Loop& entity_loop = block.clear_or_add_loop("_entity.", {"id", "type"});
  for (const auto& ent : st.entities)
    entity_loop.append_row({ent->id, ent->type_as_string()});

  // _entity_poly
  cif::Loop& entity_poly_loop = block.clear_or_add_loop("_entity_poly.",
                                                        {"entity_id", "type"});
  for (const auto& ent : st.entities)
    if (ent->type == EntityType::Polymer)
      entity_poly_loop.append_row({ent->id, ent->polymer_type_as_string()});

  // _exptl
  cif::Loop& exptl_method_loop = block.clear_or_add_loop("_exptl.",
                                                        {"entry_id", "method"});
  auto exptl_method = st.info.find("_exptl.method");
  if (exptl_method != st.info.end())
    for (const std::string& m : gemmi::split_str(exptl_method->second, "; "))
      exptl_method_loop.append_row({id, cif::quote(m)});

  // title, keywords
  auto title = st.info.find("_struct.title");
  if (title != st.info.end()) {
    block.update_value("_struct.entry_id", id);
    block.update_value(title->first, cif::quote(title->second));
  }
  auto pdbx_keywords = st.info.find("_struct_keywords.pdbx_keywords");
  auto keywords = st.info.find("_struct_keywords.text");
  if (pdbx_keywords != st.info.end() || keywords != st.info.end())
    block.update_value("_struct_keywords.entry_id", id);
  if (pdbx_keywords != st.info.end())
    block.update_value(pdbx_keywords->first, cif::quote(pdbx_keywords->second));
  if (keywords != st.info.end())
    block.update_value(keywords->first, cif::quote(keywords->second));

  // _struct_ncs_oper (MTRIX)
  if (!st.ncs.empty()) {
    cif::Loop& ncs_oper = block.clear_or_add_loop("_struct_ncs_oper.",
        {"id", "code",
         "matrix[1][1]", "matrix[1][2]", "matrix[1][3]", "vector[1]",
         "matrix[2][1]", "matrix[2][2]", "matrix[2][3]", "vector[2]",
         "matrix[3][1]", "matrix[3][2]", "matrix[3][3]", "vector[3]"});
    for (const NcsOp& op : st.ncs) {
      ncs_oper.values.emplace_back(op.id);
      ncs_oper.values.emplace_back(op.given ? "given" : "generate");
      for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 4; ++j)
          ncs_oper.values.emplace_back(to_str(op.transform[j][i]));
    }
  }

  // _struct_asym
  cif::Loop& asym_loop = block.clear_or_add_loop("_struct_asym.",
                                                 {"id", "entity_id"});
  for (const auto& ch : st.get_chains())
    asym_loop.append_row({ch.name, (ch.entity ? ch.entity->id : "?")});

  // _database_PDB_matrix (ORIGX)
  if (st.origx != Mat4x4(linalg::identity)) {
    block.update_value("_database_PDB_matrix.entry_id", id);
    std::string prefix = "_database_PDB_matrix.origx";
    for (int i = 0; i < 3; ++i) {
      std::string s = "[" + std::to_string(i+1) + "]";
      block.update_value(prefix + s + "[1]", to_str(st.origx.x[i]));
      block.update_value(prefix + s + "[2]", to_str(st.origx.y[i]));
      block.update_value(prefix + s + "[3]", to_str(st.origx.z[i]));
      block.update_value(prefix + "_vector" + s, to_str(st.origx.w[i]));
    }
  }

  // _atom_sites (SCALE)
  if (st.cell.explicit_matrices) {
    block.update_value("_atom_sites.entry_id", id);
    std::string prefix = "_atom_sites.fract_transf_";
    block.update_value(prefix + "matrix[1][1]", to_str(st.cell.frac.a11));
    block.update_value(prefix + "matrix[1][2]", to_str(st.cell.frac.a12));
    block.update_value(prefix + "matrix[1][3]", to_str(st.cell.frac.a13));
    block.update_value(prefix + "matrix[2][1]", to_str(st.cell.frac.a21));
    block.update_value(prefix + "matrix[2][2]", to_str(st.cell.frac.a22));
    block.update_value(prefix + "matrix[2][3]", to_str(st.cell.frac.a23));
    block.update_value(prefix + "matrix[3][1]", to_str(st.cell.frac.a31));
    block.update_value(prefix + "matrix[3][2]", to_str(st.cell.frac.a32));
    block.update_value(prefix + "matrix[3][3]", to_str(st.cell.frac.a33));
    block.update_value(prefix + "vector[1]",    to_str(st.cell.shift.x));
    block.update_value(prefix + "vector[2]",    to_str(st.cell.shift.y));
    block.update_value(prefix + "vector[3]",    to_str(st.cell.shift.z));
  }

  // SEQRES from PDB doesn't record microheterogeneity, so if the resulting
  // cif has unknown("?") _entity_poly_seq.num, it cannot be trusted.
  cif::Loop& poly_loop = block.clear_or_add_loop("_entity_poly_seq.", {
                                      "entity_id", "num", "mon_id"});
  for (const auto& ent : st.entities)
    if (ent->type == EntityType::Polymer)
      for (const SequenceItem& si : ent->sequence) {
        poly_loop.append_row({ent->id,
                              (si.num >= 0 ? std::to_string(si.num) : "?"),
                              si.mon});
      }

  impl::add_cif_atoms(st, block);
}

} // namespace gemmi
#endif
// vim:sw=2:ts=2:et
<commit_msg>to_mmcif: write _struct_mon_prot_cis<commit_after>// Copyright 2017 Global Phasing Ltd.
//
// Structure -> cif::Document -> mmcif (PDBx/mmCIF) file

#ifndef GEMMI_TO_MMCIF_HPP_
#define GEMMI_TO_MMCIF_HPP_

#include <string>
#include <utility>  // std::pair
#include "sprintf.hpp"
#include "cifdoc.hpp"
#include "model.hpp"

namespace gemmi {

namespace impl {

inline void add_cif_atoms(const Structure& st, cif::Block& block) {
  // atom list
  cif::Loop& atom_loop = block.clear_or_add_loop("_atom_site.", {
      "id",
      "type_symbol",
      "label_atom_id",
      "label_alt_id",
      "label_comp_id",
      "label_asym_id",
      "label_seq_id",
      "pdbx_PDB_ins_code",
      "Cartn_x",
      "Cartn_y",
      "Cartn_z",
      "occupancy",
      "B_iso_or_equiv",
      "pdbx_formal_charge",
      "auth_seq_id",
      "auth_asym_id",
      "pdbx_PDB_model_num"});
  std::vector<std::string>& vv = atom_loop.values;
  vv.reserve(count_atom_sites(st) * atom_loop.tags.size());
  std::vector<std::pair<int, const Atom*>> aniso;
  int serial = 0;
  for (const Model& model : st.models) {
    for (const Chain& chain : model.chains) {
      for (const Residue& res : chain.residues) {
        std::string seq_id = std::to_string(res.seq_id);
        std::string auth_seq_id = std::to_string(res.auth_seq_id);
        std::string ins_code(1, res.ins_code ? res.ins_code : '?');
        for (const Atom& a : res.atoms) {
          vv.emplace_back(std::to_string(++serial));
          vv.emplace_back(a.element.uname());
          vv.emplace_back(a.name);
          vv.emplace_back(1, a.altloc ? a.altloc : '.');
          vv.emplace_back(res.name);
          vv.emplace_back(chain.name);
          vv.emplace_back(seq_id);
          vv.emplace_back(ins_code);
          vv.emplace_back(to_str(a.pos.x));
          vv.emplace_back(to_str(a.pos.y));
          vv.emplace_back(to_str(a.pos.z));
          vv.emplace_back(to_str(a.occ));
          vv.emplace_back(to_str(a.b_iso));
          vv.emplace_back(a.charge == 0 ? "?" : std::to_string(a.charge));
          vv.emplace_back(auth_seq_id);
          vv.emplace_back(chain.auth_name);
          vv.emplace_back(model.name);
          if (a.u11 != 0.f)
            aniso.emplace_back(serial, &a);
        }
      }
    }
  }
  if (aniso.empty()) {
    block.delete_loop("_atom_site_anisotrop.id");
  } else {
    cif::Loop& aniso_loop = block.clear_or_add_loop("_atom_site_anisotrop.", {
                                    "id", "U[1][1]", "U[2][2]", "U[3][3]",
                                    "U[1][2]", "U[1][3]", "U[2][3]"});
    std::vector<std::string>& aniso_val = aniso_loop.values;
    aniso_val.reserve(aniso_loop.tags.size() * aniso.size());
    for (const auto& a : aniso) {
      aniso_val.emplace_back(std::to_string(a.first));
      aniso_val.emplace_back(to_str(a.second->u11));
      aniso_val.emplace_back(to_str(a.second->u22));
      aniso_val.emplace_back(to_str(a.second->u33));
      aniso_val.emplace_back(to_str(a.second->u12));
      aniso_val.emplace_back(to_str(a.second->u13));
      aniso_val.emplace_back(to_str(a.second->u23));
    }
  }
}

} // namespace impl

inline void update_cif_block(const Structure& st, cif::Block& block) {
  using std::to_string;
  if (st.models.empty())
    return;
  block.name = st.name;
  auto e_id = st.info.find("_entry.id");
  std::string id = cif::quote(e_id != st.info.end() ? e_id->second : st.name);
  block.update_value("_entry.id", id);
  auto initial_date =
         st.info.find("_pdbx_database_status.recvd_initial_deposition_date");
  if (initial_date != st.info.end()) {
    block.update_value("_pdbx_database_status.entry_id", id);
    block.update_value(initial_date->first, initial_date->second);
  }

  // unit cell and symmetry
  block.update_value("_cell.entry_id", id);
  block.update_value("_cell.length_a",    to_str(st.cell.a));
  block.update_value("_cell.length_b",    to_str(st.cell.b));
  block.update_value("_cell.length_c",    to_str(st.cell.c));
  block.update_value("_cell.angle_alpha", to_str(st.cell.alpha));
  block.update_value("_cell.angle_beta",  to_str(st.cell.beta));
  block.update_value("_cell.angle_gamma", to_str(st.cell.gamma));
  auto z_pdb = st.info.find("_cell.Z_PDB");
  if (z_pdb != st.info.end())
    block.update_value(z_pdb->first, z_pdb->second);
  block.update_value("_symmetry.entry_id", id);
  block.update_value("_symmetry.space_group_name_H-M", cif::quote(st.sg_hm));

  // _entity
  cif::Loop& entity_loop = block.clear_or_add_loop("_entity.", {"id", "type"});
  for (const auto& ent : st.entities)
    entity_loop.append_row({ent->id, ent->type_as_string()});

  // _entity_poly
  cif::Loop& entity_poly_loop = block.clear_or_add_loop("_entity_poly.",
                                                        {"entity_id", "type"});
  for (const auto& ent : st.entities)
    if (ent->type == EntityType::Polymer)
      entity_poly_loop.append_row({ent->id, ent->polymer_type_as_string()});

  // _exptl
  cif::Loop& exptl_method_loop = block.clear_or_add_loop("_exptl.",
                                                        {"entry_id", "method"});
  auto exptl_method = st.info.find("_exptl.method");
  if (exptl_method != st.info.end())
    for (const std::string& m : gemmi::split_str(exptl_method->second, "; "))
      exptl_method_loop.append_row({id, cif::quote(m)});

  // title, keywords
  auto title = st.info.find("_struct.title");
  if (title != st.info.end()) {
    block.update_value("_struct.entry_id", id);
    block.update_value(title->first, cif::quote(title->second));
  }
  auto pdbx_keywords = st.info.find("_struct_keywords.pdbx_keywords");
  auto keywords = st.info.find("_struct_keywords.text");
  if (pdbx_keywords != st.info.end() || keywords != st.info.end())
    block.update_value("_struct_keywords.entry_id", id);
  if (pdbx_keywords != st.info.end())
    block.update_value(pdbx_keywords->first, cif::quote(pdbx_keywords->second));
  if (keywords != st.info.end())
    block.update_value(keywords->first, cif::quote(keywords->second));

  // _struct_ncs_oper (MTRIX)
  if (!st.ncs.empty()) {
    cif::Loop& ncs_oper = block.clear_or_add_loop("_struct_ncs_oper.",
        {"id", "code",
         "matrix[1][1]", "matrix[1][2]", "matrix[1][3]", "vector[1]",
         "matrix[2][1]", "matrix[2][2]", "matrix[2][3]", "vector[2]",
         "matrix[3][1]", "matrix[3][2]", "matrix[3][3]", "vector[3]"});
    for (const NcsOp& op : st.ncs) {
      ncs_oper.values.emplace_back(op.id);
      ncs_oper.values.emplace_back(op.given ? "given" : "generate");
      for (int i = 0; i < 3; ++i)
        for (int j = 0; j < 4; ++j)
          ncs_oper.values.emplace_back(to_str(op.transform[j][i]));
    }
  }

  // _struct_asym
  cif::Loop& asym_loop = block.clear_or_add_loop("_struct_asym.",
                                                 {"id", "entity_id"});
  for (const auto& ch : st.get_chains())
    asym_loop.append_row({ch.name, (ch.entity ? ch.entity->id : "?")});

  // _database_PDB_matrix (ORIGX)
  if (st.origx != Mat4x4(linalg::identity)) {
    block.update_value("_database_PDB_matrix.entry_id", id);
    std::string prefix = "_database_PDB_matrix.origx";
    for (int i = 0; i < 3; ++i) {
      std::string s = "[" + to_string(i+1) + "]";
      block.update_value(prefix + s + "[1]", to_str(st.origx.x[i]));
      block.update_value(prefix + s + "[2]", to_str(st.origx.y[i]));
      block.update_value(prefix + s + "[3]", to_str(st.origx.z[i]));
      block.update_value(prefix + "_vector" + s, to_str(st.origx.w[i]));
    }
  }

  // _struct_mon_prot_cis
  cif::Loop& prot_cis_loop = block.clear_or_add_loop("_struct_mon_prot_cis.",
                             {"pdbx_id", "pdbx_PDB_model_num", "label_asym_id",
                              "label_seq_id", "label_comp_id"});
  for (const Model& model : st.models)
    for (const Chain& chain : model.chains)
        for (const Residue& res : chain.residues)
          if (res.is_cis)
            prot_cis_loop.append_row({to_string(prot_cis_loop.length()+1),
                                      model.name, chain.name,
                                      to_string(res.seq_id), res.name});

  // _atom_sites (SCALE)
  if (st.cell.explicit_matrices) {
    block.update_value("_atom_sites.entry_id", id);
    std::string prefix = "_atom_sites.fract_transf_";
    block.update_value(prefix + "matrix[1][1]", to_str(st.cell.frac.a11));
    block.update_value(prefix + "matrix[1][2]", to_str(st.cell.frac.a12));
    block.update_value(prefix + "matrix[1][3]", to_str(st.cell.frac.a13));
    block.update_value(prefix + "matrix[2][1]", to_str(st.cell.frac.a21));
    block.update_value(prefix + "matrix[2][2]", to_str(st.cell.frac.a22));
    block.update_value(prefix + "matrix[2][3]", to_str(st.cell.frac.a23));
    block.update_value(prefix + "matrix[3][1]", to_str(st.cell.frac.a31));
    block.update_value(prefix + "matrix[3][2]", to_str(st.cell.frac.a32));
    block.update_value(prefix + "matrix[3][3]", to_str(st.cell.frac.a33));
    block.update_value(prefix + "vector[1]",    to_str(st.cell.shift.x));
    block.update_value(prefix + "vector[2]",    to_str(st.cell.shift.y));
    block.update_value(prefix + "vector[3]",    to_str(st.cell.shift.z));
  }

  // SEQRES from PDB doesn't record microheterogeneity, so if the resulting
  // cif has unknown("?") _entity_poly_seq.num, it cannot be trusted.
  cif::Loop& poly_loop = block.clear_or_add_loop("_entity_poly_seq.", {
                                      "entity_id", "num", "mon_id"});
  for (const auto& ent : st.entities)
    if (ent->type == EntityType::Polymer)
      for (const SequenceItem& si : ent->sequence) {
        poly_loop.append_row({ent->id,
                              (si.num >= 0 ? to_string(si.num) : "?"),
                              si.mon});
      }

  impl::add_cif_atoms(st, block);
}

} // namespace gemmi
#endif
// vim:sw=2:ts=2:et
<|endoftext|>


<commit_before>/* BSD 2-Clause License

Copyright (c) 2016, Doi Yusuke
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LIBICS3_ICS3_PARAMETER_H
#define LIBICS3_ICS3_PARAMETER_H

#include "ics3/check_invalid.hpp"

namespace ics
{
class Parameter
{
public:
  using type = uint8_t;
  static constexpr Parameter stretch(type = 30);
  static constexpr Parameter speed(type = 127);
  static constexpr Parameter current(type = 63);
  static constexpr Parameter temperature(type = 80);
  static constexpr Parameter newParameter(const Parameter&, type);

  constexpr type get() const noexcept;
  constexpr operator type() const noexcept;
  void set(type);
  Parameter& operator=(type);
  constexpr type getSubcommand() const noexcept;
private:
  constexpr Parameter(type, type, type, type); // non explicit, user cannot touch this

  const type sc;
  const type min;
  const type max;
  type data;
};

constexpr Parameter Parameter::stretch(type data)
{
  return {0x01, 1, 127, data};
}

constexpr Parameter Parameter::speed(type data)
{
  return {0x02, 1, 127, data};
}

constexpr Parameter Parameter::current(type data)
{
  return {0x03, 0, 63, data};
}

constexpr Parameter Parameter::temperature(type data)
{
  return {0x04, 1, 127, data};
}

constexpr Parameter Parameter::newParameter(const Parameter& base, type data)
{
  return {base.sc, base.min, base.max, data};
}

constexpr Parameter::type Parameter::get() const noexcept
{
  return data;
}

constexpr Parameter::operator type() const noexcept
{
  return get();
}

inline void Parameter::set(type input)
{
  data = checkValidRange(input, min, max);
}

inline Parameter& Parameter::operator=(type rhs)
{
  set(rhs);
  return *this;
}

constexpr Parameter::type Parameter::getSubcommand() const noexcept
{
  return sc;
}

constexpr Parameter::Parameter(type sc, type min, type max, type defaultData)
: sc {sc},
  min {min},
  max {max},
  data {checkValidRange(defaultData, min, max)}
{
}
}

#endif // LIBICS3_ICS3_PARAMETER_H
<commit_msg>Reformat parameter.hpp<commit_after>/* BSD 2-Clause License

Copyright (c) 2016, Doi Yusuke
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LIBICS3_ICS3_PARAMETER_H
#define LIBICS3_ICS3_PARAMETER_H

#include "ics3/check_invalid.hpp"

namespace ics
{
class Parameter
{
public:
  using type = uint8_t;

  static constexpr Parameter stretch(type = 30);
  static constexpr Parameter speed(type = 127);
  static constexpr Parameter current(type = 63);
  static constexpr Parameter temperature(type = 80);
  static constexpr Parameter newParameter(const Parameter&, type);

  constexpr type get() const noexcept;
  constexpr operator type() const noexcept;
  void set(type);
  Parameter& operator=(type);
  constexpr type getSubcommand() const noexcept;
private:
  constexpr Parameter(type, type, type, type); // non explicit, user cannot touch this

  const type sc;
  const type min;
  const type max;
  type data;
};

constexpr Parameter Parameter::stretch(type data)
{
  return {0x01, 1, 127, data};
}

constexpr Parameter Parameter::speed(type data)
{
  return {0x02, 1, 127, data};
}

constexpr Parameter Parameter::current(type data)
{
  return {0x03, 0, 63, data};
}

constexpr Parameter Parameter::temperature(type data)
{
  return {0x04, 1, 127, data};
}

constexpr Parameter Parameter::newParameter(const Parameter& base, type data)
{
  return {base.sc, base.min, base.max, data};
}

constexpr Parameter::type Parameter::get() const noexcept
{
  return data;
}

constexpr Parameter::operator type() const noexcept
{
  return get();
}

inline void Parameter::set(type input)
{
  data = checkValidRange(input, min, max);
}

inline Parameter& Parameter::operator=(type rhs)
{
  set(rhs);
  return *this;
}

constexpr Parameter::type Parameter::getSubcommand() const noexcept
{
  return sc;
}

constexpr Parameter::Parameter(type sc, type min, type max, type defaultData)
  : sc {sc},
    min {min},
    max {max},
    data {checkValidRange(defaultData, min, max)}
{
}
}

#endif // LIBICS3_ICS3_PARAMETER_H
<|endoftext|>


<commit_before>#ifndef IMQUE_ALLOCATOR_HH
#define IMQUE_ALLOCATOR_HH

#include <inttypes.h>
#include <cassert>

namespace imque {
  class Allocator {
    struct Node {  // node of free-list
      uint32_t next;     // next free block index
      uint32_t count:29; // available block count
      uint32_t status:3;

      enum STATUS {
        FREE      = 0,
        USED      = 1,
        JOIN_HEAD = 2,
        JOIN_TAIL = 4
      };

      uint64_t* as_uint64_ptr() { return reinterpret_cast<uint64_t*>(this); }
      uint64_t as_uint64() const { return *reinterpret_cast<const uint64_t*>(this); }
    } __attribute__((__packed__));

    struct Chunk {
      char padding[32];
    };

    class Snapshot {
    public:
      Snapshot() : ptr_(NULL) {}
      Snapshot(Node* ptr) : ptr_(ptr), val_(*ptr) {}

      void update(Node* ptr) {
        ptr_ = ptr;
        val_ = *ptr;
      }

      const Node& node() const { return val_; }
      bool isModified() const { return ptr_->as_uint64() != val_.as_uint64(); }

      bool compare_and_swap(const Node& new_val) {
        if(isModified() == false &&
           __sync_bool_compare_and_swap(ptr_->as_uint64_ptr(), val_.as_uint64(), new_val.as_uint64())) {
          val_ = new_val;
          return true;
        }
        return false;
      }

      uint32_t index(Node* head) const { return ptr_ - head; }

    private:
      Node* ptr_;
      Node  val_;
    };

    static const uint32_t MAX_RETRY_COUNT = 256;

  public:
    Allocator(void* region, uint32_t size)
      : node_count_(size/(sizeof(Node)+sizeof(Chunk))),
        nodes_(reinterpret_cast<Node*>(region)),
        chunks_(reinterpret_cast<Chunk*>(nodes_+node_count_)) {
    }

    operator bool() const { return node_count_ > 2; }

    void init() {
      nodes_[0].next   = 1;
      nodes_[0].count  = 0;
      nodes_[0].status = Node::FREE;

      nodes_[1].next   = node_count_;
      nodes_[1].count  = node_count_-1;
      nodes_[1].status = Node::FREE;
    }

    void* allocate(const uint32_t size) {
      if(size == 0) {
        return NULL; // invalid argument
      }

      const uint32_t required_chunk_count = (size+sizeof(Chunk)-1) / sizeof(Chunk);

      Snapshot cand;
      if(find_candidate(IsEnoughChunk(required_chunk_count), cand) == false) {
        return NULL; // out of memory
      }

      Node new_node = {cand.node().next,
                       cand.node().count - required_chunk_count,
                       Node::FREE};

      if(cand.compare_and_swap(new_node) == false) {
        return allocate(size);
      }

      uint32_t alloced_node_index = cand.index(nodes_) + new_node.count;
      nodes_[alloced_node_index].count = required_chunk_count;

      return reinterpret_cast<void*>(&chunks_[alloced_node_index]);
    }

  private:
    class IsEnoughChunk {
    public:
      IsEnoughChunk(uint32_t required_chunk_count) : count(required_chunk_count) {}

      bool operator()(const Snapshot& pred, const Snapshot& curr) const {
        return curr.node().count >= count;
      }

    private:
      const uint32_t count;
    };

    template<class Callback>
    bool find_candidate(const Callback& fn, Snapshot& node, uint32_t retry=0) {
      Snapshot head(&nodes_[0]);
      return find_candidate(fn, head, node, retry);
    }

    template<class Callback>
    bool find_candidate(const Callback& fn, Snapshot& pred, Snapshot& curr, uint32_t retry) {
      if(retry > MAX_RETRY_COUNT) {
        return false;
      }

      if(get_next_snapshot(pred, curr) == false ||
         update_node_status(pred, curr) == false ||
         merge_adjacent_nodes_if_need(pred, curr) == false) {
        return find_candidate(fn, curr, retry+1);
      }

      if(fn(pred, curr)) {
        return true;
      }

      pred = curr;
      return find_candidate(fn, pred, curr, retry);
    }

    bool get_next_snapshot(Snapshot& pred, Snapshot& curr) const {
      if(pred.node().next == node_count_) {
        return false;
      }

      curr.update(&nodes_[pred.node().next]);
      return pred.isModified() == false;
    }

    bool update_node_status(Snapshot& pred, Snapshot& curr) {
      return true;
    }

    bool merge_adjacent_nodes_if_need(Snapshot& pred, Snapshot& curr) {
      return true;
    }

  private:
    const uint32_t node_count_;
    Node*          nodes_;
    Chunk*         chunks_;
  };
}

#endif
<commit_msg>allocateメソッド完成(未動作確認)<commit_after>#ifndef IMQUE_ALLOCATOR_HH
#define IMQUE_ALLOCATOR_HH

#include <inttypes.h>
#include <cassert>

namespace imque {
  class Allocator {
    struct Node {  // node of free-list
      uint32_t next;     // next free block index
      uint32_t count:30; // available block count
      uint32_t status:2;

      enum STATUS {
        FREE      = 0,
        JOIN_HEAD = 1,
        JOIN_TAIL = 2
      };

      uint64_t* as_uint64_ptr() { return reinterpret_cast<uint64_t*>(this); }
      uint64_t as_uint64() const { return *reinterpret_cast<const uint64_t*>(this); }
    } __attribute__((__packed__));

    struct Chunk {
      char padding[32];
    };

    class Snapshot {
    public:
      Snapshot() : ptr_(NULL) {}
      Snapshot(Node* ptr) : ptr_(ptr), val_(*ptr) {}

      void update(Node* ptr) {
        ptr_ = ptr;
        val_ = *ptr;
      }

      const Node& node() const { return val_; }
      bool isModified() const { return ptr_->as_uint64() != val_.as_uint64(); }

      bool compare_and_swap(const Node& new_val) {
        if(isModified() == false &&
           __sync_bool_compare_and_swap(ptr_->as_uint64_ptr(), val_.as_uint64(), new_val.as_uint64())) {
          val_ = new_val;
          return true;
        }
        return false;
      }

      uint32_t index(Node* head) const { return ptr_ - head; }

    private:
      Node* ptr_;
      Node  val_;
    };

    static const uint32_t MAX_RETRY_COUNT = 256;

  public:
    Allocator(void* region, uint32_t size)
      : node_count_(size/(sizeof(Node)+sizeof(Chunk))),
        nodes_(reinterpret_cast<Node*>(region)),
        chunks_(reinterpret_cast<Chunk*>(nodes_+node_count_)) {
    }

    operator bool() const { return node_count_ > 2; }

    void init() {
      nodes_[0].next   = 1;
      nodes_[0].count  = 0;
      nodes_[0].status = Node::FREE;

      nodes_[1].next   = node_count_;
      nodes_[1].count  = node_count_-1;
      nodes_[1].status = Node::FREE;
    }

    void* allocate(const uint32_t size) {
      if(size == 0) {
        return NULL; // invalid argument
      }

      const uint32_t required_chunk_count = (size+sizeof(Chunk)-1) / sizeof(Chunk);

      Snapshot cand;
      if(find_candidate(IsEnoughChunk(required_chunk_count), cand) == false) {
        return NULL; // out of memory
      }

      Node new_node = {cand.node().next,
                       cand.node().count - required_chunk_count,
                       Node::FREE};

      if(cand.compare_and_swap(new_node) == false) {
        return allocate(size);
      }

      uint32_t alloced_node_index = cand.index(nodes_) + new_node.count;
      nodes_[alloced_node_index].count = required_chunk_count;

      return reinterpret_cast<void*>(&chunks_[alloced_node_index]);
    }

  private:
    class IsEnoughChunk {
    public:
      IsEnoughChunk(uint32_t required_chunk_count) : count(required_chunk_count) {}

      bool operator()(const Snapshot& pred, const Snapshot& curr) const {
        return curr.node().count >= count;
      }

    private:
      const uint32_t count;
    };

    template<class Callback>
    bool find_candidate(const Callback& fn, Snapshot& node, uint32_t retry=0) {
      Snapshot head(&nodes_[0]);
      return find_candidate(fn, head, node, retry);
    }

    template<class Callback>
    bool find_candidate(const Callback& fn, Snapshot& pred, Snapshot& curr, uint32_t retry) {
      if(retry > MAX_RETRY_COUNT) {
        assert(retry <= MAX_RETRY_COUNT);
        return false;
      }

      if(get_next_snapshot(pred, curr) == false ||
         update_node_status(pred, curr) == false ||
         join_nodes_if_need(pred, curr) == false) {
        return find_candidate(fn, curr, retry+1);
      }

      if(fn(pred, curr)) {
        return true;
      }

      pred = curr;
      return find_candidate(fn, pred, curr, retry);
    }

    bool get_next_snapshot(Snapshot& pred, Snapshot& curr) const {
      if(pred.node().next == node_count_) {
        return false;
      }

      curr.update(&nodes_[pred.node().next]);
      return pred.isModified() == false;
    }

    bool update_node_status(Snapshot& pred, Snapshot& curr) {
      if(pred.node().next != pred.index(nodes_) + pred.node().count) {
        return true;
      }

      Node new_pred_node = {pred.node().next,
                            pred.node().count,
                            pred.node().status | Node::JOIN_HEAD};
      if(pred.compare_and_swap(new_pred_node) == false) {
        return false;
      }

      Node new_curr_node = {curr.node().next,
                            curr.node().count,
                            curr.node().status | Node::JOIN_TAIL};
      if(pred.compare_and_swap(new_curr_node) == false) {
        return false;
      }

      return true;
    }

    bool join_nodes_if_need(Snapshot& pred, Snapshot& curr) {
      if(! (pred.node().status & Node::JOIN_HEAD &&
            curr.node().status & Node::JOIN_TAIL)) {
        return true;
      }
      assert(pred.node().next == pred.index(nodes_) + pred.node().count);

      Node new_pred_node = {curr.node().next,
                            pred.node().count + curr.node().count,
                            (pred.node().status & ~Node::JOIN_HEAD) |
                            (curr.node().status & ~Node::JOIN_TAIL)};
      if(pred.compare_and_swap(new_pred_node) == false) {
        return false;
      }

      return get_next_snapshot(pred, curr);
    }

  private:
    const uint32_t node_count_;
    Node*          nodes_;
    Chunk*         chunks_;
  };
}

#endif
<|endoftext|>


<commit_before>/*******************************************************************************
 * thrill/api/read_binary.hpp
 *
 * Part of Project Thrill.
 *
 * Copyright (C) 2015 Alexander Noe <aleexnoe@gmail.com>
 * Copyright (C) 2015 Timo Bingmann <tb@panthema.net>
 *
 * This file has no license. Only Chuck Norris can compile it.
 ******************************************************************************/

#pragma once
#ifndef THRILL_API_READ_BINARY_HEADER
#define THRILL_API_READ_BINARY_HEADER

#include <thrill/api/dia.hpp>
#include <thrill/api/dop_node.hpp>
#include <thrill/common/item_serialization_tools.hpp>
#include <thrill/common/logger.hpp>
#include <thrill/common/math.hpp>
#include <thrill/core/file_io.hpp>
#include <thrill/net/buffer_builder.hpp>

#include <algorithm>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>

namespace thrill {
namespace api {

//! \addtogroup api Interface
//! \{

/*!
 * A DIANode which performs a line-based Read operation. Read reads a file from
 * the file system and emits it as a DIA.
 */
template <typename ValueType>
class ReadBinaryNode : public DOpNode<ValueType>
{
public:
    using Super = DOpNode<ValueType>;
    using Super::context_;
    using Super::result_file_;

    using FileSizePair = std::pair<std::string, size_t>;

    static const bool debug = false;

    /*!
     * Constructor for a ReadLinesNode. Sets the Context
     * and file path.
     *
     * \param ctx Reference to Context, which holds references to data and network.
     * \param filepath Path of the input file
     */
    ReadBinaryNode(Context& ctx,
                   const std::string& filepath,
                   StatsNode* stats_node)
        : Super(ctx, { }, "Read", stats_node),
          filepath_(filepath)
    {
		core::FileIO fio;
        filelist_ = fio.ReadFileList(filepath_).first;
        filesize_ = filelist_[context_.my_rank() + 1].second -
                    filelist_[context_.my_rank()].second;

        auto my_start_and_end =
            common::CalculateLocalRange(filelist_[filelist_.size() - 1].second,
                                        context_.num_workers(),
                                        context_.my_rank());

        size_t my_start = std::get<0>(my_start_and_end);
        size_t my_end = std::get<1>(my_start_and_end);
        size_t first_file = 0;
        size_t last_file = 0;

        while (filelist_[first_file + 1].second <= my_start) {
            first_file++;
            last_file++;
        }

        while (filelist_[last_file + 1].second <= my_end && last_file < filelist_.size() - 1) {
            last_file++;
        }

        auto start_iter = filelist_.begin() + first_file;
        auto end_iter = filelist_.begin() + last_file + 1;

        my_files_ = std::vector<FileSizePair>(start_iter, end_iter);
    }

    virtual ~ReadBinaryNode() { }

    //! Executes the read operation. Reads a file line by line
    //! and emmits it after applyung the read function.
    void Execute() final { }

    void PushData() final {
        static const bool debug = false;
        LOG << "READING data " << result_file_.ToString();

        bfr_.SetFileList(my_files_);

        // Hook Read
        while (bfr_.HasNext()) {
            auto item = data::Serialization<BinaryFileReader, ValueType>
                        ::Deserialize(bfr_);
			LOG << item;
            for (auto func : Super::callbacks_) {
                func(item);
            }
        }
		LOG << "DONE!";
    }

    void Dispose() final { }

    /*!
     * Produces an 'empty' function stack, which only contains the identity
     * emitter function.
     *
     * \return Empty function stack
     */
    auto ProduceStack() {
        return FunctionStack<ValueType>();
    }

    /*!
     * Returns "[ReadLinesNode]" as a string.
     * \return "[ReadLinesNode]"
     */
    std::string ToString() final {
        return "[ReadLinesNode] Id: " + result_file_.ToString();
    }

private:
    //! Path of the input file.
    std::string filepath_;

    std::streampos filesize_;

    std::vector<FileSizePair> filelist_;
    std::vector<FileSizePair> my_files_;

    class BinaryFileReader
        : public common::ItemReaderToolsBase<BinaryFileReader>
    {
    public:

        const size_t read_size = 2 * 1024;

        BinaryFileReader() { }

        virtual ~BinaryFileReader() { }

        void CloseStream() {
			close(c_file_);
        }

        void SetFileList(std::vector<FileSizePair> path) {
            filelist_ = path;
			if (filelist_.size() > 1) {
				c_file_ = fio.OpenFile(filelist_[0].first);
				buffer_.Reserve(read_size);
				current_size_ = filelist_[1].second - filelist_[0].second;
			}
        }

        bool HasNext() {
			//no input files for this worker
			if (filelist_.size() <= 1) {
				return false;
			}

			if (buffer_.size() == current_) {
				buffer_.set_size(read(c_file_, buffer_.data(), read_size));
				current_ = 0;
				//buffer is empty when file is already finished
				//->go to next file if there is another one in filelist_
				if (buffer_.size()) {
					return true;
				} else if (current_file_ < filelist_.size() - 2) {
					close(c_file_);
					current_file_++;
					current_size_ = filelist_[current_file_ + 1].second - filelist_[current_file_].second;
					c_file_ = fio.OpenFile(filelist_[current_file_].first);
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
        }

        char GetByte() {
			if (current_ == buffer_.size()) {
				buffer_.set_size(read(c_file_, buffer_.data(), read_size));
				current_ = 0;
			}
			current_++;
            return buffer_[current_ - 1];
        }

        template <typename Type>
        Type Get() {
			if (buffer_.size() < current_ + sizeof(Type)) {
				//copy rest of buffer into start of next buffer
				std::copy(buffer_.begin() + current_,
						  buffer_.end(),
						  buffer_.data());
				size_t copied_bytes = buffer_.end() - buffer_.begin() - current_;

				buffer_.set_size(
					read(c_file_,
						 buffer_.data() + copied_bytes,
						 read_size - copied_bytes) + copied_bytes);
				current_ = 0;
			}

			Type elemc;
			std::copy(buffer_.begin() + current_,
					  buffer_.begin() + current_ + sizeof(Type),
					  reinterpret_cast<char*>(&elemc));
			current_ += sizeof(Type);
			return elemc;
        }

    private:
		int c_file_;
		size_t current_ = 0;
		net::BufferBuilder buffer_;
        std::vector<FileSizePair> filelist_;
		std::streampos current_size_;
		size_t current_file_ = 0;
		core::FileIO fio;
    };

    BinaryFileReader bfr_;
};

template <typename ValueType>
DIARef<ValueType> ReadBinary(Context& ctx, std::string filepath, ValueType) {

    StatsNode* stats_node = ctx.stats_graph().AddNode("ReadBinary", NodeType::DOP);

    auto shared_node =
        std::make_shared<ReadBinaryNode<ValueType> >(
            ctx, filepath, stats_node);

    auto read_stack = shared_node->ProduceStack();

    return DIARef<ValueType, decltype(read_stack)>(
        shared_node, read_stack, { stats_node });
}

//! \}

} // namespace api
} // namespace thrill

#endif // !THRILL_API_READ_BINARY_HEADER

/******************************************************************************/
<commit_msg>block size for reads: 2KiB->2MiB<commit_after>/*******************************************************************************
 * thrill/api/read_binary.hpp
 *
 * Part of Project Thrill.
 *
 * Copyright (C) 2015 Alexander Noe <aleexnoe@gmail.com>
 * Copyright (C) 2015 Timo Bingmann <tb@panthema.net>
 *
 * This file has no license. Only Chuck Norris can compile it.
 ******************************************************************************/

#pragma once
#ifndef THRILL_API_READ_BINARY_HEADER
#define THRILL_API_READ_BINARY_HEADER

#include <thrill/api/dia.hpp>
#include <thrill/api/dop_node.hpp>
#include <thrill/common/item_serialization_tools.hpp>
#include <thrill/common/logger.hpp>
#include <thrill/common/math.hpp>
#include <thrill/core/file_io.hpp>
#include <thrill/net/buffer_builder.hpp>

#include <algorithm>
#include <fcntl.h>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>

namespace thrill {
namespace api {

//! \addtogroup api Interface
//! \{

/*!
 * A DIANode which performs a line-based Read operation. Read reads a file from
 * the file system and emits it as a DIA.
 */
template <typename ValueType>
class ReadBinaryNode : public DOpNode<ValueType>
{
public:
    using Super = DOpNode<ValueType>;
    using Super::context_;
    using Super::result_file_;

    using FileSizePair = std::pair<std::string, size_t>;

    static const bool debug = false;

    /*!
     * Constructor for a ReadLinesNode. Sets the Context
     * and file path.
     *
     * \param ctx Reference to Context, which holds references to data and network.
     * \param filepath Path of the input file
     */
    ReadBinaryNode(Context& ctx,
                   const std::string& filepath,
                   StatsNode* stats_node)
        : Super(ctx, { }, "Read", stats_node),
          filepath_(filepath)
    {
		core::FileIO fio;
        filelist_ = fio.ReadFileList(filepath_).first;
        filesize_ = filelist_[context_.my_rank() + 1].second -
                    filelist_[context_.my_rank()].second;

        auto my_start_and_end =
            common::CalculateLocalRange(filelist_[filelist_.size() - 1].second,
                                        context_.num_workers(),
                                        context_.my_rank());

        size_t my_start = std::get<0>(my_start_and_end);
        size_t my_end = std::get<1>(my_start_and_end);
        size_t first_file = 0;
        size_t last_file = 0;

        while (filelist_[first_file + 1].second <= my_start) {
            first_file++;
            last_file++;
        }

        while (filelist_[last_file + 1].second <= my_end && last_file < filelist_.size() - 1) {
            last_file++;
        }

        auto start_iter = filelist_.begin() + first_file;
        auto end_iter = filelist_.begin() + last_file + 1;

        my_files_ = std::vector<FileSizePair>(start_iter, end_iter);
    }

    virtual ~ReadBinaryNode() { }

    //! Executes the read operation. Reads a file line by line
    //! and emmits it after applyung the read function.
    void Execute() final { }

    void PushData() final {
        static const bool debug = false;
        LOG << "READING data " << result_file_.ToString();

        bfr_.SetFileList(my_files_);

        // Hook Read
        while (bfr_.HasNext()) {
            auto item = data::Serialization<BinaryFileReader, ValueType>
                        ::Deserialize(bfr_);
			LOG << item;
            for (auto func : Super::callbacks_) {
                func(item);
            }
        }
		LOG << "DONE!";
    }

    void Dispose() final { }

    /*!
     * Produces an 'empty' function stack, which only contains the identity
     * emitter function.
     *
     * \return Empty function stack
     */
    auto ProduceStack() {
        return FunctionStack<ValueType>();
    }

    /*!
     * Returns "[ReadLinesNode]" as a string.
     * \return "[ReadLinesNode]"
     */
    std::string ToString() final {
        return "[ReadLinesNode] Id: " + result_file_.ToString();
    }

private:
    //! Path of the input file.
    std::string filepath_;

    std::streampos filesize_;

    std::vector<FileSizePair> filelist_;
    std::vector<FileSizePair> my_files_;

    class BinaryFileReader
        : public common::ItemReaderToolsBase<BinaryFileReader>
    {
    public:

        const size_t read_size = 2 * 1024 * 1024;

        BinaryFileReader() { }

        virtual ~BinaryFileReader() { }

        void CloseStream() {
			close(c_file_);
        }

        void SetFileList(std::vector<FileSizePair> path) {
            filelist_ = path;
			if (filelist_.size() > 1) {
				c_file_ = fio.OpenFile(filelist_[0].first);
				buffer_.Reserve(read_size);
				current_size_ = filelist_[1].second - filelist_[0].second;
			}
        }

        bool HasNext() {
			//no input files for this worker
			if (filelist_.size() <= 1) {
				return false;
			}

			if (buffer_.size() == current_) {
				buffer_.set_size(read(c_file_, buffer_.data(), read_size));
				current_ = 0;
				//buffer is empty when file is already finished
				//->go to next file if there is another one in filelist_
				if (buffer_.size()) {
					return true;
				} else if (current_file_ < filelist_.size() - 2) {
					close(c_file_);
					current_file_++;
					current_size_ = filelist_[current_file_ + 1].second - filelist_[current_file_].second;
					c_file_ = fio.OpenFile(filelist_[current_file_].first);
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
        }

        char GetByte() {
			if (current_ == buffer_.size()) {
				buffer_.set_size(read(c_file_, buffer_.data(), read_size));
				current_ = 0;
			}
			current_++;
            return buffer_[current_ - 1];
        }

        template <typename Type>
        Type Get() {
			if (buffer_.size() < current_ + sizeof(Type)) {
				//copy rest of buffer into start of next buffer
				std::copy(buffer_.begin() + current_,
						  buffer_.end(),
						  buffer_.data());
				size_t copied_bytes = buffer_.end() - buffer_.begin() - current_;

				buffer_.set_size(
					read(c_file_,
						 buffer_.data() + copied_bytes,
						 read_size - copied_bytes) + copied_bytes);
				current_ = 0;
			}

			Type elemc;
			std::copy(buffer_.begin() + current_,
					  buffer_.begin() + current_ + sizeof(Type),
					  reinterpret_cast<char*>(&elemc));
			current_ += sizeof(Type);
			return elemc;
        }

    private:
		int c_file_;
		size_t current_ = 0;
		net::BufferBuilder buffer_;
        std::vector<FileSizePair> filelist_;
		std::streampos current_size_;
		size_t current_file_ = 0;
		core::FileIO fio;
    };

    BinaryFileReader bfr_;
};

template <typename ValueType>
DIARef<ValueType> ReadBinary(Context& ctx, std::string filepath, ValueType) {

    StatsNode* stats_node = ctx.stats_graph().AddNode("ReadBinary", NodeType::DOP);

    auto shared_node =
        std::make_shared<ReadBinaryNode<ValueType> >(
            ctx, filepath, stats_node);

    auto read_stack = shared_node->ProduceStack();

    return DIARef<ValueType, decltype(read_stack)>(
        shared_node, read_stack, { stats_node });
}

//! \}

} // namespace api
} // namespace thrill

#endif // !THRILL_API_READ_BINARY_HEADER

/******************************************************************************/
<|endoftext|>


<commit_before>
#include "gtest/gtest.h"

#include "au/ThreadManager.h"

#include "engine/MemoryManager.h"
#include "engine/MemoryRequest.h"

#include "xmlparser/xmlParser.h"

#include "samson/client/SamsonClient.h"

#include "unitTest/common_engine_test.h"

#include "samson/common/ports.h"           // for SAMSON_WORKER_PORT

TEST(samson_client, DISABLED_test_1 )
{
   // SamsonClient to play with..
   samson::SamsonClient* samson_client = init_samson_client_test();

   // Connect to samsonWorker
   au::ErrorManager error;
   samson_client->initConnection( &error , "localhost" , SAMSON_WORKER_PORT , "anonymous" , "anonymous" );

   EXPECT_EQ( error.isActivated() , false) << "Error connecting samsonClient to samsonWorker";

   // Close samson client test
   close_samson_client_test( samson_client );

}

TEST(samson_client, DISABLED_test_2 )
{

   // SamsonClient to play with..
   samson::SamsonClient* samson_client = init_samson_client_test();

   // Connect to samsonWorker
   au::ErrorManager error;
   samson_client->initConnection( &error , "localhost" , SAMSON_WORKER_PORT , "anonymous" , "anonymous" );

   ASSERT_TRUE(samson_client->connection_ready()) << "Connection not ready";

   // Close samson client test
   close_samson_client_test( samson_client );
}
<commit_msg>Test for SamsonClient<commit_after>
#include "gtest/gtest.h"

#include "au/ThreadManager.h"

#include "engine/MemoryManager.h"
#include "engine/MemoryRequest.h"

#include "xmlparser/xmlParser.h"

#include "samson/client/SamsonClient.h"

#include "unitTest/common_engine_test.h"

#include "samson/common/ports.h"           // for SAMSON_WORKER_PORT

TEST(samson_client, test_1 )
{
   // SamsonClient to play with..
   samson::SamsonClient* samson_client = init_samson_client_test();

   // Connect to samsonWorker
   au::ErrorManager error;
   samson_client->initConnection( &error , "localhost" , SAMSON_WORKER_PORT , "anonymous" , "anonymous" );

   EXPECT_EQ( error.isActivated() , false) << "Error connecting samsonClient to samsonWorker";

   // Close samson client test
   close_samson_client_test( samson_client );

}

TEST(samson_client, DISABLED_test_2 )
{

   // SamsonClient to play with..
   samson::SamsonClient* samson_client = init_samson_client_test();

   // Connect to samsonWorker
   au::ErrorManager error;
   samson_client->initConnection( &error , "localhost" , SAMSON_WORKER_PORT , "anonymous" , "anonymous" );

   ASSERT_TRUE(samson_client->connection_ready()) << "Connection not ready";

   // Close samson client test
   close_samson_client_test( samson_client );
}
<|endoftext|>


<commit_before>//===- AMDGPU.cpp ---------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Error.h"
#include "InputFiles.h"
#include "Symbols.h"
#include "Target.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Endian.h"

using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;

namespace {
class AMDGPU final : public TargetInfo {
public:
  AMDGPU();
  uint32_t calcEFlags() const override;
  void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
  RelExpr getRelExpr(RelType Type, const SymbolBody &S,
                     const uint8_t *Loc) const override;
};
} // namespace

AMDGPU::AMDGPU() {
  RelativeRel = R_AMDGPU_RELATIVE64;
  GotRel = R_AMDGPU_ABS64;
  GotEntrySize = 8;
}

uint32_t AMDGPU::calcEFlags() const {
  if (ObjectFiles.empty())
    return 0;

  uint32_t Ret =
    cast<ObjFile<ELF64LE>>(ObjectFiles.front())->getObj().getHeader()->e_flags;

  // Verify that all input files have the same e_flags.
  for (InputFile *F : ArrayRef<InputFile*>(ObjectFiles).slice(1)) {
    if (Ret != cast<ObjFile<ELF64LE>>(F)->getObj().getHeader()->e_flags) {
      error("incompatible e_flags: " + toString(F));
      return 0;
    }
  }

  return Ret;
}

void AMDGPU::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
  switch (Type) {
  case R_AMDGPU_ABS32:
  case R_AMDGPU_GOTPCREL:
  case R_AMDGPU_GOTPCREL32_LO:
  case R_AMDGPU_REL32:
  case R_AMDGPU_REL32_LO:
    write32le(Loc, Val);
    break;
  case R_AMDGPU_ABS64:
    write64le(Loc, Val);
    break;
  case R_AMDGPU_GOTPCREL32_HI:
  case R_AMDGPU_REL32_HI:
    write32le(Loc, Val >> 32);
    break;
  default:
    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
  }
}

RelExpr AMDGPU::getRelExpr(RelType Type, const SymbolBody &S,
                           const uint8_t *Loc) const {
  switch (Type) {
  case R_AMDGPU_ABS32:
  case R_AMDGPU_ABS64:
    return R_ABS;
  case R_AMDGPU_REL32:
  case R_AMDGPU_REL32_LO:
  case R_AMDGPU_REL32_HI:
    return R_PC;
  case R_AMDGPU_GOTPCREL:
  case R_AMDGPU_GOTPCREL32_LO:
  case R_AMDGPU_GOTPCREL32_HI:
    return R_GOT_PC;
  default:
    return R_INVALID;
  }
}

TargetInfo *elf::getAMDGPUTargetInfo() {
  static AMDGPU Target;
  return &Target;
}
<commit_msg>Simplify.<commit_after>//===- AMDGPU.cpp ---------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Error.h"
#include "InputFiles.h"
#include "Symbols.h"
#include "Target.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Endian.h"

using namespace llvm;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace llvm::ELF;
using namespace lld;
using namespace lld::elf;

namespace {
class AMDGPU final : public TargetInfo {
public:
  AMDGPU();
  uint32_t calcEFlags() const override;
  void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
  RelExpr getRelExpr(RelType Type, const SymbolBody &S,
                     const uint8_t *Loc) const override;
};
} // namespace

AMDGPU::AMDGPU() {
  RelativeRel = R_AMDGPU_RELATIVE64;
  GotRel = R_AMDGPU_ABS64;
  GotEntrySize = 8;
}

static uint32_t getEFlags(InputFile *File) {
  return cast<ObjFile<ELF64LE>>(File)->getObj().getHeader()->e_flags;
}

uint32_t AMDGPU::calcEFlags() const {
  assert(!ObjectFiles.empty());
  uint32_t Ret = getEFlags(ObjectFiles[0]);

  // Verify that all input files have the same e_flags.
  for (InputFile *F : makeArrayRef(ObjectFiles).slice(1)) {
    if (Ret == getEFlags(F))
      continue;
    error("incompatible e_flags: " + toString(F));
    return 0;
  }
  return Ret;
}

void AMDGPU::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
  switch (Type) {
  case R_AMDGPU_ABS32:
  case R_AMDGPU_GOTPCREL:
  case R_AMDGPU_GOTPCREL32_LO:
  case R_AMDGPU_REL32:
  case R_AMDGPU_REL32_LO:
    write32le(Loc, Val);
    break;
  case R_AMDGPU_ABS64:
    write64le(Loc, Val);
    break;
  case R_AMDGPU_GOTPCREL32_HI:
  case R_AMDGPU_REL32_HI:
    write32le(Loc, Val >> 32);
    break;
  default:
    error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
  }
}

RelExpr AMDGPU::getRelExpr(RelType Type, const SymbolBody &S,
                           const uint8_t *Loc) const {
  switch (Type) {
  case R_AMDGPU_ABS32:
  case R_AMDGPU_ABS64:
    return R_ABS;
  case R_AMDGPU_REL32:
  case R_AMDGPU_REL32_LO:
  case R_AMDGPU_REL32_HI:
    return R_PC;
  case R_AMDGPU_GOTPCREL:
  case R_AMDGPU_GOTPCREL32_LO:
  case R_AMDGPU_GOTPCREL32_HI:
    return R_GOT_PC;
  default:
    return R_INVALID;
  }
}

TargetInfo *elf::getAMDGPUTargetInfo() {
  static AMDGPU Target;
  return &Target;
}
<|endoftext|>