multi_index template compiling error - c++

2 Questions:
1. Do i really need to pass parameter to modify/modify_key via member?
2. Why do i have this compilation error
To see the entire code with the error, you can look at http://coliru.stacked-crooked.com/a/d6241361318e1925
the error is
MultiIndex4.h: In member function 'uint32_t CrMultiParameterMultiIndex::ModifyKeyBy(SearchingKey&, ModifyKeyType&) [with SearchingTagType = IMEI_tag, ModifyingTagType = IMEI_tag, SearchingKey = uint32_t, ModifyKeyType = uint32_t]':
MultiIndex4.h:183: instantiated from here
MultiIndex4.h:119: error: no matching function for call to 'boost::multi_index::multi_index_container<UsersKey, UsersKey_indices, std::allocator<UsersKey> >::modify_key(boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<UsersKey, std::allocator<UsersKey> > > >&, boost::function<void ()(uint32_t&)>&)'
*** Errors occurred during this build ***
I have a class CrMultiParameterMultiIndex that hold a multi-index that have 2 indexes.
I tried to create a template function that search only according a part of the key and modify only a part of the key.
This class have a helper function
template <typename TagType,typename SearchingKey>
typename GlobalHash::index<TagType>::type::iterator GetIteratorBy(SearchingKey & key){
return m_pGlobalHash->get<TagType>().find(key) ;
}
and modify function that use it
template <typename SearchingTagType,typename ModifyingTagType,typename SearchingKey,typename ModifyKeyType> uint32_t ModifyKeyBy(SearchingKey & key,ModifyKeyType & pi_Modifykey)
{
uint32_t hRes = 1;
//search for entry by TagType
typedef typename GlobalHash::index<SearchingTagType>::type IndexType;
typename IndexType::iterator it = GetIteratorBy<SearchingTagType>(key);
//entry found
if( it != m_pGlobalHash->get<SearchingTagType>().end() )
{
//Set parameter to modify
hRes = SetParameterKeys<ModifyingTagType>(pi_Modifykey);
if(hRes == 1)
{
//get iteraror to modify
typedef typename GlobalHash::index<ModifyingTagType>::type ModifyIndexType;
typename ModifyIndexType::iterator itToModify = m_pGlobalHash->get<ModifyingTagType>().iterator_to(*it);
boost::function<void( ModifyKeyType &)> f = boost::bind(&CrMultiParameterMultiIndex::ModifyKey<ModifyingTagType, ModifyKeyType >, this, _1);
//modify key failed
if(m_pGlobalHash->modify_key(itToModify , f)==false)
hRes = 0;
}
}
//entry not found
else
hRes = 0;
return hRes;
}
this compile
uint64_t nFromImsi = 1;
uint64_t nToImsi = 1;
m_multiParam.ModifyKeyBy<IMSI_tag,IMSI_tag>( nFromImsi,nToImsi)
but not this
uint32_t nFromImsi = 1;
uint32_t nToImsi = 1;
m_multiParam.ModifyKeyBy<IMEI_tag,IMEI_tag>( nFromImsi,nToImsi)
Why? and how can it compile
and here are the modifiers
template <> inline void CrMultiParameterMultiIndex::ModifyKey<IMEI_tag>( uint32_t & po_Key){po_Key = m_ParameterKeys.IMEI;}
template <> inline void CrMultiParameterMultiIndex::ModifyKey<IMSI_tag>(uint64_t & po_Key){po_Key = m_ParameterKeys.IMSI;}

David,
The problem with
if(m_pGlobalHash->modify_key(itToModify , f)==false)
lies in the fact that m_pGlobalHashis a view to index #0 (order_by_IMSI) and does not accept iterators to view #1 (order_by_IMEI). You can either project iterators or select the appropriate index:
if(m_pGlobalHash->get<ModifyingTagType>().modify_key(itToModify , f)==false)

Related

open62541 client fails when calling method with custom datatype input argument

