I'm trying to extend the cefsimple app that comes with the Chromium Embedded Framework to include a V8 handler. The code I've written looks like this so far;
bool SimpleHandler::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
int argn = arguments.size();
if (name == "serial_connect" && (argn == 4 || argn == 1))
{
if (connection.isOpen())
connection.~Serial();
connection = NULL;
if (argn == 1)
{
int port = (arguments[0]); // convert to int?
}
else
{
}
}
else if (name == "serial_send" && argn >= 1)
{
}
else if (name == "serial_recieve")
{
}
else
return false;
return true;
}
I'm having trouble converting the generic value object returned given by the handler into a cpp int that I can use in calculations. I've found the function CefV8Value::GetIntValue();, but I can't figure out how to use it.
try calling object->GetIntValue()
The reason is that CefRefPtr is an object that holds a reference to CefV8Value, so you need the arrow operator to access the underlying CefV8Value object it points to
Related
I am getting an error that I am having problems fixing as recursion hasn't "sunk in" yet.
It is supposed to go through an array of symbols already placed by the Class OrderManager Object and check if the symbol passed in is already there or not, if it is not there it should allow the trade, otherwise it will block it (multiple orders on the same currency compounds risk)
[Error] '}' - not all control paths return a value.
I believe it is because of the retest portion not having a return value but again I'm still newish to making my own recursive functions. However it may also be because my base and test cases are wrong possibly?
P.S I added (SE) comments in places to clarify language specific things since it is so close to C++.
P.P.S Due to the compiler error, I have no clue if this meets MVRC. Sorry everyone.
bool OrderManager::Check_Risk(const string symbol, uint iter = 0) {
if((iter + 1) != ArraySize(m_symbols) &&
m_trade_restrict != LEVEL_LOW) // Index is one less than Size (SE if
// m_trade_restrict is set to LOW, it
// allows all trades so just break out)
{
if(OrderSelect(OrderManager::Get(m_orders[iter]),
SELECT_BY_TICKET)) // Check the current iterator position
// order (SE OrderSelect() sets an
// external variable in the terminal,
// sort of like an environment var)
{
string t_base = SymbolInfoString(
OrderSymbol(),
SYMBOL_CURRENCY_BASE); // Test base (SE function pulls apart
// the Symbol into two strings
// representing the currency to check
// against)
string t_profit =
SymbolInfoString(OrderSymbol(), SYMBOL_CURRENCY_PROFIT);
string c_base =
SymbolInfoString(symbol, SYMBOL_CURRENCY_BASE); // Current base
// (SE does the same as above but for the passed variable instead):
string c_profit = SymbolInfoString(symbol, SYMBOL_CURRENCY_PROFIT);
// Uses ENUM_LEVELS from Helpers.mqh (SE ENUM of 5 levels: Strict,
// High, Normal, Low, None in that order):
switch(m_trade_restrict) {
case LEVEL_STRICT: {
if(t_base == c_base || t_profit == c_profit) {
return false; // Restrictions won't allow doubling
// orders on any currency
} else
return Check_Risk(symbol, iter++);
};
case LEVEL_NORMAL: {
if(symbol == OrderSymbol()) {
return false; // Restrictions won't allow doubling
// orders on that curr pair
} else
return Check_Risk(symbol, iter++);
};
default: {
// TODO: Logging Manager
// Hardcoded constant global (SE set to LEVEL_NORMAL):
ENB_Trade_Restrictions(default_level);
return Check_Risk(symbol, iter);
}
}
}
} else {
return true;
}
}
So, I must just have been staring at the code for too long but the problem was the if(OrderSelect(...)) on ln 7 did not have a return case if the order was not properly set in the terminal. I will need to polish this but the following code removes the error.
bool OrderManager::Check_Risk(const string symbol, uint iter=0)
{
if((iter + 1) != ArraySize(m_symbols) && m_trade_restrict != LEVEL_LOW) // Index is one less than Size
{
if(OrderSelect(OrderManager::Get(m_orders[iter]), SELECT_BY_TICKET)) //Check the current iterator position order
{
string t_base = SymbolInfoString(OrderSymbol(), SYMBOL_CURRENCY_BASE); //Test base
string t_profit = SymbolInfoString(OrderSymbol(), SYMBOL_CURRENCY_PROFIT);
string c_base = SymbolInfoString(symbol, SYMBOL_CURRENCY_BASE); //Current base
string c_profit = SymbolInfoString(symbol, SYMBOL_CURRENCY_PROFIT);
switch(m_trade_restrict) // Uses ENUM_LEVELS from Helpers.mqh
{
case LEVEL_STRICT :
{
if(t_base == c_base || t_profit == c_profit)
{
return false;
}
else return Check_Risk(symbol, ++iter);
};
case LEVEL_NORMAL :
{
if(symbol == OrderSymbol())
{
return false;
}
else return Check_Risk(symbol, ++iter);
};
default: {
// TODO: Logging Messages
ENB_Trade_Restrictions(default_level); //Hardcoded constant global
return Check_Risk(symbol, iter);
}
}
}
else {return Check_Risk(symbol, ++iter);}
}
else {return true;}
}
I am fairly new to writing Junits in such detail.
The method
returns boolean true/false
accepts two params of same non-primitive type.
This method does a whole lot of validations by comparing the two instances of the similar type using if else conditions.
And few more validations while iterating through the collection based fields of the params .
I tried writing the base scenario, but unclear as to how to do it for the underlying if_conditons. especially with regard to mocking the data.
What I tried and aware: There are lots of methods under AssertClass which I can use to validate, however for this method that return boolean based on quite a bunch of conditions, how do I write? do I have to write individual asserts for each condition under the #Test annotated method?
below is my code :
public boolean checkEligblity(A newversion, A currentversion) {
if (currentversion.getPurchaseOrders().isEmpty())
return true;
if (currentversion.getReceiptNotice() != newversion.getReceiptNotice()
|| currentversion.getSizeType() != newversion.getSizeType()
|| currentversion.getNumInPrices() != newversion.getNumInPrices())
return true;
boolean storeM = newversion.getReceiptNotice() == PASSED;
if (currentversion.getOrderInfo() != newversion.getOrderInfo()) {
if (storeM || containsCustomerString(currentversion.getOrderInfo())
|| containsCustomerString(newversion.getOrderInfo()))
return true;
}
List<PurchaseOrders> newVersionPurchaseOrders = newversion.getPurchaseOrders();
List<PurchaseOrders> currentVersionPurchaseOrders = currentversion.getPurchaseOrders();
for (Iterator<PurchaseOrders> newPOIterator = newVersionPurchaseOrders.iterator(); newPOIterator.hasNext();) {
PurchaseOrders newPO = newPOIterator.next();
for (Iterator<PurchaseOrders> currentPOIterator = currentVersionPurchaseOrders.iterator(); currentPOIterator
.hasNext();) {
PurchaseOrders currentPO = currentPOIterator.next();
if (newPO.getSku() == currentPO.getSku()
&& newPO.getSkuId() == currentPO.getSkuId()
&& newPO.getOrgin() == currentPO.getOrgin()
&& newPO.getTarget() == currentPO.getTarget()
&& newPO.getShippingDate()== currentPO.getDepratureDate()) {
if (currentPO.getStatusCode().equals(newPO.getStatusCode())
&& currentPO.getItemsInQty().equals(newPO.getItemsInQty())
&& currentPO.getVendorId().equals(newPO.getVendorId())
&& currentPO.getCodeInd().equals(newPO.getCodeInd())
&& currentPO.getPriceType().charAt(0) == newPO.getPriceType().charAt(0)) {
boolean matchingFound = true;
if (storeM && currentPO.getInbound() != newPO.getInbound()) {
matchingFound = false;
}
if (matchingFound) {
newPOIterator.remove();
currentPOIterator.remove();
break;
}
}
}
}
}
if (!newVersionPurchaseOrders.isEmpty() || !currentVersionPurchaseOrders.isEmpty()) {
return true;
}
return false;
}
private boolean containsCustomerString(String ticketingInfo) {
if (orderInfo == null)
return false;
if (orderInfo.startsWith(PASS))
return true;
return false;
}
I a newbie C++ programmer trying to test aruments/parameters passed to a program.
Multiple arguments can be passed to the program, however I want to test that if certain arguments are passed then other arguments become invalid.
e.g. PGM accepts arg(1) arg(2) arg(3) arg(4) arg(5) etc...
if arg(1) and arg(2) are supplied then arg(3), arg(4) and arg(5) etc... are invalid and the program should terminate with an error message if they are also supplied along with arg(1) and arg(2).
I've thought that using boolean IF tests would be a good way to check if certain values are true/false.
I searched on stackoverflow but not found an answer that encompasses exactly what i'm trying to do. If someone can point me in the right direction or suggest a far more efficient way of doing this I would be very grateful.
My code currently looks like this:
bool opt1 = false;
bool opt2 = false;
bool opt3 = false;
bool opt4 = false;
bool opt5 = false;
for(int i=1; i<argc; i++) {
char *str = argv[i];
if (strcmp (str, "-opt1:")==0) {opt1 = true;}
else if (strcmp (str, "-opt2:")==0) {opt2 = true;}
else if (strcmp (str, "-opt3:")==0) {opt3 = true;}
else if (strcmp (str, "-opt4:")==0) {opt4 = true;}
else if (strcmp (str, "-opt5:")==0) {opt5 = true;}
}
if((opt1) && (opt2) && (~(opt3)) && (~(opt4)) && (~(opt5)) {
** DO SOMETHING **
} else {
** DISPLAY ERROR MESSAGE AND USAGE TEXT **
}
A good solution would be using operands ! and &&
! denotes "not" (or in such case "not true") while && combines two different logical comparisons (in such case, "logic test 1" and "logic test 2")
Here's an example to do it:
if((opt1 && opt2)&&(!(opt3||opt4||opt5))){
/*
Do something if opt1 and opt2 are true and others are false
*/
}
This is practically the same as #Fareanor's solution above (first solution)
A possible fix could be (if I have well understood your problem):
if(opt1 && opt2) // opt3, opt4 and opt5 are invalid
{
if(!(opt3 || opt4 || opt5))
{
// Do something
}
else
{
// Display error message because at least opt3 or opt4 or opt5 is provided and not requested
}
}
else // opt3, opt4 and opt5 are valid
{
// Do something
}
But I think it could be better to just ignore the obsolete parameters instead of display an error while you can still run your process with only opt1 and opt2. Which could lead us to the simpler code:
if(opt1 && opt2)
{
// Do something without using opt3, opt4 and opt5
}
else
{
// Do something taking into account opt3, opt4 and opt5
}
I hope it is what you was looking for.
I'm having some trouble with the following method and I need some help trying to figure out what I am doing wrong.
I want to return a reference to a Value in a document. I am passing the Document from outside the function so that when I read a json file into it I don't "lose it".
const rapidjson::Value& CTestManager::GetOperations(rapidjson::Document& document)
{
const Value Null(kObjectType);
if (m_Tests.empty())
return Null;
if (m_current > m_Tests.size() - 1)
return Null;
Test& the_test = m_Tests[m_current];
CMyFile fp(the_test.file.c_str()); // non-Windows use "r"
if (!fp.is_open())
return Null;
u32 operations_count = 0;
CFileBuffer json(fp);
FileReadStream is(fp.native_handle(), json, json.size());
if (document.ParseInsitu<kParseCommentsFlag>(json).HasParseError())
{
(...)
}
else
{
if (!document.IsObject())
{
(...)
}
else
{
auto tests = document.FindMember("td_tests");
if (tests != document.MemberEnd())
{
for (SizeType i = 0; i < tests->value.Size(); i++)
{
const Value& test = tests->value[i];
if (test["id"].GetInt() == the_test.id)
{
auto it = test.FindMember("operations");
if (it != test.MemberEnd())
{
//return it->value; is this legitimate?
return test["operations"];
}
return Null;
}
}
}
}
}
return Null;
}
Which I am calling like this:
Document document;
auto operations = TestManager().GetOperations(document);
When I inspect the value of test["operations"] inside the function I can see everything I would expect (debug code removed from the abode code).
When I inspect the returned value outside the function I can see that it's an array (which I expect). the member count int the array is correct as well, but when print it out, I only see garbage instead.
When I "print" the Value to a string inside the methods, I get what I expect (i.e. a well formated json), but when I do it outside all keys show up as "IIIIIIII" and values that aren't strings show up correctly.
rapidjson::StringBuffer strbuf2;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer2(strbuf2);
ops->Accept(writer2);
As this didn't work I decided to change the method to receive a Value as a parameter and do a deep copy into it like this
u32 CTestManager::GetOperationsEx(rapidjson::Document& document, rapidjson::Value& operations)
{
(...)
if (document.ParseInsitu<kParseCommentsFlag>(json).HasParseError())
{
(...)
}
else
{
if (!document.IsObject())
{
(...)
}
else
{
auto tests = document.FindMember("tests");
if (tests != document.MemberEnd())
{
for (SizeType i = 0; i < tests->value.Size(); i++)
{
const Value& test = tests->value[i];
if (test["id"].GetInt() == the_test.id)
{
const Value& opv = test["operations"];
Document::AllocatorType& allocator = document.GetAllocator();
operations.CopyFrom(opv, allocator); //would Swap work?
return operations.Size();
}
}
}
}
}
return 0;
}
Which I'm calling like this:
Document document;
Value operations(kObjectType);
u32 count = TestManager().GetOperationsEx(document, operations);
But... I get same thing!!!!
I know that it's going to be something silly but I can't put my hands on it!
Any ideas?
The problem in this case lies with the use of ParseInSitu. When any of the GetOperations exist the CFileBuffer loses scope and is cleaned up. Because the json is being parsed in-situ when the buffer to the file goes, so goes the data.
I am writing a function as follows:
bool abc::GetLoggingStatus() {
//true or false is returned
int value;
if (regKey->HasValue("LoggingStatus")) {
regKey->QueryValue("LoggingStatus", &value);
if (value == 1)
return true; //no logging possible
else
return false;
}
regKey->SetValue("LoggingStatus", 1);
return true;
}
Logging level is defined as:
typedef enum {
Entry,
Exit,
Debug,
Warning,
Notification,
Error
} TYPE;
What I need if I select 1 the levels for logging must be shown namely debug,error ... In regedit and if 0 nothing should be shown and logging be disabled.
You can't create dropdown menus in regedit, but what you can do is create a new entry called LoggingLevel. This entry is ignored if LoggingStatus is 0. LoggingLevel is a string defining the level.
If you want to convert this string back to an enum, the easiest way is to create a map from string to your Enum type:
std::map<std::string, TYPE> typeMap;
typeMap["Warning"] = Warning;
...
In your code you query the logging level:
char* level;
regKey->QueryValue("LoggingLevel", level);
TYPE theLevel = typeMap[level];
Of course you need to do appropriate error checking.
edit
You should add two function to get the log settings, shouldLog() and getLevel().
The log function would then look like:
void log(Logger* logger, TYPE type, string sClassName, string sMethodName, string sMessage = "") {
if (!logger || !abc::shouldLog()) {
return;
}
TYPE curLevel = abc::getLevel();
bool shouldLog = false;
if (type == Warning && (curLevel == All || curLevel == Warning) ...) {
shouldLog = true;
}
if (shouldLog) {logger->WriteEntry(sClassName, sMethodName); }
}
If you want to avoid complicated if-structures, you could also try and map the enums to a value and compare that. For example Warning = 1 and ALL = 0. Then you can check if curLevel < type to see if the logger should log.