What is this "internal state" people talk about all the time precisely? The term really irritates me. The internet couldn't provide me with a definition yet.
From Object-Oriented Analysis and Design with Applications
The state of an object encompasses all of the (usually static)
properties of the object plus the current (usually dynamic) values of
each of these properties
In object oriented programming the objects can have state (data) and behavior (function).
The behavior specifies what the object can do, and it is usually conditioned by its state.
The state can be represented by any member or static variable, and it will depend of the definition of the class the object is instance of.
Update: The internal state refers to those private variables that affect the behavior of the object but are not visible from the outside world.
For example, let's say you have an HTTP client having the following interface:
class HttpClient {
public:
HttpClient(std::string host);
HttpResponse get(std::string path);
HttpResponse post(std::string path);
};
This object might have a getter for host but none for the current connection state.
A good optimization might be to keep the connection alive between requests (assuming the server allows it) so, in the first call to get or post the object will have to establish the connection and save the socket description in some internal variable that is not exposed to the user. The next time get or post is called the connection is already established (and the user has no idea).
In this case, the connection is part of the internal state of the object.
What is your internal state?
Hungry, Thirst,
Put some variables on that.
So in OO terms.
My state is
drinks-requirement: two glass of water,
food-requirement: sandwich
So the same concept applies in terms of an object. The sum total of the variables of anobject
Building off of what #AdamBurry said, think of an object as a black box that another piece of code can use. That code instantiates it:
Order o = new Order();
Then the code asks for the object to modify itself:
OrderItem oi = new OrderItem("Widget", 5.5);
o.AddItemToOrder(oi);
Then the code asks for the object to do something.
o.GetTotal();
How is the order computing the new total, given the item that just got added? Does it have a list of OrderItems, complete with prices? You bet. It has internal details that the code calling into may have no way of getting to. Those black-boxy details that the object needs to very carefully keep track of are the internal state of the object.
A much more practical example of something you may never want to expose to the "outside" world are variables which maintain the "dirty" state of an object. Has it been modified, but not committed to the database, yet? External code should never need to know this, but the object may need to.
What about an object that lets you step forward or backward through a list? Somewhere in that object, there's going to be an internal state variable that acts as a pointer to the current record. Again, the calling code would never need to see this, but when the code calls the .MoveNext() method, the object is going to have to increment that pointer by one to maintain the state of where it is in the list.
The internal state of an object is the set of all its attributes' values. One particular aspect of the internal state is that a method applied to the object being in a defined internal state (= a specific set of all its attributes' value) will result in another, also defined (and reproducible) internal state.
This aspect is important when you somehow record system states that you want to replay afterwards in a simulation of the recorded system. By recording the original internal state of an object you are able to reproduce all its subsequent internal states by simply calling its methods without having to store any additional data. However this is easier said than done in practice...
Applied to C++ the internal state will not be altered by a const method.
A mutable attribute (= attribute modifiable by a const method) can be altered without semantically affecting the internal state of an object. At least this is the contract the developer goes for when he uses this modifier...
Related
I just started learning about design patterns, and I'm having trouble with some should-be-simple concepts. The concepts of some of these patterns make sense, but I'm struggling with how I should implement them in C++.
Let's saying I'm working on a problem that implements an observer problem. Let's assume there is only a single observer. That leaves two objects that need to communicate: the subject and the observer.
Conceptually, what this pattern is attempting to do is very simple to understand. However, I'm getting bogged down by questions like: where do they objects live? Must they both live within some container? How do they actually make requests from one another?
Regarding that last question, is it necessary for each object to a have a data member that references the other object? As in, must the subject contain a pointer to the observer, and must the observer contain a pointer to the subject?
So stepping away from the observer. If I have any two objects that are dependent to each other (uni-directionally or bi-directionally), is it necessary that each object have a pointer to its respective object?
Thank you,
A typical high-level run-time polymorphic implementation of the observer pattern has the observable object add a data member such as std::vector<Observer*> observers_;, and when something of interest happens the observable's member function iterates over that observers_ calling some function through the Observer*s. There's not necessarily any need for the Observers to keep pointers/references to the observable object, but if it's useful they may do so, or the callbacks from the observable object might pass the this pointer or a reference to *this as a parameter. Simpler observables might only support one Observer* instead of a container thereof.
For lower-level / performance-critical code - when it's practical to do so because you know the types involved at compile time - you may prefer to stipulate one or more observers at compile time - perhaps as template arguments. That can allow the dispatch to be inlined and optimised, dead-code elimination to avoid calls to observers that do nothing etc..
where do they objects live?
Anywhere that makes sense for the object's general function in the program. For example, if a Database_Server_Connection was observable, it might let other parts of the program register interest in knowing when the connection's established asynchronously, when the connection's dropped, when async requests complete, when the database connection is closed by program code, when the database observable object's destructor runs. The observers could be anywhere else in the program - they might be local objects in some function's scope, possibly in another thread, or they might be in or managed by smart pointers in a static or dynamically allocated container.
Must they both live within some container?
Nope - as above.
How do they actually make requests from one another?
Firstly, the code adding observers needs access to the observable object, and normally calls something like observable.add_observer(this); to register themselves, taking care to call observable.remove_observer(this); in their destructor so the observable never accidentally attempts a call into an already "destructed" object. The callbacks then happen using the pointers stashed by add_observer. As above, the callbacks may be passed pointers or references to the observable as arguments, the observers might have stashed away a pointer or reference earlier, or they might not even need one if all the information they need is passed to the callback and they don't need to mutate (call a non-const function on) the observable.
So stepping away from the observer. If I have any two objects that are dependent to each other (uni-directionally or bi-directionally), is it necessary that each object have a pointer to its respective object?
It's often easiest, but sometimes some other communications mechanism may be used such as a queue or socket, in which case the communicating parties need some manner of pointer/reference/handle/id for that communications mechanism rather than pointers to each other.
Another method you can use to make objects communicate is through an intermediate Queue object, or a socket, or other type of shared memory, so storing a pointer to the other object is not always necessary. In fact, to improve decoupling and write general code it is often better to use an event Queue or a Signal (see design of QT Libraries).
Don't take that as meaning that storing a pointer is wrong: it is often a good solution and avoid over-engineering which is expensive (in terms of money, time, and other computing resources).
Is it possible that luabind checks, if a member function call to an exported class (object) is for a valid object?
lets assume that i have a Class called Actor exposed using luabind to lua. Im calling a lua function from C++ with an actor object as parameter. Now before the function finishes, a script write would put the actor object in a global lua reference to be accessed later.
Later on, the actor object is deleted from the C++ site, another function is called which tries to access the invalidated actor object (any method from it) - and obviously since it has been deleted, it results in a crash (access violation)
sample:
local myObjRef = nil
function doSomethingWithActor(actor)
-- save, still valid object
actor:Say("hello")
myObjRef = actor
end
function calledAfterActorWasDeleted()
--- will crash if the c++ object has been deleted meanwhile, works fine if it still exists
myObjRef:Say("Crash...")
end
A NIL check doesnt help here, is this something that can be checked on luabinds site? The functions are executed using lua_pcall(....) and the stacktrace shows the error at luabinds call.hpp results = maybe_yield(L, lua_gettop(L) - arguments, (Policies*)0);
If not, is there another solution how to make sure somebody who writes a script cannot create these issues?
Now before the function finishes, a script write would put the actor object in a global lua reference to be accessed later.
That right there is where your problem is coming from. If you want Lua code to own the object (that is, preserve the existence of this object), then you need to use Luabind mechanics to tell Luabind that you want to do that. Otherwise, if you pass a pointer to some Lua function, Luabind will assume that the function will not be trying to gain ownership of it.
If you want ownership to be shared between Lua and Luabind, then you should wrap your objects in a boost::shared_ptr, and use Luabind's smart pointer mechanisms to do this.
You could also simply segregate your scripts better. If you have some script that operates on a particular actor, then that script and any functions it contains should be destroyed (ie: lose all references to it) along with the object. This requires proper coding discipline on the C++ side. It will also require that you use Lua environments to properly encapsulate each instance of a script, so that they can't sneak things out via globals. Lastly, you will need to have C++ maintain total control over when scripts are called and when they aren't.
Otherwise, ownership is something your scripters are simply going to have to know about and be careful of. They can't treat C++ parameters like any old Lua value.
If exercising disciplined programming practice is not possible or practical for you, then you will simply have to not pass Lua the actual C++ object. Instead, you need to pass Lua some proxy object, which is a reference to the original. boost::weak_ptr is a good example of such an object (though you wouldn't pass it exactly to Lua). The proxy would forward calls to the actual object. If the object has been deleted, the proxy would detect this and fail or do nothing or whatever.
I solved my issue the following way:
When im about to delete an object, i iterate through all lua functions from C++ (i have them in a list, they are bound to specific actor objects each). Then i inspect each upvalue (global/local vars accessable to a function) - then i compare the userdata pointer with my object im about to delete - if they match (and their classes) and NIL the upvalue. Optionally, i could just remove that offending function because it would not work well anymore anyway.
So the next the time the function is called, im just getting a soft lua error "trying to access xxx a nil value..." - no more access violations.
I know people would say "dont use lua_getupvalue/lua_setupvalue - they are only for debugging!" - but there is actually no documented or spoken side effect - and in my case its perfectly safe and works well - also there isnt the issue with left over proxy objects i could not delete.
I'm writing a relatively big project in C++ and have a problem with object deletion. The project, to be precise is a roguelike game.
I have a class Npc which is every monster in the game. They are created and stored in a separate class, Storage<Npc>, that is responsible for their management (loading, saving, creation, deletion, etc). Whenever a monster dies, corresponding object npc has to be deleted and destroyed completely. It is not a problem to delete object itself, I just have invoke a method from Storage<NPC>. The problem is that code contains a lot of pointers to this already-dead npc, which are now invalid and trying to use them will cause a lot of issues. For example:
There may be an action he intended to perform before he died.
Tile on which he stood record store a pointer to him.
He may have been involved in some continuous activities, like grappling somebody.
There are a lot of such pointers in the code, so it is nearly impossible to simply track them. What I need is some way to determine that an npc is already dead, and there is no actual object stored on that address, so that parts of code which still have this pointer can adequately react to his death.
I myself have come up with several ideas, but so far none of them seems really good to me:
I could ask Storage<NPC> class if it has an object on such address. The potential problem is that after object deletion, another object may be allocated on the same address, which will cause bugs.
I could notify all locations that could possibly use the invalid pointer. It is a bad idea because number of such locations will increase over time and doing this is a pain.
I could implement some version of smart pointer, but I'm unsure on which one to use.
tl;dr version: I need a solution that will tell me if a pointer points to an object, or it points to a free chunk of memory, or to some other object, allocated after original object deletion.
With the information you provided, what I can suggest is you implement the Observer Pattern.
If there is code that needs to react to the NPC's death, this pattern is the way to go. Code sections having pointer references to your NPC will be notified upon NPC death and null their copy of pointer to NPC and react to the NPC's death however required. The death notification is sent to all observers before the NPC is actually deleted.
With this pattern, you could implement mechanics such as "Hero gains 50 HP for each monster killed", and it's easily scalable.
You can also use Kevin Ballard's suggestion of using shared_ptr if no code needs to actively react to the NPC's death, and just needs to handle the case where the NPC is dead.
How about using weak pointers? If you store the Npc in a std::shared_ptr (C++11, use std::tr1::shared_ptr for C++03), you can then create std::weak_ptrs (C++11 again, use std::tr1::weak_ptr for C++03) that refer to the shared_ptr. When the shared_ptr actually deletes its object, then all the weak_ptrs will be able to figure this out.
Although I have to wonder why you're deleting Npcs that are still being used elsewhere (e.g. that still have actions). If instead of trying to have all these other references discover you've deleted the Npc, you just want the Npc to die once all references disappear, then using a shared_ptr by itself (with no weak_ptr) will work correctly.
One option is to include a reference count in your class. When some other object (a room, for example) is going to hold a pointer to an npc, the room has the responsibility of increasing the npc's reference count. Now, instead of just deleting a dead npc, you flag it as dead (through another new data member if you don't already have the right flag), and only delete if its reference count is zero. The room object also has the responsibility of periodically checking that flag and if it learns the npc is dead, it decrements its reference count (which will cause a post mortem deletion, if the count is now zero).
Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++.
Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time.
I cannot see any reason who you would want to do this,
Thanks,
(No is an acceptable answer!!!)
It depends on what you really want.
If you need the same object every time you have to keep a pointer to it. If you need a new object every time (for whatever reason) you have to create a new instance each time. If you don't care keeping the object is preferable because calls to CoCreateInstance() are relatively expensive.
There is no general rule in this case because there are a number of variables that decide whether it is a good idea or not.
First: If you own the COM objects in question i.e. have source code and control over how they are used, then yes its perfectly safe.
If COM objects are 3rd party COM objects sometimes crappy code in them may force you to "createInstance" on them every time you use them - out of necessity (and self preservation).
If the COM object is acting as a proxy object you may need to create them every time you use them because of stuff behind the scene i.e. other clients using the same object.
there are more situations, but to summarize: it depends...
I would say it depends on what the COM object is and how you use it. It is generally fine to reuse an ADO connection, but if you leave it in a dirty state then you may encounter odd behavior when you reuse it. Some COM object may have a re-initialize or clear method you can call to reset them back to a clean state.
I'm writing a game and an accompanying engine in C++. The engine relies heavily on automation using a simple embedded scripting language. Scripts can create object classes, define event listeners on them, and produce instances from them. At present, an instance must be bound to a script-global identifier in order to preserve its existence. The obvious result of this is that there can be no anonymous objects, which will be by far the most common.
At present, instances are managed using a std::set<Instance*, spatial_sort>, where spatial_sort is a functor that sorts instances by position, for rendering and collision detection. Instances are removed and re-inserted each frame using their current position as a hint, under the assumption that they're not likely to move a whole lot in a fiftieth of a second. If a dead flag is set in the instance, it is erased from the set. The Instance constructors and destructor invoke insert(this) and erase(this), respectively.
In order to allow anonymous instances, I want to change the set to a std::set<boost::shared_ptr<Instance>, spatial_sort>, which would allow Instance to share ownership of instances and preserve their existence until they destroy themselves. Unfortunately, because the calls to insert() need to be placed in the constructor, shared_from_this() won't work for obtaining a shared_ptr to the Instance. It doesn't matter at all that Instance happens to already inherit from boost::enable_shared_from_this<> via its base class.
Can anyone recommend a suitable workaround?
Edit:
I did what I should have been doing in the first place, and split the behaviour of the Instance class into two classes: Instance and Reference. The expression new SomeClass in a script then returns a Reference to a new Instance. The Instance objects themselves are never managed using a shared_ptr, so they are responsible for committing suicide in response to a suitable event, e.g., end of animation, end of level, etc.
Thanks for the help! Refactoring is as good a solution as any if it Just Works.
You could add a static method to Instance that you then use to create new objects and that also does the administrative stuff like adding it to the set:
static Instance* create(int something) {
boost::shared_ptr<Instance> sptr(new Instance(something));
instanceset.insert(sptr);
return sptr.get();
}
If you want to make this the only way to construct an object of this class you could also make the normal constructor private or protected.
For more on this see also the C++ FAQ Lite entry about "Dynamic binding during initialization", which is not directly related but uses the same technique to work around the restrictions on the use of virtual functions in constructors.