TensorFlow Serving C++ API Documentation
fake_loader_source_adapter.cc
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow_serving/core/test_util/fake_loader_source_adapter.h"
17 
18 #include <functional>
19 #include <memory>
20 
21 #include "tensorflow/core/lib/strings/strcat.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow_serving/core/loader.h"
24 #include "tensorflow_serving/core/source_adapter.h"
25 
26 namespace tensorflow {
27 namespace serving {
28 namespace test_util {
29 
30 FakeLoaderSourceAdapter::FakeLoaderSourceAdapter(
31  const string& suffix, std::function<void(const string&)> call_on_destruct)
32  : SimpleLoaderSourceAdapter(
33  [this](const StoragePath& path,
34  std::unique_ptr<string>* servable_ptr) {
35  const string servable = suffix_.length() > 0
36  ? strings::StrCat(path, "/", suffix_)
37  : path;
38  servable_ptr->reset(new string(servable));
39  return Status();
40  },
41  SimpleLoaderSourceAdapter<StoragePath,
42  string>::EstimateNoResources()),
43  suffix_(suffix),
44  call_on_destruct_(call_on_destruct) {}
45 
46 FakeLoaderSourceAdapter::~FakeLoaderSourceAdapter() {
47  Detach();
48  if (call_on_destruct_) {
49  call_on_destruct_(suffix_);
50  }
51 }
52 
53 // Register the source adapter.
55  public:
56  static Status Create(
57  const FakeLoaderSourceAdapterConfig& config,
58  std::unique_ptr<SourceAdapter<StoragePath, std::unique_ptr<Loader>>>*
59  adapter) {
60  adapter->reset(new FakeLoaderSourceAdapter(config.suffix()));
61  return Status();
62  }
63 };
64 REGISTER_STORAGE_PATH_SOURCE_ADAPTER(FakeLoaderSourceAdapterCreator,
65  FakeLoaderSourceAdapterConfig);
66 
67 } // namespace test_util
68 } // namespace serving
69 } // namespace tensorflow