I'm using open62541 to connect to an OPC/UA server and I'm trying to call methods that a certain object on that server provides. Those methods have custom types as input arguments; for example, the following method takes a structure of three booleans:
<opc:Method SymbolicName="SetStatusMethodType" ModellingRule="Mandatory">
<opc:InputArguments>
<opc:Argument Name="Status" DataType="VisionStatusDataType" ValueRank="Scalar"/>
</opc:InputArguments>
<opc:OutputArguments />
</opc:Method>
Here, VisionStatusDataType is the following structure:
<opc:DataType SymbolicName="VisionStatusDataType" BaseType="ua:Structure">
<opc:ClassName>VisionStatus</opc:ClassName>
<opc:Fields>
<opc:Field Name="Camera" DataType="ua:Boolean" ValueRank="Scalar"/>
<opc:Field Name="StrobeController" DataType="ua:Boolean" ValueRank="Scalar"/>
<opc:Field Name="Server" DataType="ua:Boolean" ValueRank="Scalar"/>
</opc:Fields>
</opc:DataType>
Now, when calling the method, I'm encoding the data into an UA_ExtensionObject, and wrap that one as an UA_Variant to provide it to UA_Client_call. The encoding looks like this:
void encode(const QVariantList& vecqVar, size_t& nIdx, const DataType& dt, std::back_insert_iterator<std::vector<UAptr<UA_ByteString>>> itOut)
{
if (dt.isSimple())
{
auto&& qVar = vecqVar.at(nIdx++);
auto&& uaVar = convertToUaVar(qVar, dt.uaType());
auto pOutBuf = create<UA_ByteString>();
auto nStatus = UA_encodeBinary(uaVar.data, dt.uaType(), pOutBuf.get());
statusCheck(nStatus);
itOut = std::move(pOutBuf);
}
else
{
for (auto&& dtMember : dt.members())
encode(vecqVar, nIdx, dtMember, itOut);
}
}
UA_Variant ToUAVariant(const QVariant& qVar, const DataType& dt)
{
if (dt.isSimple())
return convertToUaVar(qVar, dt.uaType());
else
{
std::vector<UAptr<UA_ByteString>> vecByteStr;
auto&& qVarList = qVar.toList();
size_t nIdx = 0UL;
encode(qVarList, nIdx, dt, std::back_inserter(vecByteStr));
auto pExtObj = UA_ExtensionObject_new();
pExtObj->encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
auto nSizeAll = std::accumulate(vecByteStr.cbegin(), vecByteStr.cend(), 0ULL, [](size_t nSize, const UAptr<UA_ByteString>& pByteStr) {
return nSize + pByteStr->length;
});
auto&& uaEncoded = pExtObj->content.encoded;
uaEncoded.typeId = dt.uaType()->typeId;
uaEncoded.body.length = nSizeAll;
auto pData = uaEncoded.body.data = new UA_Byte[nSizeAll];
nIdx = 0UL;
for (auto&& pByteStr : vecByteStr)
{
memcpy_s(pData + nIdx, nSizeAll - nIdx, pByteStr->data, pByteStr->length);
nIdx += pByteStr->length;
}
UA_Variant uaVar;
UA_Variant_init(&uaVar);
UA_Variant_setScalar(&uaVar, pExtObj, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
return uaVar;
}
}
The DataType class is a wrapper for the UA_DataType structure; the original open62541 type can be accessed via DataType::uaType().
Now, once a have the variant (containing the extension object), the method call looks like this:
auto uavarInput = ToUAVariant(qvarArg, dtInput);
UA_Variant* pvarOut;
size_t nOutSize = 0UL;
auto nStatus = UA_Client_call(m_pClient, objNode.nodeId(), m_uaNodeId, 1UL, &uavarInput, &nOutSize, &pvarOut);
The status is 2158690304, i.e. BadInvalidArgument according to UA_StatusCode_name.
Is there really something wrong with the method argument? Are we supposed to send ExtensionObjects, or what data type should the variant contain?
Is it possible that the server itself (created using the .NET OPC/UA stack) is not configured correctly?
N.B., the types here are custom types; that is, the encoding is done manually (see above) by storing the byte representation of all members next to each other in an UA_ByteString - just the opposite of what I'm doing when reading variables or output arguments, which works just fine.
The problem is the typeId of the encoded object. For the server in order to understand the received data, it needs to know the NodeId of the encoding, not the actual NodeId of the type itself. That encoding can be found by following the HasEncoding reference (named "Default Binary") of the type:
auto pRequest = create<UA_BrowseRequest>();
auto pDescr = pRequest->nodesToBrowse = UA_BrowseDescription_new();
pRequest->nodesToBrowseSize = 1UL;
pDescr->nodeId = m_uaNodeId;
pDescr->resultMask = UA_BROWSERESULTMASK_ALL;
pDescr->browseDirection = UA_BROWSEDIRECTION_BOTH;
pDescr->referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASENCODING);
auto response = UA_Client_Service_browse(m_pClient, *pRequest);
for (auto k = 0UL; k < response.resultsSize; ++k)
{
auto browseRes = response.results[k];
for (auto n = 0UL; n < browseRes.referencesSize; ++n)
{
auto browseRef = browseRes.references[n];
if (ToQString(browseRef.browseName.name).contains("Binary"))
{
m_nodeBinaryEnc = browseRef.nodeId.nodeId;
break;
}
}
}
Once you have that NodeId, you pass it to UA_ExtensionObject::content::encoded::typeId:
auto pExtObj = UA_ExtensionObject_new();
pExtObj->encoding = UA_EXTENSIONOBJECT_ENCODED_BYTESTRING;
auto nSizeAll = std::accumulate(vecByteStr.cbegin(), vecByteStr.cend(), 0ULL, [](size_t nSize, const UAptr<UA_ByteString>& pByteStr) {
return nSize + pByteStr->length;
});
auto&& uaEncoded = pExtObj->content.encoded;
uaEncoded.typeId = dt.encoding();
uaEncoded.body.length = nSizeAll;
auto pData = uaEncoded.body.data = new UA_Byte[nSizeAll];
nIdx = 0UL;
for (auto&& pByteStr : vecByteStr)
{
memcpy_s(pData + nIdx, nSizeAll - nIdx, pByteStr->data, pByteStr->length);
nIdx += pByteStr->length;
}

