I have this method:
void schedule(){
switch (schedule.getType()) {
case MANUAL:
scheduleManual();
break;
case AUTO:
scheduleAuto();
break;
case NONE:
scheduleNone();
break;
default:
break;
}
}
And I'd like to know if there's a better design to have this instead of a switch.
void schedule(){
def method = 'schedule' + schedule.getType().toString().toLowerCase().capitalize()
this."$method"()
}
You could also use methodMissing() magic:
enum Type {MANUAL, AUTO, NONE}
schedule = Type.AUTO
def methodMissing(String name, args){
if( name in Type.values()*.toString() )
this."schedule${name.toLowerCase().capitalize()}"()
}
void schedule(){
this."$schedule"()
}
void scheduleManual(){ println "calling scheduleManual" }
void scheduleAuto(){ println "calling scheduleAuto" }
schedule()
Related
I'm trying to move playerSpaceShip by keyboard. The function is changing its position (checked in the console by getcoordinates()) but it's not displaying it on the screen(sprite is not moving). Anyone can figure out why?
player.cpp
void player::movePlayer(sf::Vector2f distance)
{
playerSpaceShipSprite.move(playerMoveSpeed * distance.x, playerMoveSpeed * distance.y);
}
void player::render(sf::RenderTarget* target)
{
target->draw(playerSpaceShipSprite);
}
game.cpp
void game::update()
{
while (window->isOpen())
{
while (window->pollEvent(sfEvent))
{
if (sfEvent.type == sf::Event::Closed)
window->close();
playerMovement(sfEvent);
}
}
}
void game::renderPlayer()
{
if (Player)
Player->render(window);
}
void game::render()
{
window->clear();
renderBackground();
renderPlayer();
renderBlueAlien();
window->display();
}
void game::playerMovement(sf::Event sfEvent)
{
if (sfEvent.type == sf::Event::EventType::KeyPressed)
{
switch (sfEvent.key.code)
{
case sf::Keyboard::Left:
{
Player->movePlayer(sf::Vector2f(-1,0));
std::cout << "Left key pressed\n";
break;
}
case sf::Keyboard::Right:
{
Player->movePlayer(sf::Vector2f(1, 0));
std::cout << "Right key pressed\n";
break;
}
case sf::Keyboard::Up:
{
Player->movePlayer(sf::Vector2f(0, -1));
std::cout << "Up key pressed\n";
break;
}
case sf::Keyboard::Down:
{
Player->movePlayer(sf::Vector2f(0, 1));
std::cout << "Down key pressed\n";
break;
}
case sf::Keyboard::Space:
{
std::cout << "Space key pressed\n";
break;
}
}
}
}
void game::run()
{
while (window->isOpen())
{
render();
update();
}
}
void game::update() has while (window->isOpen()) at the start.
This means that the update function never returns back to void game::run(), where your second while (window->isOpen()) statement is, and since it never returns to there, void game::render() never gets called again.
The player is moving, but the screen is not updating to show the player moving. Getting rid of while (window->isOpen()) in void game::update() solves the problem.
I solved this using a debugger, setting a breakpoint inside void player::movePlayer(sf::Vector2f distance), and stepping through the code until I came across the problem.
I am trying to override the default value of enum passed as a parameter, but I am getting the default value.
class MyClass
{
public:
enum MyCalculation
{
SUM,
DIVIDE,
MULTIPLY,
NOCALCULATION
};
void foo(MyCalculation cal = NOCALCULATION);
void DoCalculation();
};
void MyClass::DoCalculation()
{
MyCalculation cal;
// here I am getting the enum value at runtime
int val = // get enum value
switch(val)
{
case 0:
cal = MyClass::SUM;
break;
case 1:
cal = MyClass::DIVIDE;
break;
case 2:
cal = MyClass::MULTIPLY;
break;
}
foo(cal);
}
void MyClass::foo(MyCalculation getCal)
{
cout << getCal;
}
I tried passing by reference in DoCalculation method, but I am always getting NOCALCULATION in foo.
Do I need to do something else?
So lets say I have this enum
public enum StatType
{
Strength, Dexterity, Intelligence, Wisdom, Luck
}
and I have this function,
public void SetStats(StatType type, int value)
{
switch (type)
{
case StatType.Strength:
hero.Stats.Strength += value;
break;
case StatType.Dexterity:
hero.Stats.Dexterity += value;
break;
case StatType.Intelligence:
hero.Stats.Intelligence += value;
break;
case StatType.Wisdom:
hero.Stats.Wisdom += value;
break;
case StatType.Luck:
hero.Stats.Luck += value;
break;
}
}
How can I change this incoming stat's "type" parameter to hero.Stats.( ) variable's name?
If this is possible, then I don't need to use switch, right?
This is what I'd like to do:
class MyObject {
#Lazy volatile String test = {
//initalize with network access
}()
}
def my = new MyObject()
println my.test
//Should clear the property but throws groovy.lang.ReadOnlyPropertyException
my.test = null
//Should invoke a new initialization
println my.test
Unfortunately lazy fields are readonly fields in Groovy and clearing the property leads to an exception.
Any idea how to make a lazy field reinitializable without reimplementing the double checking logic provided by the #Lazy annotation?
UPDATE:
Considering soft=true (from the 1st answer) made me run a few tests:
class MyObject {
#Lazy() volatile String test = {
//initalize with network access
println 'init'
Thread.sleep(1000)
'test'
}()
}
def my = new MyObject()
//my.test = null
10.times { zahl ->
Thread.start {println "$zahl: $my.test"}
}
Will have the following output on my Groovy console after approx 1 sec:
init
0: test
7: test
6: test
1: test
8: test
4: test
9: test
3: test
5: test
2: test
This is as expected (and wanted). Now I add soft=trueand the result changes dramatically and it takes 10 seconds:
init
init
0: test
init
9: test
init
8: test
init
7: test
init
6: test
init
5: test
init
4: test
init
3: test
init
2: test
1: test
Maybe I'm doing the test wrong or soft=true destroys the caching effect completely. Any ideas?
Can't you use the soft attribute of Lazy, ie:
class MyObject {
#Lazy( soft=true ) volatile String test = {
//initalize with network access
}()
}
edit
With soft=true, the annotation generates a setter and a getter like so:
private volatile java.lang.ref.SoftReference $test
public java.lang.String getTest() {
java.lang.String res = $test?.get()
if ( res != null) {
return res
} else {
synchronized ( this ) {
if ( res != null) {
return res
} else {
res = {
}.call()
$test = new java.lang.ref.SoftReference( res )
return res
}
}
}
}
public void setTest(java.lang.String value) {
if ( value != null) {
$test = new java.lang.ref.SoftReference( value )
} else {
$test = null
}
}
Without soft=true, you don't get a setter
private volatile java.lang.String $test
public java.lang.String getTest() {
java.lang.Object $test_local = $test
if ( $test_local != null) {
return $test_local
} else {
synchronized ( this ) {
if ( $test != null) {
return $test
} else {
return $test = {
}.call()
}
}
}
}
So the variable is read-only. Not currently sure if this is intentional, or a side-effect of using soft=true though...
Edit #2
This looks like it might be a bug in the implementation of Lazy with soft=true
If we change the getter to:
public java.lang.String getTest() {
java.lang.String res = $test?.get()
if( res != null ) {
return res
} else {
synchronized( this ) {
// Get the reference again rather than just check the existing res
res = $test?.get()
if( res != null ) {
return res
} else {
res = {
println 'init'
Thread.sleep(1000)
'test'
}.call()
$test = new java.lang.ref.SoftReference<String>( res )
return res
}
}
}
}
I think it's working... I'll work on a bugfix
I'm using EMDK 2.5 (VS2008 and VC# and .NetCF3.5) Barcode2 class from the library to write a sample application to scan bar codes. I followed the samples available in EMDK namely CS_Barcode2Sample1 project.Every time I hardware trigger the scan the notification "E_SCN_READINCOMPATIBLE" is thrown and not able to retrieve the scanned data. The documentation doesn't say much about the cause of E_SCN_READINCOMPATIBLE notification and no luck from Google search. I tried several options including making use of Symbol.Barcode and the outcome is same.
I also tried EMDK 2.3 but the result is same.
I've pasted the whole code here....
public partial class Form1 : Form
{
private Barcode2 myBarcode2 = null;
public Form1()
{
InitializeComponent();
InitBarcode();
}
public bool InitBarcode()
{
// If the Barcode2 object is already initialized then fail the initialization.
if (myBarcode2 != null)
{
return false;
}
else // Else initialize the reader.
{
try
{
Symbol.Barcode2.Device[] AvailableDevices = Symbol.Barcode2.Devices.SupportedDevices;
if (AvailableDevices.Length == 0)
{
return false;
}
if (AvailableDevices.Length == 1)
{
//get the first available scanner in the list
Symbol.Barcode2.Device MyDevice = AvailableDevices[0];
// Create the reader, based on selected device.
myBarcode2 = new Barcode2(MyDevice);
// Attach a scan notification handler.
//this.myScanNotifyHandler = new Barcode2.OnScanHandler(myBarcode2_ScanNotify);
myBarcode2.OnScan += myBarcode2_ScanNotify;
// Attach a status notification handler.
//this.myStatusNotifyHandler = new Barcode2.OnStatusHandler(myBarcode2_StatusNotify);
myBarcode2.OnStatus += myBarcode2_StatusNotify;
myBarcode2.Config.TriggerMode = TRIGGERMODES.HARD;
// Submit a scan.
myBarcode2.Scan(5000);
}
}
catch (OperationFailureException ex)
{
MessageBox.Show("Exception Raised 1");
return false;
}
catch (InvalidRequestException ex)
{
MessageBox.Show("Exception Raised 2");
return false;
}
catch (InvalidIndexerException ex)
{
MessageBox.Show("Exception Raised 3");
return false;
}
}
return false;
}
private void myBarcode2_ScanNotify(ScanDataCollection scanDataCollection)
{
// Checks if the BeginInvoke method is required because the OnScan delegate is called by a different thread
if (this.InvokeRequired)
{
// Executes the OnScan delegate asynchronously on the main thread
this.BeginInvoke(new Barcode2.OnScanHandler(myBarcode2_ScanNotify), new object[] { scanDataCollection });
}
else
{
// Get ScanData
ScanData scanData = scanDataCollection.GetFirst;
int i;
switch (scanData.Result)
{
case Symbol.Barcode2.Results.SUCCESS:
String str = scanData.Text;
myBarcode2.Config.TriggerMode = TRIGGERMODES.HARD;
myBarcode2.Scan(5000);
break;
case Symbol.Barcode2.Results.E_SCN_READTIMEOUT:
break;
case Symbol.Barcode2.Results.CANCELED:
break;
case Symbol.Barcode2.Results.E_SCN_DEVICEFAILURE:
i = 93;
break;
default:
if (scanData.Result == Symbol.Barcode2.Results.E_SCN_READINCOMPATIBLE)
{
// If the failure is E_SCN_READINCOMPATIBLE, exit the application.
MessageBox.Show("Fatal Error");
this.Close();
return;
}
break;
}
}
}
private void myBarcode2_StatusNotify(StatusData statusData)
{
// Checks if the Invoke method is required because the OnStatus delegate is called by a different thread
if (this.InvokeRequired)
{
// Executes the OnStatus delegate on the main thread
this.Invoke(new Barcode2.OnStatusHandler(myBarcode2_StatusNotify), new object[] { statusData });
}
else
{
int i;
switch (statusData.State)
{
case States.IDLE:
break;
case States.READY:
break;
default:
break;
}
}
}
}
}
I've went thru this recently also, as I observed, it probably due to the scanner device is occupied by other application, where the scan request has been queued already, you can go to memory management, and kill the suspect app, and try your app again.
Refer to the Symbol FAQ