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.
Related
I have a map structure as below which has shared_pointer of a protobuf structure Stats:
map<string, shared_pointer<vol::proto::Stats> statsMap;
I am storing and retrieving the map entries through a LRU cache implementation
template class LRUCache<string,std::shared_ptr<vol::proto::Stats>>;
Protobuf structure:-
message Stats {
required string oid = 1;
required int64 logical_size = 2;
required int64 committed_size = 3;
}
message StatsRequest {
required string uuid = 1;
}
The function for storing entry into the map:-
template<class K, class V>
void
LRUCache<K, V>::Put(const K& key, V value)
{
Vmacore::System::WriteSynchronized lock(this);
auto val = refMap.find(key);
if (val == refMap.end()) {
if (cacheSize == refMap.size()) {
K leastUsedKey = lruList.back();
lruList.pop_back();
refMap.erase(leastUsedKey);
}
} else {
lruList.erase(val->second.lruKeyRef);
}
lruList.push_front(key);
refMap[key] = LRUValueReference<K, V>(lruList.begin(), value);
}
void PutS(const string& Id, const vol::proto::Stats& info)
{
shared_ptr<vol::proto::Stats> statsInfo = make_shared<vol::proto::Stats>(info);
_StatsCache.Put(Id, statsInfo);
}
void PutStats(vol::proto::StatsRequest &req) {
vol::proto::Stats *stats;
GetStats(stats); //stats gets populated here.
PutS(stats->oid(), *stats);
}
To Get the entry from the map:-
template<class K, class V>
bool
LRUCache<K, V>::Get(const K& key, V& value)
{
Vmacore::System::WriteSynchronized lock(this);
auto val = refMap.find(key);
if (val == refMap.end()) {
return false;
}
lruList.splice(lruList.begin(), lruList, val->second.lruKeyRef);
val->second.lruKeyRef = lruList.begin();
value = val->second.value;
return true;
}
void GetS(const string& Id, shared_ptr<vol::proto::Stats> info)
{
if (!_StatsCache.Get(Id, info))
return false;
return true;
}
void GetStats(vol::proto::StatsRequest &req) {
shared_ptr<vol::proto::Stats> stats;
if (GetS(req.uuid(), stats)) {
Log("object id is %s", stats.get()->oid()); // Crash at oid (this=0x0)
}
}
Now while doing GetStats, I am seeing a crash as to in GetStats's Log statement that is program terminated with signal SIGSEGV, Segmentation fault. Should I do a make_share for Stats in GetStats? Can you please describe as to what has caused this crash and how to fix the crash here?
You're passing a raw pointer from GetStats to GetS, which then wraps it into a shared_ptr. Now lets start ref-counting.
GetS -> increase on entry > 1
_StatsCache.Get -> pass by ref > 1
GetS -> decrease on exit > 0
Ref count 0 -> delete the pointer.
In GetStats you then try to de-reference the stats pointer by calling .get, but it was just deleted.
You should either always use smart pointers, or be very very very careful when you convert from raw pointer to smart pointer, and back.
Actually what I wrote is wrong. What you're doing is actually much worse.
You're wrapping a raw pointer in GetS into a smart pointer, and then pass it by ref to your _StatsCache.Get, which then assigns a different shared_ptr to yours. So you've overwritten the original raw pointer with something else.
But when GetS returns, that shared_ptr is now lost and destructed, but the original stats pointer is accessed in stats.get(). What's in it? Nothing from GetS, that's for sure.
I'm currently trying for a game project to get rid of a dangling reference when a plane crashes before reaching the terminal it was booked to reach.
I would like to go through <algorithm> functions only to better understand how they work.
At the moment I've tried going through the map that contains all the planes associated with a terminal, comparing it with the list of all the planes and checking if a plane that is in the map is no longer in the vector then delete it from the map to free the associated terminal.
void remove_crashed_aircraft(std::unordered_map<const Aircraft*, size_t>& reserved_terminals, std::vector<std::unique_ptr<Aircraft>>& aircrafts)
{
auto it = std::all_of(reserved_terminals.begin(), reserved_terminals.end(),
[aircrafts](const Aircraft* a1){ return std::find_if(aircrafts.begin(), aircrafts.end(),
[a1](std::unique_ptr<Aircraft> a2){ return a1==a2.get();});});
reserved_terminals.erase(it);
}
And this is my Aircraft class:
class Aircraft : public GL::Displayable, public GL::DynamicObject
{
private:
const AircraftType& type;
const std::string flight_number;
Point3D pos, speed; // note: the speed should always be normalized to length 'speed'
WaypointQueue waypoints = {};
Tower& control;
bool landing_gear_deployed = false; // is the landing gear deployed?
bool is_at_terminal = false;
int fuel = 0;
// turn the aircraft to arrive at the next waypoint
// try to facilitate reaching the waypoint after the next by facing the
// right way to this end, we try to face the point Z on the line spanned by
// the next two waypoints such that Z's distance to the next waypoint is
// half our distance so: |w1 - pos| = d and [w1 - w2].normalize() = W and Z
// = w1 + W*d/2
void turn_to_waypoint();
void turn(Point3D direction);
// select the correct tile in the plane texture (series of 8 sprites facing
// [North, NW, W, SW, S, SE, E, NE])
unsigned int get_speed_octant() const;
// when we arrive at a terminal, signal the tower
void arrive_at_terminal();
// deploy and retract landing gear depending on next waypoints
void operate_landing_gear();
void add_waypoint(const Waypoint& wp, const bool front);
bool is_on_ground() const { return pos.z() < DISTANCE_THRESHOLD; }
float max_speed() const { return is_on_ground() ? type.max_ground_speed : type.max_air_speed; }
bool is_paused = false;
Aircraft(const Aircraft&) = delete;
Aircraft& operator=(const Aircraft&) = delete;
public:
Aircraft(const AircraftType& type_, const std::string_view& flight_number_, const Point3D& pos_,
const Point3D& speed_, Tower& control_, int fuel_) :
GL::Displayable { pos_.x() + pos_.y() },
type { type_ },
flight_number { flight_number_ },
pos { pos_ },
speed { speed_ },
control { control_ },
fuel { fuel_ }
{
speed.cap_length(max_speed());
}
const std::string& get_flight_num() const { return flight_number; }
float distance_to(const Point3D& p) const { return pos.distance_to(p); }
bool is_circling() const
{
if (!has_terminal() && !is_at_terminal)
{
return true;
}
return false;
}
bool has_terminal() const
{
if (waypoints.empty())
{
return false;
}
else
{
return waypoints.back().type == wp_terminal;
}
}
bool is_low_on_fuel() const
{
if (fuel<200)
{
return true;
}
else
{
return false;
}
}
void display() const override;
bool move() override;
void refill(int& fuel_stock);
friend class Tower;
friend class AircraftManager;
};
The code of the function generates errors that I can't understand unfortunately.
use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Aircraft; _Dp = std::default_delete<Aircraft>]’
static assertion failed: result type must be constructible from value type of input range
If anyone has an idea of how I can achieve this, I would be very grateful!
First, you have to use std::remove_if, because std::all_of returns bool, not iterator. But std::remove_if does what you want, it removes all instances that match bool predicate.
Second, your compile errors appear because you pass by value everywhere, so instead of (std::unique_ptr<Aircraft> a2) pass reference (std::unique_ptr<Aircraft> const & a2), instead [aircrafts] and [a1] pass [&aircrafts] and [&a1].
Third, inner predicate should not just return result of std::find_if (which returns iterator) but return bool, i.e. compare result of find_if to aircrafts.end().
Speed-wise your algorithm can be optimized if needed, for that you have to convert std::vector to std::unordered_set and then iterate through first map and check containment inside second set. If you don't have too many elements and speed is not of much importance then your algorithm is alright.
Final working code below:
void remove_crashed_aircraft(
std::unordered_map<const Aircraft*, size_t>& reserved_terminals,
std::vector<std::unique_ptr<Aircraft>>& aircrafts
) {
std::remove_if(reserved_terminals.begin(), reserved_terminals.end(),
[&aircrafts](auto const & a1){
return std::find_if(aircrafts.begin(), aircrafts.end(),
[&a1](auto const & a2){ return a1.first == a2.get(); })
== aircrafts.end();
}
);
}
So I have been working on an Arduino program that sends MIDI data over USB, and I can't seem to figure out how to pass the function that handles sending of midi data to my main class. It worked at some point but for some reason, it doesn't, and I feel like I have tried every combination of things.
Here is my main file, simplified down to just output the MIDI data over serial:
#include "XiaoPiezoDrum.h"
void sendNoteOn(int note, int velocity, int chan) {
Serial.printf("WORKS! %d %d %d\n", note, velocity, chan);
};
XiaoPiezoDrum drum(9, 2, 3, 4);
void setup() {
drum.setNoteComFunctions(sendNoteOn);
}
Here is what that setNoteComFunctions method looks like:
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = [onFunc](auto && PH1, auto && PH2, auto && PH3) { onFunc(PH1, PH2, PH3); };
}
And here is the relevant parts of the main class definition:
class XiaoPiezoDrum {
public:
XiaoPiezoDrum();
XiaoPiezoDrum(int piezoPin, int rPin, int gPin, int bPin);
void setNoteComFunctions(const function<void(int, int, int)>& onFunc);
void RunCycle();
private:
std::function<void(int, int, int)> sendNote;
}
So everything runs just fine until I call sendNote from within RunCycle, which is just called every loop, and then serial communication just stops suddenly. What am I doing wrong? I also tried using bind instead of a lambda, and the same thing happens. When I just copy the same print statement that is in sendNoteOn in the main file into the lambda in setNoteComFunctions, it prints just fine, so I know the issue is all about linking the function from the main file.
Things I have tried:
passing the function in setNoteComFunctions both as a reference and not
using bind rather than lambda
changing the body of sendNoteOn
making the lambda arguments references
making the lambda arguments not references
returning onFunc in the lambda
I have been banging my head against the keyboard for hours with this. If anybody knows what I'm doing wrong, I'd love an explanation!
EDIT
Here is the full code of RunCycle. Many of these variables are instance variables that I left out of the provided class definition, but I know the issue is with the call of sendNote, because if I comment it out, things continue running normally. I have also tried replacing the velocity variable with a fixed integer, and the same thing happened.
void XiaoPiezoDrum::RunCycle() {
double val = sensor.read();
val = (val > 0) ? val : 0;
Serial.println(val);
unsigned long timeElapsed;
int velocity;
MaxVal = (val > MaxVal) ? val : MaxVal;
trigger = val > THRESHOLD && !triggering;
if (val > THRESHOLD && !triggering) trigger = true;
if (trigger) {
triggerTime = millis();
trigger = false;
triggering = true;
triggerBuffer.clear();
}
if (triggering) {
timeElapsed = millis() - triggerTime;
if (timeElapsed < SAMPLE_TIME) {
loopCounter++;
triggerBuffer.addValue(val);
Serial.println(val);
}
else {
velocity = map(round(triggerBuffer.getMax()), 0, 300, THRESHOLD, 127);
Serial.printf("Velocity: %d\n", velocity);
if (comFunctionsSet) sendNote(40, velocity, 1);
noteEndTime = millis();
triggerTime = 0;
triggerBuffer.clear();
triggering = false;
resting = true;
}
}
}
jignatius' answer did it! I should have known I could do that!! Thanks!
I just had to replace
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = [onFunc](auto && PH1, auto && PH2, auto && PH3) { onFunc(PH1, PH2, PH3); };
}
with:
void XiaoPiezoDrum::setNoteComFunctions(const std::function<void(int, int, int)>& onFunc) {
sendNote = onFunc;
}
So glad to have that solved! Thanks!
I'm trying to use dht to keep mutable data with libtorrent. As far as I can understand, the right way is to use the method dht_put_item from session. The problem is that I need to pass a callback function and I don't know what I'm doing wrong... my code looks like the following
namespace lt = libtorrent;
//The callback function
void cb(lt::entry& cdentry, boost::array<char,64>& cbarray, boost::uint64_t& cbint, std::string const& cbstring){
//My stuff here
}
void main(){
//The session
lt::session ses;
//The data I want to insert into DHT
std::string cadenaStr = "519d818411de49652b4aaf34850321de28bb2dce";
//Now I create the keys
unsigned char seed[32];
unsigned char public_key[32];
unsigned char private_key[64];
unsigned char signature[32];
ed25519_create_seed(seed);
ed25519_create_keypair(public_key, private_key, seed);
ed25519_sign(signature, cadenaStr.c_str(), sizeof(cadenaStr.c_str()), public_key, private_key);
//How can I use this?, where is the data supposed to go? :|
ses.dht_put_item(public_key, cb, false);
}
On libtorrent/session_handler.hpp this method is defined as
void dht_put_item(boost::array<char, 32> key
, boost::function<void(entry&, boost::array<char,64>&
, boost::uint64_t&, std::string const&)> cb
, std::string salt = std::string());
Can someone please tell me what I'm doing wrong.
Thanks!
There is an example in the libtorrent repository that I use for testing. It can generate keys, put and get both mutable and immutable items.
https://github.com/arvidn/libtorrent/blob/master/tools/dht_put.cpp
How can I use this?, where is the data supposed to go? :|
You provide the data in the callback that's called. The reason for this kind of API is that there are use cases where you want to mutate the data, and then you need to first know whether something is already stored under this key, and what it is.
You are missing the settings pack for your session.
lt::settings_pack settings;
settings.set_bool(settings_pack::enable_dht, false);
settings.set_int(settings_pack::alert_mask, 0xffffffff);
ses.apply_settings(settings);
settings.set_bool(settings_pack::enable_dht, true);
ses.apply_settings(settings);
Then you need to wait until you receive a boostrap message by waiting for an alert.
wait_for_alert(ses, dht_bootstrap_alert::alert_type);
Last, your dht_put_item call:
char const* cadenaStr = "519d818411de49652b4aaf34850321de28bb2dce";
dht_put_item(public_key, std::bind(&put_string, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, public_key, private_key, cadenaStr));
You will need these functions:
static alert* wait_for_alert(session* ses, int alert_type)
{
alert* ret = nullptr;
bool found = false;
while (!found)
{
ses->wait_for_alert(seconds(5));
std::vector<alert*> alerts;
ses->pop_alerts(&alerts);
for (std::vector<alert*>::iterator i = alerts.begin()
, end(alerts.end()); i != end; ++i)
{
if ((*i)->type() != alert_type)
{
continue;
}
ret = *i;
found = true;
}
}
return ret;
}
static void put_string(
entry& e
,boost::array<char, 64>& sig
,boost::int64_t& seq
,std::string const& salt
,boost::array<char, 32> const& pk
,boost::array<char, 64> const& sk
,char const* str)
{
using dht::sign_mutable_item;
if (str != NULL) {
e = std::string(str);
std::vector<char> buf;
bencode(std::back_inserter(buf), e);
dht::signature sign;
seq++;
sign = sign_mutable_item(buf, salt, dht::sequence_number(seq)
, dht::public_key(pk.data())
, dht::secret_key(sk.data()));
sig = sign.bytes;
}
}
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)