I am using libdbus c++ for dbus ipc communication, i don't know how to pass a structure as a argument in method call. can any one help me?
mercredi 1 février 2017
Range-based loop use another operator
I have a prime generator based on what I've seen in python by Sieve of Eratosthenes so this generator basically generate prime numbers with good performances.
What I would want is to use the range based loop on a range of prime number so here is what I did :
//Consider prime_generator a class with both operator*, operator!= and operator< overloaded
class primes_range {
private:
unsigned int max;
public:
primes_range(unsigned int max) : max(max) {}
prime_generator begin() const {
return prime_generator(); //so this begin from 2 to
//infinity and beyond but of course
//all primes
}
prime_generator end() const {
prime_generator result;
for (:*result < max; ++result) {} //so this thing actually create a
//generator and increment it until it
//gives the first prime number
//under max so it basically do
//all the work that I don't
//want it to do now
return rest;
}
};
So in my main, I would want to use the range-based loop, that's the point of the primes_range class.
int main() {
for (auto && i : primes_range(10)) { //So here, this is silly because
//the range-based loop will use end()
//wich will calculate all the prime
//numbers at the very beginning
//and i will increment apart from
//this starting process
cout << i << endl;
}
return 0;
}
Of course instead I could use a simple loop :
int main() {
for (prime_generator pg; *pg < 10; ++pg) {
cout << *pg << endl;
}
return 0;
}
But because the range-base loop is easier to read and prevent to use the operator*, I would want to use it instead, so my question is : Is there a way to make the range-base loop use another operator than != (in this case, I want it to stay inferior to max so it should use >=) ? Maybe overloard a particular function for primes_range or specialize a comparator ?
How to use std::string effectively in the C-style functions which deals with conventional c-strings?
This is an old problem, which I have observed in past. So thought of getting a clarification once & for all. There are many standard / orthodox C library functions, which deal only with C-style strings. For example, my current implementation looks like this:
std::string TimeStamp (const time_t seconds) // version-1
{
auto tm = *std::localtime(&seconds); // <ctime>
char readable[30] = {};
std::strftime(&readable[0], sizeof(readable) - 1, "%Y-%h-%d %H:%M:%S:", &tm);
return readable;
}
Above works as expected. But as you can see, that the readable is copied from stack array to std::string. Now this function is called very frequently for logging & other purposes.
Hence, I converted it to following:
std::string TimeStamp (const time_t seconds) // version-2
{
auto tm = *std::localtime(&seconds); // <ctime>
std::string readable(30,0);
std::strftime(&readable[0], readable.length(), "%Y-%h-%d %H:%M:%S:", &tm);
return readable;
}
At unit test level, it apparently seems to work. But for overall logging in my much larger code, it somehow gets messed up. A new line character appears after this output & many of the output strings which are called outside this function are not printed. Such issue happens only when the "version-1" is changed to "version-2".
Even following modification also doesn't help:
readable.resize(1 + std::strftime(&readable[0], readable.length(), "%Y-%h-%d %H:%M:%S:", &tm));
Is there anything wrong in my code? What is the correct way of directly using std::string in the C-style string functions?
thread safety in a signal-slot system (C++11)
I have some problems designing a Signal/Slot system in C++11.
My main design goals are: simple but still offering some features and thread safe. My personal opinion on a Signal/Slot system is that emitting should be as fast as possible. Because of that I try to keep the slot list inside the signal tidy. Many other Signal/Slot systems leave disconnected slots empty. That means more slots to iterate and checking slot validity during signal emission.
Here is the concrete problem:
Signal class have one function for emitting and one function for disconnecting a slot:
template<typename... Args>
void Signal<void(Args...)>::operator()(Args&&... args)
{
std::lock_guard<std::mutex> mutex_lock(_mutex);
for (auto const& slot : _slots) {
if (slot.connection_data->enabled) {
slot.callback(std::forward<Args>(args)...);
}
}
}
template<typename... Args>
void Signal<void(Args...)>::destroy_connection(std::shared_ptr<Connection::Data> connection_data)
{
std::lock_guard<std::mutex> mutex_lock(_mutex);
connection_data->reset();
for (auto it = _slots.begin(); it != _slots.end(); ++it) {
if (it->connection_data == connection_data) {
*it = _slots.back(); _slots.pop_back();
break;
}
}
}
This works fine until one tries to make a connection that disconnects itself when signal i emitted:
Connection con;
Signal<void()> sig;
con = sig.connect([&]() { con.disconnect(); });
sig();
I have two problems here:
- The emit function must probably be redesigned because slots can potentially be removed when iterated.
- There are two mutex locks inside the same thread.
Is it possible to make this work (maybe with recursive mutex?), or should I redesign the system to not interfere with slots list and just leave empty slots (as many other similar projects do) when disconnecting the signal?
C++ event to notify when any application window opens
How to get an event with window handle or process id when any application window opens eg:) get the process id of calculator when some on opens calculator application
Value of variable changes
In the code below, I am trying to store the value of array at index 0 in the temp variable. In this line of code: a[i-1]=a[i]-a[i-1]; when i=0, a[i-1] becomes a[-1].
- Why is compiler not giving any error?
- Why does the value of temp variable is affected and becomes zero after the first iteration, though it is assigned a value only when i=0 and temp is not used anywhere else?
For example, when I gave input as:
3 1 2 3
Output:
i:0
a[0]: 1
TEMP: 1
TEMP: 0
TEMP: 0
TEMP: 0
What's actually happening? Please explain with reference to the working of compiler. I know that if I put a condition if(i!=0) a[i-1]=a[i]-a[i-1]; the code will work normally. But I want to know why is this happening with the given scenario.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[10],i,n,temp;
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
if(i==0){
temp=a[i];
cout<<"i: "<<i<<endl;
cout<<"a[0]: "<<a[i]<<endl;
}
cout<<"TEMP: "<<temp<<endl;
a[i-1]=a[i]-a[i-1];
}
cout<<endl<<"TEMP: "<<temp;
}
mardi 31 janvier 2017
Is there any way to trick std::make_shared into using default initialization?
You are expected to use std::make_shared to ensure that block with counters is stored next to data. Unfortunately internally std::make_shared uses zero initialization for T (i.e. uses T() to initialize data block). Is there any way to trick it into using default initialization? I know I can use std::shared_ptr( new T, [](auto p){delete p;}), but I'll end up with two allocations here (data and counter blocks won't be next to each other)