CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
argument.hpp
1 #ifndef CPPAD_CG_ARGUMENT_INCLUDED
2 #define CPPAD_CG_ARGUMENT_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
21 template<class Base>
23 
30 template<class Base>
31 class Argument {
32 private:
33  OperationNode<Base>* operation_;
34  std::unique_ptr<Base> parameter_;
35 public:
36 
37  inline Argument() :
38  operation_(nullptr) {
39  }
40 
41  inline Argument(OperationNode<Base>& operation) :
42  operation_(&operation) {
43  }
44 
45  inline Argument(const Base& parameter) :
46  operation_(nullptr),
47  parameter_(new Base(parameter)) {
48  }
49 
50  inline Argument(const Argument& orig) :
51  operation_(orig.operation_),
52  parameter_(orig.parameter_ != nullptr ? new Base(*orig.parameter_) : nullptr) {
53  }
54 
55  inline Argument(Argument&& orig) :
56  operation_(orig.operation_),
57  parameter_(std::move(orig.parameter_)) {
58  }
59 
60  inline Argument& operator=(const Argument& rhs) {
61  if (&rhs == this) {
62  return *this;
63  }
64  if (rhs.operation_ != nullptr) {
65  operation_ = rhs.operation_;
66  parameter_.reset();
67  } else {
68  operation_ = nullptr;
69  if (parameter_ != nullptr) {
70  *parameter_ = *rhs.parameter_;
71  } else {
72  parameter_.reset(new Base(*rhs.parameter_)); // to replace with parameter_ = std::make_unique once c++14 is used
73  }
74  }
75  return *this;
76  }
77 
78  inline Argument& operator=(Argument&& rhs) {
79  assert(this != &rhs);
80 
81  operation_ = rhs.operation_;
82 
83  // steal the parameter
84  parameter_ = std::move(rhs.parameter_);
85 
86  return *this;
87  }
88 
89  virtual ~Argument() = default;
90 
91  inline OperationNode<Base>* getOperation() const {
92  return operation_;
93  }
94 
95  inline Base* getParameter() const {
96  return parameter_.get();
97  }
98 
99 };
100 
101 } // END cg namespace
102 } // END CppAD namespace
103 
104 #endif