PageRenderTime 32ms CodeModel.GetById 9ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/scannerthread.h

https://code.google.com/p/dwarftherapist/
C++ Header | 183 lines | 147 code | 10 blank | 26 comment | 1 complexity | d86ef6537f2d4f1c6238152f5897b013 MD5 | raw file
  1/*
  2Dwarf Therapist
  3Copyright (c) 2009 Trey Stout (chmod)
  4
  5Permission is hereby granted, free of charge, to any person obtaining a copy
  6of this software and associated documentation files (the "Software"), to deal
  7in the Software without restriction, including without limitation the rights
  8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9copies of the Software, and to permit persons to whom the Software is
 10furnished to do so, subject to the following conditions:
 11
 12The above copyright notice and this permission notice shall be included in
 13all copies or substantial portions of the Software.
 14
 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21THE SOFTWARE.
 22*/
 23#ifndef SCANNER_THREAD_H
 24#define SCANNER_THREAD_H
 25
 26#include <QThread>
 27#include "dfinstance.h"
 28#include "scannerjob.h"
 29#include "translationvectorsearchjob.h"
 30#include "stdstringsearchjob.h"
 31#include "nullterminatedstringsearchjob.h"
 32#include "stonevectorsearchjob.h"
 33#include "vectorsearchjob.h"
 34#include "dwarfraceindexsearchjob.h"
 35#include "creaturevectorsearchjob.h"
 36#include "positionvectorsearchjob.h"
 37#include "narrowingvectorsearchjob.h"
 38#include "squadvectorsearchjob.h"
 39#include "currentyearsearchjob.h"
 40
 41class ScannerThread : public QThread {
 42    Q_OBJECT
 43public:
 44    ScannerThread(QObject *parent = 0)
 45        : QThread(parent)
 46        , m_meta(QByteArray())
 47        , m_result(NULL)
 48    {}
 49
 50    void set_job(SCANNER_JOB_TYPE type) {
 51        m_type = type;
 52    }
 53    void set_meta(const QByteArray &meta) {
 54        m_meta = meta;
 55    }
 56    void set_search_vector(const QVector<VIRTADDR> &searchvector) {
 57        m_searchvector = searchvector;
 58    }
 59
 60    void * get_result() {
 61        return m_result;
 62    }
 63
 64    void clear_result() {
 65        m_result = NULL;
 66    }
 67
 68    void run() {
 69        // Don't forget your 'break' when adding new sections, Trey. You've done
 70        // it twice now for a total waste of about 50 minutes >:|
 71        switch (m_type) {
 72            case FIND_TRANSLATIONS_VECTOR:
 73                m_job = new TranslationVectorSearchJob;
 74                break;
 75            case FIND_STONE_VECTOR:
 76                m_job = new StoneVectorSearchJob;
 77                break;
 78            case FIND_DWARF_RACE_INDEX:
 79                m_job = new DwarfRaceIndexSearchJob;
 80                break;
 81            case FIND_CREATURE_VECTOR:
 82                m_job = new CreatureVectorSearchJob;
 83                break;
 84            case FIND_POSITION_VECTOR:
 85                m_job = new PositionVectorSearchJob;
 86                break;
 87            case FIND_STD_STRING:
 88                {
 89                    StdStringSearchJob *job = new StdStringSearchJob;
 90                    job->set_needle(m_meta);
 91                    m_job = job;
 92                }
 93                break;
 94            case FIND_NULL_TERMINATED_STRING:
 95                {
 96                    // what is this, Java?
 97                    NullTerminatedStringSearchJob *job = new NullTerminatedStringSearchJob;
 98                    job->set_needle(m_meta);
 99                    m_job = job;
100                }
101                break;
102            case FIND_VECTORS_OF_SIZE:
103                {
104                    VectorSearchJob *job = new VectorSearchJob;
105                    job->set_needle(m_meta);
106                    m_job = job;
107                }
108                break;
109            case FIND_NARROWING_VECTORS_OF_SIZE:
110                {
111                    NarrowingVectorSearchJob * job = new NarrowingVectorSearchJob;
112                    job->set_needle(m_meta);
113                    job->set_search_vector(m_searchvector);
114                    m_job = job;
115                }
116                break;
117            case FIND_SQUADS_VECTOR:
118                {
119                    m_job = new SquadVectorSearchJob;
120                    break;
121                }
122                break;
123        case FIND_CURRENT_YEAR:
124                {
125                    m_job = new CurrentYearSearchJob;
126                }
127                break;
128            default:
129                LOGW << "JOB TYPE NOT SET, EXITING THREAD";
130                return;
131        }
132        // forward the status signals
133        connect(m_job->df(), SIGNAL(scan_total_steps(int)),
134                SIGNAL(sub_scan_total_steps(int)));
135        connect(m_job->df(), SIGNAL(scan_progress(int)),
136                SIGNAL(sub_scan_progress(int)));
137        connect(m_job->df(), SIGNAL(scan_message(const QString&)),
138                SIGNAL(scan_message(const QString&)));
139        connect(m_job, SIGNAL(scan_message(const QString&)),
140                SIGNAL(scan_message(const QString&)));
141        connect(m_job, SIGNAL(found_address(const QString&, const quint32&)),
142                SIGNAL(found_address(const QString&, const quint32&)));
143        connect(m_job, SIGNAL(found_offset(const QString&, const int&)),
144                SIGNAL(found_offset(const QString&, const int&)));
145        connect(m_job, SIGNAL(main_scan_total_steps(int)),
146                SIGNAL(main_scan_total_steps(int)));
147        connect(m_job, SIGNAL(main_scan_progress(int)),
148                SIGNAL(main_scan_progress(int)));
149        connect(m_job, SIGNAL(sub_scan_total_steps(int)),
150                SIGNAL(sub_scan_total_steps(int)));
151        connect(m_job, SIGNAL(sub_scan_progress(int)),
152                SIGNAL(sub_scan_progress(int)));
153        connect(m_job, SIGNAL(quit()), this, SLOT(quit()));
154        connect(m_job, SIGNAL(got_result(void *)), this, SLOT(set_result(void *)));
155        QTimer::singleShot(0, m_job, SLOT(go()));
156        exec();
157        m_job->deleteLater();
158    }
159
160private:
161    SCANNER_JOB_TYPE m_type;
162    ScannerJob *m_job;
163    QByteArray m_meta;
164    QVector<VIRTADDR> m_searchvector;
165
166protected:
167    void * m_result;
168
169protected slots:
170    void set_result(void * result) {
171        m_result = result;
172    }
173
174signals:
175    void main_scan_total_steps(int);
176    void main_scan_progress(int);
177    void sub_scan_total_steps(int);
178    void sub_scan_progress(int);
179    void scan_message(const QString&);
180    void found_address(const QString&, const quint32&);
181    void found_offset(const QString&, const int&);
182};
183#endif