Skip to content

Commit 068b3cc

Browse files
committed
replaced call-back classes by lambda expressions
1 parent fbf5489 commit 068b3cc

File tree

9 files changed

+65
-220
lines changed

9 files changed

+65
-220
lines changed

include/brlcad/Database/ConstDatabase.h

+9-38
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,11 @@ namespace BRLCAD {
9797

9898
/// @name Accessing objects
9999
//@{
100-
class ObjectCallback {
101-
public:
102-
virtual ~ObjectCallback(void) {}
103-
104-
/// the user has to implement this object method to evaluate the object
105-
virtual void operator()(const Object& object) = 0;
106-
107-
protected:
108-
ObjectCallback(void) {}
109-
ObjectCallback(const ObjectCallback&) {}
110-
const ObjectCallback& operator=(const ObjectCallback&) {return *this;}
111-
};
100+
typedef std::function<void(const Object& object)> ObjectCallback;
112101

113102
/// selects a single object and hand it over to an ObjectCallback (read only)
114-
void Get(const char* objectName,
115-
ObjectCallback& callback) const;
103+
void Get(const char* objectName,
104+
const ObjectCallback& callback) const;
116105

117106
/// overloaded member function, provided for convenience: selects a single object and and returns a copy of it
118107
/** Do not forget to BRLCAD::Object::Destroy() the copy when you are finished with it! */
@@ -171,35 +160,17 @@ namespace BRLCAD {
171160
const Hit& operator=(const Hit&) {return *this;}
172161
};
173162

163+
typedef std::function<bool(const Hit& hit)> HitCallback;
174164

175-
class HitCallback {
176-
public:
177-
virtual ~HitCallback(void) {}
178-
179-
/** return true: go on; false: stop
180-
The return value gives the calling function the possibility to optimize.
181-
However be aware the return value may be ignored. */
182-
/** Do not throw en exception here.
183-
This method will be called from deep inside the brlcad libraries.
184-
The status of the program after an exception will be undetermined. */
185-
virtual bool operator()(const Hit& hit) = 0;
186-
187-
protected:
188-
HitCallback(void) {}
189-
HitCallback(const HitCallback&) {}
190-
const HitCallback& operator=(const HitCallback&) {return *this;}
191-
};
192-
193-
194-
void ShootRay(const Ray3D& ray,
195-
HitCallback& callback) const;
165+
void ShootRay(const Ray3D& ray,
166+
const HitCallback& callback) const;
196167

197168
static const int StopAfterFirstHit = 1;
198169
static const int WithOverlaps = 2;
199170

200-
void ShootRay(const Ray3D& ray,
201-
HitCallback& callback,
202-
int flags) const;
171+
void ShootRay(const Ray3D& ray,
172+
const HitCallback& callback,
173+
int flags) const;
203174
//@}
204175

205176
protected:

include/brlcad/Database/Database.h

+3-14
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,11 @@ namespace BRLCAD {
5454
//@{
5555
using ConstDatabase::Get;
5656

57-
class ObjectCallback {
58-
public:
59-
virtual ~ObjectCallback(void) {}
60-
61-
/// the user has to implement this object method to evaluate and modify the object
62-
virtual void operator()(Object& object) = 0;
63-
64-
protected:
65-
ObjectCallback(void) {}
66-
ObjectCallback(const ObjectCallback&) {}
67-
const ObjectCallback& operator=(const ObjectCallback&) {return *this;}
68-
};
57+
typedef std::function<void(Object& object)> ObjectCallback;
6958

7059
/// selects a single object and hand it over to an ObjectCallback (for read and write)
71-
void Get(const char* objectName,
72-
ObjectCallback& callback);
60+
void Get(const char* objectName,
61+
const ObjectCallback& callback);
7362

7463
/// provided for convenience: selects a single object and sets it to \a object
7564
/** The type of the object in the database and \a object must match. */

include/brlcad/Database/Sketch.h

+8-29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef BRLCAD_SKETCH_INCLUDED
2727
#define BRLCAD_SKETCH_INCLUDED
2828

29+
#include <functional>
30+
2931
#include <brlcad/vector.h>
3032
#include <brlcad/Database/Object.h>
3133

@@ -218,39 +220,16 @@ namespace BRLCAD {
218220
friend class Sketch;
219221
};
220222

221-
class ConstSegmentCallback {
222-
public:
223-
virtual ~ConstSegmentCallback(void) {}
224-
225-
/// the user has to implement this method to evaluate the object
226-
virtual void operator()(const Segment& segment) = 0;
227-
228-
protected:
229-
ConstSegmentCallback(void) {}
230-
ConstSegmentCallback(const ConstSegmentCallback&) {}
231-
const ConstSegmentCallback& operator=(const ConstSegmentCallback&) {return *this;}
232-
};
233-
234-
class SegmentCallback {
235-
public:
236-
virtual ~SegmentCallback(void) {}
237-
238-
/// the user has to implement this method to evaluate the object
239-
virtual void operator()(Segment& segment) = 0;
240-
241-
private:
242-
SegmentCallback(void) {}
243-
SegmentCallback(const SegmentCallback&) {}
244-
const SegmentCallback& operator=(const SegmentCallback&) {return *this;}
245-
};
223+
typedef std::function<void(const Segment& segment)> ConstSegmentCallback;
224+
typedef std::function<void(Segment& segment)> SegmentCallback;
246225

247226
size_t NumberOfSegments(void) const;
248227

249228
/// selects a single object and hand it over to an SegmentCallback
250-
void Get(size_t index,
251-
ConstSegmentCallback& callback) const;
252-
void Get(size_t index,
253-
SegmentCallback& callback);
229+
void Get(size_t index,
230+
const ConstSegmentCallback& callback) const;
231+
void Get(size_t index,
232+
const SegmentCallback& callback);
254233

255234
/// overloaded member function, provided for convenience: selects a single segment and and returns it
256235
/** Do not forget to BRLCAD::Sketch::Segment::Destroy() the copy when you are finished with it! */

include/brlcad/VectorList.h

+4-25
Original file line numberDiff line numberDiff line change
@@ -468,32 +468,11 @@ namespace BRLCAD {
468468
friend class VectorList;
469469
};
470470

471-
class ConstElementCallback {
472-
public:
473-
virtual ~ConstElementCallback(void) {}
474-
475-
virtual bool operator()(const Element* element) = 0;
476-
477-
protected:
478-
ConstElementCallback(void) {}
479-
ConstElementCallback(const ConstElementCallback&) {}
480-
const ConstElementCallback& operator=(const ConstElementCallback&) {return *this;}
481-
};
482-
483-
class ElementCallback {
484-
public:
485-
virtual ~ElementCallback(void) {}
486-
487-
virtual bool operator()(Element* element) = 0;
488-
489-
protected:
490-
ElementCallback(void) {}
491-
ElementCallback(const ElementCallback&) {}
492-
const ElementCallback& operator=(const ElementCallback&) {return *this;}
493-
};
471+
typedef std::function<bool(const Element* element)> ConstElementCallback;
472+
typedef std::function<bool(Element* element)> ElementCallback;
494473

495-
void Iterate(ConstElementCallback& callBack) const;
496-
void Iterate(ElementCallback& callBack);
474+
void Iterate(const ConstElementCallback& callBack) const;
475+
void Iterate(const ElementCallback& callBack);
497476

498477
bool Append(const Element& element);
499478
void Clear(void);

include/brlcad/globals.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef BRLCAD_GLOBALS_INCLUDED
2828
#define BRLCAD_GLOBALS_INCLUDED
2929

30+
#include <functional>
31+
3032

3133
namespace BRLCAD {
3234
/// @name get BRL-CAD version information
@@ -40,17 +42,7 @@ namespace BRLCAD {
4042
/// @name capturing the logging output
4143
//@{
4244
/// log handler (to be implemented by the caller)
43-
class LogHandler {
44-
public:
45-
virtual ~LogHandler(void) {}
46-
47-
virtual void operator()(const char* logString) = 0;
48-
49-
protected:
50-
LogHandler(void) {}
51-
LogHandler(const LogHandler&) {}
52-
const LogHandler& operator=(const LogHandler&) {return *this;}
53-
};
45+
typedef std::function<void(const char* logString)> LogHandler;
5446

5547
void BRLCAD_MOOSE_EXPORT RegisterLogHandler(LogHandler& logHandler);
5648
void BRLCAD_MOOSE_EXPORT DeRegisterLogHandler(LogHandler& logHandler);

src/Database/ConstDatabase.cpp

+16-36
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ ConstDatabase::TopObjectIterator ConstDatabase::FirstTopObject(void) const {
228228

229229
void ConstDatabase::Get
230230
(
231-
const char* objectName,
232-
ObjectCallback& callback
231+
const char* objectName,
232+
const ObjectCallback& callback
233233
) const {
234234
if (m_rtip != 0) {
235235
if (!BU_SETJUMP) {
@@ -333,31 +333,11 @@ Object* ConstDatabase::Get
333333
(
334334
const char* objectName
335335
) const {
336-
class ObjectCallbackIntern : public ObjectCallback {
337-
public:
338-
ObjectCallbackIntern(void) : ConstDatabase::ObjectCallback(),
339-
m_object(0) {}
336+
Object* ret = 0;
340337

341-
virtual ~ObjectCallbackIntern(void) {}
338+
Get(objectName, [&ret](const Object& object){try{ret = object.Clone();}catch(std::bad_alloc&){}});
342339

343-
virtual void operator()(const Object& object) {
344-
try {
345-
m_object = object.Clone();
346-
}
347-
catch(std::bad_alloc&) {}
348-
}
349-
350-
Object* GetObject(void) const {
351-
return m_object;
352-
}
353-
354-
private:
355-
Object* m_object;
356-
} callbackIntern;
357-
358-
Get(objectName, callbackIntern);
359-
360-
return callbackIntern.GetObject();
340+
return ret;
361341
}
362342

363343

@@ -707,8 +687,8 @@ static int HitDo
707687
partition* partitionHead,
708688
seg* UNUSED(segment)
709689
) {
710-
ConstDatabase::HitCallback* callback = static_cast<ConstDatabase::HitCallback*>(ap->a_uptr);
711-
int& ret = ap->a_return;
690+
const ConstDatabase::HitCallback* callback = static_cast<const ConstDatabase::HitCallback*>(ap->a_uptr);
691+
int& ret = ap->a_return;
712692

713693
if (ret == 0) {
714694
for (partition* part = partitionHead->pt_forw;
@@ -727,8 +707,8 @@ static int HitDo
727707

728708
void ConstDatabase::ShootRay
729709
(
730-
const Ray3D& ray,
731-
HitCallback& callback
710+
const Ray3D& ray,
711+
const HitCallback& callback
732712
) const {
733713
if (!SelectionIsEmpty()) {
734714
application ap;
@@ -743,7 +723,7 @@ void ConstDatabase::ShootRay
743723
ap.a_onehit = 0; // all hits
744724
ap.a_resource = m_resp;
745725
ap.a_return = 0;
746-
ap.a_uptr = &callback;
726+
ap.a_uptr = const_cast<HitCallback*>(&callback);
747727

748728
VMOVE(ap.a_ray.r_pt, ray.origin.coordinates);
749729
VMOVE(ap.a_ray.r_dir, ray.direction.coordinates);
@@ -770,8 +750,8 @@ static void MultioverlapDo
770750
bu_ptbl* regiontable,
771751
partition* UNUSED(inputHdp)
772752
) {
773-
ConstDatabase::HitCallback* callback = static_cast<ConstDatabase::HitCallback*>(ap->a_uptr);
774-
int& ret = ap->a_return;
753+
const ConstDatabase::HitCallback* callback = static_cast<const ConstDatabase::HitCallback*>(ap->a_uptr);
754+
int& ret = ap->a_return;
775755

776756
if (ret == 0) {
777757
for (size_t i = 0; i < BU_PTBL_LEN(regiontable); ++i) {
@@ -795,9 +775,9 @@ static void MultioverlapDo
795775

796776
void ConstDatabase::ShootRay
797777
(
798-
const Ray3D& ray,
799-
HitCallback& callback,
800-
int flags
778+
const Ray3D& ray,
779+
const HitCallback& callback,
780+
int flags
801781
) const {
802782
if (!SelectionIsEmpty()) {
803783
application ap;
@@ -811,7 +791,7 @@ void ConstDatabase::ShootRay
811791
ap.a_onehit = flags & StopAfterFirstHit;
812792
ap.a_resource = m_resp;
813793
ap.a_return = 0;
814-
ap.a_uptr = &callback;
794+
ap.a_uptr = const_cast<HitCallback*>(&callback);
815795

816796
if (flags & WithOverlaps)
817797
ap.a_multioverlap = MultioverlapDo;

src/Database/Database.cpp

+13-39
Original file line numberDiff line numberDiff line change
@@ -336,33 +336,22 @@ void Database::Delete
336336

337337
void Database::Get
338338
(
339-
const char* objectName,
340-
ObjectCallback& callback
339+
const char* objectName,
340+
const ObjectCallback& callback
341341
) {
342-
class ObjectCallbackIntern : public ConstDatabase::ObjectCallback {
343-
public:
344-
ObjectCallbackIntern(Database::ObjectCallback& cb) : ConstDatabase::ObjectCallback(),
345-
m_callback(cb) {}
342+
ConstDatabase::ObjectCallback callbackIntern = [callback](const Object& object) {
343+
Object& objectIntern = const_cast<Object&>(object);
346344

347-
virtual ~ObjectCallbackIntern(void) {}
345+
callback(objectIntern);
348346

349-
virtual void operator()(const Object& object) {
350-
Object& objectIntern = const_cast<Object&>(object);
351-
352-
m_callback(objectIntern);
353-
354-
if (!BU_SETJUMP)
355-
rt_db_put_internal(objectIntern.m_pDir,
356-
objectIntern.m_dbip,
357-
objectIntern.m_ip,
358-
objectIntern.m_resp);
359-
360-
BU_UNSETJUMP;
361-
}
347+
if (!BU_SETJUMP)
348+
rt_db_put_internal(objectIntern.m_pDir,
349+
objectIntern.m_dbip,
350+
objectIntern.m_ip,
351+
objectIntern.m_resp);
362352

363-
private:
364-
Database::ObjectCallback& m_callback;
365-
} callbackIntern(callback);
353+
BU_UNSETJUMP;
354+
};
366355

367356
ConstDatabase::Get(objectName, callbackIntern);
368357
}
@@ -372,22 +361,7 @@ void Database::Set
372361
(
373362
const Object& object
374363
) {
375-
class ObjectCallbackIntern : public ObjectCallback {
376-
public:
377-
ObjectCallbackIntern(const Object& obj) : ObjectCallback(),
378-
m_object(obj) {}
379-
380-
virtual ~ObjectCallbackIntern(void) {}
381-
382-
virtual void operator()(Object& obj) {
383-
obj = m_object;
384-
}
385-
386-
private:
387-
const Object& m_object;
388-
} callbackIntern(object);
389-
390-
Get(object.Name(), callbackIntern);
364+
Get(object.Name(), [&object](Object& obj){obj = object;});
391365
}
392366

393367

0 commit comments

Comments
 (0)