Post

Useful Code Snippets

C/C++

Rule Of Three

1
2
3
4
5
6
7
8
template<typename Function>
inline auto LazyLoad(HMODULE library, const std::string& procName) {
    return (library) ? reinterpret_cast<Function*>(GetProcAddress(library, procName.data())) : nullptr;
}
#define LAZY_LOAD_PROC(LIBRARY, PROC) \
    auto lazy_##PROC{ LazyLoad<decltype(PROC)>(LIBRARY, #PROC) };

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class IObject
{
public:
	virtual ~IObject() {}
	virtual int CompareWith(const IObject &) const { throw std::runtime_error("Abstract method"); }
};

template <typename Object>
class ObjectList : public IObject
{
	//not implemented
	ObjectList &operator=(const ObjectList &);
public:
	explicit ObjectList() : IObject() {}
	explicit ObjectList(const ObjectList &src) : IObject(src) {}
	virtual ~ObjectList() { clear(); }
	virtual void clear()
	{
		while (!v_.empty()) {
			delete v_.back();
		}
	}
	void Delete(size_t index)
	{
		if (index >= v_.size())
			throw std::runtime_error("subscript out of range");
		delete v_[index]; 
	}
	size_t count() const { return v_.size(); }
	Object *item(size_t index) const
	{ 
		if (index >= v_.size())
			throw std::runtime_error("subscript out of range");
		return v_[index]; 
	}
	void resize(size_t size) {
		v_.resize(size);
	}
	Object *last() const { return v_.empty() ? NULL : *v_.rbegin(); }
	static bool CompareObjects(const Object *obj1, const Object *obj2) { return obj1->CompareWith(*obj2) < 0; }
	void Sort() { std::sort(v_.begin(), v_.end(), CompareObjects); }
	typedef typename std::vector<Object*>::const_iterator const_iterator;
	typedef typename std::vector<Object*>::iterator iterator;
	size_t IndexOf(const Object *obj) const
	{
		const_iterator it = std::find(v_.begin(), v_.end(), obj);
		return (it == v_.end()) ? -1 : it - v_.begin();
	}

	size_t IndexOf(const Object *obj, size_t index) const
	{
		const_iterator it = std::find((v_.begin()+index), v_.end(), obj);
		return (it == v_.end()) ? -1 : it - v_.begin();
	}
	void SwapObjects(size_t i, size_t j) { std::swap(v_[i], v_[j]); }
	virtual void AddObject(Object *obj) { v_.push_back(obj); }
	virtual void InsertObject(size_t index, Object *obj) { v_.insert(v_.begin() + index, obj); }
	virtual void RemoveObject(Object *obj)
	{ 
		for (size_t i = count(); i > 0; i--) {
			if (item(i - 1) == obj) {
				erase(i - 1);
				break;
			}
		}
	}
	void erase(size_t index) { v_.erase(v_.begin() + index); }
	void assign(const std::list<Object*> &src) 
	{ 
		v_.clear(); 
		for (typename std::list<Object*>::const_iterator it = src.begin(); it != src.end(); it++) {
			v_.push_back(*it);
		}
	}

	const_iterator begin() const { return v_.begin(); }
	const_iterator end() const { return v_.end(); }

	iterator _begin() { return v_.begin(); }
	iterator _end() { return v_.end(); }

protected:
	void Reserve(size_t count) { v_.reserve(count); }
	std::vector<Object*> v_;
};

Python

1
2
3
4
5
6
7
8
9
10
11
12
def enableHTTPdebug():
    try:
        import http.client as http_client
    except ImportError:
    # Python 2
        import httplib as http_client
        http_client.HTTPConnection.debuglevel = 1
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
This post is licensed under CC BY 4.0 by the author.