Skip to content

Commit 1bd5657

Browse files
author
Adam Horvath
committed
Added examples for
+ Move-menatics & R-value references + Lambdas
1 parent 81b1cfd commit 1bd5657

File tree

6 files changed

+266
-4
lines changed

6 files changed

+266
-4
lines changed

src/Lambdas.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ void Lambdas::TestCountIf()
3535
std::endl;
3636
}
3737

38+
3839
void Lambdas::TestCapture()
3940
{
4041
int myMoney = 1000;
4142
auto addSome = [&myMoney](int addition){ myMoney += addition; };
4243
auto multiplySome = [&myMoney](int mult){ myMoney *= mult; };
44+
++myMoney;
4345
auto printMoney = [myMoney](){ std::cout << "My Money: " << myMoney << std::endl; };
4446
// Can you see a bug above?
4547

src/MemoryBlock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MemoryBlock
5757
return *this;
5858
}
5959

60-
//#define cpp11
60+
#define cpp11
6161
#ifdef cpp11
6262
// NEW: Move constructor
6363
MemoryBlock(MemoryBlock&& other) noexcept: m_pData(nullptr), m_size(0)

src/TestItAgainSam.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <chrono>
55
#include <ctime>
66
#include <thread>
7+
#include <utility>
78

89
#include "HeaderInit.h"
910

@@ -41,11 +42,11 @@ void TestItAgainSam::Run() const
4142
//TestLegacyEnums();
4243
//TestEnums();
4344

44-
TestAsync();
45+
//TestAsync();
4546

4647
//TestMove();
4748

48-
//TestLambdas();
49+
TestLambdas();
4950

5051
// This is only to keep the command prompt alive when running inside Visual Studio
5152
//std::this_thread::sleep_for(std::chrono::seconds(30));
@@ -183,7 +184,7 @@ void TestItAgainSam::TestMove() const
183184

184185
std::vector<MemoryBlock> vec;
185186

186-
vec.push_back(MemoryBlock(2000));
187+
vec.emplace(vec.begin(), 2000);
187188
std::cout << " === " << std::endl;
188189
vec.insert(vec.begin(), MemoryBlock(1000));
189190
std::cout << " === " << std::endl;

src/schematron_example/Sequence.h

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#ifndef _SEQUENCE_H_
2+
#define _SEQUENCE_H_
3+
4+
#include <list>
5+
6+
#include "Value.h"
7+
8+
namespace MafXpathParser
9+
{
10+
11+
// A class that represents a xpath Sequence.
12+
class Sequence
13+
{
14+
public:
15+
Sequence();
16+
explicit Sequence(const std::list<const Value*>& nodes);
17+
explicit Sequence(const Value* expr);
18+
Sequence(const Sequence& s);
19+
/**
20+
* no virtual destructor since there is a need
21+
* for this class to be memory efficient.
22+
* Also the NotOwningSequence dont want this destructor
23+
* to be called.
24+
*/
25+
~Sequence();
26+
27+
std::ostream& put(std::ostream& os) const;
28+
const std::string toString(const std::string& networkManagedElementId="") const;
29+
bool exists(EvalContext& evalContext) const;
30+
bool isTrue() const;
31+
32+
/**
33+
* Methods to access/add values etc
34+
*/
35+
36+
/**
37+
* Since the Dns class has a set of values, counting is needed.
38+
* this should not be too bad since size on list has O(n)
39+
* complexity according to specs.
40+
*/
41+
size_t size() const
42+
{
43+
size_t result = 0;
44+
for (std::list<const Value*>::const_iterator i = _values.begin();
45+
i != _values.end();
46+
++i) {
47+
result += (*i)->size();
48+
}
49+
return result;
50+
}
51+
52+
void addValue(const Value* n);
53+
void addValueFront(const Value* n);
54+
const Value& firstValue() const;
55+
const Value& secondValue() const;
56+
std::list<const Value*>& getValues();
57+
const std::list<const Value*>& getValues() const;
58+
void splice(const Sequence& s)
59+
{
60+
_values.splice(_values.end(), const_cast<Sequence&>(s)._values);
61+
}
62+
struct Destroy {
63+
void operator ()(const Value* v)
64+
{
65+
delete v;
66+
}
67+
};
68+
private:
69+
Sequence& operator = (const Sequence&); // Not allowed
70+
struct NotExist {
71+
NotExist(EvalContext& evalContext): _evalContext(evalContext) {}
72+
bool operator() (const Value* v)
73+
{
74+
return !v->exists(_evalContext);
75+
}
76+
EvalContext& _evalContext;
77+
};
78+
struct Copy {
79+
Copy(std::list<const Value*>& values) : _values(values)
80+
{
81+
;
82+
}
83+
void operator ()(const Value* v)
84+
{
85+
_values.push_back(v->clone());
86+
}
87+
std::list<const Value*>& _values;
88+
};
89+
protected:
90+
/**
91+
* A sequence is a list of values
92+
*/
93+
std::list<const Value*> _values;
94+
};
95+
96+
// Class to make sure the destructor does not delete
97+
// the values in the sequence.
98+
class NotOwningSequence : public Sequence
99+
{
100+
public:
101+
~NotOwningSequence()
102+
{
103+
_values.clear();
104+
}
105+
};
106+
107+
inline
108+
std::ostream&
109+
operator<<(std::ostream& os, const Sequence& s)
110+
{
111+
s.put(os);
112+
return os;
113+
}
114+
115+
} // namespace XpathParser
116+
117+
#endif