Which is the best constructor of unordered_map to replace one old project defined class UtilHashTable

In our base code, we are trying to replace one project defined class UtilHashTable by unordered_map to improve performance.
C'tor of UtilHashTable looks like:
template< class Key, class Val, class Hash >
UtilHashTable< Key, Val, Hash >::UtilHashTable( unsigned inInitialBuckets,
unsigned inMaxProbes ) :
cMaxProbes( inMaxProbes )
{
cLoadFactor = 0.5;
mSize = 0;
unsigned size = 1;
while( inInitialBuckets > 0 )
{
size <<= 1;
inInitialBuckets >>= 1;
}
//mTable = new vector< Entry >( size );
mTable = new unordered_map< Key, Val >( size );
mNumRehashes = 0;
mNumLookups = 0;
mNumProbes = 0;
mHashMask = static_cast< uint32>( mTable->size() - 1 );
}
In the header file, its declaration looks like:
UtilHashTable( unsigned inInitialBuckets = 500, unsigned inMaxProbes = 40 );
We are providing flexibility to the user to set InitialBuckets and MaxProbes if they want!
Please suggest me which c'tor of unordered_map will work best to replace everything inside the c'tor of UtilHashTable.
Tell me if you need any further info.
Thanks in advance! :D

llvm createCall Calling a function with a bad signature

