As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I need to IMPLEMENT(not to use some library/open source) an event/message system.
I have the following restrictions:
It must be fast. It will be use for games and speed is the main restriction. I think I can't create/delete message/event classes every time a new message/event is sent even if I use custom allocators for that.
I must be able to predict when a messages/event sent/created will be received.
It must be easy to use. Doesn't matter how complicated the implementations of the system will be, the programmer that uses it must have an easy to use interface.
I will prefer to avoid giant switches like on Windows messages, but I also want to avoid overriding a class for only one function - the event handler or something like this. I think something like the MFC style would be nice.
It must be able to handle lots (maybe 1000/frame at 60 frames/second, don't know exactly this) of messages/events without performance issues.
It can't use compilers hacks that are not available on other platforms. It must be portable. I will use C++ for implementation.
Any architecture/design/link/book that you think is suitable for/might help this would be highly appreciated. Thanks!
Let me address your points one by one:
It must be fast. It will be use for games and speed is the main
restriction. I think I can't
create/delete message/event classes
every time a new message/event is sent
even if I use custom allocators for
that.
It would suffice and perhaps be even more efficient (it was for me in one project) to reuse and refill existing messages. No need for a custom allocator.
I must be able to predict when a
messages/event sent/created will be
received.
You can make predictions but normal networks (you want portability) will make your predictions sometimes a bit off and sometimes way off.
It must be easy to use. Doesn't matter
how complicated will be the
implementations of the system, the
programmer that uses it must have an
easy to use interface.
That should be possible, albeit this could cost you some extra effort. Error handling and special cases (platform, networking) come to mind.
I will prefer to avoid giant switches
like on Windows messages, but I also
want to avoid overriding a class for
only one function - the event handler
or something like this. I think
something like the MFC style would be
nice.
Avoiding manually written giant switches is a thing I 100% subscribe to.
It must be able to handle lots (maybe
1000/frame at 60 frames/second, don't
know exactly this) of messages/events
without performance issues.
If you take care during implementation, you should only be bounded by the network.
It can't use compilers hacks that are
not available on other platforms. It
must be portable. I will use C++ for
implementation.
Not even C++ is available on all platforms. Could you please list the platforms you are addressing?
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm using an API where I need to pass in a callback function, but it doesn't support any user defined parameter to pass an object pointer.
What are my options of recovering an object from my static / extern "C" callback function?
I'm using SetAbortProc(), which takes an HDC and passes through to the AbortProc() callback. Unfortunately, I don't see any way to associate further data with the HDC.
Solutions I can think of:
Use a global Cls *my_abort_object
Should work as only one print job can be active at a time. It seems a bit sloppy, but maybe that's just me?
Use a global std::map<HDC, Cls*>
Probably useless for me, as only one print job can be active. The global pointer solution is easier and doesn't have drawbacks in this case.
Use a singleton which encapsulates the whole aborting thing
Probably the sanest approach without too much work.
Use ATL style thunks
(This is basically runtime-generated code that calls real_callback(HARDCODED_OBJ_PTR, cb_arg1, cb_arg2, ...); That code passed in as callback function).
Would be very nice but hard to do on your own, problematic with data execution prevention, etc. Something a framework can do which you can't easily emulate.
I'm currently leaning towards the singleton solution as it seems the cleanest without too much overhead. I'd appreciate any suggestions!
Extra Info: C++ Win32 programming using MSVC Espress 2010
If you want a mildly robust solution I would go ahead and spring for a namespace-level API that wraps a singleton-esque map of HDC->class* relationships. Then you're good ot go if you ever need the capability in multiple HDC contexts. I would imagine the public API would be very similar to SetAbortProc() but it also creates and maps the corresponding handler object per HDC.
A close second since you only need one HDC is to just use a global pointer (with some sort of assertion/constness so it can't mutate) and rely on that being set when you get the abort callback.
It surprises me, because Microsoft are, IME, very good at giving userdata parameters. But, failing that, the optimal solution is to use LLVM. They offer simple and easy functions to JIT thunks for you.
It is sad to see APIs with callbacks that don't allow for user-data which is always limiting to other users. I would personally sway towards the use of a singleton type if it came to it though.
Possibly you should report this to the developers of the API though. You can also find that if you can access an object from the API within the callback there are sometimes other parts of their architecture you could poke your pointer/data into, asking the developers for help with this is the best bet in my opinion. You may even help them while helping yourself.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking to use Qt for a non-UI application. It has the potential to run on an appliance, but will start out on the desktop. The UI part (I know, I said non-UI) would be a web server with HTML(5)/AJAX.
I would really only use Qt for basic cross platform stuff like threads, synchronization, serialization, resources (strings, maybe images), internationalization, etc.
Which would be better for something like this, Qt or Boost and creating the cross platform layer myself?
Qt feels a little heavy for what I need, but I want to hear what experiences others have.
Yes, in my opinion it is perfectly OK. I wouldn't say Qt is heavy compared to Java, for example, which is extremely widely used for such tasks. Qt is very powerful, clean, easy and fast. I use it a lot, and I don't know any major drawbacks with it.
Yes, using QtCore (and other non-GUI modules) should do just what you need. As choosing between Boost and QtCore: both do good jobs and sometimes they interleave. But not always.
Qt(Core) offers mainly functionality. Boost offer mainly tools to achieve functionality. For example, you have templates and functors in Boost, not in Qt. OTOH, if you need message pumps and the like, you will only find those in Qt.
It really depends on what you are trying to achieve.
What you're proposing is perfectly reasonable.
You want to use a number of features (threading, etc. that you mention) across platforms.
Essentially you have a number of options, as follows:
Option 1 (Bad): Write your own cross-platform wrappers. You'd be reinventing the wheel, and you probably won't be able to tackle as many cross-platform cases and features as Qt already does. This option also means that whoever inherits your code will have to deal with your custom library instead of a well-supported and well-documented easily accessible library.
Option 2 (Not recommended): Use individual cross-platform solutions for every feature you want, like threading, networking, etc. This means that you (and your successor) will have to maintain compatibility with a large number of libraries in the future.
Option 3 (Recommended): Use a single, well documented, easily accessible library to meet all your needs. Qt fits the bill.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am developing a project in C++. I realised that my program is not OO.
I have a main.cpp, and several headers for different purposes. Each header is basically a collection of related functions with some global variables to retain data. I also have a windowing.h for managing windows. This contains the winMain() and winProc(). It calls the functions that resides in my main.cpp when events happen (like clicking a button) or when it needs information (like 'how big to make this window?'). These functions are declared in a seperate .h file included into windowing.h.
Is it worth changing this to be OO? Is it worth the work. Is there any better way I can construct the program without too many changes?
All feedback welcome, thankyou for taking the time to read this.
No, I think if it ain't broke, don't fix it.
Any windowing system is inherently OO to a degree. You have a handle to a window managed by the OS, and you can perform certain operations on it. Whether you use window->resize() or resize(window) is immaterial. There is clearly no value in such syntactic rearrangement.
However, as the application grows, you will likely find that many windows are mostly similar and subtly different. The best implementation is boilerplate basic functionality with special functions attached. And the way to do that is with a base class and polymorphism.
So, if you can refactor the program to be more elegant with OO, go for it. If it grows into the OO paradigm with natural evolution, follow best practices and let it be so. But don't just try to be buzzword-compliant.
Two things you need to think about: cost/benefit analysis and opportunity cost.
What is the cost of changing your code to be OO? What is the benefit? If the latter outweighs the former, then I'd tend towards changing it.
Costs include traditional costs such as time spent, money spent and so on. Benefits include a cleaner implementation, leading to easier maintenance in future. Whatever other costs and benefits there are depend really upon your own situation.
But one thing often overlooked is the opportunity cost. This is a cost that should be factored in to your analysis. It's an economic term meaning foregone opportunities.
In other words, if you do convert your code, your cost includes your inability to do something else with that time.
Classic example. If you do the conversion and a customer decides to not buy your software because you're not adding the feature they want, that lost sales opportunity is a cost.
It depends on what you want to accomplish with the project. If not using the OO features of C++ works for you and there are no good reasons to change, then keep going the way you're going. If, on the other hand, you would like to learn more about OOP and you have the time to apply to it, refactoring it to be more OO style will provide you with a great learning opportunity.
I would follow the best practices for working with whatever window manager you are using. Most use an OO style, which you'll automatically inherit (!) as you follow its usage patterns.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
I wont call myself a novice programmer, I have been working for a while now in c and c++ however I have never worked on something of my own. I think a bigger learning can be accomplished if one works on some project on there own apart from there work. Keeping this in mind, could you guys tell me some project I can implement? I recently learned posix threads so something I can do with that would be good. unfortunately I dont know anything about doing and making UI so I would like to avoid that.
You could write your own threadpool, that would be interesting, challenging, and wouldn't require much UI work.
You may not get a lot of threading exposure, but anything on http://projecteuler.net/ is recommended.
Optionally, you could make a program to draw and color the Mandelbrot Set. You could do that with multiple threads and it lends itself to extended features for a larger project as you desire.
Projects that meant a lot to me on the start were:
1) Small picture editing project. Various operations on the .bmp files like rotations, changing contrast, black and white conversions, convolution filters etc. You really see some results so it is kind of cool. You can easily add posix threads in that story.
2) API for B and B* trees.
3) Preemptive multithreaded OS kernel. Things like semaphores, signals, fork function etc. It can be really hard if you don't have mentor, but it is extremely useful.
4) Thread safe FAT16 file system.
5) Distributed image processing.
I did those projects during my first 2-3 years of "programming career" and those meant a lot to me, so you can try some of them.
Have you tried the free community-based FOSS advertisements? Those projects are all looking for contributors :)
You could try to code a VST Effect if you like music (MAO).
with a simple interface you could learn more about thread, dynamic plugin, real time constraints ...
Another idea could be a basic command line app hosting plugins each plugin implementing a simple action. To process different actions on data depending on the plugin you load.
Or implement a parser (or compiler) for basic MIPS for example.
Something I wish I had:
Design a data description language (ddl) that allows you to express some C (and/or whatever) types.
Write a program that, given specifications in the ddl, writes C (and/or whatever) code that :
defines the types in C (and/or whatever)
writes text representing instances of the types
reads text to generate instances of the types
frees instances of the types
Such a facility could be handy in other projects; one can spend a lot of time, over the years, producing such boiler plate by hand.
Further possibilities:
converts type instances to binary (e.g. for sending over the network)
converts binary to type instances
Then there is the tricky part: implement a facility for updating type specifications, so that for example if you have a file of revision a text type instances, you can read that with a rev-b program.
A few suggestions:
implement a HTTP and/or FTP server;
implement thread-safe containers and the associated test programs. It will be a good exercise to look for potential deadlocks and improving the locking strategy.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 14 years ago.
I always work in windows environment and most often to write the program in C/C++.
Is it necessary to a Windows Application Programmer to remember Win32 APIs as more
as possible??
Dazza
Well, I can't say it would hurt, but I think that it's better to remember how to quickly reference the API documentation instead of actually remebering the documentation itself. That mental currency (of which there is a limited amount, of course) could be better used remembering other things to make you a better developer in your environment.
You shouldn't worry about brute memorization. You probably want to understand the basics though such as message pumps, posting messages, resource files, posting to threads, and just the general gist of how to do things in Win32. You'll probably know this after writing your first Win32 program.
In general, though, concern yourself about learning the best way to do something when its needed. Keep good references around, such as
Programming Windows by Petzold
As a programmer you have so many other things to learn, ie your problem domain or other technologies you'll have to integrate with, that wasting time on brute memorization is usually a waste of resources and won't work as well as google or a good index in the back of a book.
Yes. Even if you use a framework like MFC or WTL, you still need to use Win32 calls for many things.
Depends, if you are doing heavy GUI work, knowing Win32 (with or without MFC) is going to benefit you.
OTOH If you are writing background tasks and services, I would segregate as much of the Win32 necessary functions away from the normal code in order to keep the code as portable as possible. Every place I've ever worked where there was a client/server setup (as it use to be called), as the product gained popularity/industry notice, there always came along a customer who was interested in the product, but not on Windows. If you have Win32 calls all over your code, porting is a pain.
Of course, this doesn't mean you shouldn't know the APIs. When in Rome.... If you are doing an app that needs to do a lot of I/O, you should be using the Win32 I/O completion API. I'm just saying hide that stuff whenever you can.
Short answer, Remember individual APIs? don't be silly. Be aware of many APIs, indeed.