codac 2.0.0
Loading...
Searching...
No Matches
codac2_FunctionArgsList.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <list>
13#include <vector>
14#include <iostream>
15#include <algorithm>
16#include "codac2_VarBase.h"
17
18namespace codac2
19{
24 class FunctionArgsList : private std::vector<std::shared_ptr<VarBase>>
25 {
26 using base = std::vector<std::shared_ptr<VarBase>>;
27
28 public:
29
30 // Expose only selected const (read-only) members of the underlying
31 // std::vector via `using` to keep iteration/access convenient.
32 // Mutating operations (push_back/insert/erase/...) are intentionally
33 // not exposed in order to keep consistency with the _total_args_size value.
34
35 using base::size;
36 using base::empty;
37 using base::cbegin;
38 using base::cend;
39 using base::crbegin;
40 using base::crend;
41
42 const std::shared_ptr<VarBase>& operator[](std::size_t i) const { return base::operator[](i); }
43 const std::shared_ptr<VarBase>& at(std::size_t i) const { return base::at(i); }
44
45 base::const_iterator begin() const noexcept { return base::begin(); }
46 base::const_iterator end() const noexcept { return base::end(); }
47
48 public:
49
53 FunctionArgsList() = default;
54
63 : std::vector<std::shared_ptr<VarBase>>(args.size())
64 {
65 size_t i = 0;
66 for(const auto& arg : args)
67 base::operator[](i++) = arg->arg_copy();
68 compute_unique_arg_names();
69 }
70
79 FunctionArgsList(std::initializer_list<std::reference_wrapper<VarBase>> args)
80 : FunctionArgsList(std::vector<std::reference_wrapper<VarBase>>(args))
81 { }
82
91 FunctionArgsList(const std::vector<std::reference_wrapper<VarBase>>& args)
92 {
93 for(const auto& arg : args)
94 push_back(arg.get().arg_copy());
95 compute_unique_arg_names();
96 }
97
98 FunctionArgsList(const std::vector<std::shared_ptr<VarBase>>& args)
99 : std::vector<std::shared_ptr<VarBase>>(args)
100 {
101 compute_unique_arg_names();
102 }
103
110 Index total_size() const
111 {
112 return _total_args_size;
113 }
114
115 protected:
116
117 void compute_unique_arg_names()
118 {
119 std::list<std::string> var_names;
120 for(const auto& arg : *this)
121 var_names.push_back(arg->name());
122
123 _total_args_size = 0;
124
125 int i = 23; // default first variable is x, then y,z, then starting from a...
126 for(auto& arg : *this)
127 {
128 _total_args_size += arg->size();
129 if(arg->name() == "?")
130 {
131 std::string new_name;
132 do {
133 new_name = 'a' + ((i++)%26);
134 } while(std::find(var_names.begin(),var_names.end(),new_name) != var_names.end());
135 arg->_name = new_name;
136 }
137 }
138 }
139
140 Index _total_args_size = 0;
141 };
142}
FunctionArgsList(const FunctionArgsList &args)
Copy constructor.
Definition codac2_FunctionArgsList.h:62
Index total_size() const
Calculates the total size of the function arguments, as the sum of the sizes of each argument.
Definition codac2_FunctionArgsList.h:110
FunctionArgsList()=default
Default constructor. It creates an empty list of arguments.
FunctionArgsList(const std::vector< std::reference_wrapper< VarBase > > &args)
Constructor to initialize a FunctionArgsList from a vector of of references to VarBase objects.
Definition codac2_FunctionArgsList.h:91
FunctionArgsList(std::initializer_list< std::reference_wrapper< VarBase > > args)
Constructor to initialize a FunctionArgsList from an initializer list of references to VarBase object...
Definition codac2_FunctionArgsList.h:79
Abstract base class for representing variables in analytic or set functions.
Definition codac2_VarBase.h:24
virtual std::shared_ptr< VarBase > arg_copy() const =0
Creates a copy of the argument.
Definition codac2_OctaSym.h:21