Skip to content

Commit 3a3a300

Browse files
committed
finishing addition of storage policies
1 parent bf4f59e commit 3a3a300

File tree

8 files changed

+415
-362
lines changed

8 files changed

+415
-362
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
obj/
22
bin/test
3+
bin/kit
34
toys/obj/
45
toys/bin/
56
*.make

kit/cache/cache.h

+30-18
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@
77
#include "../kit.h"
88
#include "icache.h"
99

10-
template<class Class, class T, class Mutex=kit::dummy_mutex>
10+
template<
11+
class Class,
12+
class T,
13+
template <typename> class Ptr=std::shared_ptr,
14+
class TMeta=Meta,
15+
class Mutex=kit::dummy_mutex
16+
>
1117
class Cache:
12-
public Factory<Class, std::tuple<T, ICache*>, std::string, Mutex>,
18+
public Factory<Class, std::tuple<T, ICache*>, std::string, Ptr, TMeta, Mutex>,
1319
public ICache,
1420
virtual public kit::mutexed<Mutex>
1521
{
1622
public:
23+
//using factory_type = Factory<Class, std::tuple<T, ICache*>, Ptr, std::string, Mutex>;
1724

1825
Cache() = default;
19-
Cache(std::string fn):
20-
Factory<Class, std::tuple<T, ICache*>>(fn)
21-
{}
22-
Cache(std::shared_ptr<Meta> cfg):
23-
Factory<Class, std::tuple<T, ICache*>>(cfg)
24-
{}
26+
explicit Cache(std::string fn)
27+
//Factory<>(fn)
28+
{
29+
this->config(fn);
30+
}
31+
explicit Cache(typename TMeta::ptr cfg)
32+
//Factory<>(cfg)
33+
{
34+
this->config(cfg);
35+
}
2536

2637
virtual ~Cache() {}
2738

@@ -44,18 +55,19 @@ class Cache:
4455
* Cache resource, force type
4556
*/
4657
template<class Cast>
47-
std::shared_ptr<Cast> cache_as(T arg) {
58+
Ptr<Cast> cache_as(T arg) {
4859
auto l = this->lock();
4960
arg = transform(arg);
5061
if(m_Preserve && not m_Preserve(arg)) {
51-
return std::make_shared<Cast>(
62+
return kit::make<Ptr<Cast>>(
5263
std::tuple<T, ICache*>(arg, this)
5364
);
5465
}
5566
try{
5667
return std::dynamic_pointer_cast<Cast>(m_Resources.at(arg));
5768
}catch(...){
58-
auto p = std::make_shared<Cast>(
69+
auto p = kit::make<Ptr<Cast>>(
70+
//arg,this
5971
std::tuple<T, ICache*>(arg, this)
6072
);
6173
m_Resources[arg] = std::static_pointer_cast<Class>(p);
@@ -66,18 +78,18 @@ class Cache:
6678
/*
6779
* Cache resource, bypass parameter transformer
6880
*/
69-
virtual std::shared_ptr<Class> cache_raw(const T& arg) {
81+
virtual Ptr<Class> cache_raw(const T& arg) {
7082
auto l = this->lock();
7183
if(m_Preserve && not m_Preserve(arg)) {
72-
return Factory<Class, std::tuple<T, ICache*>, std::string, Mutex>::create(
84+
return this->create(
7385
std::tuple<T, ICache*>(arg, this)
7486
);
7587
}
7688
try{
7789
return m_Resources.at(arg);
7890
}catch(const std::out_of_range&){
7991
return (m_Resources[arg] =
80-
Factory<Class, std::tuple<T, ICache*>, std::string, Mutex>::create(
92+
this->create(
8193
std::tuple<T, ICache*>(arg, this)
8294
)
8395
);
@@ -87,24 +99,24 @@ class Cache:
8799
/*
88100
* Cache resource and let factory choose type
89101
*/
90-
virtual std::shared_ptr<Class> cache(T arg) {
102+
virtual Ptr<Class> cache(T arg) {
91103
auto l = this->lock();
92104
arg = transform(arg);
93105
return cache_raw(arg);
94106
}
95107

96108
//template<class Cast>
97-
//virtual void cache(const std::shared_ptr<Cast>& blah) {
109+
//virtual void cache(const Ptr<Cast>& blah) {
98110
//}
99111

100112
/*
101113
* Cache resource and attempt to cast resource to a given type
102114
*/
103115
template<class Cast>
104-
std::shared_ptr<Cast> cache_cast(T arg) {
116+
Ptr<Cast> cache_cast(T arg) {
105117
auto l = this->lock();
106118
arg = transform(arg);
107-
std::shared_ptr<Cast> p;
119+
Ptr<Cast> p;
108120
//try{
109121
p = std::dynamic_pointer_cast<Cast>(cache_raw(arg));
110122
if(!p)

kit/factory/factory.h

+16-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ template<
2323
class Class,
2424
class T,
2525
class ClassName=std::string,
26-
class Mutex=kit::dummy_mutex
26+
template <typename> class Ptr=std::shared_ptr,
27+
class TMeta=Meta, // use MetaMT for config thread safety
28+
class Mutex=kit::dummy_mutex // or std::recursive_mutex
2729
>
2830
class Factory:
2931
public IFactory,
@@ -34,7 +36,7 @@ class Factory:
3436
// derived class make_shared() functors
3537
std::vector<
3638
std::function<
37-
std::shared_ptr<Class>(const T&)
39+
Ptr<Class>(const T&)
3840
>
3941
> m_Classes;
4042

@@ -50,28 +52,30 @@ class Factory:
5052

5153
std::function<unsigned(const T&)> m_Resolver;
5254
std::function<T(const T&)> m_Transformer;
53-
std::shared_ptr<MetaBase<Mutex>> m_pConfig;
55+
typename TMeta::ptr m_pConfig;
5456

5557
public:
5658

5759
Factory():
58-
m_pConfig(std::make_shared<MetaBase<Mutex>>())
60+
m_pConfig(kit::make<typename TMeta::ptr>())
5961
{}
60-
Factory(std::string fn):
61-
m_pConfig(std::make_shared<MetaBase<Mutex>>(fn))
62+
explicit Factory(std::string fn):
63+
m_pConfig(kit::make<typename TMeta::ptr>(fn))
6264
{}
63-
Factory(std::shared_ptr<Meta> cfg):
64-
m_pConfig(std::make_shared<MetaBase<Mutex>>(cfg))
65+
explicit Factory(typename TMeta::ptr cfg):
66+
m_pConfig(kit::make<typename TMeta::ptr>(cfg))
6567
{}
6668

67-
std::shared_ptr<MetaBase<Mutex>> config() {
69+
typename TMeta::ptr config() {
6870
auto l = this->lock();
6971
return m_pConfig;
7072
}
71-
std::shared_ptr<const MetaBase<Mutex>> config() const {
73+
typename TMeta::cptr config() const {
7274
auto l = this->lock();
7375
return m_pConfig;
7476
}
77+
void config(typename TMeta::ptr cfg) {m_pConfig=cfg;}
78+
void config(std::string cfg) {m_pConfig=kit::make<typename TMeta::ptr>(cfg);}
7579
void with(std::function<void()> cb){
7680
auto l = this->lock();
7781
cb();
@@ -170,11 +174,11 @@ class Factory:
170174
return m_Transformer(t);
171175
}
172176

173-
//void share_config(std::shared_ptr<MetaBase<Mutex>> cfg){
177+
//void share_config(TMeta::Ptr cfg){
174178
// //auto l = this->lock();
175179
// m_pConfig = cfg;
176180
//}
177-
//std::shared_ptr<MetaBase<Mutex>> share_config() {
181+
//TMeta::Ptr share_config() {
178182
// //auto l = this->lock();
179183
// return m_pConfig;
180184
//}

kit/kit.h

+73-73
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,77 @@ namespace kit
137137
// mutable T m_Data;
138138
//};
139139

140+
template<class T, class ...Args>
141+
std::future<T> make_future(Args&&... args)
142+
{
143+
std::promise<T> tmp;
144+
tmp.set_value(std::forward<Args>(args)...);
145+
return tmp.get_future();
146+
}
147+
148+
template<class T, class... Args>
149+
std::unique_ptr<T> make_unique(Args&&... args)
150+
{
151+
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
152+
}
153+
154+
template<class T, class... Args>
155+
T make(Args&&... args)
156+
{
157+
return T(new typename T::element_type(std::forward<Args>(args)...));
158+
}
159+
160+
template<class T, class... Args>
161+
std::unique_ptr<T> init_unique(Args&&... args)
162+
{
163+
auto p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
164+
p->init();
165+
return p;
166+
}
167+
template<class T, class... Args>
168+
std::shared_ptr<T> init_shared(Args&&... args)
169+
{
170+
auto p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
171+
p->init();
172+
return p;
173+
}
174+
//template<class T, class... Args>
175+
//std::unique_ptr<T>& make(std::unique_ptr<T>& p, Args&&... args)
176+
//{
177+
// p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
178+
// return p;
179+
//}
180+
//template<class T, class... Args>
181+
//std::shared_ptr<T>& make(std::shared_ptr<T>& p, Args&&... args)
182+
//{
183+
// p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
184+
// return p;
185+
//}
186+
template<class T, class... Args>
187+
std::unique_ptr<T>& init(std::unique_ptr<T>& p, Args&&... args)
188+
{
189+
try {
190+
p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
191+
p->init();
192+
}catch(...){
193+
p = std::unique_ptr<T>();
194+
throw;
195+
}
196+
return p;
197+
}
198+
template<class T, class... Args>
199+
std::shared_ptr<T>& init(std::shared_ptr<T>& p, Args&&... args)
200+
{
201+
try {
202+
p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
203+
p->init();
204+
}catch(...){
205+
p = std::shared_ptr<T>();
206+
throw;
207+
}
208+
return p;
209+
}
210+
140211
struct dummy_mutex
141212
{
142213
void lock() {}
@@ -148,7 +219,7 @@ namespace kit
148219
struct optional_mutex
149220
{
150221
optional_mutex():
151-
mutex(std::make_shared<Mutex>())
222+
mutex(kit::make_unique<Mutex>())
152223
{}
153224
optional_mutex(const optional_mutex<Mutex>&) = default;
154225
optional_mutex(optional_mutex<Mutex>&&) = default;
@@ -167,7 +238,7 @@ namespace kit
167238
if(mutex)
168239
mutex->unlock();
169240
}
170-
std::shared_ptr<Mutex> mutex;
241+
std::unique_ptr<Mutex> mutex;
171242
};
172243

173244
template<class Mutex=std::mutex>
@@ -799,77 +870,6 @@ namespace kit
799870
return c.at(start);
800871
}
801872

802-
803-
template<class T, class ...Args>
804-
std::future<T> make_future(Args&&... args)
805-
{
806-
std::promise<T> tmp;
807-
tmp.set_value(std::forward<Args>(args)...);
808-
return tmp.get_future();
809-
}
810-
811-
template<class T, class... Args>
812-
std::unique_ptr<T> make_unique(Args&&... args)
813-
{
814-
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
815-
}
816-
template<class T, class... Args>
817-
T make(Args&&... args)
818-
{
819-
return T(new typename T::element_type(std::forward<Args>(args)...));
820-
}
821-
822-
template<class T, class... Args>
823-
std::unique_ptr<T> init_unique(Args&&... args)
824-
{
825-
auto p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
826-
p->init();
827-
return p;
828-
}
829-
template<class T, class... Args>
830-
std::shared_ptr<T> init_shared(Args&&... args)
831-
{
832-
auto p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
833-
p->init();
834-
return p;
835-
}
836-
//template<class T, class... Args>
837-
//std::unique_ptr<T>& make(std::unique_ptr<T>& p, Args&&... args)
838-
//{
839-
// p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
840-
// return p;
841-
//}
842-
//template<class T, class... Args>
843-
//std::shared_ptr<T>& make(std::shared_ptr<T>& p, Args&&... args)
844-
//{
845-
// p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
846-
// return p;
847-
//}
848-
template<class T, class... Args>
849-
std::unique_ptr<T>& init(std::unique_ptr<T>& p, Args&&... args)
850-
{
851-
try {
852-
p = std::unique_ptr<T>(new T(std::forward<Args>(args)...));
853-
p->init();
854-
}catch(...){
855-
p = std::unique_ptr<T>();
856-
throw;
857-
}
858-
return p;
859-
}
860-
template<class T, class... Args>
861-
std::shared_ptr<T>& init(std::shared_ptr<T>& p, Args&&... args)
862-
{
863-
try {
864-
p = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
865-
p->init();
866-
}catch(...){
867-
p = std::shared_ptr<T>();
868-
throw;
869-
}
870-
return p;
871-
}
872-
873873
inline std::vector<std::string> lines(const std::string& s)
874874
{
875875
std::vector<std::string> v;

0 commit comments

Comments
 (0)