Page 1 of 1

Value of States

Posted: 22 Apr 2009, 11:58
by alenhardt
Hi Community!

I am encountering problems while trying to fetch the value of states in my application.
A brief look into the States.h file reveals that Value() is a private method and only a few friend classes can access it.
So my question is, if every class is allowed to MODIFY a state, why isnt every allowed to READ the state?
I'm sure there are ways to acces the value but i just cant find it. Anyone able to help me there?

So far i tried :

int my_value = State("TheState"); // doesnt work since it returns a StateRef

int my_val = State("TheState")->Value() // Now its a state but Value() is private :-(

The reverse works fine, e.g.:
State("TheState") = my_value; // this way its implemented in StateRef, no getter method though

And on a sidenote, where is "State(const char*)" defined/implemented? Its not in States.h

best
Alex

States ...

Posted: 22 Apr 2009, 13:35
by gschalk
Alex,

You should definitely be able to read from states. For example, if you check out BCI2000/src/core/Application/FeedbackDemo/FeedbackDemoTask.cppm (or any other existing BCI2000 module), you will see extensive use of State ( "StateName" ) constructs to read from states.

I think access to states is prohibited in certain functions, e.g., the filter's constructor. At the very least anything in Process() should have access to states.

Gerv

Posted: 23 Apr 2009, 03:51
by alenhardt
Well, I think figured out why it didnt work. It was rather confusing to see something instantiated with the name State(const char*) which wasnt defined in State.h and not in the State class itself.

I looked at the example code you pointed me at and there is only one line that is comparable to my case (State must be interpreted in a number context):

Code: Select all

mpTarget->Top = mpTarget->Height * ( State( "TargetCode" ) - 1 );
Since the State class itself doesnt implement any casting operator or - operator, I assume that this State(...) expression gets interpreted as

Code: Select all

double Expression::State( const char* inName )
.
At least this would explain why my int val = State("blabla") didnt work, because there is no method which return an int so the the term can not be evaluated.

Still the question for me remains, if I instantiate a State object (which obviously has a Value() method) why is it made private? Write access is public though, doesnt make sense to me.

This is what i mean (non-relevant lines removed):

Code: Select all

class State
{
  friend class StateVector; // calls SetLocation(), GetValue(), Commit()
  friend class StateList;   // calls GetValue()
  friend class CoreModule;  // calls GetValue()

 public:
  typedef unsigned long ValueType;

 public:
  State();
  ~State() {}

  State& AssignValue( const State& s )
      { return SetValue( s.Value() ); }
  State& SetValue( ValueType );

 private:
  ValueType Value() const
            { return mValue; }

...
};
My lack of deeper understanding of your code structure makes it still difficult to understand why this "magical" interpretation occurs. So bear with me if i ask stupid questions :-)


Alex

Posted: 24 Apr 2009, 08:02
by mellinger
Hi,

the State("Name") construct is intended to make access to states easier, and filter code more readable.

What happens is that State("Name") returns an object of type StateRef, which in turn has conversion operators defined for conversion into numeric types, and assignment operators for conversion from numeric types.

For the State("Name") construct to be available, your code needs to be in a class that inherits from the EnvironmentBase or Environment classes directly, or indirectly via the GenericFilter class.

For more details about access to parameters and states, please see
http://www.bci2000.org/wiki/index.php/P ... ment_Class.

Regards,
Juergen