Skip to content

Commit a357000

Browse files
author
carm
committed
Merge branch 'preset-factory-error-handling'
2 parents 176fad1 + e4541d0 commit a357000

File tree

12 files changed

+127
-92
lines changed

12 files changed

+127
-92
lines changed

src/libprojectM/MilkdropPresetFactory/Expr.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,25 @@ float TreeExpr::eval_tree_expr ( int mesh_i, int mesh_j )
157157
/* Otherwise, this node is an infix operator. Evaluate
158158
accordingly */
159159

160-
assert(left);
161-
left_arg = left->eval_tree_expr ( mesh_i, mesh_j );
160+
/* Safe guard in case the user gave a partially written expression */
161+
if (left == NULL) {
162+
if (infix_op == Eval::infix_mult)
163+
left_arg = 1;
164+
else
165+
left_arg = 0;
166+
}
167+
else
168+
left_arg = left->eval_tree_expr ( mesh_i, mesh_j );
162169

163-
assert(right);
164-
right_arg = right->eval_tree_expr ( mesh_i, mesh_j );
170+
/* Safe guard in case the user gave a partially written expression */
171+
if (right == NULL) {
172+
if (infix_op == Eval::infix_mult)
173+
right_arg = 1;
174+
else
175+
right_arg = 0;
176+
}
177+
else
178+
right_arg = right->eval_tree_expr ( mesh_i, mesh_j );
165179

166180

167181
switch ( infix_op->type )

src/libprojectM/MilkdropPresetFactory/MilkdropPreset.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737
#include "fatal.h"
3838
#include <iostream>
3939
#include <fstream>
40+
#include <sstream>
4041

4142
#include "PresetFrameIO.hpp"
4243

44+
#include "PresetFactoryManager.hpp"
45+
46+
4347
MilkdropPreset::MilkdropPreset(std::istream & in, const std::string & presetName, PresetOutputs & presetOutputs):
4448
Preset(presetName),
4549
builtinParams(_presetInputs, presetOutputs),
@@ -291,15 +295,7 @@ void MilkdropPreset::initialize(const std::string & pathname)
291295
if (MILKDROP_PRESET_DEBUG)
292296
std::cerr << "[Preset] loading file \"" << pathname << "\"..." << std::endl;
293297

294-
if ((retval = loadPresetFile(pathname)) < 0)
295-
{
296-
if (MILKDROP_PRESET_DEBUG)
297-
std::cerr << "[Preset] failed to load file \"" <<
298-
pathname << "\"!" << std::endl;
299-
300-
/// @bug how should we handle this problem? a well define exception?
301-
throw retval;
302-
}
298+
loadPresetFile(pathname);
303299

304300
postloadInitialize();
305301
}
@@ -317,7 +313,7 @@ void MilkdropPreset::initialize(std::istream & in)
317313
std::cerr << "[Preset] failed to load from stream " << std::endl;
318314

319315
/// @bug how should we handle this problem? a well define exception?
320-
throw retval;
316+
throw PresetFactoryException("failed to read from input stream");
321317
}
322318

323319
postloadInitialize();
@@ -530,9 +526,12 @@ int MilkdropPreset::loadPresetFile(const std::string & pathname)
530526
/* Open the file corresponding to pathname */
531527
std::ifstream fs(pathname.c_str());
532528
if (!fs || fs.eof()) {
533-
if (MILKDROP_PRESET_DEBUG)
534-
std::cerr << "loadPresetFile: loading of file \"" << pathname << "\" failed!\n";
535-
return PROJECTM_ERROR;
529+
530+
std::ostringstream oss;
531+
oss << "Problem reading file from path: \"" << pathname << "\"";
532+
533+
throw PresetFactoryException(oss.str());
534+
536535
}
537536

538537
return readIn(fs);

src/libprojectM/MilkdropPresetFactory/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ int Parser::insert_gen_rec(GenExpr * gen_expr, TreeExpr * root)
10701070

10711071
if (root == NULL)
10721072
{
1073-
//if (PARSE_DEBUG) printf("insert_gen_rec: root is null, returning failure\n");
1073+
if (PARSE_DEBUG) printf("insert_gen_rec: root is null, returning failure\n");
10741074
return PROJECTM_FAILURE;
10751075
}
10761076

src/libprojectM/MilkdropPresetFactory/Parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#ifndef _PARSER_H
3030
#define _PARSER_H
3131
//#define PARSE_DEBUG 2
32-
#define PARSE_DEBUG 0
32+
#define PARSE_DEBUG 2
3333

3434
#include <stdio.h>
3535

src/libprojectM/PresetFactoryManager.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "NativePresetFactory/NativePresetFactory.hpp"
2020
#endif
2121

22+
#include "Common.hpp"
2223
#include <sstream>
2324
PresetFactoryManager::PresetFactoryManager() : _gx(0), _gy(0), initialized(false) {}
2425

@@ -75,11 +76,29 @@ void PresetFactoryManager::registerFactory(const std::string & extensions, Prese
7576
}
7677
}
7778

