How to convert GDI+ Status to string? - c++

Actually, the subject. I haven't found any standard way to convert GDI+ Status (error status returned by GDI+ methods) to string, something like FormatMessage()

If you want to convert labels in GDI+ Status into string, then the simplest thing you can do is this:
const char* StatusMsgMap[] =
{
"Ok", //StatusMsgMap[Ok] = "Ok";
"GenericError", //StatusMsgMap[GenericError] = "GenericError";
"InvalidParameter", //StatusMsgMap[InvalidParameter] = "InvalidParameter";
"OutOfMemory", //StatusMsgMap[OutOfMemory] = "OutOfMemory";
//so on
};
//Usage:
std::string error = StatusMsgMap[status]; // where status is Status type!
Or if you want more descriptive message, then this:
const char* StatusMsgMap[] =
{
"the method call was successful",
"there was an error on the method call, which is identified as something other than those defined by the other elements of this enumeration",
"one of the arguments passed to the method was not valid",
//so on
};
Since there are only 22 labels in the Status enum, creating a StatusMsgMap in the above way would not be much task, in my opinion. 5 minute is more than enough!

Here's something I wrote that uses hardcoded values.
std::string statusString(const Gdiplus::Status status) {
switch (status) {
case Gdiplus::Ok: return "Ok";
case Gdiplus::GenericError: return "GenericError";
case Gdiplus::InvalidParameter: return "InvalidParameter";
case Gdiplus::OutOfMemory: return "OutOfMemory";
case Gdiplus::ObjectBusy: return "ObjectBusy";
case Gdiplus::InsufficientBuffer: return "InsufficientBuffer";
case Gdiplus::NotImplemented: return "NotImplemented";
case Gdiplus::Win32Error: return "Win32Error";
case Gdiplus::Aborted: return "Aborted";
case Gdiplus::FileNotFound: return "FileNotFound";
case Gdiplus::ValueOverflow: return "ValueOverflow";
case Gdiplus::AccessDenied: return "AccessDenied";
case Gdiplus::UnknownImageFormat: return "UnknownImageFormat";
case Gdiplus::FontFamilyNotFound: return "FontFamilyNotFound";
case Gdiplus::FontStyleNotFound: return "FontStyleNotFound";
case Gdiplus::NotTrueTypeFont: return "NotTrueTypeFont";
case Gdiplus::UnsupportedGdiplusVersion: return "UnsupportedGdiplusVersion";
case Gdiplus::GdiplusNotInitialized: return "GdiplusNotInitialized";
case Gdiplus::PropertyNotFound: return "PropertyNotFound";
case Gdiplus::PropertyNotSupported: return "PropertyNotSupported";
default: return "Status Type Not Found.";
}
}
Use as follows:
const std::string statusMsg = statusString(theStatus);
//And then just output it.
std::cout << "Operation returned message: " << statusMsg << '\n';

Related

How to return QVariant type array

I am recently creating a model for qml with c++, but I face a problem when returning a QVariant type empty array. How should I define my return statement?
switch (role) {
case NameRole:
return QVariant(QStringLiteral("AAAAA"));
case LevelRole:
return QVariant(QStringLiteral("1"));
case ParentRole:
return QVariant(QStringLiteral("null"));
case SublevelRole:
return ???// I would like to return an empty array
}
Use QVariantList:
switch (role) {
case NameRole:
return QVariant(QStringLiteral("AAAAA"));
case LevelRole:
return QVariant(QStringLiteral("1"));
case ParentRole:
return QVariant(QStringLiteral("null"));
case SublevelRole:
return QVariantList();
}

C++ wrap multiple returns

