Argonne National Laboratory

Experimental Physics and
Industrial Control System

2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  <20132014  2015  2016  2017  2018  2019  Index 2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  <20132014  2015  2016  2017  2018  2019 
<== Date ==> <== Thread ==>

Subject: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base
From: Jeff Hill <johill@lanl.gov>
To: mp+196010@code.launchpad.net
Date: Wed, 20 Nov 2013 20:40:46 -0000
Jeff Hill has proposed merging lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base.

Requested reviews:
  EPICS Core Developers (epics-core)

For more details, see:
https://code.launchpad.net/~johill-lanl/epics-base/epics-base-enclosure-of/+merge/196010

o A new header file with C++ template functions for obtaining a reference to the enclosing class given a C++ pointer to member data, and a pointer to the enclosed member. The interface is const preserving. For example, if the pointer to the member is const then a const reference to the enclosing class is returned. In C we have used macros for this purpose; with this C++ template based implementation we hope for improved type safety.
o A simple test for the new C++ template functions
-- 
https://code.launchpad.net/~johill-lanl/epics-base/epics-base-enclosure-of/+merge/196010
Your team EPICS Core Developers is requested to review the proposed merge of lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base.
=== modified file 'src/libCom/misc/Makefile'
--- src/libCom/misc/Makefile	2011-02-25 21:39:44 +0000
+++ src/libCom/misc/Makefile	2013-11-20 20:34:31 +0000
@@ -19,6 +19,7 @@
 INC += epicsStdlib.h
 INC += epicsString.h
 INC += epicsTypes.h
+INC += epicsEnclosureOf.h
 INC += shareLib.h
 INC += epicsExport.h
 INC += unixFileName.h

=== added file 'src/libCom/misc/epicsEnclosureOf.h'
--- src/libCom/misc/epicsEnclosureOf.h	1970-01-01 00:00:00 +0000
+++ src/libCom/misc/epicsEnclosureOf.h	2013-11-20 20:34:31 +0000
@@ -0,0 +1,69 @@
+
+/*************************************************************************\
+* Copyright (c) 2013 LANS LLC, as Operator of
+*     Los Alamos National Laboratory.
+* EPICS BASE is distributed subject to a Software License Agreement found
+* in file LICENSE that is included with this distribution. 
+\*************************************************************************/
+
+// Author: Jeffrey O. Hill
+
+#ifndef epicsEnclosureOfh
+#define epicsEnclosureOfh
+
+#include <cstddef>
+
+namespace epics {
+
+//
+// memberDataOffset ()
+//
+template < class T, class M >
+inline std :: ptrdiff_t memberDataOffset ( M T :: * pMD )
+{
+    typedef const unsigned char Octet;
+    const T * const pT = 0;
+    Octet * const pTM = reinterpret_cast < Octet * > ( & ( pT ->* pMD ) );
+    return pTM - reinterpret_cast < Octet * > ( pT );
+}
+
+template < class T, class M >
+T & enclosureOf ( M * pData, M T :: * pMemberData )
+{
+    typedef unsigned char Octet;
+    Octet * p = reinterpret_cast < Octet * > ( pData );
+    return * reinterpret_cast < T * > 
+                        ( p - memberDataOffset ( pMemberData ) ); 
+}
+
+// reference to mutable member and pointer to constant member
+// is disable 
+template < class T, class M >
+inline T & enclosureOf ( M * pData, const M T :: * pMemberData );
+
+// reference to constant member, so return constant reference to
+// its enclosure
+template < class T, class M >
+inline const T & enclosureOf ( const M * pData, M T :: * pMemberData )
+{
+    typedef const unsigned char Octet;
+    Octet * p = reinterpret_cast < Octet * > ( pData );
+    return * reinterpret_cast < const T * > 
+                        ( p - memberDataOffset ( pMemberData ) ); 
+}
+
+// reference to constant member and pointer to constant member, so 
+// return constant reference to its enclosure
+template < class T, class M >
+inline const T & enclosureOf ( const M * pData, 
+                                        const M T :: * pMemberData )
+{
+    typedef const unsigned char Octet;
+    Octet * p = reinterpret_cast < Octet * > ( pData );
+    return * reinterpret_cast < const T * > 
+                        ( p - memberDataOffset ( pMemberData ) ); 
+}
+
+} // end of name space epics
+
+#endif // ifdef epicsEnclosureOfh