79+
80+
81+
std::auto_ptr<Preset> PresetFactoryManager::allocate(const std::string & url, const std::string & name)
82+
{
83+
try {
84+
const std::string extension = parseExtension (url);
85+
86+
return factory(extension).allocate(url, name);
87+
} catch (const PresetFactoryException & e) {
88+
throw e;
89+
} catch (const std::exception & e) {
90+
throw PresetFactoryException(e.what());
91+
} catch (...) {
92+
throw PresetFactoryException("uncaught preset factory exception");
93+
}
94+
return std::auto_ptr<Preset>();
95+
}
96+
7897
PresetFactory & PresetFactoryManager::factory(const std::string & extension) {
7998

80-
if (!_factoryMap.count(extension)) {
99+
if (!extensionHandled(extension)) {
81100
std::ostringstream os;
82-
os << "No factory associated with \"" << extension << "\"." << std::endl;
101+
os << "No preset factory associated with \"" << extension << "\"." << std::endl;
83102
throw PresetFactoryException(os.str());
84103
}
85104
return *_factoryMap[extension];

src/libprojectM/PresetFactoryManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class PresetFactoryManager {
4848
/// \param extension the file name extension to verify
4949
/// \returns true if a factory exists, false otherwise
5050
bool extensionHandled(const std::string & extension) const;
51+
std::auto_ptr<Preset> allocate(const std::string & url, const std::string & name);
5152

5253
private:
5354
int _gx, _gy;

src/libprojectM/PresetLoader.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,7 @@ std::auto_ptr<Preset> PresetLoader::loadPreset ( unsigned int index ) const
136136
// Check that index isn't insane
137137
assert ( index >= 0 );
138138
assert ( index < _entries.size() );
139-
140-
// Return a new autopointer to a preset
141-
const std::string extension = parseExtension ( _entries[index] );
142-
143-
return _presetFactoryManager.factory(extension).allocate
139+
return _presetFactoryManager.allocate
144140
( _entries[index], _presetNames[index] );
145141

146142
}
@@ -149,13 +145,17 @@ std::auto_ptr<Preset> PresetLoader::loadPreset ( unsigned int index ) const
149145
std::auto_ptr<Preset> PresetLoader::loadPreset ( const std::string & url ) const
150146
{
151147

152-
// Return a new autopointer to a preset
153-
const std::string extension = parseExtension ( url );
154-
155-
/// @bug probably should not use url for preset name
156-
return _presetFactoryManager.factory(extension).allocate
157-
(url, url);
158148

149+
try {
150+
/// @bug probably should not use url for preset name
151+
return _presetFactoryManager.allocate
152+
(url, url);
153+
} catch (const std::exception & e) {
154+
throw PresetFactoryException(e.what());
155+
} catch (...) {
156+
throw PresetFactoryException("preset factory exception of unknown cause");
157+
}
158+
return std::auto_ptr<Preset>();
159159
}
160160

161161
void PresetLoader::handleDirectoryError()

src/libprojectM/projectM.cpp

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -693,27 +693,35 @@ static void *thread_callback(void *prjm) {
693693
return index;
694694
}
695695

696-
void projectM::selectPreset ( unsigned int index, bool hardCut)
696+
void projectM::selectPreset(unsigned int index, bool hardCut)
697697
{
698698

699699
if (m_presetChooser->empty())
700700
return;
701701

702-
if (!hardCut) {
703-
timeKeeper->StartSmoothing();
704-
}
705702

706703
*m_presetPos = m_presetChooser->begin(index);
704+
switchPreset(hardCut);
705+
}
707706

707+
void projectM::switchPreset(const bool hardCut) {
708+
std::string result;
708709
if (!hardCut) {
709-
switchPreset(m_activePreset2);
710+
result = switchPreset(m_activePreset2);
710711
} else {
711-
switchPreset(m_activePreset);
712-
timeKeeper->StartPreset();
712+
result = switchPreset(m_activePreset);
713+
if (result.empty())
714+
timeKeeper->StartPreset();
713715
}
714716

715-
presetSwitchedEvent(hardCut, **m_presetPos);
717+
if (result.empty() && !hardCut) {
718+
timeKeeper->StartSmoothing();
719+
}
716720

721+
if (result.empty())
722+
presetSwitchedEvent(hardCut, **m_presetPos);
723+
else
724+
presetSwitchFailedEvent(hardCut, **m_presetPos, result);
717725
}
718726

719727

@@ -722,20 +730,9 @@ void projectM::selectRandom(const bool hardCut) {
722730
if (m_presetChooser->empty())
723731
return;
724732

725-
if (!hardCut) {
726-
timeKeeper->StartSmoothing();
727-
}
728-
729733
*m_presetPos = m_presetChooser->weightedRandom(hardCut);
730734

731-
if (!hardCut) {
732-
switchPreset(m_activePreset2);
733-
} else {
734-
switchPreset(m_activePreset);
735-
timeKeeper->StartPreset();
736-
}
737-
738-
presetSwitchedEvent(hardCut, **m_presetPos);
735+
switchPreset(hardCut);
739736

740737
}
741738

@@ -744,72 +741,51 @@ void projectM::selectPrevious(const bool hardCut) {
744741
if (m_presetChooser->empty())
745742
return;
746743

747-
if (!hardCut) {
748-
timeKeeper->StartSmoothing();
749-
}
750744

751745
m_presetChooser->previousPreset(*m_presetPos);
752746

753-
if (!hardCut) {
754-
switchPreset(m_activePreset2);
755-
} else {
756-
switchPreset(m_activePreset);
757-
timeKeeper->StartPreset();
758-
}
759-
760-
presetSwitchedEvent(hardCut, **m_presetPos);
761-
762-
// m_activePreset = m_presetPos->allocate();
763-
// renderer->SetPipeline(m_activePreset->pipeline());
764-
// renderer->setPresetName(m_activePreset->name());
765-
766-
//timeKeeper->StartPreset();
767-
747+
switchPreset(hardCut);
768748
}
769749

770750
void projectM::selectNext(const bool hardCut) {
771751

772752
if (m_presetChooser->empty())
773753
return;
774754

775-
if (!hardCut) {
776-
timeKeeper->StartSmoothing();
777-
std::cout << "start smoothing" << std::endl;
778-
}
779-
780755
m_presetChooser->nextPreset(*m_presetPos);
781756

782-
if (!hardCut) {
783-
switchPreset(m_activePreset2);
784-
} else {
785-
switchPreset(m_activePreset);
786-
timeKeeper->StartPreset();
787-
}
788-
presetSwitchedEvent(hardCut, **m_presetPos);
789-
790-
757+
switchPreset(hardCut);
791758
}
792759

793760
/**
794-
*
795-
* @param targetPreset
796-
*/
797-
void projectM::switchPreset(std::auto_ptr<Preset> & targetPreset) {
761+
* Switches to the target preset.
762+
* @param targetPreset
763+
* @return a message indicating an error, empty otherwise.
764+
*/
765+
std::string projectM::switchPreset(std::auto_ptr<Preset> & targetPreset) {
798766

799767
#ifdef SYNC_PRESET_SWITCHES
800768
pthread_mutex_lock(&preset_mutex);
801769
#endif
802-
770+
try {
803771
targetPreset = m_presetPos->allocate();
804-
772+
} catch (const PresetFactoryException & e) {
773+
#ifdef SYNC_PRESET_SWITCHES
774+
pthread_mutex_unlock(&preset_mutex);
775+
#endif
776+
std::cerr << "problem allocating target preset: " << e.message() << std::endl;
777+
return e.message();
778+
}
805779
// Set preset name here- event is not done because at the moment this function is oblivious to smooth/hard switches
806780
renderer->setPresetName(targetPreset->name());
807781
renderer->SetPipeline(targetPreset->pipeline());
808782

809783
#ifdef SYNC_PRESET_SWITCHES
810784
pthread_mutex_unlock(&preset_mutex);
811785
#endif
812-
}
786+
787+
return std::string();
788+
}
813789

814790
void projectM::setPresetLock ( bool isLocked )
815791
{

src/libprojectM/projectM.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class DLLEXPORT projectM
243243
/// Occurs when active preset has switched. Switched to index is returned
244244
virtual void presetSwitchedEvent(bool isHardCut, unsigned int index) const {};
245245
virtual void shuffleEnabledValueChanged(bool isEnabled) const {};
246+
virtual void presetSwitchFailedEvent(bool hardCut, unsigned int index, const std::string & message) const {};
247+
246248

247249
/// Occurs whenever preset rating has changed via changePresetRating() method
248250
virtual void presetRatingChanged(unsigned int index, int rating, PresetRatingType ratingType) const {};
@@ -320,7 +322,9 @@ class DLLEXPORT projectM
320322

321323
Pipeline* currentPipe;
322324

323-
void switchPreset(std::auto_ptr<Preset> & targetPreset);
325+
std::string switchPreset(std::auto_ptr<Preset> & targetPreset);
326+
void switchPreset(const bool hardCut);
327+
324328

325329

326330
};

src/projectM-qt/qprojectm.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ class QProjectM : public QObject, public projectM {
3535
presetSwitchedSignal(hardCut, index);
3636
}
3737

38+
void presetSwitchFailedEvent(bool hardCut, unsigned int index, const std::string & message) const {
39+
presetSwitchFailedSignal(hardCut, index, QString(message.c_str()));
40+
}
41+
3842
void presetRatingChanged(unsigned int index, int rating,
3943
PresetRatingType ratingType) const {
4044
presetRatingChangedSignal(index, rating, ratingType);
4145
}
4246

4347
signals:
4448
void presetSwitchedSignal(bool hardCut, unsigned int index) const;
49+
void presetSwitchFailedSignal(bool hardCut, unsigned int index, const QString & message) const;
4550
void presetRatingChangedSignal(unsigned int index, int rating,
4651
PresetRatingType ratingType) const;
4752

0 commit comments

Comments
 (0)