29.04.2014 - Version 0.4.0_prealpha. PICodeParser, namespace PICodeInfo, new tool "pip_cmg" in dir "code_model_generator". New feature in PIIODevice - "createFromFullPath", all parameters of all I/O devices now works with PIObjects`s properties.
This commit is contained in:
65
piiodevice.h
65
piiodevice.h
@@ -4,7 +4,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Abstract input/output device
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -23,12 +23,27 @@
|
||||
#ifndef PIIODEVICE_H
|
||||
#define PIIODEVICE_H
|
||||
|
||||
#include "picollection.h"
|
||||
#include "pivariant.h"
|
||||
#include "pitimer.h"
|
||||
|
||||
// function executed from threaded read, pass ThreadedReadData, readedData, sizeOfData
|
||||
typedef bool (*ReadRetFunc)(void * , uchar * , int );
|
||||
|
||||
#ifdef DOXYGEN
|
||||
|
||||
//! \relatesalso PIIODevice \brief Use this macro to enable automatic creation instances of your class with \a createFromFullPath() function
|
||||
# define REGISTER_DEVICE(class)
|
||||
|
||||
//! \relatesalso PIIODevice \brief Use this macro instead of PIOBJECT when describe your own PIIODevice
|
||||
# define PIIODEVICE(class)
|
||||
|
||||
#else
|
||||
|
||||
# define REGISTER_DEVICE(class) ADD_NEW_TO_COLLECTION(__PIIODevices__, class)
|
||||
# define PIIODEVICE(class) PIOBJECT(class) PIIODevice * copy() const {return new class();}
|
||||
|
||||
#endif
|
||||
|
||||
class PIP_EXPORT PIIODevice: public PIThread
|
||||
{
|
||||
@@ -45,17 +60,17 @@ public:
|
||||
ReadWrite /*! Device can both read and write */ = 0x03
|
||||
};
|
||||
|
||||
PIIODevice(const PIString & path, DeviceMode type = ReadWrite, bool initNow = true);
|
||||
PIIODevice(const PIString & path, DeviceMode mode = ReadWrite);
|
||||
virtual ~PIIODevice();
|
||||
|
||||
//! Current open mode of device
|
||||
DeviceMode mode() const {return mode_;}
|
||||
|
||||
//! Current path of device
|
||||
PIString path() const {return path_;}
|
||||
PIString path() const {return property("path").toString();}
|
||||
|
||||
//! Set path of device
|
||||
void setPath(const PIString & path) {path_ = path;}
|
||||
void setPath(const PIString & path) {setProperty("path", path);}
|
||||
|
||||
//! Return \b true if mode is ReadOnly or ReadWrite
|
||||
bool isReadable() const {return (mode_ & ReadOnly);}
|
||||
@@ -79,17 +94,17 @@ public:
|
||||
|
||||
|
||||
//! Set execution of \a open enabled while threaded read on closed device
|
||||
void setReopenEnabled(bool yes = true) {reopen_enabled_ = yes;}
|
||||
void setReopenEnabled(bool yes = true) {setProperty("reopenEnabled", yes);}
|
||||
|
||||
//! Set timeout in milliseconds between \a open tryings if reopen is enabled
|
||||
void setReopenTimeout(int msecs = 1000) {reopen_timeout_ = msecs;}
|
||||
void setReopenTimeout(int msecs) {setProperty("reopenTimeout", msecs);}
|
||||
|
||||
|
||||
//! Return reopen enable
|
||||
bool isReopenEnabled() const {return reopen_enabled_;}
|
||||
bool isReopenEnabled() const {return property("reopenEnabled").toBool();}
|
||||
|
||||
//! Return reopen timeout
|
||||
int reopenTimeout() {return reopen_timeout_;}
|
||||
int reopenTimeout() {return property("reopenTimeout").toInt();}
|
||||
|
||||
|
||||
/** \brief Set "threaded read slot"
|
||||
@@ -172,12 +187,18 @@ public:
|
||||
|
||||
//! Configure device from section "section" of file "config_file", if "parent_section" parent section also will be read
|
||||
bool configure(const PIString & config_file, const PIString & section, bool parent_section = false);
|
||||
|
||||
|
||||
|
||||
//! \brief Try to determine suitable device, create new one, configure it with \a configureFromFullPath() and returns it.
|
||||
//! \details To function \a configureFromFullPath() "full_path" passed without \a fullPathPrefix() + "://".
|
||||
//! See \ref PIIODevice_sec7
|
||||
static PIIODevice * createFromFullPath(const PIString & full_path);
|
||||
|
||||
|
||||
EVENT_HANDLER(bool, open) {if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER1(bool, open, const PIString &, _path) {path_ = _path; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER1(bool, open, const PIString &, _path) {setPath(_path); if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER1(bool, open, const DeviceMode &, _type) {mode_ = _type; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER2(bool, open, const PIString &, _path, const DeviceMode &, _type) {path_ = _path; mode_ = _type; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER2(bool, open, const PIString &, _path, const DeviceMode &, _mode) {setPath(_path); mode_ = _mode; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER(bool, close) {opened_ = !closeDevice(); if (!opened_) closed(); return !opened_;}
|
||||
EVENT_HANDLER(bool, initialize) {init_ = init(); return init_;}
|
||||
|
||||
@@ -249,12 +270,9 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
//! Function executed from the end of constructor
|
||||
virtual void created() {;}
|
||||
|
||||
//! Function executed before first \a openDevice() or from constructor
|
||||
virtual bool init() {return true;}
|
||||
|
||||
|
||||
//! Reimplement to configure device from entries "e_main" and "e_parent", cast arguments to \a PIConfig::Entry*
|
||||
virtual bool configureDevice(const void * e_main, const void * e_parent = 0) {return true;}
|
||||
|
||||
@@ -267,20 +285,31 @@ protected:
|
||||
//! Function executed when thread read some data, default implementation execute external slot "ret_func_"
|
||||
virtual bool threadedRead(uchar * readed, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, readed, size); return true;}
|
||||
|
||||
|
||||
//! Reimplement to construct full unambiguous string prefix. \ref PIIODevice_sec7
|
||||
virtual PIString fullPathPrefix() const {return PIString();}
|
||||
|
||||
//! Reimplement to construct full unambiguous string, describes this device, default returns \a path()
|
||||
virtual PIString constructFullPath() const {return path();}
|
||||
|
||||
//! Reimplement to configure your device with parameters of full unambiguous string. Default implementation does nothing
|
||||
virtual void configureFromFullPath(const PIString & full_path) {;}
|
||||
|
||||
|
||||
void terminate();
|
||||
|
||||
|
||||
PIString path_;
|
||||
DeviceMode mode_;
|
||||
ReadRetFunc ret_func_;
|
||||
bool init_, opened_, thread_started_, reopen_enabled_, raise_threaded_read_;
|
||||
int reopen_timeout_;
|
||||
bool init_, opened_, thread_started_, raise_threaded_read_;
|
||||
void * ret_data_;
|
||||
|
||||
private:
|
||||
EVENT_HANDLER2(void, check_start, void * , data, int, delim);
|
||||
EVENT_HANDLER(void, write_func);
|
||||
|
||||
virtual PIIODevice * copy() const {return 0;}
|
||||
void _init();
|
||||
void begin();
|
||||
void run();
|
||||
void end() {terminate();}
|
||||
|
||||
Reference in New Issue
Block a user