src/schematron_example/Struct.cc

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <map>
4+
5+
#include "Sequence.h"
6+
#include "Struct.h"
7+
8+
using namespace MafXpathParser;
9+
10+
Struct::Struct(const std::string& name) : _name(name)
11+
{
12+
}
13+
14+
Struct::Struct(const Struct& s) : _name(s._name)
15+
{
16+
// C++03
17+
Clone cloneObj(_attributes);
18+
std::for_each(s._attributes.begin(), s._attributes.end(), cloneObj);
19+
20+
// C++14 - Here is another nifty use of lambdas
21+
// This could be re-written something like below
22+
// We no longer need the "class Clone" functor.
23+
auto cloneFunc = [this](auto& attr){this->_attributes[attr.first] = new Sequence(*(attr.second));
24+
for(const auto& attr: s._attributes) cloneFunc();
25+
26+
// Or
27+
auto cloneFunc2 = [this](auto& attr){this->_attributes[attr->first] = new Sequence(*(attr->second));
28+
std::for_each(s._attributes.begin(), s._attributes.end(), cloneFunc2);
29+
30+
// A nice explanation of the template expansion of [this](auto& attr)
31+
// Can be found here:
32+
// http://stackoverflow.com/questions/17233547/how-does-generic-lambda-work-in-c14/17233649#17233649
33+
}
34+
35+
Struct::~Struct()
36+
{
37+
Destroy destroy;
38+
(void) std::for_each(_attributes.begin(), _attributes.end(), destroy);
39+
}
40+
41+
void
42+
Struct::lookupAttribute(const std::string& name,
43+
Sequence& result) const
44+
{
45+
std::map<std::string, Sequence*>::const_iterator it =
46+
_attributes.find(name);
47+
if (it != _attributes.end()) {
48+
Sequence tmp(*(it->second));
49+
result.splice(tmp);
50+
}
51+
}
52+
53+
void
54+
Struct::addAttribute(const std::string& name, Sequence* value)
55+
{
56+
_attributes[name] = value;
57+
}
58+
59+
std::ostream&
60+
Struct::put(std::ostream& os) const
61+
{
62+
os << "(Struct " << _name << ")";
63+
return os;
64+
}
65+
66+
const Value*
67+
Struct::clone() const
68+
{
69+
return new Struct(*this);
70+
}
71+
72+
const std::string
73+
Struct::toString(const std::string& networkManagedElementId) const
74+
{
75+
std::string result("struct(");
76+
77+
for (std::map<std::string, Sequence*>::const_iterator i = _attributes.begin();
78+
i != _attributes.end();
79+
++i) {
80+
if (i != _attributes.begin()) {
81+
result += ", ";
82+
}
83+
result += i->first;
84+
result += "=";
85+
result += i->second->toString(networkManagedElementId);
86+
}
87+
88+
result += ")";
89+
90+
return result;
91+
}

src/schematron_example/Struct.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef _STRUCT_HH_
2+
#define _STRUCT_HH_
3+
4+
#include <string>
5+
#include <map>
6+
7+
#include "Value.h"
8+
9+
namespace MafXpathParser
10+
{
11+
12+
// A value class representing struct attributes.
13+
// It is created _not_ by the bison parser but is a result of
14+
// looking up a struct attribute in the db.
15+
16+
class Struct : public Value
17+
{
18+
public:
19+
explicit Struct(const std::string& name);
20+
~Struct();
21+
void lookupAttribute(const std::string& name, Sequence& result) const;
22+
void addAttribute(const std::string& name, Sequence* value);
23+
std::ostream& put(std::ostream& os) const;
24+
const Value* clone() const;
25+
const std::string toString(const std::string& networkManagedElementId="") const;
26+
private:
27+
Struct(const Struct& name);
28+
29+
struct Destroy {
30+
void operator() (const std::pair<const std::string, Sequence*>& p)
31+
{
32+
delete p.second;
33+
}
34+
};
35+
36+
struct Clone {
37+
Clone(std::map<std::string, Sequence*>& attributes) : _attributes(attributes) {}
38+
void operator() (const std::pair<const std::string, Sequence*>& p)
39+
{
40+
_attributes[p.first] = new Sequence(*(p.second));
41+
}
42+
std::map<std::string, Sequence*>& _attributes;
43+
};
44+
45+
std::string _name;
46+
std::map<std::string, Sequence*> _attributes; // name, sequence of values
47+
friend class Db;
48+
};
49+
50+
} // namespace XpathParser
51+
#endif

0 commit comments

Comments
 (0)