=== modified file 'src/libCom/test/Makefile'
--- src/libCom/test/Makefile	2013-06-07 23:08:38 +0000
+++ src/libCom/test/Makefile	2013-11-20 20:34:31 +0000
@@ -141,6 +141,11 @@
 testHarness_SRCS += epicsAtomicTest.cpp
 TESTS += epicsAtomicTest
 
+TESTPROD_HOST += epicsEnclosureOfTest
+enclLnkTest_SRCS += epicsEnclosureOfTest.cpp
+testHarness_SRCS += epicsEnclosureOfTest.cpp
+TESTS += epicsEnclosureOfTest
+
 TESTPROD_HOST += epicsExceptionTest
 epicsExceptionTest_SRCS += epicsExceptionTest.cpp
 testHarness_SRCS += epicsExceptionTest.cpp

=== added file 'src/libCom/test/epicsEnclosureOfTest.cpp'
--- src/libCom/test/epicsEnclosureOfTest.cpp	1970-01-01 00:00:00 +0000
+++ src/libCom/test/epicsEnclosureOfTest.cpp	2013-11-20 20:34:31 +0000
@@ -0,0 +1,62 @@
+
+/*************************************************************************\
+* Copyright (c) 2013 LANS LLC, as Operator of
+*     Los Alamos National Laboratory.
+* EPICS BASE is distributed subject to a Software License Agreement found
+* in file LICENSE that is included with this distribution. 
+\*************************************************************************/
+
+#include <cstdio>
+
+#include "epicsUnitTest.h"
+#include "testMain.h"
+
+#include "epicsEnclosureOf.h"
+
+using namespace epics;
+
+struct Dog;
+
+struct Cat {
+    Cat () {}
+    int m_int;
+    float m_float;
+    double m_double;
+    void selfTest ( const Dog & dog, Cat Dog :: * pMD )
+    {
+        Dog & dogEncl = enclosureOf ( this, pMD );
+        testOk1( & dogEncl == & dog );
+    }
+    void selfTest ( const Dog & dog, const Cat Dog :: * pMD ) const
+    {
+        const Dog & dogEncl = enclosureOf ( this, pMD );
+        testOk1( & dogEncl == & dog );
+    }
+};
+
+struct Dog {
+    Dog () {}
+    int m_int0;
+    float m_float0;
+    double m_double0;
+    Cat m_cat;
+    int m_int1;
+    float m_float1;
+    const Cat m_cat_const;
+    double m_double1;
+};
+
+MAIN(enclLnkTest)
+{
+    testPlan(4);
+        
+    Dog dog;
+    dog.m_cat.selfTest ( dog, & Dog :: m_cat );
+    dog.m_cat_const.selfTest ( dog, & Dog :: m_cat_const );
+    
+    const Dog const_dog;
+    const_dog.m_cat.selfTest ( const_dog, & Dog :: m_cat );
+    const_dog.m_cat_const.selfTest ( const_dog, & Dog :: m_cat_const );
+    
+    return testDone();
+}


Replies:
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Andrew Johnson
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Jeff Hill
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Jeff Hill
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Jeff Hill
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Jeff Hill
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Jeff Hill
Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Andrew Johnson

Navigate by Date:
Prev: Build failed in Jenkins: epics-base-3.15 #29 APS Jenkins
Next: Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Andrew Johnson
Index: 2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  <20132014  2015  2016  2017  2018  2019 
Navigate by Thread:
Prev: RE: make clean builds the install target on R3.15? Hill, Jeff
Next: Re: [Merge] lp:~johill-lanl/epics-base/epics-base-enclosure-of into lp:epics-base Andrew Johnson
Index: 2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  <20132014  2015  2016  2017  2018  2019 
ANJ, 21 Nov 2013 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·