I want to make a function in LLVM which is an adapter with only a function call foo(idx, mn). The function prototype of foo is void foo(unsigned char, const char*).
// adapter Function with only a function call foo(idx, mn)
llvm::Function* createCallFun(llvm::Module* M, llvm::Function* exit_f) {
llvm::LLVMContext& Ctx = M->getContext();
llvm::Function* foo_f = foo_prototype(Ctx, M);
llvm::Constant* c = M->getOrInsertFunction("__call_fun", FunctionType::getVoidTy(Ctx), llvm::Type::getInt32Ty(Ctx), llvm::Type::getInt32Ty(Ctx), NULL);
llvm::Function* call_fun_f = llvm::cast<llvm::Function>(c);
llvm::BasicBlock* entry = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", call_fun_f);
llvm::IRBuilder<> builder(entry);
llvm::Function::arg_iterator args = call_fun_f->arg_begin();
llvm::Value* idx = &*args++;
idx->setName("idx");
llvm::Value* mn = &*args++;
mn->setName("mn");
llvm::Value* greater = builder.CreateICmpSGE(idx, mn, "tmp");
std::vector<llvm::Value*> fun_args;
fun_args.push_back(greater);
fun_args.push_back(err_msg);
builder.CreateCall(foo_f, fun_args);
return call_fun_f;
}
Then I got this error:
lib/IR/Instructions.cpp:245: void llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef, llvm::ArrayRef >, const llvm::Twine&): Assertion `(i >= FTy->getNumParams() || FTy->getParamType(i) == Args[i]->getType()) && "Calling a function with a bad signature!"' failed.
It seems the first argument of foo has a type mismatch. How can I cast the Value greater to unsigned char type?
I fixed this error by cast greater with CreateZExt.
llvm::Value *castuchar =
builder.CreateZExt(greater, llvm::Type::getInt8Ty(Ctx), "tmp1");

Call to non-constexpr function 'std::map...'

I'm attempting to create a pseudo-media center thing on an Arduino and a breadboard, not all that complicated but this is my first project with C++ that amounted to more than 'Hello world!' or a calculator.
Digressing to my point though, I started rewriting almost the entire original program, and in an effort to clean things up, started using a map to hold the keys and values (IR codes), and to further organize it, put the assignments inside of a function (IR_generateMap()), but I'm getting an error when I compile it.
C:\Users\trevo\Documents\Arduino\improvedIRRemote\improvedIRRemote.ino: In function 'void IRLoop(bool)':
improvedIRRemote:78: error: call to non-constexpr function 'std::map<Key, T, Compare, Allocator>::reference std::map<Key, T, Compare, Allocator>::operator[](const key_type&) [with Key = std::basic_string<char>; T = int; Compare = std::less<std::basic_string<char> >; Allocator = std::allocator<int>; std::map<Key, T, Compare, Allocator>::reference = int&; std::map<Key, T, Compare, Allocator>::key_type = std::basic_string<char>]'
case IRCodes["Power Button"]:
^
improvedIRRemote:78: error: call to non-constexpr function 'std::map<Key, T, Compare, Allocator>::reference std::map<Key, T, Compare, Allocator>::operator[](const key_type&) [with Key = std::basic_string<char>; T = int; Compare = std::less<std::basic_string<char> >; Allocator = std::allocator<int>; std::map<Key, T, Compare, Allocator>::reference = int&; std::map<Key, T, Compare, Allocator>::key_type = std::basic_string<char>]'
exit status 1
call to non-constexpr function 'std::map<Key, T, Compare, Allocator>::reference std::map<Key, T, Compare, Allocator>::operator[](const key_type&) [with Key = std::basic_string<char>; T = int; Compare = std::less<std::basic_string<char> >; Allocator = std::allocator<int>; std::map<Key, T, Compare, Allocator>::reference = int&; std::map<Key, T, Compare, Allocator>::key_type = std::basic_string<char>]'
I've looked up and attempted to understand errors with non-constexpr calls, but to be perfectly honest, I don't understand how what I'm trying to do with the map relates to the problems being described in the posts that deal with said error.
#include <IRremote.h>
#include <StandardCplusplus.h>
#include <map>
//Variables responsible for recieving and decoding IR signals/codes.
IRrecv receiver(10); //IR receiver initialization.
decode_results results; //Object that is responsible for decoding the IR signal.
//Variables responsible for tracking channel changing.
int channel = 1; //The active channel.
int desiredChannel = ""; //The string that is used to concatenate the numbers pressed on the remote keypad together.
int channelEnterDelay = 4000; //The delay between the last time a number was pressed on the remote keypad and when the desiredChannel is cleared.
unsigned long channelEnterStartTime = 0; //The time in which that last number was pressed on the remote keypad.
String intConverter; //The string responsible for casting an integer to a string.
//Variables responsible for tracking volume changing.
int volume = 50; //The current volume.
int lastVolume = 50; //Holds the value of the volume previous to the volume being muted.
bool isMuted = false; //Whether or not the sound has been muted.
//Map responsible for the setting and getting of IR codes and their respective keys.
const std::map<std::string, int> IRCodes;
//Miscellanious Stuff - mostly waiting for depreciation.
int IRFlasherPin = 8;
int mode = 0;
int IRFlasherBlinkRate = 10;
//Debug mode shows channel switching info when entering from the remote keypad, turn it off to declutter the serial feed if it is not being used.
bool DEBUG_MODE = false;
void setup() {
receiver.enableIRIn(); //Tells the receiver to start listening for IR communication.
pinMode(IRFlasherPin, OUTPUT); //Configuring the pin that flashes upon recieving the 'excess communication' IR code.
Serial.begin(9600); //Begins serial communication at 9600 baud.
IR_generateMap(); //Sets all of the values for the map of IR keys and values.
//Ready message.
Serial.println("The Arduino is ON and ready for communication.");
if (DEBUG_MODE) { Serial.println("Also please note that debug mode has been preemptively enabled. The serial monitor can be expected to be much more cluttered than normal. To disable this, press the 'U/SD' button on the remote."); }
Serial.println();
}
void loop() {
//Pre-loop checks.
//empty
IRLoop(true);
}
void IRLoop(bool toldToContinueLooking) {
if (receiver.decode(&results)) {
switch(results.value) {
case IRCodes["Power Button"]:
Serial.println("Power Toggle");
break;
default:
if (DEBUG_MODE) {
Serial.print("Extraneous code: ");
Serial.print(results.value);
}
break;
}
}
if (toldToContinueLooking) { receiver.resume(); }
}
void IR_generateMap() {
//General Keys
IRCodes["Power Button"] = 16753245;
IRCodes["Mode Button"] = 16736925;
IRCodes["Back Button"] = 16750695;
IRCodes["EQ Button"] = 16769055;
IRCodes["USD Button"] = 16756815;
//Media Keys
IRCodes["PlayPause Button"] = 16720605;
IRCodes["Rewind Button"] = 16712445;
IRCodes["Fast Forward Button"] = 16761405;
//Volume Keys
IRCodes["Mute Button"] = 16769565;
IRCodes["Minus Button"] = 16754775;
IRCodes["Plus Button"] = 16748655;
//Numpad Keys
IRCodes["Numpad 0"] = 16738455;
IRCodes["Numpad 1"] = 16724175;
IRCodes["Numpad 2"] = 16718055;
IRCodes["Numpad 3"] = 16743045;
IRCodes["Numpad 4"] = 16716015;
IRCodes["Numpad 5"] = 16726215;
IRCodes["Numpad 6"] = 16734885;
IRCodes["Numpad 7"] = 16728765;
IRCodes["Numpad 8"] = 16730805;
IRCodes["Numpad 9"] = 16732845;
//Non-Key Codes
IRCodes["Excess Communication"] = 4294967295;
}
Can someone explain what I'm doing wrong to me in terms that someone of my early C++ knowledge-base would understand?
The expression in the 'case' must be compile time constant. Just use an if/else construction instead and it should work.
Thomas
I'd go in opposite way by using map to decode IR Code to handler function or by using enum. Something like this:
#include <StandardCplusplus.h>
#include <map>
#include <functional>
void power_key();
void num_key0();
void num_key1();
void num_key2();
void num_key3();
void num_key4();
void num_key5();
void num_key6();
void num_key7();
void num_key8();
void num_key9();
void num_key(int8_t);
typedef void(*handler_t)();
std::map<uint32_t, handler_t> keys;
void setup() {
keys[16753245UL] = power_key;
keys[16738455UL] = num_key0;
keys[16724175UL] = num_key1;
keys[16718055UL] = num_key2;
keys[16743045UL] = num_key3;
keys[16716015UL] = num_key4;
keys[16726215UL] = num_key5;
keys[16734885UL] = num_key6;
keys[16728765UL] = num_key7;
keys[16730805UL] = num_key8;
keys[16732845UL] = num_key9;
Serial.begin(57600);
}
void loop() {
uint32_t recvd_code = 16753245UL; // literal value, for testing purpose without IR receiver and remote
auto iter = keys.find(recvd_code); // find received code and execute it
if (iter != keys.end()) {
iter->second();
} else {
Serial.print(F("IR Code "));
Serial.print(recvd_code);
Serial.println(F("not found"));
}
delay(1000);
}
void power_key() {
Serial.println(F("Power key pressed"));
}
void num_key0() { num_key(0); }
void num_key1() { num_key(1); }
void num_key2() { num_key(2); }
void num_key3() { num_key(3); }
void num_key4() { num_key(4); }
void num_key5() { num_key(5); }
void num_key6() { num_key(6); }
void num_key7() { num_key(7); }
void num_key8() { num_key(8); }
void num_key9() { num_key(9); }
void num_key(int8_t num) {
Serial.print(F("Numeric key pressed: "));
Serial.println(num);
}
Initializer list somehow didin't work, so initialization must be done in setup and map can't be const.

error C2663: overloads have no legal conversion for 'this' pointer

please help me for this errors
code:
u16 ip_defragment(){
u16 result;
fragip_set::iterator i;
IP_FRAGMENTED new_defrag;
IP* pcurpack = (IP*) malloc(cur.len);
memcpy(pcurpack, cur.data, cur.len);
new_defrag.saddr = cur.saddr;
new_defrag.daddr = cur.daddr;
new_defrag.protocol = cur.ip.ppack->protocol;
new_defrag.id = i2i(cur.ip.ppack->id);
i = ip_frags.find(new_defrag);
if(i != ip_frags.end()){
i->packets.insert(pcurpack);
const_cast<u16&>(i->cur_len) += cur.ip.len - cur.ip.hlen;
const_cast<u32&>(i->last_time) = time();
if(!(cur.ip.bmore_fr) && (i->tot_len == 0)){
const_cast<u16&>(i->tot_len) = cur.ip.fr_offs + cur.ip.len;
}
if(i->cur_len == i->tot_len){
for(set<IP*>::iterator k = i->packets.begin(); k != i->packets.end(); k++){
// must copy to another buffer
if(i2i((*k)->frag_off) & IP_OFFMASK){
memcpy(ip_defrag_buffer, *k, (*k)->ihl<<2);
} else {
memcpy(ip_defrag_buffer + (i2i((*k)->frag_off) & IP_OFFMASK) * 8,
*k + ((*k)->ihl<<2), (i2i((*k)->tot_len))-((*k)->ihl<<2));
}
}
IP* defr_ip = (IP*) &ip_defrag_buffer;
defr_ip->tot_len = i2i(i->tot_len);
defr_ip->frag_off = 0;
result = i->tot_len;
ip_frags.erase(i);
return result;
}
return 0;
}
if(!(cur.ip.bmore_fr)){
new_defrag.tot_len = cur.ip.fr_offs + cur.len;
} else {
new_defrag.tot_len = 0;
}
new_defrag.cur_len = cur.ip.len; // with header size
new_defrag.last_time = time();
i = ip_frags.insert(new_defrag).first;
if(i != ip_frags.end())
i->packets.insert(pcurpack);
return 0;
}
compiled project and view only 2 errors similar
line 15 : i->packets.insert(pcurpack);
end line : i->packets.insert(pcurpack);
error with 2 lines : error C2663: 'std::_Tree<_Traits>::insert' : 4 overloads have no legal conversion for 'this' pointer
IntelliSense: no instance of overloaded function "std::set<_Kty, _Pr, _Alloc>::insert [with _Kty=IP *, _Pr=std::less<IP *>, _Alloc=std::allocator<IP *>]" matches the argument list and object (the object has type qualifiers that prevent a match)
please help me?
I had exact same error with std::set, while passing it to a lambda expression:
C2663 'std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::insert': 5 overloads have no legal conversion for 'this' pointer
lambda expression prototype was:
[se1, ele1](auto val) {
/* here I was editing se1 set, but above se1 is passed value type */
}
I have changed to:
[&se1, ele1](auto val) {
/* now since se1 set is sent as reference type above, it is good to edit
changes stays as I expect it to be
*/
}
Now compilation succeeds.
I used with count_if function, which calls the lamda expression for eac element, so the compiler knows that modifications should persist in set se1, which is perfectly logical.
If you desire to have the original set unchanged, then send a copy of it.