class DataStore
{
public:
using Date = std::string;
using Path = std::string;
using Paths = std::set < Path > ;
using PathIter = Paths::iterator;
using ListOfIters = std::list < PathIter > ;
using Item = std::pair<PathIter, Date>;
using ListOfItems = std::list<Item>;
using File = std::string;
using Store = std::map < File, ListOfItems >;
using iterator = Store::iterator;
using PathCollection = std::vector < Path > ;
void save(const std::string& filespec);
void save(const std::string& filename, const std::string& path);
PathCollection getPaths(const File& file);
size_t numberOfFiles();
size_t numberOfPaths();
iterator begin() { return store.begin(); }
iterator end() { return store.end(); }
private:
Store store;
Paths paths;
};
inline size_t DataStore::numberOfFiles() { return store.size(); }
inline size_t DataStore::numberOfPaths() { return paths.size(); }
class AFunctor {
public:
AFunctor(const X& x) : x_(x) {}
void operator()(const Y& y) { /* do something useful to x_, using y */ }
X value() { return x_; }
private:
X x_;
};
AFunctor func;
Y y;
func(y); // syntactically looks like a function call
func.operator(y); // equivalent to the statement above
Functors are used in CallableObjects code demo in CppBasicDemos
repository.
std::function makeLambda(const std::string& msg)
{
// [=] implies capture by value, [&] implies capture by reference
std::function fun = [=]()
{
std::cout << "\n displaying captured data: " << msg;
};
return fun;
}
// in some other scope
std::function myFun = makeLambda("hello CSE687-OnLine");
std::cout << "\n using Lambda: " << myFun() << "\n";
CodeSnap-LambdaCapture.cpp.html
template<typename T>
void invoker(T& callable) // callable could be a lamda with captured data
{
callable(); // use captured data instead of function arguments
}
CppBasicDemos Repository holds Callable Objects demo code
template <typename T>
class BlockingQueue {
public:
BlockingQueue() {}
BlockingQueue(BlockingQueue<T>&& bq);
BlockingQueue<T>& operator=(BlockingQueue<T>&& bq);
BlockingQueue(const BlockingQueue<T>&) = delete;
BlockingQueue<T>& operator=(const BlockingQueue<T>&) = delete;
T deQ();
void enQ(const T& t);
T& front();
void clear();
size_t size();
private:
std::queue<T> q_;
std::mutex mtx_;
std::condition_variable cv_;
};