I have the following code which returns ERROR in many lines:
bool func()
{
if (acondition)
{
return 0;
}
return 1;
}
int cmdfun()
{
other_funcs;
if (func()) return ERROR#NUMBER;
other_funcs;
if (func()) return ERROR#NUMBER;
}
But I found its becoming longer and longer. How can I encapsulate return ERROR#NUMBER into func() also? Or any way to encapsulate if (func()) return ERROR; into another independent function?
You can't really achieve this using return on its own.
But you could throw an exception in func which will bubble up the call stack, in the way you seem to want program control to:
struct myexception{}; /*ToDo - inherit from std::exception?*/
bool func()
{
if (acondition){
return 0; /*normal behaviour, perhaps make `func` void if not needed?*/
}
throw myexception();
}
cmdfun then takes the form:
int cmdfun()
{
other_funcs;
func();
other_funcs;
func();
/* don't forget to return something*/
}
Finally, make sure you catch the exception in the caller to cmdfun.
As I said it is not an exception and cannot be handled by std::exception, it is just an error message and ERROR#NUMBER is just another macro. And I cannot access to the caller to cmdfun(). So unable to adopt the first answer. But after asked someone else, it is possible to encapsulate returns and save time when typing them, though it's not recommended, but in this particular case, I can use macro. A complete example is given below:
#include <iostream>
using namespace std;
#define CHECK_VEC(acondition)\
if(checkcondition(acondition)) return -1;
bool checkcondition(bool acondition)
{
if (acondition) return 1;
return 0;
}
int fun_called_by_main()
{
int a = 5 + 4;
bool acondition = a;
CHECK_VEC(acondition);
return 1;
}
int main()
{
int a = fun_called_by_main();
cout << a << endl;
cin.get();
return 0;
}
If I understood corectly your question, you are asking for an 'error reporter' for your own errors. There are 2 solutions for 2 separate cases:
Case 1 - you still want to use a return statement to make an 'error reporter':
To do this, you'll have to make another function or just learn how to use goto. However, you don't need to - your function returns a boolean(bool) - which means you only have 2 possible results: 0 (False) and 1 (True)
bool func()
{
if (acondition)
{
return (bool)0; // False (no error)
}
return (bool)1; // True (error)
// Note: I used (bool)0 and (bool)1 because it is
// more correct because your returning type is bool.
}
void errorcase(bool trueorfalse)
{
switch(trueorfalse)
{
case False:
... // your code (func() returned 0)
break;
default:
... // your code (func() returned 1)
break;
// Note that you will not need to check if an error occurred every time.
}
return;
}
int cmdfun()
{
... // your code
errorcase(func());
... // again - your code
return 0; // I suppouse that you will return 0...
}
But I think that the second case is more interesting (unfortunetly it is also preety hard to understand as a beginner and the first solution might be a lot easier for you):
Case 2 - you decided to do it somehow else - that's by learning throw and catch - I won't repeat the answer because it is already given: #Bathsheba answered preety good...

Switch case Statement in Python. Instead of using if else statement is there any other way

Below is the code in javascript. I need to convert it into Python. Having a multiple switch cases with one return value.:
textOfBetType: function(tip) {
switch (tip) {
case "tip1":
case "tip0":
case "tip2":
return "Livewette 0:0"
//return "Sieger (3-Weg)"
case "tipOver":
case "tipUnder":
return "Über/Unter 2,5";
}
return null;
}
I used "if and else" statement in Python.
def text_of_bet_type(self, tip):
if tip == "tip1" or tip == "tip0" or tip == "tip2":
return "Livewette 0:0"
else:
return 'null';
but is there any other way to do this this..

C++ Error with operator= of unique_ptr using std::move(nullptr)

