MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
property.h
1//
2// property.h
3//
4// Copyright (C) 2013 David Hollman
5//
6// Author: David Hollman
7// Maintainer: DSH
8// Created: Dec 13, 2013
9//
10// This file is part of the SC Toolkit.
11//
12// The SC Toolkit is free software; you can redistribute it and/or modify
13// it under the terms of the GNU Library General Public License as published by
14// the Free Software Foundation; either version 2, or (at your option)
15// any later version.
16//
17// The SC Toolkit is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Library General Public License for more details.
21//
22// You should have received a copy of the GNU Library General Public License
23// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
24// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25//
26// The U.S. Government is granted a limited license as per AL 91-7.
27//
28
29#ifndef _util_misc_property_h
30#define _util_misc_property_h
31
32#include <functional>
33#include <utility>
34
35namespace sc {
36
37// Taken from:
38// http://www.daniweb.com/software-development/cpp/threads/443096/c-property-template
39
46template <typename ClassType, typename MemberType>
47class property {
48public:
49 typedef property<ClassType, MemberType> self; // good to have a "self" type.
50 typedef MemberType (ClassType::*getter_type)() const;
51 typedef void (ClassType::*setter_type)(const MemberType&);
52
57 property(ClassType* aTarget,
58 getter_type aGetter = NULL,
59 setter_type aSetter = NULL) :
60 target(aTarget),
61 getter(aGetter),
62 setter(aSetter) {
63 if(!getter)
64 throw std::logic_error("Cannot have a NULL getter function for a property!");
65 };
66
67 // Delete the default copy and move constructors, since they will make this refer to an old instance
68 property(self&& other) : target(std::move(other.target)), getter(std::move(other.getter)), setter(std::move(other.setter)) { }
69 self& operator=(self&) = delete;
70 self& operator=(const self&) = delete;
71
77 self& operator=(const MemberType& value) {
78 if(setter) // for non-nullity of setter pointer.
79 (target->*setter)(value);
80 return *this;
81 };
82
86 operator MemberType() const {
87 return (target->*getter)();
88 };
89
90 void set_target(ClassType* a) { target = a; }
91 void set_getter(getter_type g){ getter = g; }
92 void set_setter(setter_type s){ setter = s; }
93 void set_target_getter_setter(ClassType* a, getter_type g, setter_type s = NULL) {
94 target = a;
95 getter = g;
96 setter = s;
97 }
98
99private:
100 ClassType* target;
101 getter_type getter;
102 setter_type setter;
103
104};
105
106
107
108} // end namespace sc
109
110#endif /* end _util_misc_property_h */
Helper class to connect a 'property' in a c++ class to getter/setter methods.
Definition property.h:47
self & operator=(const MemberType &value)
assignment operator.
Definition property.h:77
property(ClassType *aTarget, getter_type aGetter=NULL, setter_type aSetter=NULL)
ctor NOTE: Avoid using leading underscores, technically, the C++ standard forbids them.
Definition property.h:57
SpinCase1 other(SpinCase1 S)
given 1-spin return the other 1-spin
Contains all MPQC code up to version 3.
Definition mpqcin.h:14
STL namespace.

Generated at Wed Sep 25 2024 02:45:31 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.