What we mean when we say that a class is loaded? - classloader

That is :
What is actually loaded. The logic or the class file or some metadata related to the file.

It would usually mean that the logic and properties of a class have loaded, but depending on the context, it could mean other things too.

From a class loader implementation perspective, class loading has completed when calling ClassLoader.defineClass(...). I.e. when the class is made known to the JVM.

Related

C++ how to implement settings of different types that are serializable/deserializable

I am trying to implement a system where I have a template class which implements a Serializable interface.
Right now, the interface contains serialize/deserialize methods while the template Setting class has get/set, and private members settingName, settingValue and a template function T adaptType() to adapt string to correct type using the >> operator (https://gist.github.com/mark-d-holmberg/862733). The file also contains a custom struct with << and >> operators overloaded for everything to work.
Settings are serialized in form of settingName:settingValue or settingName:val1;val2;val3 in case of the struct.
There are two problems I'm facing with this design:
I want to hold all these setting objects in a map<string, ISerializable*(?)> to access them but then I can't call other functions get/set on these objects because the interface doesn't define the methods (they must be in Setting class because their type depends on the template type), if I switch the second type in map to template class, I must define a concrete type
When deserializing there's no way to know which type it is and ISerializable can't be instantiated since it's an abstract class, again I need to know which type I'm deserializing and instantiate the Setting class with appropriate type
Is there a better way to design this or something I'm missing, note that I am not very proficient with c++
Bit of a background for the problem:
I'm working on an embedded device where settings need to be loaded/saved to flash memory but there's also another framework running on the device which holds these settings in RAM and serves them on a webserver to be edited, I cannot change this part. My goal is to manually save these settings to my custom class that manages settings and save/load to flash so it is persistent between reboots and synced with the web framework.
Any help or advice is welcome
As far as I understand your problem it is similar to what the Oops library is doing. It creates a Type Descriptor class for all types having similar functionality: giving some description of the type and provide text and binary serialization for them. It is also designed to handle configuration, so it even may help to solve your problem.
It worth to have a look at least for getting some ideas: https://bitbucket.org/barczpe/oops

Level Editor Import Objects (Classes)

i was working on making my own game tools in c++ to expand my knowledge on problem solving. But i was stuck thinking on the design of my level editor. I want to be able to import any header that by default will be assumed to contain one class and instantiate an object from it. Now i don't know how to allocate those custom classes on the scene as each class is considered different from the others so i can't use a vector to put them all there.
Now i wanted to know whether making a base class like EmptyGameObject and then making each class inherit from it will solve the problem.
But then i encountered another problem.
How do we construct a class depending on its name ?
like:
class name : CustomClass, imported from custom_class.h
thanks you for your help in advance.

Inheritance is called object code level reuse?

In object oriented programming class the instructor is telling that
Inheritance is called Object code level reuse.
But I was thinking how because We don't have the source code is not available in object or any other compiled library file so how can we inherit a function or class for reuse if We don't have the source code.
Normally we inherit a class whose source code or class information is available.
Can somebody explain it?
The other question need the explanation is :
how container-ship is different than inheritance?
Because I read that container-ship is a derive which has a relation-ship from base class as "has a relation", which I feel is inheritance only.
Any explanation is appreciated.
Thanks.

Consuming a C++ class reference from an external "plugin" library that doesn't have access to the class implementation

I'm using the Boost.DLL library for my project in order to offload its functionality into a suite of pluggable modules. The core application provides headers defining various structs and classes that will be used both within the core application, and which will (in some cases) be passed to plugins when they need to perform various tasks.
One of the initial things I need to do is pass a class ServiceCatalog to each plugin's initialization method, to allow that plugin to register various capabilities with the core application. I've grouped the definitions I'd like to share with my plugins into a header called core.h, and I'm referencing this header both in more application and in my plugin projects.
ServiceCatalog has a number of larger functions that, to me, make sense to offload into a cpp file, rather than trying to bundle them all into the header file. I'd still like the plugin to know the "shape" of the class though, because, while I don't need it to instantiate the class, I need it to be able to call instance methods.
However because I'm referencing the header file in the plugin, but not the cpp file containing the implementation, I get "unresolved external symbol" errors when linking. This makes sense on a basic level of course (i.e. what would happen if the plugin tried to instantiate something it doesn't have access to the implementation for?) but there doesn't seem to be any way (that I can find) to declare that the class is, I guess, "extern" or something?
Do I literally need to use a base abstract class to achieve this? Such a solution feels a little convoluted. I guess I could replace my class with a struct containing some function pointers instead.
What approach do you recommend? In a nutshell, I want my core application to be able to pass instances of things to my plugins, where each thing has a number of related methods that can be called.
// core.h, "main.exe"
class ServiceCatalog
{
public:
void complex_method_with_external_implementation();
}
//plugin.cpp, "plugin.dll"
#include "core.h"
void register_plugin(ServiceCatalog* catalog)
{
catalog->complex_method_with_external_implementation();
}
If I'm understanding this right, the ServiceCatalog object is only created by the "main.exe" program, and not in any of the plugins. If that's the case, you can declare the functions that can be called by the plugins as virtual.
class ServiceCatalog
{
public:
virtual void complex_method_with_external_implementation();
}
Then all of the reference to them will be in the vtable, referenced by the constructor. The plugins will access the functions thru the vtable so they don't need any sort of export.
There are issues with versioning, as adding new 'exports' needs to be done in such a way not to mess up the existing layout of the vtable (you need to add them after all existing virtual functions, not insert them in between two existing virtual functions), and you can't remove any, either, without replacing them with something else.