I have seen this and I have corrected my code:
int solutionChooser = m_configFile.getChosenSolution();
ISolution* currentSolution;
switch (solutionChooser)
{
case 1:
{
currentSolution = new Solution1());
break;
}
case 2:
{
currentSolution = new Solution2());
break;
}
case 3:
{
currentSolution = new Solution3());
break;
}
case 4:
{
currentSolution = new Solution4());
break;
}
default:
{
std::cout << "The specified solution does not exists\n";
return;
}
}
using unique_ptr as:
int solutionChooser = m_configFile.getChosenSolution();
std::unique_ptr<ISolution> currentSolution;
switch (solutionChooser)
{
case 1:
{
currentSolution.reset(new Solution1());
break;
}
case 2:
{
currentSolution.reset(new Solution2());
break;
}
case 3:
{
currentSolution.reset(new Solution3());
break;
}
case 4:
{
currentSolution.reset(new Solution4());
break;
}
default:
{
currentSolution = std::move(nullptr); // here is the error
std::cout << "The specified solution does not exists\n";
return;
}
}
and now I am getting the error below:
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<ISolution>’ and ‘std::remove_reference<long int>::type {aka long int}’)
I have ISolution as interface and the SolutionX are classes derived from ISolution
How to fix this? What am I doing wrong?
std::unique_ptr has deleted operator=, that is why you can not use it.
To reset the std::unique_ptr, use reset() method :
currentSolution.reset(nullptr);
but you do not have to do it, since the initial value is nullptr anyway.
Your compiler is wrong, by n3376 std::unique_ptr should have following overloading
unique_ptr& operator=(nullptr_t) noexcept;
so, your code should work fine.
I can't add comments so I'll just post my idea about the answer..
I believe your problem is the copy constructor. The operator = is not defined with unique_ptr so instead you'll have to use a move constructor. I don't remember the correct syntax but it should be something similar to:
/* Header file */
std::unique_ptr<ISolution> currentSolution;
/* CPP file */
std::unique_ptr<ISolution> currentSolution2(new ISolution);
currentSolution = std::move(currentSolution2);
There's probably some mistakes here but hopefully it can get you to the right track. If you want a working example, I got one on floobits, user Simple2012.
Link here: https://floobits.com/Simple2012/Laboration_SearchMethods
Check arr.h and arr.cpp for a concrete example, however I'm using an array there instead of a class, not much difference though.

Retrieving the status of another user from libpurple (the IM library underpinning Pidgin)

I'm trying to pull the current status of another person on a SIMPLE network (Microsoft Office Communicator). I'm using libpurple, built a c++ wrapper around libpurple, and I can send/receive IMs with other users on the SIMPLE network. What I still need is to get the current status of other users
Here's my current attempt at retrieving status of another user.
Previously defined and initialized:
PurpleAccount *CommonIM::m_account -> I can send messages using this account
// the username of the person I want to get the status of, e.g.
username = "sip:blah#blah.blah.com";
//TEST instance 1
PurpleBuddy* newbody1 = purple_buddy_new(m_account, username.c_str(), NULL);
sleep(5);
PurplePresence *p1 = purple_buddy_get_presence(newbody1);
PurpleStatus *status1 = purple_presence_get_active_status(p1);
PurpleStatusType *statusType1 = purple_status_get_type(status1);
PurpleStatusPrimitive prim1 = purple_status_type_get_primitive(statusType1);
switch(prim1)
{
case PURPLE_STATUS_UNSET:
{
status = "unset";
}
break;
case PURPLE_STATUS_OFFLINE:
{
status = "offline";
}
break;
case PURPLE_STATUS_AVAILABLE:
{
status = "available";
}
break;
case PURPLE_STATUS_UNAVAILABLE:
{
status = "unavailable";
}
break;
case PURPLE_STATUS_INVISIBLE:
{
status = "invisible";
}
break;
case PURPLE_STATUS_AWAY:
{
status = "away";
}
break;
case PURPLE_STATUS_EXTENDED_AWAY:
{
status = "extended away";
}
break;
case PURPLE_STATUS_MOBILE:
{
status = "mobile";
}
break;
case PURPLE_STATUS_TUNE:
{
status = "tune";
}
break;
case PURPLE_STATUS_NUM_PRIMITIVES:
default:
{
status = "unknown";
}
break;
}
//TEST instance 1 complete
cout << _TAG << "Test instance 1: Status for " << username << " is reported as " << status << endl;
This code always returns offline as the status. It's as if purple doesn't refresh the buddy after creating a new instance, it always remains as "offline". I've dived into libpurple and pidgin to try to find this for the past few days but can't find the 'proper' way of retrieving status.
For some reason, calling this from the signed-on signal doesn't work.
Calling it from the buddy-signed-on signal works for me. Of course, in that case it will be called once for each signed-on buddy ...
sample function to be called from the "buddy-signed-on" signal:
static void buddy_signed_on(PurpleBuddy *buddy) {
GSList *buddies = purple_blist_get_buddies();
for(; buddies; buddies = buddies->next) {
PurpleBuddy *b = (PurpleBuddy *) buddies->data;
PurplePresence *presence = purple_buddy_get_presence(b);
PurpleStatus *status = purple_presence_get_active_status(presence);
printf("%s is now %s\n", b->name, purple_status_get_id(status));
}
}
Connect the signal:
purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", &handle,
PURPLE_CALLBACK(buddy_signed_on), NULL);