Module and classes handling (dynamic linking)

Run into a bit of an issue, and I'm looking for the best solution concept/theory.
I have a system that needs to use objects. Each object that the system uses has a known interface, likely implemented as an abstract class. The interfaces are known at build time, and will not change. The exact implementation to be used will vary and I have no idea ahead of time what module will be providing it. The only guarantee is that they will provide the interface. The class name and module (DLL) come from a config file or may be changed programmatically.
Now, I have all that set up at the moment using a relatively simple system, set up something like so (rewritten pseudo-code, just to show the basics):
struct ClassID
{
Module * module;
int number;
};
class Module
{
HMODULE module;
function<void * (int)> * createfunc;
static Module * Load(String filename);
IObject * CreateClass(int number)
{
return createfunc(number);
}
};
class ModuleManager
{
bool LoadModule(String filename);
IObject * CreateClass(String classname)
{
ClassID class = AvailableClasses.find(classname);
return class.module->CreateObject(class.number);
}
vector<Module*> LoadedModules;
map<String, ClassID> AvailableClasses;
};
Modules have a few exported functions to give the number of classes they provide and the names/IDs of those, which are then stored. All classes derive from IObject, which has a virtual destructor, stores the source module and has some methods to get the class' ID, what interface it implements and such.
The only issue with this is each module has to be manually loaded somewhere (listed in the config file, at the moment). I would like to avoid doing this explicitly (outside of the ModuleManager, inside that I'm not really concerned as to how it's implemented).
I would like to have a similar system without having to handle loading the modules, just create an object and (once it's all set up) it magically appears.
I believe this is similar to what COM is intended to do, in some ways. I looked into the COM system briefly, but it appears to be overkill beyond belief. I only need the classes known within my system and don't need all the other features it handles, just implementations of interfaces coming from somewhere.
My other idea is to use the registry and keep a key with all the known/registered classes and their source modules and numbers, so I can just look them up and it will appear that Manager::CreateClass finds and makes the object magically. This seems like a viable solution, but I'm not sure if it's optimal or if I'm reinventing something.
So, after all that, my question is: How to handle this? Is there an existing technology, if not, how best to set it up myself? Are there any gotchas that I should be looking out for?
COM very likely is what you want. It is very broad but you don't need to use all the functionality. For example, you don't need to require participants to register GUIDs, you can define your own mechanism for creating instances of interfaces. There are a number of templates and other mechanisms to make it easy to create COM interfaces. What's more, since it is a standard, it is easy to document the requirements.
One very important thing to bear in mind is that importing/exporting C++ objects requires all participants to be using the same compiler. If you think that ever could be a problem to you then you should use COM. If you are happy to accept that restriction then you can carry on as you are.
I don't know if any technology exists to do this.
I do know that I worked with a system very similar to this. We used XML files to describe the various classes that different modules made available. Our equivalent of ModuleManager would parse the xml files to determine what to create for the user at run time based on the class name they provided and the configuration of the system. (Requesting an object that implemented interface 'I' could give back any of objects 'A', 'B' or 'C' depending on how the system was configured.)
The big gotcha we found was that the system was very brittle and at times hard to debug/understand. Just reading through the code, it was often near impossible to see what concrete class was being instantiated. We also found that maintaining the XML created more bugs and overhead than expected.
If I was to do this again, I would keep the design pattern of exposing classes from DLL's through interfaces, but I would not try to build a central registry of classes, nor would I derive everything from a base class such as IObject.
I would instead make each module responsible for exposing its own factory functions(s) to instantiate objects.